1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.jxpath;
  17. import java.net.URL;
  18. import javax.xml.transform.Source;
  19. import javax.xml.transform.Transformer;
  20. import javax.xml.transform.TransformerFactory;
  21. import javax.xml.transform.dom.DOMResult;
  22. import org.apache.commons.jxpath.xml.DocumentContainer;
  23. import org.w3c.dom.Document;
  24. /**
  25. * An XML document container reads and parses XML only when it is
  26. * accessed. JXPath traverses Containers transparently -
  27. * you use the same paths to access objects in containers as you
  28. * do to access those objects directly. You can create
  29. * XMLDocumentContainers for various XML documents that may or
  30. * may not be accessed by XPaths. If they are, they will be automatically
  31. * read, parsed and traversed. If they are not - they won't be
  32. * read at all.
  33. *
  34. * @deprecated 1.1 Please use org.apache.commons.jxpath.xml.DocumentContainer
  35. *
  36. * @author Dmitri Plotnikov
  37. * @version $Revision: 1.11 $ $Date: 2004/02/29 14:17:42 $
  38. */
  39. public class XMLDocumentContainer implements Container {
  40. private DocumentContainer delegate;
  41. private Object document;
  42. private URL xmlURL;
  43. private Source source;
  44. private String parser;
  45. /**
  46. * @param URL is a URL for an XML file. Use getClass().getResource
  47. * (resourceName) to load XML from a resource file.
  48. */
  49. public XMLDocumentContainer(URL xmlURL) {
  50. delegate = new DocumentContainer(xmlURL);
  51. }
  52. public XMLDocumentContainer(Source source) {
  53. this.source = source;
  54. if (source == null) {
  55. throw new RuntimeException("Source is null");
  56. }
  57. }
  58. /**
  59. * Reads XML, caches it internally and returns the Document.
  60. */
  61. public Object getValue() {
  62. if (document == null) {
  63. try {
  64. if (source != null) {
  65. DOMResult result = new DOMResult();
  66. Transformer trans =
  67. TransformerFactory.newInstance().newTransformer();
  68. trans.transform(source, result);
  69. document = (Document) result.getNode();
  70. }
  71. else {
  72. document = delegate.getValue();
  73. }
  74. }
  75. catch (Exception ex) {
  76. throw new JXPathException(
  77. "Cannot read XML from: "
  78. + (xmlURL != null
  79. ? xmlURL.toString()
  80. : (source != null
  81. ? source.getSystemId()
  82. : "<<undefined source>>")),
  83. ex);
  84. }
  85. }
  86. return document;
  87. }
  88. /**
  89. * Throws an UnsupportedOperationException
  90. */
  91. public void setValue(Object value) {
  92. throw new UnsupportedOperationException();
  93. }
  94. }