You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dm...@apache.org on 2002/08/10 03:28:49 UTC
cvs commit: jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml DocumentContainer.java DOMParser.java XMLParser.java
dmitri 2002/08/09 18:28:49
Modified: jxpath/src/java/org/apache/commons/jxpath
XMLDocumentContainer.java
Added: jxpath/src/java/org/apache/commons/jxpath/xml
DocumentContainer.java DOMParser.java
XMLParser.java
Log:
Refactored XML parsing by container to allow for alternative parsers
Revision Changes Path
1.6 +27 -33 jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/XMLDocumentContainer.java
Index: XMLDocumentContainer.java
===================================================================
RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/XMLDocumentContainer.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- XMLDocumentContainer.java 8 Jun 2002 22:46:11 -0000 1.5
+++ XMLDocumentContainer.java 10 Aug 2002 01:28:49 -0000 1.6
@@ -61,17 +61,26 @@
*/
package org.apache.commons.jxpath;
+import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.StreamSource;
+
+import org.apache.commons.jxpath.xml.DocumentContainer;
import org.w3c.dom.Document;
-import org.w3c.dom.Node;
+import org.xml.sax.SAXException;
+
+import org.apache.commons.jxpath.xml.DocumentContainer;
/**
* An XML document container reads and parses XML only when it is
@@ -83,24 +92,25 @@
* read, parsed and traversed. If they are not - they won't be
* read at all.
*
+ * @deprecated 1.1 Please use org.apache.commons.jxpath.xml.DocumentContainer
+ *
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class XMLDocumentContainer implements Container {
- private Node document;
+ private DocumentContainer delegate;
+ private Object document;
private URL xmlURL;
private Source source;
+ private String parser;
/**
* @param URL is a URL for an XML file. Use getClass().getResource(resourceName)
* to load XML from a resource file.
*/
public XMLDocumentContainer(URL xmlURL){
- this.xmlURL = xmlURL;
- if (xmlURL == null){
- throw new JXPathException("URL is null");
- }
+ delegate = new DocumentContainer(xmlURL);
}
public XMLDocumentContainer(Source source){
@@ -116,30 +126,14 @@
public Object getValue(){
if (document == null){
try {
- InputStream stream = null;
- try {
- if (xmlURL != null){
- stream = xmlURL.openStream();
- }
- if (stream != null){
- DocumentBuilderFactory factory =
- DocumentBuilderFactory.newInstance();
- document = factory.newDocumentBuilder().parse(stream);
- }
- else {
- if (xmlURL != null){
- source = new StreamSource(stream);
- }
- DOMResult result = new DOMResult();
- Transformer trans = TransformerFactory.newInstance().newTransformer();
- trans.transform(source, result);
- document = (Document) result.getNode();
- }
+ if (source != null){
+ DOMResult result = new DOMResult();
+ Transformer trans = TransformerFactory.newInstance().newTransformer();
+ trans.transform(source, result);
+ document = (Document) result.getNode();
}
- finally {
- if (stream != null){
- stream.close();
- }
+ else {
+ document = delegate.getValue();
}
}
catch (Exception ex){
1.1 jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml/DocumentContainer.java
Index: DocumentContainer.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml/DocumentContainer.java,v 1.1 2002/08/10 01:28:49 dmitri Exp $
* $Revision: 1.1 $
* $Date: 2002/08/10 01:28:49 $
*
* ====================================================================
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2001, Plotnix, Inc,
* <http://www.plotnix.com/>.
* For more information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.jxpath.xml;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import org.apache.commons.jxpath.Container;
import org.apache.commons.jxpath.JXPathException;
/**
* An XML document container reads and parses XML only when it is
* accessed. JXPath traverses Containers transparently -
* you use the same paths to access objects in containers as you
* do to access those objects directly. You can create
* XMLDocumentContainers for various XML documents that may or
* may not be accessed by XPaths. If they are, they will be automatically
* read, parsed and traversed. If they are not - they won't be
* read at all.
*
* @author Dmitri Plotnikov
* @version $Revision: 1.1 $ $Date: 2002/08/10 01:28:49 $
*/
public class DocumentContainer implements Container {
public static final String MODEL_DOM = "DOM";
// public static final String MODEL_JDOM = "JDOM";
private Object document;
private URL xmlURL;
private String model;
private static HashMap parserClasses = new HashMap();
static {
parserClasses.put(MODEL_DOM,
"org.apache.commons.jxpath.xml.DOMParser");
// parserClasses.put(MODEL_JDOM,
// "org.apache.commons.jxpath.xml.JDOMParser");
}
private static HashMap parsers = new HashMap();
/**
* Use this constructor if the desired model is DOM.
*
* @param URL is a URL for an XML file.
* Use getClass().getResource(resourceName) to load XML from a
* resource file.
*/
public DocumentContainer(URL xmlURL){
this(xmlURL, MODEL_DOM);
}
/**
* @param URL is a URL for an XML file. Use getClass().getResource(resourceName)
* to load XML from a resource file.
*
* @param model is one of the MODEL_* constants defined in this class. It
* determines which parser should be used to load the XML.
*/
public DocumentContainer(URL xmlURL, String model){
this.xmlURL = xmlURL;
if (xmlURL == null){
throw new JXPathException("XML URL is null");
}
this.model = model;
}
/**
* Reads XML, caches it internally and returns the Document.
*/
public Object getValue(){
if (document == null){
try {
InputStream stream = null;
try {
if (xmlURL != null){
stream = xmlURL.openStream();
}
document = getParser(model).parseXML(stream);
}
finally {
if (stream != null){
stream.close();
}
}
}
catch (Exception ex){
throw new JXPathException(
"Cannot read XML from: " + xmlURL.toString(),
ex);
}
}
return document;
}
/**
* Throws an UnsupportedOperationException
*/
public void setValue(Object value){
throw new UnsupportedOperationException();
}
/**
* Maps a model type to a parser.
*/
private static final XMLParser getParser(String model){
XMLParser parser = (XMLParser)parsers.get(model);
if (parser == null){
String className = (String)parserClasses.get(model);
if (className == null){
throw new JXPathException("Unsupported XML model: " + model);
}
try {
Class clazz = Class.forName(className);
parser = (XMLParser)clazz.newInstance();
}
catch (Exception ex){
throw new JXPathException("Cannot allocate XMLParser: " +
className);
}
parsers.put(model, parser);
}
return parser;
}
}
1.1 jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml/DOMParser.java
Index: DOMParser.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml/DOMParser.java,v 1.1 2002/08/10 01:28:49 dmitri Exp $
* $Revision: 1.1 $
* $Date: 2002/08/10 01:28:49 $
*
* ====================================================================
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2001, Plotnix, Inc,
* <http://www.plotnix.com/>.
* For more information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.jxpath.xml;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.jxpath.JXPathException;
/**
* An implementation of the XMLParser interface that produces a DOM Document.
*
* @author Dmitri Plotnikov
* @version $Revision: 1.1 $ $Date: 2002/08/10 01:28:49 $
*/
public class DOMParser implements XMLParser {
public Object parseXML(InputStream stream){
try {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
return factory.newDocumentBuilder().parse(stream);
}
catch (Exception ex){
throw new JXPathException("DOM parser error", ex);
}
}
}
1.1 jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml/XMLParser.java
Index: XMLParser.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml/XMLParser.java,v 1.1 2002/08/10 01:28:49 dmitri Exp $
* $Revision: 1.1 $
* $Date: 2002/08/10 01:28:49 $
*
* ====================================================================
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2001, Plotnix, Inc,
* <http://www.plotnix.com/>.
* For more information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.jxpath.xml;
import java.io.InputStream;
/**
* Common interface for XML parsers. We are not using JAXP because
* some of the parsers we want to support may not be JAXP compliant.
*
* @author Dmitri Plotnikov
* @version $Revision: 1.1 $ $Date: 2002/08/10 01:28:49 $
*/
public interface XMLParser {
/**
* Reads the supplied XML file and returns the resulting model,
* which is not necessarily DOM.
*/
Object parseXML(InputStream stream);
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>