servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r413897 - in /incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp: DOMStreamReader.java DOMStreamWriter.java W3CDOMStreamReader.java W3CDOMStreamWriter.java W3CNamespaceContext.java
Date Tue, 13 Jun 2006 14:00:16 GMT
Author: gnodet
Date: Tue Jun 13 07:00:15 2006
New Revision: 413897

URL: http://svn.apache.org/viewvc?rev=413897&view=rev
Log:
Add an XMLStreamWriter implementation on top of DOM (borrowed from xfire)

Added:
    incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/DOMStreamWriter.java
    incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamWriter.java
    incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/W3CNamespaceContext.java
Modified:
    incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/DOMStreamReader.java
    incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamReader.java

Modified: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/DOMStreamReader.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/DOMStreamReader.java?rev=413897&r1=413896&r2=413897&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/DOMStreamReader.java
(original)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/DOMStreamReader.java
Tue Jun 13 07:00:15 2006
@@ -1,17 +1,6 @@
 /*
- * Copyright 2005-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.
+ * This implementation comes from the XFire project
+ * https://svn.codehaus.org/xfire/trunk/xfire/xfire-core/src/main/org/codehaus/xfire/util/stax/
  */
 package org.apache.servicemix.jbi.jaxp;
 
@@ -33,228 +22,263 @@
  * @author <a href="mailto:tsztelak@gmail.com">Tomasz Sztelak</a>
  */
 public abstract class DOMStreamReader implements XMLStreamReader {
-	public Map properties = new HashMap();
+    public Map properties = new HashMap();
 
-	private FastStack frames = new FastStack();
+    private FastStack frames = new FastStack();
 
-	private ElementFrame frame;
+    private ElementFrame frame;
 
-	private int currentEvent = XMLStreamConstants.START_DOCUMENT;
+    private int currentEvent = XMLStreamConstants.START_DOCUMENT;
 
-	/**
-	 *     
-	 */
-	public static class ElementFrame {
-		public ElementFrame(Object element) {
-			this.element = element;
-		}
-
-		Object element;
-
-		boolean started = false;
-
-		boolean ended = false;
-
-		int currentChild = -1;
-
-		int currentAttribute = -1;
-
-		int currentNamespace = -1;
-
-		List uris;
-
-		List prefixes;
-
-		List attributes;
-	}
-
-	/**
-	 * @param element
-	 */
-	public DOMStreamReader(ElementFrame frame) {
-		this.frame = frame;
-		frames.push(this.frame);
-		newFrame(frame);
-	}
-
-	protected ElementFrame getCurrentFrame() {
-		return frame;
-	}
-
-	/* (non-Javadoc)
-	 * @see javax.xml.stream.XMLStreamReader#getProperty(java.lang.String)
-	 */
-	public Object getProperty(String key) throws IllegalArgumentException {
-		return properties.get(key);
-	}
-
-	/* (non-Javadoc)
-	 * @see javax.xml.stream.XMLStreamReader#next()
-	 */
-	public int next() throws XMLStreamException {
-
-		if (frame.ended) {
-			frames.pop();
-			if (!frames.isEmpty()) {
-				frame = (ElementFrame) frames.peek();
-			} else {
-				currentEvent = END_DOCUMENT;
-				return currentEvent;
-			}
-		}
-
-		if (!frame.started) {
-			frame.started = true;
-			currentEvent = START_ELEMENT;
-		} else if (frame.currentAttribute < getAttributeCount() - 1) {
-			frame.currentAttribute++;
-			currentEvent = ATTRIBUTE;
-		} else if (frame.currentNamespace < getNamespaceCount() - 1) {
-			frame.currentNamespace++;
-			currentEvent = NAMESPACE;
-		} else if (frame.currentChild < getChildCount() - 1) {
-			frame.currentChild++;
-
-			currentEvent = moveToChild(frame.currentChild);
-
-			if (currentEvent == START_ELEMENT) {
-				ElementFrame newFrame = getChildFrame(frame.currentChild);
-				newFrame.started = true;
-				frame = newFrame;
-				frames.push(this.frame);
-				currentEvent = START_ELEMENT;
-
-				newFrame(newFrame);
-			}
-		} else {
-			frame.ended = true;
-			currentEvent = END_ELEMENT;
-			endElement();
-		}
-		return currentEvent;
-	}
-
-	protected void newFrame(ElementFrame newFrame) {
-	}
-
-	protected void endElement() {
-	}
-
-	protected abstract int moveToChild(int currentChild);
-
-	protected abstract ElementFrame getChildFrame(int currentChild);
-
-	protected abstract int getChildCount();
-
-	/* (non-Javadoc)
-	 * @see javax.xml.stream.XMLStreamReader#require(int, java.lang.String, java.lang.String)
-	 */
-	public void require(int arg0, String arg1, String arg2)
-			throws XMLStreamException {
-		throw new UnsupportedOperationException();
-	}
-
-	/* (non-Javadoc)
-	 * @see javax.xml.stream.XMLStreamReader#getElementText()
-	 */
-	public abstract String getElementText() throws XMLStreamException;
-
-	/* (non-Javadoc)
-	 * @see javax.xml.stream.XMLStreamReader#nextTag()
-	 */
-	public int nextTag() throws XMLStreamException {
-		while (hasNext()) {
-			if (START_ELEMENT == next()) {
-				return START_ELEMENT;
-			}
-		}
-		return currentEvent;
-	}
-
-	/* (non-Javadoc)
-	 * @see javax.xml.stream.XMLStreamReader#hasNext()
-	 */
-	public boolean hasNext() throws XMLStreamException {
-		return !(frames.size() == 0 && frame.ended);
-	}
-
-	/* (non-Javadoc)
-	 * @see javax.xml.stream.XMLStreamReader#close()
-	 */
-	public void close() throws XMLStreamException {
-	}
-
-	/* (non-Javadoc)
-	 * @see javax.xml.stream.XMLStreamReader#getNamespaceURI(java.lang.String)
-	 */
-	public abstract String getNamespaceURI(String prefix);
-
-	/* (non-Javadoc)
-	 * @see javax.xml.stream.XMLStreamReader#isStartElement()
-	 */
-	public boolean isStartElement() {
-		return (currentEvent == START_ELEMENT);
-	}
-
-	/* (non-Javadoc)
-	 * @see javax.xml.stream.XMLStreamReader#isEndElement()
-	 */
-	public boolean isEndElement() {
-		return (currentEvent == END_ELEMENT);
-	}
-
-	/* (non-Javadoc)
-	 * @see javax.xml.stream.XMLStreamReader#isCharacters()
-	 */
-	public boolean isCharacters() {
-		return (currentEvent == CHARACTERS);
-	}
-
-	/* (non-Javadoc)
-	 * @see javax.xml.stream.XMLStreamReader#isWhiteSpace()
-	 */
-	public boolean isWhiteSpace() {
-		return (currentEvent == SPACE);
-	}
-
-	public int getEventType() {
-		return currentEvent;
-	}
-
-	public int getTextCharacters(int sourceStart, char[] target,
-			int targetStart, int length) throws XMLStreamException {
-		throw new UnsupportedOperationException();
-	}
-
-	public boolean hasText() {
-		return (currentEvent == CHARACTERS || currentEvent == DTD
-				|| currentEvent == ENTITY_REFERENCE || currentEvent == COMMENT 
-				|| currentEvent == SPACE);
-	}
-
-	public Location getLocation() {
-		return null;
-	}
-
-	public boolean hasName() {
-		return (currentEvent == START_ELEMENT || currentEvent == END_ELEMENT);
-	}
-
-	public String getVersion() {
-		return null;
-	}
-
-	public boolean isStandalone() {
-		return false;
-	}
-
-	public boolean standaloneSet() {
-		// TODO Auto-generated method stub
-		return false;
-	}
-
-	public String getCharacterEncodingScheme() {
-		// TODO Auto-generated method stub
-		return null;
-	}
+    /**
+     *     
+     */
+    public static class ElementFrame {
+        public ElementFrame(Object element, ElementFrame parent) {
+            this.element = element;
+            this.parent = parent;
+        }
+
+        Object element;
+
+        boolean started = false;
+
+        boolean ended = false;
+
+        int currentChild = -1;
+
+        int currentAttribute = -1;
+
+        int currentNamespace = -1;
+
+        int currentElement = -1;
+
+        List uris;
+
+        List prefixes;
+
+        List attributes;
+
+        List allAttributes;
+
+        final ElementFrame parent;
+    }
+
+    /**
+     * @param element
+     */
+    public DOMStreamReader(ElementFrame frame) {
+        this.frame = frame;
+        frames.push(this.frame);
+        newFrame(frame);
+    }
+
+    protected ElementFrame getCurrentFrame() {
+        return frame;
+    }
+
+    /* (non-Javadoc)
+     * @see javax.xml.stream.XMLStreamReader#getProperty(java.lang.String)
+     */
+    public Object getProperty(String key) throws IllegalArgumentException {
+        return properties.get(key);
+    }
+
+    /* (non-Javadoc)
+     * @see javax.xml.stream.XMLStreamReader#next()
+     */
+    public int next() throws XMLStreamException {
+        if (frame.ended) {
+            frames.pop();
+            if (!frames.empty()) {
+                frame = (ElementFrame) frames.peek();
+            } else {
+                currentEvent = END_DOCUMENT;
+                return currentEvent;
+            }
+        }
+
+        if (!frame.started) {
+            frame.started = true;
+            currentEvent = START_ELEMENT;
+        } else if (frame.currentAttribute < getAttributeCount() - 1) {
+            frame.currentAttribute++;
+            currentEvent = ATTRIBUTE;
+        } else if (frame.currentNamespace < getNamespaceCount() - 1) {
+            frame.currentNamespace++;
+            currentEvent = NAMESPACE;
+        } else if (frame.currentChild < getChildCount() - 1) {
+            frame.currentChild++;
+
+            currentEvent = moveToChild(frame.currentChild);
+
+            if (currentEvent == START_ELEMENT) {
+                ElementFrame newFrame = getChildFrame(frame.currentChild);
+                newFrame.started = true;
+                frame = newFrame;
+                frames.push(this.frame);
+                currentEvent = START_ELEMENT;
+
+                newFrame(newFrame);
+            }
+        } else {
+            frame.ended = true;
+            currentEvent = END_ELEMENT;
+            endElement();
+        }
+        return currentEvent;
+    }
+
+    protected void newFrame(ElementFrame newFrame) {
+    }
+
+    protected void endElement() {
+    }
+
+    protected abstract int moveToChild(int currentChild);
+
+    protected abstract ElementFrame getChildFrame(int currentChild);
+
+    protected abstract int getChildCount();
+
+    /* (non-Javadoc)
+     * @see javax.xml.stream.XMLStreamReader#require(int, java.lang.String, java.lang.String)
+     */
+    public void require(int arg0, String arg1, String arg2) throws XMLStreamException {
+        throw new UnsupportedOperationException();
+    }
+
+    /* (non-Javadoc)
+     * @see javax.xml.stream.XMLStreamReader#getElementText()
+     */
+    public abstract String getElementText() throws XMLStreamException;
+
+    /* (non-Javadoc)
+     * @see javax.xml.stream.XMLStreamReader#nextTag()
+     */
+    public int nextTag() throws XMLStreamException {
+        while (hasNext()) {
+            if (START_ELEMENT == next())
+                return START_ELEMENT;
+        }
+
+        return currentEvent;
+    }
+
+    /* (non-Javadoc)
+     * @see javax.xml.stream.XMLStreamReader#hasNext()
+     */
+    public boolean hasNext() throws XMLStreamException {
+        return !(frames.size() == 0 && frame.ended);
+
+    }
+
+    /* (non-Javadoc)
+     * @see javax.xml.stream.XMLStreamReader#close()
+     */
+    public void close() throws XMLStreamException {
+    }
+
+    /* (non-Javadoc)
+     * @see javax.xml.stream.XMLStreamReader#getNamespaceURI(java.lang.String)
+     */
+    public abstract String getNamespaceURI(String prefix);
+
+    /* (non-Javadoc)
+     * @see javax.xml.stream.XMLStreamReader#isStartElement()
+     */
+    public boolean isStartElement() {
+        return (currentEvent == START_ELEMENT);
+    }
+
+    /* (non-Javadoc)
+     * @see javax.xml.stream.XMLStreamReader#isEndElement()
+     */
+    public boolean isEndElement() {
+        return (currentEvent == END_ELEMENT);
+    }
+
+    /* (non-Javadoc)
+     * @see javax.xml.stream.XMLStreamReader#isCharacters()
+     */
+    public boolean isCharacters() {
+        return (currentEvent == CHARACTERS);
+    }
+
+    /* (non-Javadoc)
+     * @see javax.xml.stream.XMLStreamReader#isWhiteSpace()
+     */
+    public boolean isWhiteSpace() {
+        return (currentEvent == SPACE);
+    }
+
+    public int getEventType() {
+        return currentEvent;
+    }
+
+    public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length)
throws XMLStreamException {
+        char[] src = getText().toCharArray();
+
+        if (sourceStart + length >= src.length)
+            length = src.length - sourceStart;
+
+        for (int i = 0; i < length; i++) {
+            target[targetStart + i] = src[i + sourceStart];
+        }
+
+        return length;
+    }
+
+    public boolean hasText() {
+        return (currentEvent == CHARACTERS || currentEvent == DTD || currentEvent == ENTITY_REFERENCE
+                        || currentEvent == COMMENT || currentEvent == SPACE);
+    }
+
+    public Location getLocation() {
+        return new Location() {
+
+            public int getCharacterOffset() {
+                return 0;
+            }
+
+            public int getColumnNumber() {
+                return 0;
+            }
+
+            public int getLineNumber() {
+                return 0;
+            }
+
+            public String getPublicId() {
+                return null;
+            }
+
+            public String getSystemId() {
+                return null;
+            }
+
+        };
+    }
+
+    public boolean hasName() {
+        return (currentEvent == START_ELEMENT || currentEvent == END_ELEMENT);
+    }
+
+    public String getVersion() {
+        return null;
+    }
+
+    public boolean isStandalone() {
+        return false;
+    }
+
+    public boolean standaloneSet() {
+        // TODO Auto-generated method stub
+        return false;
+    }
+
+    public String getCharacterEncodingScheme() {
+        // TODO Auto-generated method stub
+        return null;
+    }
 }

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/DOMStreamWriter.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/DOMStreamWriter.java?rev=413897&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/DOMStreamWriter.java
(added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/DOMStreamWriter.java
Tue Jun 13 07:00:15 2006
@@ -0,0 +1,22 @@
+/*
+ * This implementation comes from the XFire project
+ * https://svn.codehaus.org/xfire/trunk/xfire/xfire-core/src/main/org/codehaus/xfire/util/stax/
+ */
+package org.apache.servicemix.jbi.jaxp;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+public abstract class DOMStreamWriter implements XMLStreamWriter
+{
+
+    public void close()
+        throws XMLStreamException
+    {
+    }
+
+    public void flush()
+        throws XMLStreamException
+    {
+    }
+}

Modified: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamReader.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamReader.java?rev=413897&r1=413896&r2=413897&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamReader.java
(original)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamReader.java
Tue Jun 13 07:00:15 2006
@@ -1,17 +1,6 @@
 /*
- * Copyright 2005-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.
+ * This implementation comes from the XFire project
+ * https://svn.codehaus.org/xfire/trunk/xfire/xfire-core/src/main/org/codehaus/xfire/util/stax/
  */
 package org.apache.servicemix.jbi.jaxp;
 
@@ -36,11 +25,13 @@
 
     private Document document;
 
+    private W3CNamespaceContext context;
+
     /**
      * @param element
      */
     public W3CDOMStreamReader(Element element) {
-        super(new ElementFrame(element));
+        super(new ElementFrame(element, null));
 
         this.document = element.getOwnerDocument();
     }
@@ -64,8 +55,14 @@
         frame.prefixes = new ArrayList();
         frame.attributes = new ArrayList();
 
+        if (context == null)
+            context = new W3CNamespaceContext();
+
+        context.setElement(element);
+
         NamedNodeMap nodes = element.getAttributes();
 
+        String nsURI = element.getNamespaceURI();
         String ePrefix = element.getPrefix();
         if (ePrefix == null) {
             ePrefix = "";
@@ -77,6 +74,7 @@
             String localName = node.getLocalName();
             String value = node.getNodeValue();
             String name = node.getNodeName();
+            String uri = node.getNamespaceURI();
 
             if (prefix == null)
                 prefix = "";
@@ -106,7 +104,7 @@
     }
 
     protected ElementFrame getChildFrame(int currentChild) {
-        return new ElementFrame(getCurrentElement().getChildNodes().item(currentChild));
+        return new ElementFrame(getCurrentElement().getChildNodes().item(currentChild), getCurrentFrame());
     }
 
     protected int getChildCount() {
@@ -135,11 +133,18 @@
     }
 
     public String getNamespaceURI(String prefix) {
-        int index = getCurrentFrame().prefixes.indexOf(prefix);
-        if (index == -1)
-            return null;
+        ElementFrame frame = getCurrentFrame();
+
+        while (null != frame) {
+            int index = frame.prefixes.indexOf(prefix);
+            if (index != -1) {
+                return (String) frame.uris.get(index);
+            }
+
+            frame = frame.parent;
+        }
 
-        return (String) getCurrentFrame().uris.get(index);
+        return null;
     }
 
     public String getAttributeValue(String ns, String local) {
@@ -227,7 +232,7 @@
     }
 
     public NamespaceContext getNamespaceContext() {
-        throw new UnsupportedOperationException();
+        return context;
     }
 
     public String getText() {

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamWriter.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamWriter.java?rev=413897&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamWriter.java
(added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamWriter.java
Tue Jun 13 07:00:15 2006
@@ -0,0 +1,270 @@
+/*
+ * This implementation comes from the XFire project
+ * https://svn.codehaus.org/xfire/trunk/xfire/xfire-core/src/main/org/codehaus/xfire/util/stax/
+ */
+package org.apache.servicemix.jbi.jaxp;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Stack;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.stream.XMLStreamException;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+public class W3CDOMStreamWriter
+    extends DOMStreamWriter
+{
+    static final String XML_NS = "http://www.w3.org/2000/xmlns/";
+    private Stack stack = new Stack();
+    private Document document;
+    private Element currentNode;
+    private NamespaceContext context;
+    private Map properties = new HashMap();
+
+    public W3CDOMStreamWriter() throws ParserConfigurationException
+    {
+        this(DocumentBuilderFactory.newInstance().newDocumentBuilder());
+    }
+    
+    public W3CDOMStreamWriter(DocumentBuilder builder)
+    {
+        document = builder.newDocument();
+    }
+    
+    public W3CDOMStreamWriter(Document document)
+    {
+        this.document = document;
+    }
+    
+    public Document getDocument()
+    {
+        return document;
+    }
+    
+    public void writeStartElement(String local)
+        throws XMLStreamException
+    {
+        newChild(document.createElement(local));
+    }
+
+    private void newChild(Element element)
+    {
+        if (currentNode != null)
+        {
+            stack.push(currentNode);
+            currentNode.appendChild(element);
+        }
+        else
+        {
+            document.appendChild(element);
+        }
+        
+        W3CNamespaceContext context = new W3CNamespaceContext();
+        context.setElement(element);
+        this.context = context;
+        
+        currentNode = element;
+    }
+
+    public void writeStartElement(String namespace, String local)
+        throws XMLStreamException
+    {
+        newChild(document.createElementNS(namespace, local));
+    }
+
+    public void writeStartElement(String prefix, String local, String namespace)
+        throws XMLStreamException
+    {
+        if (prefix == null || prefix.equals(""))
+        {
+            writeStartElement(namespace, local);
+        }
+        else
+        {
+            newChild(document.createElementNS(namespace, prefix + ":" + local));
+        }
+    }
+
+    public void writeEmptyElement(String namespace, String local)
+        throws XMLStreamException
+    {
+        writeStartElement(namespace, local);
+    }
+
+    public void writeEmptyElement(String prefix, String namespace, String local)
+        throws XMLStreamException
+    {
+        writeStartElement(prefix, namespace, local);
+    }
+
+    public void writeEmptyElement(String local)
+        throws XMLStreamException
+    {
+        writeStartElement(local);
+    }
+
+    public void writeEndElement()
+        throws XMLStreamException
+    {
+        if (stack.size() > 0)
+            currentNode = (Element) stack.pop();
+        else
+            currentNode = null;
+    }
+
+    public void writeEndDocument()
+        throws XMLStreamException
+    {
+    }
+
+    public void writeAttribute(String local, String value)
+        throws XMLStreamException
+    {
+        Attr a = document.createAttribute(local);
+        a.setValue(value);
+        currentNode.setAttributeNode(a);
+    }
+
+    public void writeAttribute(String prefix, String namespace, String local, String value)
+        throws XMLStreamException
+    {
+        if (prefix.length() > 0)
+            local = prefix + ":" + local;
+        
+        Attr a = document.createAttributeNS(namespace, local);
+        a.setValue(value);
+        currentNode.setAttributeNodeNS(a);
+    }
+
+    public void writeAttribute(String namespace, String local, String value)
+        throws XMLStreamException
+    {
+        Attr a = document.createAttributeNS(namespace, local);
+        a.setValue(value);
+        currentNode.setAttributeNodeNS(a);
+    }
+
+    public void writeNamespace(String prefix, String namespace)
+        throws XMLStreamException
+    {
+        if (prefix.length() == 0) 
+        {
+            writeDefaultNamespace(namespace);
+        }
+        else
+        {
+            currentNode.setAttributeNS(XML_NS, "xmlns:" + prefix, namespace);
+        }
+    }
+
+    public void writeDefaultNamespace(String namespace)
+        throws XMLStreamException
+    {
+        currentNode.setAttributeNS(XML_NS, "xmlns", namespace);
+    }
+
+    public void writeComment(String value)
+        throws XMLStreamException
+    {
+        currentNode.appendChild(document.createComment(value));
+    }
+
+    public void writeProcessingInstruction(String target)
+        throws XMLStreamException
+    {
+        currentNode.appendChild(document.createProcessingInstruction(target, null));
+    }
+
+    public void writeProcessingInstruction(String target, String data)
+        throws XMLStreamException
+    {
+        currentNode.appendChild(document.createProcessingInstruction(target, data));
+    }
+
+    public void writeCData(String data)
+        throws XMLStreamException
+    {
+        currentNode.appendChild(document.createCDATASection(data));
+    }
+
+    public void writeDTD(String arg0)
+        throws XMLStreamException
+    {
+        throw new UnsupportedOperationException();
+    }
+
+    public void writeEntityRef(String ref)
+        throws XMLStreamException
+    {
+        currentNode.appendChild(document.createEntityReference(ref));
+    }
+
+    public void writeStartDocument()
+        throws XMLStreamException
+    {
+    }
+
+    public void writeStartDocument(String version)
+        throws XMLStreamException
+    {
+        writeStartDocument();
+    }
+
+    public void writeStartDocument(String encoding, String version)
+        throws XMLStreamException
+    {
+        writeStartDocument();
+    }
+
+    public void writeCharacters(String text)
+        throws XMLStreamException
+    {
+        currentNode.appendChild(document.createTextNode(text));
+    }
+
+    public void writeCharacters(char[] text, int start, int len)
+        throws XMLStreamException
+    {
+        writeCharacters(new String(text, start, len));
+    }
+
+    public String getPrefix(String uri)
+        throws XMLStreamException
+    {
+        return context.getPrefix(uri);
+    }
+
+    public void setPrefix(String arg0, String arg1)
+        throws XMLStreamException
+    {
+    }
+
+    public void setDefaultNamespace(String arg0)
+        throws XMLStreamException
+    {
+    }
+
+    public void setNamespaceContext(NamespaceContext context)
+        throws XMLStreamException
+    {
+        this.context = context;
+    }
+
+    public NamespaceContext getNamespaceContext()
+    {
+        return context;
+    }
+
+    public Object getProperty(String prop)
+        throws IllegalArgumentException
+    {
+        return properties.get(prop);
+    }
+}

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/W3CNamespaceContext.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/W3CNamespaceContext.java?rev=413897&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/W3CNamespaceContext.java
(added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/W3CNamespaceContext.java
Tue Jun 13 07:00:15 2006
@@ -0,0 +1,98 @@
+/**
+ * 
+ */
+package org.apache.servicemix.jbi.jaxp;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.xml.namespace.NamespaceContext;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+
+public class W3CNamespaceContext implements NamespaceContext
+{
+    private Element currentNode;
+    
+    public String getNamespaceURI(String prefix)
+    {
+        String name = prefix;
+        if (name.length() == 0) name = "xmlns";
+        else name = "xmlns:" + prefix;
+        
+        return getNamespaceURI(currentNode, name);
+    }
+
+    private String getNamespaceURI(Element e, String name)
+    {
+        Attr attr = e.getAttributeNode(name);
+        if (attr == null)
+        {
+            Node n = e.getParentNode();
+            if (n instanceof Element && n != e)
+            {
+                return getNamespaceURI((Element) n, name);
+            }
+        }
+        else
+        {
+            return attr.getValue();
+        }
+        
+        return null;
+    }
+    
+    public String getPrefix(String uri)
+    {
+        return getPrefix(currentNode, uri);
+    }
+
+    private String getPrefix(Element e, String uri)
+    {
+        NamedNodeMap attributes = e.getAttributes();
+        for (int i = 0; i < attributes.getLength(); i++)
+        {
+            Attr a = (Attr) attributes.item(i);
+            
+            String val = a.getValue();
+            if (val != null && val.equals(uri))
+            {
+                String name = a.getNodeName();
+                if (name.equals("xmlns")) return "";
+                else return name.substring(6);
+            }
+        }
+        
+        Node n = e.getParentNode();
+        if (n instanceof Element && n != e)
+        {
+            return getPrefix((Element) n, uri);
+        }
+        
+        return null;
+    }
+
+    public Iterator getPrefixes(String uri)
+    {
+        List prefixes = new ArrayList();
+        
+        String prefix = getPrefix(uri);
+        if (prefix != null) prefixes.add(prefix);
+        
+        return prefixes.iterator();
+    }
+
+    public Element getElement()
+    {
+        return currentNode;
+    }
+
+    public void setElement(Element currentNode)
+    {
+        this.currentNode = currentNode;
+    }
+}
\ No newline at end of file



Mime
View raw message