You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cxf.apache.org by Daniel Kulp <dk...@apache.org> on 2008/03/20 16:55:03 UTC

Re: svn commit: r639325 - /incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/TunedDocumentLoader.java

One concern I have with this is that is introduces a DIRECT dependency on 
woodstox in stead of "any StAX parser".   You MIGHT want to try 
detecting if the required woodstox class is there and if not, just 
default down to the slower behavior.

I'm thinking about his from a Java6 update 4 standpoint.  With java6, a 
Stax parser is already available so the wdstx jars are amoungst those 
marked "optional" in the WHICH_JARS file.

Dan


On Thursday 20 March 2008, bimargulies@apache.org wrote:
> Author: bimargulies
> Date: Thu Mar 20 08:46:48 2008
> New Revision: 639325
>
> URL: http://svn.apache.org/viewvc?rev=639325&view=rev
> Log:
> Stax with a more compatible SAX interface, and very slightly speedier,
> to boot.
>
> Modified:
>    
> incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/Tu
>nedDocumentLoader.java
>
> Modified:
> incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/Tu
>nedDocumentLoader.java URL:
> http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/main/java
>/org/apache/cxf/bus/spring/TunedDocumentLoader.java?rev=639325&r1=63932
>4&r2=639325&view=diff
> ======================================================================
>======== ---
> incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/Tu
>nedDocumentLoader.java (original) +++
> incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/Tu
>nedDocumentLoader.java Thu Mar 20 08:46:48 2008 @@ -21,12 +21,9 @@
>
>  import javax.xml.parsers.DocumentBuilderFactory;
>  import javax.xml.parsers.ParserConfigurationException;
> -import javax.xml.stream.Location;
> -import javax.xml.stream.XMLInputFactory;
> -import javax.xml.stream.XMLReporter;
> -import javax.xml.stream.XMLResolver;
> -import javax.xml.stream.XMLStreamException;
> -import javax.xml.stream.XMLStreamReader;
> +import javax.xml.parsers.SAXParser;
> +import javax.xml.transform.TransformerFactory;
> +import javax.xml.transform.dom.DOMResult;
>  import javax.xml.transform.sax.SAXSource;
>
>  import org.w3c.dom.Document;
> @@ -34,92 +31,45 @@
>  import org.xml.sax.EntityResolver;
>  import org.xml.sax.ErrorHandler;
>  import org.xml.sax.InputSource;
> -import org.xml.sax.SAXException;
> -import org.xml.sax.SAXParseException;
> -import org.xml.sax.ext.EntityResolver2;
> +import org.xml.sax.XMLReader;
>
> -import org.apache.cxf.staxutils.StaxUtils;
> -import org.apache.cxf.staxutils.W3CDOMStreamWriter;
> +import com.ctc.wstx.sax.WstxSAXParserFactory;
> +
> +import org.apache.cxf.helpers.XMLUtils;
>  import org.springframework.beans.factory.xml.DefaultDocumentLoader;
>  import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
>
> +
> +
>  /**
> - *
> + * A Spring DocumentLoader that uses WoodStox when we are not
> validating to speed up the process. */
> -public class TunedDocumentLoader extends DefaultDocumentLoader {
> +class TunedDocumentLoader extends DefaultDocumentLoader {
> +
> +    private TransformerFactory transformerFactory;
> +
> +    TunedDocumentLoader() {
> +        transformerFactory = TransformerFactory.newInstance();
> +    }
>
>      @Override
>      public Document loadDocument(InputSource inputSource,
> EntityResolver entityResolver, ErrorHandler errorHandler, int
> validationMode, boolean namespaceAware) throws Exception {
>          if (validationMode ==
> XmlBeanDefinitionReader.VALIDATION_NONE) { -
> -            final EntityResolver finalResolver = entityResolver;
> -            final ErrorHandler finalHandler = errorHandler;
> -
> -            W3CDOMStreamWriter domStreamWriter = new
> W3CDOMStreamWriter(); -
> -            final XMLInputFactory inputFactory =
> StaxUtils.createXMLInputFactory(namespaceAware); -           
> inputFactory.setXMLReporter(new XMLReporter() {
> -
> -                public void report(String message, String errorType,
> Object relatedInformation, -                                  
> Location location) throws XMLStreamException { -                   
> final Location finalLocation = location; -                   
> org.xml.sax.Locator saxLocator = new org.xml.sax.Locator() { -
> -                        public int getColumnNumber() {
> -                            return finalLocation.getColumnNumber();
> -                        }
> -
> -                        public int getLineNumber() {
> -                            return finalLocation.getLineNumber();
> -                        }
> -
> -                        public String getPublicId() {
> -                            return finalLocation.getPublicId();
> -                        }
> -
> -                        public String getSystemId() {
> -                            return finalLocation.getSystemId();
> -                        }
> -                    };
> -
> -                    try {
> -                        finalHandler.error(new
> SAXParseException(message, saxLocator)); -                    } catch
> (SAXException e) {
> -                        // this is not what we want, but
> -                        // I don't see how to get it right.
> -                        throw new XMLStreamException(e);
> -                    }
> -                }
> -            });
> -
> -            inputFactory.setXMLResolver(new XMLResolver() {
> -
> -                public Object resolveEntity(String publicID,
> -                                            String systemID, String
> baseURI, String namespace) -                    throws
> XMLStreamException {
> -                    try {
> -                        if (finalResolver instanceof EntityResolver2)
> { -                            EntityResolver2 er2 =
> (EntityResolver2)finalResolver; -
> -                            InputSource entitySource = er2
> -                                .resolveEntity("[dtd]", publicID,
> baseURI, systemID); -                            return
> inputFactory.createXMLStreamReader(new SAXSource(entitySource)); -    
>                    } else {
> -                            InputSource entitySource =
> finalResolver.resolveEntity(publicID, systemID); -                    
>        return inputFactory.createXMLStreamReader(new
> SAXSource(entitySource)); -                        }
> -                    } catch (Exception e) {
> -                        throw new XMLStreamException(e);
> -                    }
> -                }
> -            });
> -
> -            SAXSource saxSource = new SAXSource(inputSource);
> -            XMLStreamReader reader =
> inputFactory.createXMLStreamReader(saxSource); -           
> StaxUtils.copy(reader, domStreamWriter);
> -            return domStreamWriter.getDocument();
> +            WstxSAXParserFactory woodstoxParserFactory;
> +            woodstoxParserFactory = new WstxSAXParserFactory();
> +           
> woodstoxParserFactory.setFeature("http://xml.org/sax/features/namespac
>e-prefixes", +                                            
> namespaceAware); +            SAXParser parser =
> woodstoxParserFactory.newSAXParser(); +            XMLReader reader =
> parser.getXMLReader();
> +            reader.setEntityResolver(entityResolver);
> +            reader.setErrorHandler(errorHandler);
> +            SAXSource saxSource = new SAXSource(reader, inputSource);
> +            Document document = XMLUtils.newDocument();
> +            DOMResult domResult = new DOMResult(document,
> inputSource.getSystemId()); +           
> transformerFactory.newTransformer().transform(saxSource, domResult); +
>            return document;
>          } else {
>              return super.loadDocument(inputSource, entityResolver,
> errorHandler, validationMode, namespaceAware);



-- 
J. Daniel Kulp
Principal Engineer, IONA
dkulp@apache.org
http://www.dankulp.com/blog