You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2006/09/01 02:53:38 UTC

svn commit: r439122 - in /incubator/tuscany/java/sca/databinding/databinding-framework: pom.xml src/main/java/org/apache/tuscany/databinding/xml/Node2XMLStreamReader.java src/main/java/org/apache/tuscany/databinding/xml/StAXHelper.java

Author: rfeng
Date: Thu Aug 31 17:53:38 2006
New Revision: 439122

URL: http://svn.apache.org/viewvc?rev=439122&view=rev
Log:
Remove the woodstax compile dependency 
Add our own impl of DOM2StAX

Modified:
    incubator/tuscany/java/sca/databinding/databinding-framework/pom.xml
    incubator/tuscany/java/sca/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/Node2XMLStreamReader.java
    incubator/tuscany/java/sca/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/StAXHelper.java

Modified: incubator/tuscany/java/sca/databinding/databinding-framework/pom.xml
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/databinding/databinding-framework/pom.xml?rev=439122&r1=439121&r2=439122&view=diff
==============================================================================
--- incubator/tuscany/java/sca/databinding/databinding-framework/pom.xml (original)
+++ incubator/tuscany/java/sca/databinding/databinding-framework/pom.xml Thu Aug 31 17:53:38 2006
@@ -40,27 +40,9 @@
         <dependency>
             <groupId>woodstox</groupId>
             <artifactId>wstx-asl</artifactId>
-            <scope>compile</scope>
-            <optional>true</optional>
-        </dependency>
-
-
-<!-- 
-        <dependency>
-            <groupId>xerces</groupId>
-            <artifactId>xercesImpl</artifactId>
-            <version>2.6.0</version>
             <scope>test</scope>
+            <optional>true</optional>
         </dependency>
-
-
-        <dependency>
-            <groupId>xalan</groupId>
-            <artifactId>xalan</artifactId>
-            <version>2.7.0</version>
-            <scope>compile</scope>
-        </dependency>
- -->      
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>

Modified: incubator/tuscany/java/sca/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/Node2XMLStreamReader.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/Node2XMLStreamReader.java?rev=439122&r1=439121&r2=439122&view=diff
==============================================================================
--- incubator/tuscany/java/sca/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/Node2XMLStreamReader.java (original)
+++ incubator/tuscany/java/sca/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/Node2XMLStreamReader.java Thu Aug 31 17:53:38 2006
@@ -19,17 +19,14 @@
 package org.apache.tuscany.databinding.xml;
 
 import javax.xml.stream.XMLStreamReader;
-import javax.xml.transform.dom.DOMSource;
 
 import org.apache.tuscany.databinding.PullTransformer;
 import org.apache.tuscany.databinding.TransformationContext;
 import org.apache.tuscany.databinding.TransformationException;
 import org.apache.tuscany.databinding.extension.TransformerExtension;
+import org.apache.tuscany.databinding.xml.StAXHelper.XMLDocumentStreamReader;
 import org.w3c.dom.Node;
 
-import com.ctc.wstx.api.ReaderConfig;
-import com.ctc.wstx.dom.DOMWrappingReader;
-
 /**
  * Transform DOM Node to XML XMLStreamReader
  * 
@@ -38,10 +35,8 @@
 
     public XMLStreamReader transform(Node source, TransformationContext context) {
         try {
-            DOMSource domSource = new DOMSource(source);
-            ReaderConfig config = ReaderConfig.createFullDefaults();
-            DOMWrappingReader wrappingReader = DOMWrappingReader.createFrom(config, domSource);
-            return wrappingReader;
+            DOMXMLStreamReader reader = new DOMXMLStreamReader(source);
+            return new XMLDocumentStreamReader(reader);
         } catch (Exception e) {
             throw new TransformationException(e);
         }

Modified: incubator/tuscany/java/sca/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/StAXHelper.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/StAXHelper.java?rev=439122&r1=439121&r2=439122&view=diff
==============================================================================
--- incubator/tuscany/java/sca/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/StAXHelper.java (original)
+++ incubator/tuscany/java/sca/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/StAXHelper.java Thu Aug 31 17:53:38 2006
@@ -24,8 +24,11 @@
 import java.io.StringReader;
 import java.io.StringWriter;
 import java.io.Writer;
+import java.util.NoSuchElementException;
 
 import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.stream.Location;
 import javax.xml.stream.XMLInputFactory;
 import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamConstants;
@@ -355,5 +358,447 @@
         save(reader, writer);
         return writer.toString();
     }
+    
+    public static interface XMLFragmentStreamReader extends XMLStreamReader {
+
+        // this will help to handle Text within the current element.
+        // user should pass the element text to the property list as this
+        // ELEMENT_TEXT as the key. This key deliberately has a space in it
+        // so that it is not a valid XML name
+        static final String ELEMENT_TEXT = "Element Text";
+
+        /**
+         * Extra method to query the state of the pullparser
+         * @return
+         */
+         boolean isEndOfFragment();
+
+        /**
+         * add the parent namespace context to this parser
+         */
+         void setParentNamespaceContext(NamespaceContext nsContext);
+
+        /**
+         * Initiate the parser - this will do whatever the needed
+         * tasks to initiate the parser and must be called before
+         * attempting any specific parsing using this parser
+         */
+         void init();
+    }
+    
+    /**
+     * 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 static class XMLDocumentStreamReader implements XMLStreamReader {
+        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;
+
+        public XMLDocumentStreamReader(XMLStreamReader realReader) {
+            if (realReader == null) {
+                throw new UnsupportedOperationException("Reader cannot be null");
+            }
+
+            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 {
+            realReader.close();
+        }
+
+        public int next() throws XMLStreamException {
+            int returnEvent = -1;
+
+            switch (state) {
+            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:
+                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 (isDelegating()) {
+                return realReader.nextTag();
+            } else {
+                throw new XMLStreamException();
+            }
+        }
+
+        public void require(int i, String s, String s1) throws XMLStreamException {
+            if (isDelegating()) {
+                realReader.require(i, s, s1);
+            }
+        }
+
+        public boolean standaloneSet() {
+            if (isDelegating()) {
+                return realReader.standaloneSet();
+            } else {
+                return false;
+            }
+        }
+
+        public int getAttributeCount() {
+            if (isDelegating()) {
+                return realReader.getAttributeCount();
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public String getAttributeLocalName(int i) {
+            if (isDelegating()) {
+                return realReader.getAttributeLocalName(i);
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public QName getAttributeName(int i) {
+            if (isDelegating()) {
+                return realReader.getAttributeName(i);
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public String getAttributeNamespace(int i) {
+            if (isDelegating()) {
+                return realReader.getAttributeNamespace(i);
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public String getAttributePrefix(int i) {
+            if (isDelegating()) {
+                return realReader.getAttributePrefix(i);
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public String getAttributeType(int i) {
+            if (isDelegating()) {
+                return realReader.getAttributeType(i);
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public String getAttributeValue(int i) {
+            if (isDelegating()) {
+                return realReader.getAttributeValue(i);
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public String getAttributeValue(String s, String s1) {
+            if (isDelegating()) {
+                return realReader.getAttributeValue(s, s1);
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public String getCharacterEncodingScheme() {
+            return realReader.getCharacterEncodingScheme();
+        }
+
+        public String getElementText() throws XMLStreamException {
+            if (isDelegating()) {
+                return realReader.getElementText();
+            } else {
+                throw new XMLStreamException();
+            }
+        }
+
+        public String getEncoding() {
+            return realReader.getEncoding();
+        }
+
+        public int getEventType() {
+            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 (isDelegating()) {
+                return realReader.getLocation();
+            } else {
+                return null;
+            }
+        }
+
+        public QName getName() {
+            if (isDelegating()) {
+                return realReader.getName();
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public String getLocalName() {
+            if (isDelegating()) {
+                return realReader.getLocalName();
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public NamespaceContext getNamespaceContext() {
+            return realReader.getNamespaceContext();
+        }
+
+        public int getNamespaceCount() {
+            if (isDelegating()) {
+                return realReader.getNamespaceCount();
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public String getNamespacePrefix(int i) {
+            if (isDelegating()) {
+                return realReader.getNamespacePrefix(i);
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public String getNamespaceURI() {
+            if (isDelegating()) {
+                return realReader.getNamespaceURI();
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public String getNamespaceURI(int i) {
+            if (isDelegating()) {
+                return realReader.getNamespaceURI(i);
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public String getNamespaceURI(String s) {
+            if (isDelegating()) {
+                return realReader.getNamespaceURI(s);
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public String getPIData() {
+            if (isDelegating()) {
+                return realReader.getPIData();
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public String getPITarget() {
+            if (isDelegating()) {
+                return realReader.getPITarget();
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public String getPrefix() {
+            if (isDelegating()) {
+                return realReader.getPrefix();
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public Object getProperty(String s) throws IllegalArgumentException {
+            if (isDelegating()) {
+                return realReader.getProperty(s);
+            } else {
+                throw new IllegalArgumentException();
+            }
+        }
+
+        public String getText() {
+            if (isDelegating()) {
+                return realReader.getText();
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public char[] getTextCharacters() {
+            if (isDelegating()) {
+                return realReader.getTextCharacters();
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public int getTextCharacters(int i, char[] chars, int i1, int i2) throws XMLStreamException {
+            if (isDelegating()) {
+                return realReader.getTextCharacters(i, chars, i1, i2);
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public int getTextLength() {
+            if (isDelegating()) {
+                return realReader.getTextLength();
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public int getTextStart() {
+            if (isDelegating()) {
+                return realReader.getTextStart();
+            } else {
+                throw new IllegalStateException();
+            }
+        }
+
+        public String getVersion() {
+            if (isDelegating()) {
+                return realReader.getVersion();
+            } else {
+                return null;
+            }
+        }
+
+        public boolean hasName() {
+            if (isDelegating()) {
+                return realReader.hasName();
+            } else {
+                return false;
+            }
+        }
+
+        public boolean hasNext() throws XMLStreamException {
+            if (state == STATE_COMPLETE_AT_NEXT) {
+                return true;
+            } else if (state == STATE_COMPLETED) {
+                return false;
+            } else if (state == STATE_SWITCHED) {
+                return realReader.hasNext();
+            } else {
+                return true;
+            }
+        }
+
+        public boolean hasText() {
+            if (isDelegating()) {
+                return realReader.hasText();
+            } else {
+                return false;
+            }
+        }
+
+        public boolean isAttributeSpecified(int i) {
+            if (isDelegating()) {
+                return realReader.isAttributeSpecified(i);
+            } else {
+                return false;
+            }
+        }
+
+        public boolean isCharacters() {
+            if (isDelegating()) {
+                return realReader.isCharacters();
+            } else {
+                return false;
+            }
+        }
+
+        public boolean isEndElement() {
+            if (isDelegating()) {
+                return realReader.isEndElement();
+            } else {
+                return false;
+            }
+        }
+
+        public boolean isStandalone() {
+            if (isDelegating()) {
+                return realReader.isStandalone();
+            } else {
+                return false;
+            }
+        }
+
+        public boolean isStartElement() {
+            if (isDelegating()) {
+                return realReader.isStartElement();
+            } else {
+                return false;
+            }
+        }
+
+        public boolean isWhiteSpace() {
+            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