You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by mr...@apache.org on 2007/08/20 21:57:33 UTC

svn commit: r567806 - in /xerces/java/branches/stax-dev/src/org/apache/xerces/stax: AbstractStAXParser.java StAXFilterParser.java StAXLocation.java StAXParser.java XMLInputFactoryImpl.java

Author: mrglavas
Date: Mon Aug 20 12:57:32 2007
New Revision: 567806

URL: http://svn.apache.org/viewvc?rev=567806&view=rev
Log:
JIRA Issue #1259:
http://issues.apache.org/jira/browse/XERCESJ-1259

Updates to the implementation from Wei Duan to address comments I made on this JIRA issue.

Modified:
    xerces/java/branches/stax-dev/src/org/apache/xerces/stax/AbstractStAXParser.java
    xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXFilterParser.java
    xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXLocation.java
    xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXParser.java
    xerces/java/branches/stax-dev/src/org/apache/xerces/stax/XMLInputFactoryImpl.java

Modified: xerces/java/branches/stax-dev/src/org/apache/xerces/stax/AbstractStAXParser.java
URL: http://svn.apache.org/viewvc/xerces/java/branches/stax-dev/src/org/apache/xerces/stax/AbstractStAXParser.java?rev=567806&r1=567805&r2=567806&view=diff
==============================================================================
--- xerces/java/branches/stax-dev/src/org/apache/xerces/stax/AbstractStAXParser.java (original)
+++ xerces/java/branches/stax-dev/src/org/apache/xerces/stax/AbstractStAXParser.java Mon Aug 20 12:57:32 2007
@@ -30,6 +30,9 @@
 import org.apache.xerces.xni.XMLResourceIdentifier;
 import org.apache.xerces.xni.XMLString;
 import org.apache.xerces.xni.XNIException;
+import org.apache.xerces.xni.parser.XMLConfigurationException;
+import org.apache.xerces.xni.parser.XMLErrorHandler;
+import org.apache.xerces.xni.parser.XMLEntityResolver;
 
 /**
  * @author Wei Duan
@@ -117,6 +120,30 @@
         atrributeStack.clear();
 	} // reset()
 
+    /**
+     * Allow an application to register an error event handler.
+     *
+     * @param errorHandler The error handler.
+     */
+    public void setErrorHandler(XMLErrorHandler errorHandler) {
+        if (errorHandler != null)
+        {
+            fConfiguration.setProperty(ERROR_HANDLER, errorHandler);
+        }
+    } 
+    
+    /**
+     * Allow an application to register an entity resolver handler.
+     *
+     * @param resolver The Entity Resolver
+     */
+    public void setEntityResolver(XMLEntityResolver resolver) {
+        if (resolver != null)
+        {
+            fConfiguration.setProperty(ENTITY_RESOLVER, resolver);
+        }
+    } 
+    
 	//
 	// XMLDocumentHandler methods
 	//

Modified: xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXFilterParser.java
URL: http://svn.apache.org/viewvc/xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXFilterParser.java?rev=567806&r1=567805&r2=567806&view=diff
==============================================================================
--- xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXFilterParser.java (original)
+++ xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXFilterParser.java Mon Aug 20 12:57:32 2007
@@ -18,6 +18,7 @@
 package org.apache.xerces.stax;
 
 import javax.xml.stream.StreamFilter;
+import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.util.StreamReaderDelegate;
@@ -47,6 +48,7 @@
             throw new XMLStreamException("The StreamReader parameter can't be null for StAXFilterParser");
         }
         this.streamFilter = streamFilter;
+        next();
     }
 
     /**
@@ -67,32 +69,50 @@
      *             if there is an error processing the underlying XML source
      */
     public int next() throws XMLStreamException {
-        while (streamFilter != null && !streamFilter.accept(this))
+        while (super.hasNext())
         {
-            next();
+            int nextEvent = super.next();
+            if (streamFilter.accept(this))
+            {
+                return nextEvent;
+            }
         }
-        
-        return next();
+    
+        throw new IllegalStateException("No more document to parse");
     }
     
     /**
-     * Returns true if there are more parsing events and false if there are no
-     * more events. This method will return false if the current state of the
-     * XMLStreamReader is END_DOCUMENT
+     * Skips any white space (isWhiteSpace() returns true), COMMENT, or
+     * PROCESSING_INSTRUCTION, until a START_ELEMENT or END_ELEMENT is reached.
+     * If other than white space characters, COMMENT, PROCESSING_INSTRUCTION,
+     * START_ELEMENT, END_ELEMENT are encountered, an exception is thrown.
+     * return eventType;
      * 
-     * @return true if there are more events, false otherwise
+     * </pre>
+     * 
+     * @return the event type of the element read (START_ELEMENT or END_ELEMENT)
      * @throws XMLStreamException
-     *             if there is a fatal error detecting the next state
+     *             if the current event is not white space,
+     *             PROCESSING_INSTRUCTION, START_ELEMENT or END_ELEMENT
+     * @throws NoSuchElementException
+     *             if this is called when hasNext() returns false
      */
-    public boolean hasNext() throws XMLStreamException {
-        boolean hasNext = hasNext();
-      
-        while (streamFilter != null && !streamFilter.accept(this))
-        {
-            next();
-            hasNext = hasNext();
+    public int nextTag() throws XMLStreamException {
+        int eventType = next();
+        while ((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) // skip
+                // whitespace
+                || (eventType == XMLStreamConstants.CDATA && isWhiteSpace())
+                // skip whitespace
+                || eventType == XMLStreamConstants.SPACE
+                || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
+                || eventType == XMLStreamConstants.COMMENT) {
+            eventType = next();
         }
-
-        return hasNext;
+        if (eventType != XMLStreamConstants.START_ELEMENT
+                && eventType != XMLStreamConstants.END_ELEMENT) {
+            throw new XMLStreamException("expected start or end tag",
+                    getLocation());
+        }
+        return eventType;
     }
 }

Modified: xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXLocation.java
URL: http://svn.apache.org/viewvc/xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXLocation.java?rev=567806&r1=567805&r2=567806&view=diff
==============================================================================
--- xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXLocation.java (original)
+++ xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXLocation.java Mon Aug 20 12:57:32 2007
@@ -32,7 +32,7 @@
 	public StAXLocation(XMLLocator loc) {
 		this.xniLocator = loc;
 	}
-
+	
 	/**
 	 * Return the line number where the current event ends, returns -1 if none
 	 * is available.

Modified: xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXParser.java
URL: http://svn.apache.org/viewvc/xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXParser.java?rev=567806&r1=567805&r2=567806&view=diff
==============================================================================
--- xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXParser.java (original)
+++ xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXParser.java Mon Aug 20 12:57:32 2007
@@ -26,6 +26,7 @@
 import javax.xml.stream.XMLStreamReader;
 
 import org.apache.xerces.parsers.XML11Configuration;
+import org.apache.xerces.xni.parser.XMLConfigurationException;
 import org.apache.xerces.xni.parser.XMLInputSource;
 import org.apache.xerces.xni.XMLAttributes;
 import org.apache.xerces.util.XMLSymbols;
@@ -42,6 +43,9 @@
 
     // XML configuration for StAXParser
     private XML11Configuration configuration;
+    
+    // Input XMLInputSource
+    private XMLInputSource inputSource;
 
     // XNI-based internal parser for StAXParser
     private AbstractStAXParser staxParser;
@@ -67,16 +71,48 @@
      */
     public StAXParser(XMLInputSource inputSource, XMLInputFactory inputFactory)
             throws XMLStreamException {
+        this.inputFactory = inputFactory;
+        this.inputSource = inputSource;        
+    }
+    
+    /**
+     * Get the XMLConfiguration of current StAXParser.  
+     * So the XMLConfiguration can be reused by other parser instance
+     * 
+     * @return XML11Configuration
+     */
+    public XML11Configuration GetXMLConfiguration()
+    {
+        return this.configuration;
+    }
+    
+    /**
+     * Set the XMLConfiguration to current StAXParser
+     * 
+     * @param XML11Configuration
+     * @throws XMLStreamException if fail to set XMLConfiguration
+     */
+    public void InitStAXParser(XML11Configuration config)throws XMLStreamException
+    {
+        this.configuration = config;
         try {
-            this.inputFactory = inputFactory;
-            this.configuration = new XML11Configuration();
-
-            configuration.setInputSource(inputSource);
-            staxParser = new AbstractStAXParser(configuration);
-        } catch (Exception e) {
-            throw new XMLStreamException("Fail to create StAXParser instance");
+            this.configuration.setInputSource(this.inputSource);
+            this.staxParser = new AbstractStAXParser(configuration);           
+            if (inputFactory.getXMLReporter() != null)
+            {
+                staxParser.setErrorHandler(new StAXErrorHandler(inputFactory.getXMLReporter()));
+            }
+            
+            if (inputFactory.getXMLResolver() != null)
+            {
+                staxParser.setEntityResolver(new StAXResolver(inputFactory.getXMLResolver()));
+            }
+        }catch(Exception e)
+        {
+            throw new XMLStreamException("Fail to create StAXParser instance!", e);
         }
     }
+
     
     /**
      * Get the value of a feature/property from the underlying implementation
@@ -96,11 +132,11 @@
 
         if (name == notationProperty)
         {
-            // TODO : Add notation property support when current enent is DTD
+            // TODO : Add notation property support when current event is DTD
         }
         else if (name == entityProperty)
         {
-            // TODO : Add notation property support when current enent is DTD
+            // TODO : Add notation property support when current event is DTD
         }
         
         return null;
@@ -311,10 +347,15 @@
      */
     public void close() throws XMLStreamException {
         if (curStAXEventType == XMLStreamConstants.END_DOCUMENT) {
-            configuration.cleanup();
+            try
+            {
+                this.inputSource.getByteStream().close();
+            }catch(java.io.IOException e)
+            {
+                throw new XMLStreamException(
+                   "There are errors freeing associated resources!", e);                
+            }
         }
-        throw new IllegalStateException(
-                "Current state is not START_ELEMENT or ATTRIBUTE");
     }
 
     /**

Modified: xerces/java/branches/stax-dev/src/org/apache/xerces/stax/XMLInputFactoryImpl.java
URL: http://svn.apache.org/viewvc/xerces/java/branches/stax-dev/src/org/apache/xerces/stax/XMLInputFactoryImpl.java?rev=567806&r1=567805&r2=567806&view=diff
==============================================================================
--- xerces/java/branches/stax-dev/src/org/apache/xerces/stax/XMLInputFactoryImpl.java (original)
+++ xerces/java/branches/stax-dev/src/org/apache/xerces/stax/XMLInputFactoryImpl.java Mon Aug 20 12:57:32 2007
@@ -33,6 +33,7 @@
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.sax.SAXSource;
 
+import org.apache.xerces.parsers.XML11Configuration;
 import org.apache.xerces.xni.parser.XMLInputSource;
 import org.w3c.dom.Node;
 import org.xml.sax.InputSource;
@@ -87,7 +88,10 @@
     throws XMLStreamException {
         XMLInputSource inputsource = new XMLInputSource(null, null, null,
                 stream, null);
+        XML11Configuration config = new XML11Configuration();
         StAXParser xmlStreamReader = new StAXParser(inputsource, this);
+        xmlStreamReader.InitStAXParser(config);
+        
         return xmlStreamReader;
     }
     



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xerces.apache.org
For additional commands, e-mail: commits-help@xerces.apache.org