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/06/27 05:57:45 UTC

svn commit: r551027 - in /xerces/java/branches/stax-dev/src/org/apache/xerces/stax: AbstractStAXParser.java StAXParser.java

Author: mrglavas
Date: Tue Jun 26 20:57:44 2007
New Revision: 551027

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

Updates to the implementation from Wei Duan:

"The work done includes: 
   * Use XMLPullConfiguration to simulate stax parsing process. 
   * Implement basic XML infosets such as element, attribute, text, namespacecontext, location, namespace."

Modified:
    xerces/java/branches/stax-dev/src/org/apache/xerces/stax/AbstractStAXParser.java
    xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXParser.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?view=diff&rev=551027&r1=551026&r2=551027
==============================================================================
--- 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 Tue Jun 26 20:57:44 2007
@@ -17,6 +17,7 @@
 
 package org.apache.xerces.stax;
 
+import java.util.Stack;
 import javax.xml.stream.XMLStreamConstants;
 
 import org.apache.xerces.parsers.AbstractXMLDocumentParser;
@@ -36,11 +37,6 @@
  * @version $Id$
  */
 public class AbstractStAXParser extends AbstractXMLDocumentParser {
-	// Represent the XNI type such as xmlDecl but not existed in stax
-	// TODO : The parsing steps of XNI and StAX are not the same. For example,
-	// XNI will treat
-	// START_DOCUMENT and XML_DECL as two events, and stax will treat as one
-	static protected int START_DOCUMENT_DECL = -1;
 
 	// The current event type
 	protected int eventType;
@@ -72,10 +68,13 @@
 	// Element QName
 	protected QName elementName = null;
 
-	// Element attribute
-	// TODO : Actually, a attribute stack is needed to store the attribute for
-	// endElement event
-	protected XMLAttributes elementAttr = null;
+	// Element attribute stack
+    // The XNI interface "endElement" will not give out XMLAttributes info.
+    // However, stax inteface needs to give namespace information when
+    // START_ELEMENT or END_ELEMENT¡¡
+    protected Stack atrributeStack = new Stack();
+    
+	protected XMLAttributes curElementAttr = null;
 
 	// Namespace context
 	protected NamespaceContext namespaceContext = null;
@@ -109,8 +108,9 @@
 		piTarget = null;
 		piData = null;
 		elementName = null;
-		elementAttr = null;
+		curElementAttr = null;
 		namespaceContext = null;
+        atrributeStack.clear();
 	} // reset()
 
 	//
@@ -172,9 +172,6 @@
 		this.versionXML = version;
 		this.encodingXML = encoding;
 		this.standaloneXML = standalone;
-
-		// No according event type in stax event
-		eventType = START_DOCUMENT_DECL;
 	} // xmlDecl(String,String,String)
 
 	/**
@@ -219,7 +216,9 @@
 		eventType = XMLStreamConstants.START_ELEMENT;
 
 		this.elementName = element;
-		this.elementAttr = attributes;
+        
+        this.curElementAttr = attributes;
+		this.atrributeStack.push(attributes);
 	} // startElement(QName,XMLAttributes)
 
 	/**
@@ -300,7 +299,10 @@
 		eventType = XMLStreamConstants.END_ELEMENT;
 
 		this.elementName = element;
-	} // endElement(QName)
+        
+        // The endElement and startElement are in pair
+        this.curElementAttr = (XMLAttributes) this.atrributeStack.pop();
+    } // endElement(QName)
 
 	/**
 	 * The start of a CDATA section.

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?view=diff&rev=551027&r1=551026&r2=551027
==============================================================================
--- 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 Tue Jun 26 20:57:44 2007
@@ -130,7 +130,10 @@
      */
     public int next() throws XMLStreamException {
         try {
+            // Initialize the StAXParser event type to zero
+            staxParser.eventType = 0;
             configuration.parse(false);
+           
         } catch (Exception e) {
             throw new XMLStreamException(
                     "Error occurs when processing the underlying XML source", e);
@@ -167,7 +170,36 @@
      */
     public void require(int type, String namespaceURI, String localName)
             throws XMLStreamException {
-        // Need to be realized
+        if (type == curStAXEventType)
+        {
+           try{
+               if (namespaceURI != null)
+               {
+                   String curNamespace = this.getNamespaceURI();
+                   if (!namespaceURI.equals(curNamespace))
+                   {
+                       throw new XMLStreamException(
+                               "Namespace " + curNamespace + " doesn't match with input "+ namespaceURI);  
+                   }
+               }
+               if (localName != null)
+               {
+                   String curLocalName = this.getLocalName();
+                   if (!localName.equals(curLocalName))
+                   {
+                       throw new XMLStreamException(
+                               "Local name " + curLocalName + " doesn't match with input "+ localName);  
+                   }
+               }    
+           }catch(IllegalStateException e)
+           {
+               throw new XMLStreamException(e);
+           }       
+        }
+        else{
+            throw new XMLStreamException(
+               "Event type " + curStAXEventType + " doesn't match with input type "+ type);   
+        }
     }
 
     /**
@@ -340,7 +372,7 @@
             if (localName == null) {
                 throw new IllegalStateException("Local name can't be null");
             }
-            XMLAttributes attrs = staxParser.elementAttr;
+            XMLAttributes attrs = staxParser.curElementAttr;
             return attrs.getValue(namespaceURI, localName);
         }
 
@@ -359,7 +391,7 @@
      */
     public int getAttributeCount() {
         if (curStAXEventType == XMLStreamConstants.START_ELEMENT) {
-            return staxParser.elementAttr.getLength();
+            return staxParser.curElementAttr.getLength();
         }
 
         throw new IllegalStateException(
@@ -398,7 +430,7 @@
      */
     public String getAttributeNamespace(int index) {
         if (curStAXEventType == XMLStreamConstants.START_ELEMENT) {
-            return staxParser.elementAttr.getURI(index);
+            return staxParser.curElementAttr.getURI(index);
         }
 
         throw new IllegalStateException(
@@ -416,7 +448,7 @@
      */
     public String getAttributeLocalName(int index) {
         if (curStAXEventType == XMLStreamConstants.START_ELEMENT) {
-            return staxParser.elementAttr.getLocalName(index);
+            return staxParser.curElementAttr.getLocalName(index);
         }
 
         throw new IllegalStateException(
@@ -434,7 +466,7 @@
      */
     public String getAttributePrefix(int index) {
         if (curStAXEventType == XMLStreamConstants.START_ELEMENT) {
-            return staxParser.elementAttr.getPrefix(index);
+            return staxParser.curElementAttr.getPrefix(index);
         }
 
         throw new IllegalStateException(
@@ -452,7 +484,7 @@
      */
     public String getAttributeType(int index) {
         if (curStAXEventType == XMLStreamConstants.START_ELEMENT) {
-            return staxParser.elementAttr.getType(index);
+            return staxParser.curElementAttr.getType(index);
         }
 
         throw new IllegalStateException(
@@ -470,7 +502,7 @@
      */
     public String getAttributeValue(int index) {
         if (curStAXEventType == XMLStreamConstants.START_ELEMENT) {
-            return staxParser.elementAttr.getValue(index);
+            return staxParser.curElementAttr.getValue(index);
         }
 
         throw new IllegalStateException(
@@ -489,7 +521,7 @@
      */
     public boolean isAttributeSpecified(int index) {
         if (curStAXEventType == XMLStreamConstants.START_ELEMENT) {
-            return staxParser.elementAttr.isSpecified(index);
+            return staxParser.curElementAttr.isSpecified(index);
         }
 
         throw new IllegalStateException(
@@ -509,7 +541,7 @@
         int countNamespace = 0;
         if (curStAXEventType == XMLStreamConstants.START_ELEMENT
                 || curStAXEventType == XMLStreamConstants.END_ELEMENT) {
-            XMLAttributes attrs = staxParser.elementAttr;
+            XMLAttributes attrs = staxParser.curElementAttr;
 
             for (int i = 0; i < attrs.getLength(); i++) {
                 if (attrs.getLocalName(i) == XMLSymbols.PREFIX_XMLNS) {
@@ -540,10 +572,30 @@
     public String getNamespacePrefix(int index) {
         if (curStAXEventType == XMLStreamConstants.START_ELEMENT
                 || curStAXEventType == XMLStreamConstants.END_ELEMENT) {
-            // TODO : Need to be done
-            return null;
+            
+            int namespaceCount = getNamespaceCount();      
+            if (index + 1 > namespaceCount || index < 0)
+            {
+                throw new IndexOutOfBoundsException("Illegle of namespace index");
+            }
+     
+            XMLAttributes attrs = staxParser.curElementAttr;
+            
+            String prefix = null;
+            int count = 0;
+            for (int i = 0; i < attrs.getLength() && count <= index ; i++) {
+                if (attrs.getLocalName(i) == XMLSymbols.PREFIX_XMLNS) {
+                    // default namesapce 
+                    count++;
+                    prefix = null;
+                } else if (attrs.getPrefix(i) == XMLSymbols.PREFIX_XMLNS) {
+                    count++;
+                    prefix = attrs.getLocalName(i);
+                }
+            }
+            
+            return prefix;
         }
-
         throw new IllegalStateException(
                 "Current state is not START_ELEMENT or END_ELEMENT");
     }
@@ -560,8 +612,28 @@
     public String getNamespaceURI(int index) {
         if (curStAXEventType == XMLStreamConstants.START_ELEMENT
                 || curStAXEventType == XMLStreamConstants.END_ELEMENT) {
-            // TODO : Need to be done
-            return null;
+            int namespaceCount = getNamespaceCount();      
+            if (index + 1 > namespaceCount || index < 0)
+            {
+                throw new IndexOutOfBoundsException("Illegle of namespace index");
+            }
+     
+            XMLAttributes attrs = staxParser.curElementAttr;
+            
+            String uri = null;
+            int count = 0;
+            for (int i = 0; i < attrs.getLength() && count <= index ; i++) {
+                if (attrs.getLocalName(i) == XMLSymbols.PREFIX_XMLNS) {
+                    // default namesapce 
+                    count++;
+                    uri = null;
+                } else if (attrs.getPrefix(i) == XMLSymbols.PREFIX_XMLNS) {
+                    count++;
+                    uri = attrs.getValue(i);
+                }
+            }
+            
+            return uri;
         }
 
         throw new IllegalStateException(



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