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 2006/07/30 20:48:25 UTC

svn commit: r426914 - in /xerces/java/branches/stax-dev/src/org/apache/xerces/stax: NamespaceContextImpl.java SAXLocation.java SAXXMLStreamReaderImpl.java StAXSAXHandler.java

Author: mrglavas
Date: Sun Jul 30 11:48:24 2006
New Revision: 426914

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

Updates to the SAX XMLStreamReader from Hua Lei.

Updates to SAXXMLStreamReaderImpl: 

0) Method "public SAXXMLStreamReaderImpl(XMLReader xr, InputSource is, XMLInputFactory xif) " is updated because the SAX Parser location information is added. 
1) Method "public String getAttributeValue(String namespaceURI, String localName) " is updated 
2) Namespace related methods "public int getNamespaceCount(); public String getNamespacePrefix(int index); public String getNamespaceURI(int index)" are realized 
3) Method "public String getText()" are realized. 
4) Method "public String getEncoding()" are realized. 
5) Element related methods "public QName getName() ;public String getLocalName(); public String getNamespaceURI(); public String getPrefix()" are realized. 
6) Method "public Location getLocation()" is realized. The SAXLocation is added to record the location information 

Updates to NamespaceContextImpl: 
1) An ArrayList "eleNamespaces" is added for SAXSource 
2) Methods "protected ArrayList getNamespaces(); protected String getNamespacePrefix(int index);protected String getNamespaceURI(int index)" are added. 
3) Method "public void onStartElement()" is updated 
4) The inline class "DOMNamespace" is renamed as "Namespace", because it's not only used for DOMSource. 

SAXLocation has been added. 

Updates to StAXSAXHandler: 
1) An attribute "private Locator loc" is added 
2) The initialization method and method " public synchronized void startDocument()" are updated.

Added:
    xerces/java/branches/stax-dev/src/org/apache/xerces/stax/SAXLocation.java   (with props)
Modified:
    xerces/java/branches/stax-dev/src/org/apache/xerces/stax/NamespaceContextImpl.java
    xerces/java/branches/stax-dev/src/org/apache/xerces/stax/SAXXMLStreamReaderImpl.java
    xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXSAXHandler.java

Modified: xerces/java/branches/stax-dev/src/org/apache/xerces/stax/NamespaceContextImpl.java
URL: http://svn.apache.org/viewvc/xerces/java/branches/stax-dev/src/org/apache/xerces/stax/NamespaceContextImpl.java?rev=426914&r1=426913&r2=426914&view=diff
==============================================================================
--- xerces/java/branches/stax-dev/src/org/apache/xerces/stax/NamespaceContextImpl.java (original)
+++ xerces/java/branches/stax-dev/src/org/apache/xerces/stax/NamespaceContextImpl.java Sun Jul 30 11:48:24 2006
@@ -35,12 +35,16 @@
     // Record the avaliable namespaces
     private Stack namespaceStack;
     
+    // Record the namespaces of START_ELEMENT and END_ELEMENT for SAXSource
+    private ArrayList eleNamespaces;
+    
     /**
      * The initialization method of DOMNamespaceContext, 
      * the DOMNamespaceContext is a singleton
      */
     public NamespaceContextImpl() {
         namespaceStack = new Stack();
+        eleNamespaces = new ArrayList();
     }
     
     /**
@@ -52,7 +56,8 @@
         if (!namespaceStack.empty()) {
             Iterator iter = namespaceStack.iterator();
             while(iter.hasNext()){
-                DOMNamespace dc = (DOMNamespace)iter.next();
+                Namespace dc = (Namespace)iter.next();
+                if(dc.level == 0) eleNamespaces.add(dc);
                 dc.increaseLevel();
             }
         }
@@ -69,9 +74,11 @@
             Iterator iter = namespaceStack.iterator();
             
             while (iter.hasNext()) {
-                DOMNamespace dc = (DOMNamespace)iter.next();
+                Namespace dc = (Namespace)iter.next();
                 int level = dc.decreaseLevel();
                 
+                // When encounter endElement event, the namespace whose level equals zero will not
+                // removed from namespace stack util next end element
                 if(level <= 0){
                     int index = namespaceStack.indexOf(dc);
                     int deleteNum = namespaceStack.size() - index;
@@ -84,13 +91,49 @@
     }
     
     /**
+     * Get the ArrayList which records the namespaces of element.
+     * This method is for SAXSource. 
+     * 
+     * @return
+     */
+    protected ArrayList getNamespaces() {
+        return eleNamespaces;
+    }
+    
+    /**
+     * Get the prefix of element namespace at specified index
+     * This method is for SAXSource
+     * 
+     * @param index
+     * @return
+     */
+    protected String getNamespacePrefix(int index) {
+        Namespace dm = (Namespace)eleNamespaces.get(index);
+        
+        return dm.getPrefix();
+    }
+    
+    /**
+     * Get the uri of element namespace at specified index.
+     * This method is for SAXSource
+     * 
+     * @param index
+     * @return
+     */
+    protected String getNamespaceURI(int index) {
+        Namespace dm = (Namespace)eleNamespaces.get(index);
+        
+        return dm.getNamespaceURI();
+    }
+    
+    /**
      * Add the prefix and namespaceURI to the namespace stack
      * 
      * @param prefix
      * @param namespaceURI
      */
     public void addNamespace(String prefix, String namespaceURI) {
-        DOMNamespace dn = new DOMNamespace(prefix, namespaceURI);
+        Namespace dn = new Namespace(prefix, namespaceURI);
         namespaceStack.push(dn);
     }
     
@@ -107,7 +150,7 @@
         int size = namespaceStack.size();
         
         while (size > 0) {
-            DOMNamespace dc = (DOMNamespace) namespaceStack.elementAt(--size);
+            Namespace dc = (Namespace) namespaceStack.elementAt(--size);
             String pre = dc.getPrefix();
             if (prefix.equals(pre))
                 return dc.getNamespaceURI();
@@ -129,7 +172,7 @@
         int size = namespaceStack.size();
         
         while (size > 0) {
-            DOMNamespace dc = (DOMNamespace) namespaceStack.elementAt(--size);
+            Namespace dc = (Namespace) namespaceStack.elementAt(--size);
             String namespace = dc.getNamespaceURI();
             if (namespaceURI.equals(namespace))
                 return dc.getPrefix();
@@ -149,7 +192,7 @@
         if (!namespaceStack.empty()) {
             Iterator iter = namespaceStack.iterator();
             while(iter.hasNext()){
-                DOMNamespace dc = (DOMNamespace)iter.next();
+                Namespace dc = (Namespace)iter.next();
                 String namespace = dc.getNamespaceURI();
                 if(namespaceURI.equals(namespace))
                     prefixes.add(dc.getPrefix());
@@ -160,17 +203,17 @@
     }
     
     /**
-     * Class to represent the namespace in DOMSource
+     * Class to represent the namespace in DOMSource and SAXSource
      * 
      * @author Hua Lei
      */
-    final class DOMNamespace {
+    final class Namespace {
         
         private String prefix;
         private String namespaceURI;
         private int level;
         
-        DOMNamespace(String prefix, String namespaceURI) {
+        Namespace(String prefix, String namespaceURI) {
             this.prefix = prefix;
             this.namespaceURI = namespaceURI;
             level = 0;

Added: xerces/java/branches/stax-dev/src/org/apache/xerces/stax/SAXLocation.java
URL: http://svn.apache.org/viewvc/xerces/java/branches/stax-dev/src/org/apache/xerces/stax/SAXLocation.java?rev=426914&view=auto
==============================================================================
--- xerces/java/branches/stax-dev/src/org/apache/xerces/stax/SAXLocation.java (added)
+++ xerces/java/branches/stax-dev/src/org/apache/xerces/stax/SAXLocation.java Sun Jul 30 11:48:24 2006
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed 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.xerces.stax;
+
+import org.xml.sax.Locator;
+
+import javax.xml.stream.Location;
+
+/**
+ * <p>Location wrapper around a SAX Locator.</p>
+ * 
+ * @version $Id$
+ */
+public final class SAXLocation implements Location {
+    
+    private Locator loc;
+    
+    public SAXLocation(Locator loc) {
+        this.loc = loc;
+    }
+    
+    /**
+     * Return the line number where the current event ends,
+     * returns -1 if none is available.
+     * @return the current line number
+     */
+    public int getLineNumber() {
+        return loc.getLineNumber();
+    }
+    
+    /**
+     * Return the column number where the current event ends,
+     * returns -1 if none is available.
+     * @return the current column number
+     */
+    public int getColumnNumber() {
+        return loc.getColumnNumber();
+    }
+    
+    /**
+     * Return the byte or character offset into the input source this location
+     * is pointing to. If the input source is a file or a byte stream then 
+     * this is the byte offset into that stream, but if the input source is 
+     * a character media then the offset is the character offset. 
+     * Returns -1 if there is no offset available.
+     * @return the current offset
+     */
+    public int getCharacterOffset() {
+        return -1;
+    }
+    
+    /**
+     * Returns the public ID of the XML
+     * @return the public ID, or null if not available
+     */
+    public String getPublicId() {
+        return loc.getPublicId();
+    }
+    
+    /**
+     * Returns the system ID of the XML
+     * @return the system ID, or null if not available
+     */
+    public String getSystemId() {
+        return loc.getSystemId();
+    }
+    
+}
+

Propchange: xerces/java/branches/stax-dev/src/org/apache/xerces/stax/SAXLocation.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xerces/java/branches/stax-dev/src/org/apache/xerces/stax/SAXLocation.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: xerces/java/branches/stax-dev/src/org/apache/xerces/stax/SAXXMLStreamReaderImpl.java
URL: http://svn.apache.org/viewvc/xerces/java/branches/stax-dev/src/org/apache/xerces/stax/SAXXMLStreamReaderImpl.java?rev=426914&r1=426913&r2=426914&view=diff
==============================================================================
--- xerces/java/branches/stax-dev/src/org/apache/xerces/stax/SAXXMLStreamReaderImpl.java (original)
+++ xerces/java/branches/stax-dev/src/org/apache/xerces/stax/SAXXMLStreamReaderImpl.java Sun Jul 30 11:48:24 2006
@@ -29,7 +29,9 @@
 
 import org.xml.sax.Attributes;
 import org.xml.sax.InputSource;
+import org.xml.sax.Locator;
 import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.LocatorImpl;
 
 /**
  * <p>An XMLStreamReader created from a SAXSource.</p>
@@ -52,6 +54,11 @@
     // The current event type
     private int curType;
     
+    // Record the location of SAX parser
+    private Locator loc;
+    
+    private SAXLocation sl;
+    
     // The attribute of element event
     private Attributes attrs;
     
@@ -83,14 +90,17 @@
      * @param xif
      */
     public SAXXMLStreamReaderImpl(XMLReader xr, InputSource is, XMLInputFactory xif)
-        throws XMLStreamException {
+    throws XMLStreamException {
         
         this.xif = xif;
         this.curAttrs = new ArrayList();
         
         try {
             asp = new AsyncSAXParser(xr, is);
-            handler = new StAXSAXHandler(asp, this);
+            loc = new LocatorImpl();
+            sl = new SAXLocation(loc);		
+            
+            handler = new StAXSAXHandler(asp, this, loc);
             
             xr.setContentHandler(handler);
             xr.setDTDHandler(handler);
@@ -185,7 +195,7 @@
             this.attrs = attrs;
             if (attrs != null) {
                 for (int i = 0; i < attrs.getLength(); i++) {
-                                       
+                    
                     String name = attrs.getQName(i);
                     String value = attrs.getValue(i);
                     String type = attrs.getType(i);
@@ -400,10 +410,15 @@
      */
     public String getAttributeValue(String namespaceURI,
             String localName) {
-        if (curType == XMLStreamConstants.START_ELEMENT) {
-            String value = attrs.getValue(namespaceURI, localName);
+        if(curType == XMLStreamConstants.START_ELEMENT){
+            String value = null;
+            for(int i = 0; i < curAttrs.size(); i++){
+                Attribute attr = (Attribute)curAttrs.get(i);
+                if(attr.local.equals(localName)) return attr.value;
+            }
             return value;
         }
+        
         throw new IllegalStateException("Current event is not START_ELEMENT or ATTRIBUTE");
     }
     
@@ -582,12 +597,13 @@
      * @throws IllegalStateException if this is not a START_ELEMEN or, END_ELEMENT
      */
     public int getNamespaceCount() { 
-        if (curType == XMLStreamConstants.START_ELEMENT || curType == XMLStreamConstants.END_ELEMENT) {
-            return 0;
+        if(curType == XMLStreamConstants.START_ELEMENT || curType == XMLStreamConstants.END_ELEMENT){
+            ArrayList al = dc.getNamespaces();
+            
+            return al.size();
         }
-        if (curType == XMLStreamConstants.NAMESPACE) {
+        if(curType == XMLStreamConstants.NAMESPACE)
             return 1;
-        }
         return 0;
     }
     
@@ -602,7 +618,17 @@
      *    END_ELEMENT or NAMESPACE
      */
     public String getNamespacePrefix(int index) {
-        return null;
+        int leng = getNamespaceCount();
+        
+        if(index + 1 > leng || index < 0)
+            throw new IndexOutOfBoundsException("The index "+ index+ " should be between 0..."+ (leng-1));
+        
+        if(curType == XMLStreamConstants.START_ELEMENT || curType == XMLStreamConstants.START_ELEMENT) { 
+            return dc.getNamespacePrefix(index);
+        }
+        else
+            throw new IllegalStateException(
+            "Current event is not START_ELEMENT, END_ELEMENT or ATTRIBUTE");	
     }
     
     /**
@@ -615,7 +641,17 @@
      *   END_ELEMENT or NAMESPACE
      */
     public String getNamespaceURI(int index) {
-        return null;	
+        int leng = getNamespaceCount();
+        
+        if(index + 1 > leng || index < 0)
+            throw new IndexOutOfBoundsException("The index "+ index+ " should be between 0..."+ (leng-1));
+        
+        if(curType == XMLStreamConstants.START_ELEMENT || curType == XMLStreamConstants.START_ELEMENT) { 
+            return dc.getNamespaceURI(index);
+        }
+        else
+            throw new IllegalStateException(
+            "Current event is not START_ELEMENT, END_ELEMENT or ATTRIBUTE");		
     }
     
     /**
@@ -655,7 +691,14 @@
      * a valid text state.
      */
     public String getText() {
-        return null;
+        if(curType == XMLStreamConstants.CHARACTERS || curType == XMLStreamConstants.SPACE){
+            char[] chars = asp.getCharacters();
+            
+            return  String.valueOf(chars);
+        }
+        
+        throw new IllegalStateException(
+        "The current event is not a valid text state.");	
     }
     
     /**
@@ -695,7 +738,7 @@
      * @throws NullPointerException is if target is null
      */
     public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length) 
-        throws XMLStreamException {
+    throws XMLStreamException {
         
         if (target == null)
             throw new NullPointerException();
@@ -755,7 +798,7 @@
      * @return the encoding of this instance or null
      */
     public String getEncoding() {
-        return null;
+        return inputEncoding;
     }
     
     /**
@@ -780,7 +823,7 @@
      * called.
      */
     public Location getLocation() {
-        return null;
+        return sl;
     }
     
     /**
@@ -809,8 +852,16 @@
      * END_ELEMENT or ENTITY_REFERENCE
      */
     public String getLocalName() {
-        
-        return null;
+        if(curType == XMLStreamConstants.START_ELEMENT || curType == XMLStreamConstants.END_ELEMENT) { 
+            String local = asp.getElementName();
+            int indexPre = local.indexOf(":");
+            if (indexPre != -1){
+                local = local.substring(indexPre + 1);		
+            }
+            return local;
+        }
+        else
+            throw new IllegalStateException("Current event is not START_ELEMENT, END_ELEMENT or ENTITY_REFERENCE");
     }
     
     /**
@@ -835,9 +886,10 @@
      *    or ATTRIBUTE
      */
     public String getNamespaceURI() {
+        String prefix = getPrefix();  
         
-        
-        return null;
+        if(prefix == null) prefix = ""; 
+        return dc.getNamespaceURI(prefix);
     }
     
     /**
@@ -847,8 +899,17 @@
      * @throws IllegalStateException if this is not a START_ELEMENT or END_ELEMENT
      */
     public String getPrefix() {
-        
-        return null;
+        if(curType == XMLStreamConstants.START_ELEMENT || curType == XMLStreamConstants.START_ELEMENT) { 
+            String pre = null;
+            String name = asp.getElementName();
+            int indexPre = name.indexOf(":");
+            if(indexPre != -1){
+                pre = name.substring(0, indexPre);		
+            }
+            return pre;
+        }
+        else
+            throw new IllegalStateException("Current event is not START_ELEMENT, END_ELEMENT");
     }
     
     /**

Modified: xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXSAXHandler.java
URL: http://svn.apache.org/viewvc/xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXSAXHandler.java?rev=426914&r1=426913&r2=426914&view=diff
==============================================================================
--- xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXSAXHandler.java (original)
+++ xerces/java/branches/stax-dev/src/org/apache/xerces/stax/StAXSAXHandler.java Sun Jul 30 11:48:24 2006
@@ -19,6 +19,7 @@
 import javax.xml.stream.XMLStreamConstants;
 
 import org.xml.sax.Attributes;
+import org.xml.sax.Locator;
 import org.xml.sax.helpers.DefaultHandler;
 
 /**
@@ -30,10 +31,12 @@
     
     private AsyncSAXParser asp;
     private SAXXMLStreamReaderImpl reader;
+    private Locator loc;
     
-    public StAXSAXHandler(AsyncSAXParser asp, SAXXMLStreamReaderImpl reader) {
+    public StAXSAXHandler(AsyncSAXParser asp, SAXXMLStreamReaderImpl reader, Locator loc) {
         this.asp = asp;
         this.reader = reader;  
+        this.loc = loc;
     }
     
     public void characters(char[] ch, int start, int length) {
@@ -140,6 +143,8 @@
             synchronized (asp) {
                 while (asp.getRunningFlag() == false)
                     asp.wait();
+                
+                setDocumentLocator(loc);
                 
                 reader.setCurType(XMLStreamConstants.START_DOCUMENT);
                 



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