tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jboy...@apache.org
Subject svn commit: r416162 - in /incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource: DataObjectXMLStreamReader.java SDOXMLResourceImpl.java XMLDocumentStreamReader.java
Date Wed, 21 Jun 2006 23:10:00 GMT
Author: jboynes
Date: Wed Jun 21 16:09:59 2006
New Revision: 416162

URL: http://svn.apache.org/viewvc?rev=416162&view=rev
Log:
apply patch from Raymond Feng for stax serialization improvements

Modified:
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/DataObjectXMLStreamReader.java
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/SDOXMLResourceImpl.java
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/XMLDocumentStreamReader.java

Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/DataObjectXMLStreamReader.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/DataObjectXMLStreamReader.java?rev=416162&r1=416161&r2=416162&view=diff
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/DataObjectXMLStreamReader.java
(original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/DataObjectXMLStreamReader.java
Wed Jun 21 16:09:59 2006
@@ -862,7 +862,7 @@
      */
     private void registerNamespace(String prefix, String uri) {
         if (!uri.equals(namespaceContext.getNamespaceURI(prefix))) {
-            namespaceContext.put(prefix, uri);
+            namespaceContext.registerMapping(prefix, uri);
             declaredNamespaceMap.put(prefix, uri);
         }
     }
@@ -1301,7 +1301,7 @@
             // namespaces are having no prefixes
             if (!uri.equals(namespaceContext.getNamespaceURI(prefix))) {
                 // this namespace is not there. Need to declare it
-                namespaceContext.put(prefix, uri);
+                namespaceContext.registerMapping(prefix, uri);
             }
         }
 
@@ -1416,19 +1416,22 @@
     protected static class DelegatingNamespaceContext implements NamespaceContext {
         private NamespaceContext parent;
 
-        private Map nsMap = new HashMap();
+        private Map prefixToNamespaceMapping = new HashMap();
 
         public DelegatingNamespaceContext(NamespaceContext parent) {
             super();
             this.parent = parent;
 
-            nsMap.put("xml", "http://www.w3.org/XML/1998/namespace");
-            nsMap.put("xmlns", "http://www.w3.org/2000/xmlns/");
-            nsMap.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");
+            prefixToNamespaceMapping.put("xml", "http://www.w3.org/XML/1998/namespace");
+            prefixToNamespaceMapping.put("xmlns", "http://www.w3.org/2000/xmlns/");
+            prefixToNamespaceMapping.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");
         }
 
         public String getNamespaceURI(String prefix) {
-            String ns = (String) nsMap.get(prefix);
+            if (prefix == null)
+                throw new IllegalArgumentException("Prefix is null");
+
+            String ns = (String) prefixToNamespaceMapping.get(prefix);
             if (ns != null)
                 return ns;
             else if (parent != null)
@@ -1440,7 +1443,7 @@
         public String getPrefix(String nsURI) {
             if (nsURI == null)
                 throw new IllegalArgumentException("Namespace is null");
-            for (Iterator i = nsMap.entrySet().iterator(); i.hasNext();) {
+            for (Iterator i = prefixToNamespaceMapping.entrySet().iterator(); i.hasNext();)
{
                 Map.Entry entry = (Map.Entry) i.next();
                 if (entry.getValue().equals(nsURI)) {
                     return (String) entry.getKey();
@@ -1454,17 +1457,22 @@
 
         public Iterator getPrefixes(String nsURI) {
             List prefixList = new ArrayList();
-            for (Iterator i = nsMap.entrySet().iterator(); i.hasNext();) {
+            for (Iterator i = prefixToNamespaceMapping.entrySet().iterator(); i.hasNext();)
{
                 Map.Entry entry = (Map.Entry) i.next();
                 if (entry.getValue().equals(nsURI)) {
                     prefixList.add(entry.getKey());
                 }
             }
+            if (parent != null) {
+                for (Iterator i = parent.getPrefixes(nsURI); i.hasNext();) {
+                    prefixList.add(i.next());
+                }
+            }
             return prefixList.iterator();
         }
 
-        public void put(String prefix, String nsURI) {
-            nsMap.put(prefix, nsURI);
+        public void registerMapping(String prefix, String nsURI) {
+            prefixToNamespaceMapping.put(prefix, nsURI);
         }
 
         private int counter = 0;
@@ -1476,12 +1484,12 @@
             if (prefix == null)
                 prefix = "";
             if (nsURI != null)
-                nsMap.put(prefix, nsURI);
+                prefixToNamespaceMapping.put(prefix, nsURI);
             return new QName(nsURI, name, prefix);
         }
 
-        public void remove(String prefix) {
-            nsMap.remove(prefix);
+        public void removeMapping(String prefix) {
+            prefixToNamespaceMapping.remove(prefix);
         }
 
         public void setParent(NamespaceContext parent) {

Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/SDOXMLResourceImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/SDOXMLResourceImpl.java?rev=416162&r1=416161&r2=416162&view=diff
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/SDOXMLResourceImpl.java
(original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/SDOXMLResourceImpl.java
Wed Jun 21 16:09:59 2006
@@ -79,7 +79,7 @@
 
                     uri = null;
                 }
-                return uri != null ? uri : super.getURI(prefix);
+                return uri != null && uri.length()!=0 ? uri : super.getURI(prefix);
             }
 
             public StreamNamespaceSupport(XMLStreamReader reader) {

Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/XMLDocumentStreamReader.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/XMLDocumentStreamReader.java?rev=416162&r1=416161&r2=416162&view=diff
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/XMLDocumentStreamReader.java
(original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/XMLDocumentStreamReader.java
Wed Jun 21 16:09:59 2006
@@ -3,6 +3,8 @@
  */
 package org.apache.tuscany.sdo.util.resource;
 
+import java.util.NoSuchElementException;
+
 import javax.xml.namespace.NamespaceContext;
 import javax.xml.namespace.QName;
 import javax.xml.stream.Location;
@@ -10,22 +12,24 @@
 import javax.xml.stream.XMLStreamReader;
 
 /**
- * This class is derived from Apache Axis2 class 
- * <a href="http://svn.apache.org/repos/asf/webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/util/StreamWrapper.java">
- * org.apache.axis2.util.StreamWrapper</a>. It's used wrap a XMLStreamReader to create
a XMLStreamReader representing a document and
- * it will produce START_DOCUMENT, END_DOCUMENT events.
- *
+ * This class is derived from Apache Axis2 class <a
+ * href="http://svn.apache.org/repos/asf/webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/util/StreamWrapper.java">
+ * org.apache.axis2.util.StreamWrapper</a>. It's used wrap a XMLStreamReader to create
a XMLStreamReader representing a document and it will produce
+ * START_DOCUMENT, END_DOCUMENT events.
+ * 
  */
 public class XMLDocumentStreamReader implements XMLStreamReader {
-    private static final int STATE_SWITCHED = 0;
-    private static final int STATE_INIT = 1;
-    private static final int STATE_SWITCH_AT_NEXT = 2;
-    private static final int STATE_COMPLETE_AT_NEXT = 3;
-    private static final int STATE_COMPLETED = 4;
+    private static final int STATE_INIT = 0; // The wrapper will produce START_DOCUMENT
+
+    private static final int STATE_SWITCHED = 1; // The real reader will produce events
+
+    private static final int STATE_COMPLETE_AT_NEXT = 2; // The wrapper will produce END_DOCUMENT
+
+    private static final int STATE_COMPLETED = 3; // Done
+
     private XMLStreamReader realReader = null;
-    private int state = STATE_INIT;
-    private int prevState = state;
 
+    private int state = STATE_INIT;
 
     public XMLDocumentStreamReader(XMLStreamReader realReader) {
         if (realReader == null) {
@@ -33,63 +37,52 @@
         }
 
         this.realReader = realReader;
+
+        // If the real reader is positioned at START_DOCUMENT, always use the real reader
+        if (realReader.getEventType() == START_DOCUMENT)
+            state = STATE_SWITCHED;
     }
 
     public void close() throws XMLStreamException {
-        if (prevState != STATE_INIT) {
-            realReader.close();
-        } else {
-            throw new XMLStreamException();
-        }
+        realReader.close();
     }
 
     public int next() throws XMLStreamException {
-        prevState = state;
         int returnEvent = -1;
 
         switch (state) {
-            // Commented out by Raymond Feng. It's a bug in the Axis2 code and it will produce
START_DOCUMENT twice
-            /*
-            case STATE_INIT:
-                if (realReader.getEventType() == START_DOCUMENT) {
-                    state = STATE_SWITCHED;
-                    returnEvent = realReader.getEventType();
-                } else {
-                    state = STATE_SWITCH_AT_NEXT;
-                    returnEvent = START_DOCUMENT;
-                }
-                break;
-            */    
-            case STATE_SWITCHED:
-                returnEvent = realReader.next();
-                if (returnEvent == END_DOCUMENT) {
-                    state = STATE_COMPLETED;
-                } else if (!realReader.hasNext()) {
-                    state = STATE_COMPLETE_AT_NEXT;
-                }
-                break;
-            case STATE_INIT:
-                prevState = STATE_SWITCH_AT_NEXT;
-            case STATE_SWITCH_AT_NEXT:
-                state = STATE_SWITCHED;
-                returnEvent = realReader.getEventType();
-                break;
-            case STATE_COMPLETE_AT_NEXT:
+        case STATE_SWITCHED:
+            returnEvent = realReader.next();
+            if (returnEvent == END_DOCUMENT) {
                 state = STATE_COMPLETED;
-                returnEvent = END_DOCUMENT;
-                break;
-            case STATE_COMPLETED:
-                //oops - no way we can go beyond this
-                throw new XMLStreamException("end reached!");
-            default:
-                throw new UnsupportedOperationException();
+            } else if (!realReader.hasNext()) {
+                state = STATE_COMPLETE_AT_NEXT;
+            }
+            break;
+        case STATE_INIT:
+            state = STATE_SWITCHED;
+            returnEvent = realReader.getEventType();
+            break;
+        case STATE_COMPLETE_AT_NEXT:
+            state = STATE_COMPLETED;
+            returnEvent = END_DOCUMENT;
+            break;
+        case STATE_COMPLETED:
+            // oops - no way we can go beyond this
+            throw new NoSuchElementException("End of stream has reached.");
+        default:
+            throw new UnsupportedOperationException();
         }
 
         return returnEvent;
     }
 
+    private boolean isDelegating() {
+        return state == STATE_SWITCHED || state == STATE_COMPLETE_AT_NEXT;
+    }
+
     public int nextTag() throws XMLStreamException {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.nextTag();
         } else {
             throw new XMLStreamException();
@@ -97,13 +90,13 @@
     }
 
     public void require(int i, String s, String s1) throws XMLStreamException {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             realReader.require(i, s, s1);
         }
     }
 
     public boolean standaloneSet() {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.standaloneSet();
         } else {
             return false;
@@ -111,79 +104,75 @@
     }
 
     public int getAttributeCount() {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getAttributeCount();
         } else {
-            return 0;
+            throw new IllegalStateException();
         }
     }
 
     public String getAttributeLocalName(int i) {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getAttributeLocalName(i);
         } else {
-            return null;
+            throw new IllegalStateException();
         }
     }
 
     public QName getAttributeName(int i) {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getAttributeName(i);
         } else {
-            return null;
+            throw new IllegalStateException();
         }
     }
 
     public String getAttributeNamespace(int i) {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getAttributeNamespace(i);
         } else {
-            return null;
+            throw new IllegalStateException();
         }
     }
 
     public String getAttributePrefix(int i) {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getAttributePrefix(i);
         } else {
-            return null;
+            throw new IllegalStateException();
         }
     }
 
     public String getAttributeType(int i) {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getAttributeType(i);
         } else {
-            return null;
+            throw new IllegalStateException();
         }
     }
 
     public String getAttributeValue(int i) {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getAttributeValue(i);
         } else {
-            return null;
+            throw new IllegalStateException();
         }
     }
 
     public String getAttributeValue(String s, String s1) {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getAttributeValue(s, s1);
         } else {
-            return null;
+            throw new IllegalStateException();
         }
     }
 
     public String getCharacterEncodingScheme() {
-        if (prevState != STATE_INIT) {
-            return realReader.getCharacterEncodingScheme();
-        } else {
-            return null;
-        }
+        return realReader.getCharacterEncodingScheme();
     }
 
     public String getElementText() throws XMLStreamException {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getElementText();
         } else {
             throw new XMLStreamException();
@@ -191,31 +180,28 @@
     }
 
     public String getEncoding() {
-        if (prevState != STATE_INIT) {
-            return realReader.getEncoding();
-        } else {
-            return null;
-        }
+        return realReader.getEncoding();
     }
 
     public int getEventType() {
-        if (prevState == STATE_INIT) {
-            return START_DOCUMENT;
-        } else {
-            return realReader.getEventType();
-        }
-    }
-
-    public String getLocalName() {
-        if (prevState != STATE_INIT) {
-            return realReader.getLocalName();
-        } else {
-            return null;
+        int event = -1;
+        switch (state) {
+        case STATE_SWITCHED:
+        case STATE_COMPLETE_AT_NEXT:
+            event = realReader.getEventType();
+            break;
+        case STATE_INIT:
+            event = START_DOCUMENT;
+            break;
+        case STATE_COMPLETED:
+            event = END_DOCUMENT;
+            break;
         }
+        return event;
     }
 
     public Location getLocation() {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getLocation();
         } else {
             return null;
@@ -223,87 +209,91 @@
     }
 
     public QName getName() {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getName();
         } else {
-            return null;
+            throw new IllegalStateException();
         }
     }
 
-    public NamespaceContext getNamespaceContext() {
-        if (prevState != STATE_INIT) {
-            return realReader.getNamespaceContext();
+    public String getLocalName() {
+        if (isDelegating()) {
+            return realReader.getLocalName();
         } else {
-            return null;
+            throw new IllegalStateException();
         }
     }
 
+    public NamespaceContext getNamespaceContext() {
+        return realReader.getNamespaceContext();
+    }
+
     public int getNamespaceCount() {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getNamespaceCount();
         } else {
-            return 0;
+            throw new IllegalStateException();
         }
     }
 
     public String getNamespacePrefix(int i) {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getNamespacePrefix(i);
         } else {
-            return null;
+            throw new IllegalStateException();
         }
     }
 
     public String getNamespaceURI() {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getNamespaceURI();
         } else {
-            return null;
+            throw new IllegalStateException();
         }
     }
 
     public String getNamespaceURI(int i) {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getNamespaceURI(i);
         } else {
-            return null;
+            throw new IllegalStateException();
         }
     }
 
     public String getNamespaceURI(String s) {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getNamespaceURI(s);
         } else {
-            return null;
+            throw new IllegalStateException();
         }
     }
 
     public String getPIData() {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getPIData();
         } else {
-            return null;
+            throw new IllegalStateException();
         }
     }
 
     public String getPITarget() {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getPITarget();
         } else {
-            return null;
+            throw new IllegalStateException();
         }
     }
 
     public String getPrefix() {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getPrefix();
         } else {
-            return null;
+            throw new IllegalStateException();
         }
     }
 
     public Object getProperty(String s) throws IllegalArgumentException {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getProperty(s);
         } else {
             throw new IllegalArgumentException();
@@ -311,47 +301,47 @@
     }
 
     public String getText() {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getText();
         } else {
-            return null;
+            throw new IllegalStateException();
         }
     }
 
     public char[] getTextCharacters() {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getTextCharacters();
         } else {
-            return new char[0];
+            throw new IllegalStateException();
         }
     }
 
     public int getTextCharacters(int i, char[] chars, int i1, int i2) throws XMLStreamException
{
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getTextCharacters(i, chars, i1, i2);
         } else {
-            return 0;
+            throw new IllegalStateException();
         }
     }
 
     public int getTextLength() {
-        if (prevState != STATE_INIT) {
-            return realReader.getTextStart();
+        if (isDelegating()) {
+            return realReader.getTextLength();
         } else {
-            return 0;
+            throw new IllegalStateException();
         }
     }
 
     public int getTextStart() {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getTextStart();
         } else {
-            return 0;
+            throw new IllegalStateException();
         }
     }
 
     public String getVersion() {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.getVersion();
         } else {
             return null;
@@ -359,7 +349,7 @@
     }
 
     public boolean hasName() {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.hasName();
         } else {
             return false;
@@ -371,7 +361,7 @@
             return true;
         } else if (state == STATE_COMPLETED) {
             return false;
-        } else if (prevState != STATE_INIT) {
+        } else if (state == STATE_SWITCHED) {
             return realReader.hasNext();
         } else {
             return true;
@@ -379,7 +369,7 @@
     }
 
     public boolean hasText() {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.hasText();
         } else {
             return false;
@@ -387,7 +377,7 @@
     }
 
     public boolean isAttributeSpecified(int i) {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.isAttributeSpecified(i);
         } else {
             return false;
@@ -395,7 +385,7 @@
     }
 
     public boolean isCharacters() {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.isCharacters();
         } else {
             return false;
@@ -403,7 +393,7 @@
     }
 
     public boolean isEndElement() {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.isEndElement();
         } else {
             return false;
@@ -411,7 +401,7 @@
     }
 
     public boolean isStandalone() {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.isStandalone();
         } else {
             return false;
@@ -419,7 +409,7 @@
     }
 
     public boolean isStartElement() {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.isStartElement();
         } else {
             return false;
@@ -427,7 +417,7 @@
     }
 
     public boolean isWhiteSpace() {
-        if (prevState != STATE_INIT) {
+        if (isDelegating()) {
             return realReader.isWhiteSpace();
         } else {
             return false;



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


Mime
View raw message