Author: veithen Date: Mon Apr 4 16:47:42 2011 New Revision: 1088682 URL: http://svn.apache.org/viewvc?rev=1088682&view=rev Log: AXIOM-353: Also integrated SAXOMBuilder into the OMXMLParserWrapper/OMXMLBuilderFactory/OMMetaFactory API. This will allow alternative implementations of the Axiom API to do extra optimizations that will benefit e.g. to the XmlBeans databinding in Axis2.
Added: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/SAXOMXMLParserWrapper.java (with props) webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/sax/ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/sax/AbstractXMLReader.java (with props) Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMMetaFactory.java webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMXMLBuilderFactory.java webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/AbstractOMMetaFactory.java webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/OMXMLReader.java Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMMetaFactory.java URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMMetaFactory.java?rev=1088682&r1=1088681&r2=1088682&view=diff ============================================================================== --- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMMetaFactory.java (original) +++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMMetaFactory.java Mon Apr 4 16:47:42 2011 @@ -20,6 +20,7 @@ package org.apache.axiom.om; import javax.xml.stream.XMLStreamReader; +import javax.xml.transform.sax.SAXSource; import org.apache.axiom.om.util.StAXParserConfiguration; import org.apache.axiom.soap.SOAPFactory; @@ -97,6 +98,21 @@ public interface OMMetaFactory { OMXMLParserWrapper createOMBuilder(OMFactory omFactory, StAXParserConfiguration configuration, InputSource is); /** + * Create an object model builder for plain XML that gets its input from a {@link SAXSource}. + * + * @param omFactory + * The object model factory to use. This factory must be obtained from the same + * {@link OMMetaFactory} instance as the one used to invoke this method. In general + * the factory will be retrieved from {@link #getOMFactory()}), but in some cases it + * may be necessary to pass a {@link SOAPFactory} instance, although this method will + * never produce a SOAP infoset. + * @param source + * the source of the XML document + * @return the builder + */ + OMXMLParserWrapper createSAXOMBuilder(OMFactory omFactory, SAXSource source); + + /** * Create an object model builder for SOAP that pulls events from a StAX stream reader. The * implementation will select the appropriate {@link SOAPFactory} based on the namespace URI of * the SOAP envelope. Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMXMLBuilderFactory.java URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMXMLBuilderFactory.java?rev=1088682&r1=1088681&r2=1088682&view=diff ============================================================================== --- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMXMLBuilderFactory.java (original) +++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMXMLBuilderFactory.java Mon Apr 4 16:47:42 2011 @@ -22,6 +22,7 @@ import java.io.InputStream; import java.io.Reader; import javax.xml.stream.XMLStreamReader; +import javax.xml.transform.sax.SAXSource; import org.apache.axiom.om.util.StAXParserConfiguration; import org.apache.axiom.soap.SOAPFactory; @@ -180,6 +181,33 @@ public class OMXMLBuilderFactory { } /** + * Create an object model builder that reads a plain XML document from the provided + * {@link SAXSource}. + * + * @param source + * the source of the XML document + * @return the builder + */ + public static OMXMLParserWrapper createSAXOMBuilder(SAXSource source) { + OMMetaFactory metaFactory = OMAbstractFactory.getMetaFactory(); + return metaFactory.createSAXOMBuilder(metaFactory.getOMFactory(), source); + } + + /** + * Create an object model builder that reads an XML document from the provided {@link SAXSource} + * using a specified object model factory. + * + * @param omFactory + * the object model factory to use + * @param source + * the source of the XML document + * @return the builder + */ + public static OMXMLParserWrapper createSAXOMBuilder(OMFactory omFactory, SAXSource source) { + return omFactory.getMetaFactory().createSAXOMBuilder(omFactory, source); + } + + /** * Create an object model builder for SOAP that pulls events from a StAX stream reader. * The method will select the appropriate {@link SOAPFactory} * based on the namespace URI of the SOAP envelope. Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/AbstractOMMetaFactory.java URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/AbstractOMMetaFactory.java?rev=1088682&r1=1088681&r2=1088682&view=diff ============================================================================== --- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/AbstractOMMetaFactory.java (original) +++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/AbstractOMMetaFactory.java Mon Apr 4 16:47:42 2011 @@ -20,11 +20,13 @@ package org.apache.axiom.om.impl; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; +import javax.xml.transform.sax.SAXSource; import org.apache.axiom.om.OMException; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMMetaFactory; import org.apache.axiom.om.OMXMLParserWrapper; +import org.apache.axiom.om.impl.builder.SAXOMXMLParserWrapper; import org.apache.axiom.om.impl.builder.StAXOMBuilder; import org.apache.axiom.om.util.StAXParserConfiguration; import org.apache.axiom.om.util.StAXUtils; @@ -69,6 +71,10 @@ public abstract class AbstractOMMetaFact public OMXMLParserWrapper createOMBuilder(OMFactory omFactory, StAXParserConfiguration configuration, InputSource is) { return createStAXOMBuilder(omFactory, createXMLStreamReader(configuration, is)); } + + public OMXMLParserWrapper createSAXOMBuilder(OMFactory omFactory, SAXSource source) { + return new SAXOMXMLParserWrapper(omFactory, source); + } public SOAPModelBuilder createStAXSOAPModelBuilder(XMLStreamReader parser) { StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(this, parser); Added: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/SAXOMXMLParserWrapper.java URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/SAXOMXMLParserWrapper.java?rev=1088682&view=auto ============================================================================== --- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/SAXOMXMLParserWrapper.java (added) +++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/SAXOMXMLParserWrapper.java Mon Apr 4 16:47:42 2011 @@ -0,0 +1,131 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.axiom.om.impl.builder; + +import java.io.IOException; + +import javax.xml.transform.sax.SAXSource; + +import org.apache.axiom.om.OMDocument; +import org.apache.axiom.om.OMElement; +import org.apache.axiom.om.OMException; +import org.apache.axiom.om.OMFactory; +import org.apache.axiom.om.OMXMLParserWrapper; +import org.xml.sax.ContentHandler; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; + +/** + * {@link OMXMLParserWrapper} implementation for SAX parsers. By "SAX parser" we mean anything that + * is able to write a sequence of events to a SAX {@link ContentHandler}. + * <p> + * <b>Note:</b> This is a very naive implementation. Other implementations of the Axiom API may + * provide smarter implementations. For example, in theory it should be possible to defer the + * invocation of the parser until the {@link OMElement} object returned by + * {@link #getDocumentElement()} is accessed. + */ +public class SAXOMXMLParserWrapper implements OMXMLParserWrapper { + private final OMFactory factory; + private final SAXSource source; + private OMDocument document; + + public SAXOMXMLParserWrapper(OMFactory factory, SAXSource source) { + this.factory = factory; + this.source = source; + } + + public OMDocument getDocument() { + if (document == null) { + SAXOMBuilder builder = new SAXOMBuilder(factory); + XMLReader reader = source.getXMLReader(); + reader.setContentHandler(builder); + try { + reader.parse(source.getInputSource()); + } catch (IOException ex) { + throw new OMException(ex); + } catch (SAXException ex) { + throw new OMException(ex); + } + } + return document; + } + + public OMElement getDocumentElement() { + return getDocument().getOMDocumentElement(); + } + + public OMElement getDocumentElement(boolean discardDocument) { + return getDocumentElement(); + } + + public void close() { + // TODO + throw new UnsupportedOperationException(); + } + + public void discard(OMElement el) throws OMException { + // TODO + throw new UnsupportedOperationException(); + } + + public short getBuilderType() { + // TODO + throw new UnsupportedOperationException(); + } + + public String getCharacterEncoding() { + // TODO + throw new UnsupportedOperationException(); + } + + public Object getParser() { + // TODO + throw new UnsupportedOperationException(); + } + + public Object getRegisteredContentHandler() { + // TODO + throw new UnsupportedOperationException(); + } + + public boolean isCache() { + // TODO + throw new UnsupportedOperationException(); + } + + public boolean isCompleted() { + // TODO + throw new UnsupportedOperationException(); + } + + public int next() throws OMException { + // TODO + throw new UnsupportedOperationException(); + } + + public void registerExternalContentHandler(Object obj) { + // TODO + throw new UnsupportedOperationException(); + } + + public void setCache(boolean b) throws OMException { + // TODO + throw new UnsupportedOperationException(); + } +} Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/SAXOMXMLParserWrapper.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/OMXMLReader.java URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/OMXMLReader.java?rev=1088682&r1=1088681&r2=1088682&view=diff ============================================================================== --- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/OMXMLReader.java (original) +++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/OMXMLReader.java Mon Apr 4 16:47:42 2011 @@ -34,17 +34,12 @@ import org.apache.axiom.om.OMNamespace; import org.apache.axiom.om.OMNode; import org.apache.axiom.om.OMProcessingInstruction; import org.apache.axiom.om.OMText; +import org.apache.axiom.util.sax.AbstractXMLReader; import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; -import org.xml.sax.DTDHandler; -import org.xml.sax.EntityResolver; -import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; -import org.xml.sax.SAXNotRecognizedException; -import org.xml.sax.SAXNotSupportedException; import org.xml.sax.XMLReader; -import org.xml.sax.ext.LexicalHandler; /** * SAX {@link XMLReader} implementation that traverses a given OM tree and invokes the @@ -66,97 +61,14 @@ import org.xml.sax.ext.LexicalHandler; * aware of the namespace mapping for the <tt>ns</tt> prefix, even if the serialization starts * only at the child element. */ -public class OMXMLReader implements XMLReader { - private static final String URI_LEXICAL_HANDLER = "http://xml.org/sax/properties/lexical-handler"; - +public class OMXMLReader extends AbstractXMLReader { private final OMElement element; private final AttributesAdapter attributesAdapter = new AttributesAdapter(); - private boolean namespaces = true; - private boolean namespacePrefixes = false; - - private ContentHandler contentHandler; - private LexicalHandler lexicalHandler; - private DTDHandler dtdHandler; - private EntityResolver entityResolver; - private ErrorHandler errorHandler; - public OMXMLReader(OMElement element) { this.element = element; } - public ContentHandler getContentHandler() { - return contentHandler; - } - - public void setContentHandler(ContentHandler contentHandler) { - this.contentHandler = contentHandler; - } - - public DTDHandler getDTDHandler() { - return dtdHandler; - } - - public void setDTDHandler(DTDHandler dtdHandler) { - this.dtdHandler = dtdHandler; - } - - public EntityResolver getEntityResolver() { - return entityResolver; - } - - public void setEntityResolver(EntityResolver entityResolver) { - this.entityResolver = entityResolver; - } - - public ErrorHandler getErrorHandler() { - return errorHandler; - } - - public void setErrorHandler(ErrorHandler errorHandler) { - this.errorHandler = errorHandler; - } - - public boolean getFeature(String name) - throws SAXNotRecognizedException, SAXNotSupportedException { - throw new SAXNotRecognizedException(name); - } - - public void setFeature(String name, boolean value) - throws SAXNotRecognizedException, SAXNotSupportedException { - - if ("http://xml.org/sax/features/namespaces".equals(name)) { - namespaces = value; - } else if ("http://xml.org/sax/features/namespace-prefixes".equals(name)) { - namespacePrefixes = value; - } else { - throw new SAXNotRecognizedException(name); - } - } - - public Object getProperty(String name) - throws SAXNotRecognizedException, SAXNotSupportedException { - - if ("http://xml.org/sax/features/namespaces".equals(name)) { - return Boolean.valueOf(namespaces); - } else if ("http://xml.org/sax/features/namespace-prefixes".equals(name)) { - return Boolean.valueOf(namespacePrefixes); - } else if (URI_LEXICAL_HANDLER.equals(name)) { - return lexicalHandler; - } else { - throw new SAXNotRecognizedException(name); - } - } - - public void setProperty(String name, Object value) - throws SAXNotRecognizedException, SAXNotSupportedException { - if (URI_LEXICAL_HANDLER.equals(name)) { - lexicalHandler = (LexicalHandler)value; - } else { - throw new SAXNotRecognizedException(name); - } - } - public void parse(InputSource input) throws IOException, SAXException { parse(); } Added: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/sax/AbstractXMLReader.java URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/sax/AbstractXMLReader.java?rev=1088682&view=auto ============================================================================== --- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/sax/AbstractXMLReader.java (added) +++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/sax/AbstractXMLReader.java Mon Apr 4 16:47:42 2011 @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.axiom.util.sax; + +import org.xml.sax.ContentHandler; +import org.xml.sax.DTDHandler; +import org.xml.sax.EntityResolver; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXNotSupportedException; +import org.xml.sax.XMLReader; +import org.xml.sax.ext.LexicalHandler; + +/** + * Partial implementation of the {@link XMLReader} interface. It implements all the getters and + * setters so that subclasses only need to implement {@link XMLReader#parse(InputSource)} and + * {@link XMLReader#parse(String)}. Subclasses can access the various handlers and properties set on + * the reader through protected attributes. + */ +public abstract class AbstractXMLReader implements XMLReader { + private static final String URI_LEXICAL_HANDLER = "http://xml.org/sax/properties/lexical-handler"; + + protected boolean namespaces = true; + protected boolean namespacePrefixes = false; + + protected ContentHandler contentHandler; + protected LexicalHandler lexicalHandler; + protected DTDHandler dtdHandler; + protected EntityResolver entityResolver; + protected ErrorHandler errorHandler; + + public ContentHandler getContentHandler() { + return contentHandler; + } + + public void setContentHandler(ContentHandler contentHandler) { + this.contentHandler = contentHandler; + } + + public DTDHandler getDTDHandler() { + return dtdHandler; + } + + public void setDTDHandler(DTDHandler dtdHandler) { + this.dtdHandler = dtdHandler; + } + + public EntityResolver getEntityResolver() { + return entityResolver; + } + + public void setEntityResolver(EntityResolver entityResolver) { + this.entityResolver = entityResolver; + } + + public ErrorHandler getErrorHandler() { + return errorHandler; + } + + public void setErrorHandler(ErrorHandler errorHandler) { + this.errorHandler = errorHandler; + } + + public boolean getFeature(String name) + throws SAXNotRecognizedException, SAXNotSupportedException { + throw new SAXNotRecognizedException(name); + } + + public void setFeature(String name, boolean value) + throws SAXNotRecognizedException, SAXNotSupportedException { + + if ("http://xml.org/sax/features/namespaces".equals(name)) { + namespaces = value; + } else if ("http://xml.org/sax/features/namespace-prefixes".equals(name)) { + namespacePrefixes = value; + } else { + throw new SAXNotRecognizedException(name); + } + } + + public Object getProperty(String name) + throws SAXNotRecognizedException, SAXNotSupportedException { + + if ("http://xml.org/sax/features/namespaces".equals(name)) { + return Boolean.valueOf(namespaces); + } else if ("http://xml.org/sax/features/namespace-prefixes".equals(name)) { + return Boolean.valueOf(namespacePrefixes); + } else if (URI_LEXICAL_HANDLER.equals(name)) { + return lexicalHandler; + } else { + throw new SAXNotRecognizedException(name); + } + } + + public void setProperty(String name, Object value) + throws SAXNotRecognizedException, SAXNotSupportedException { + if (URI_LEXICAL_HANDLER.equals(name)) { + lexicalHandler = (LexicalHandler)value; + } else { + throw new SAXNotRecognizedException(name); + } + } + + +} Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/sax/AbstractXMLReader.java ------------------------------------------------------------------------------ svn:eol-style = native