servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r671330 [3/4] - in /servicemix/smx3/trunk/core: ./ servicemix-core/ servicemix-core/src/main/java/org/apache/servicemix/expression/ servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/ servicemix-core/src/main/java/org/apache/servi...
Date Tue, 24 Jun 2008 20:18:56 GMT
Added: servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamReader.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamReader.java?rev=671330&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamReader.java (added)
+++ servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamReader.java Tue Jun 24 13:18:54 2008
@@ -0,0 +1,347 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.servicemix.jbi.jaxp;
+
+/*
+ * This implementation comes from the XFire project
+ * https://svn.codehaus.org/xfire/trunk/xfire/xfire-core/src/main/org/codehaus/xfire/util/stax/
+ */
+import java.util.ArrayList;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.CDATASection;
+import org.w3c.dom.Comment;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.EntityReference;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.Text;
+
+public class W3CDOMStreamReader extends DOMStreamReader {
+    private Node content;
+
+    private Document document;
+
+    private W3CNamespaceContext context;
+
+    /**
+     * @param element
+     */
+    public W3CDOMStreamReader(Element element) {
+        super(new ElementFrame(element, null));
+
+        this.document = element.getOwnerDocument();
+    }
+
+    /**
+     * Get the document associated with this stream.
+     * 
+     * @return
+     */
+    public Document getDocument() {
+        return document;
+    }
+
+    /**
+     * Find name spaces declaration in atrributes and move them to separate
+     * collection.
+     */
+    protected void newFrame(ElementFrame frame) {
+        Element element = getCurrentElement();
+        frame.uris = new ArrayList<String>();
+        frame.prefixes = new ArrayList<String>();
+        frame.attributes = new ArrayList<Attr>();
+
+        if (context == null) {
+            context = new W3CNamespaceContext();
+        }
+
+        context.setElement(element);
+
+        NamedNodeMap nodes = element.getAttributes();
+
+        String ePrefix = element.getPrefix();
+        if (ePrefix == null) {
+            ePrefix = "";
+        }
+
+        for (int i = 0; i < nodes.getLength(); i++) {
+            Node node = nodes.item(i);
+            String prefix = node.getPrefix();
+            String localName = node.getLocalName();
+            String value = node.getNodeValue();
+            String name = node.getNodeName();
+
+            if (prefix == null) {
+                prefix = "";
+            }
+
+            if (name != null && "xmlns".equals(name)) {
+                frame.uris.add(value);
+                frame.prefixes.add("");
+            } else if (prefix.length() > 0 && "xmlns".equals(prefix)) {
+                frame.uris.add(value);
+                frame.prefixes.add(localName);
+            } else if (name.startsWith("xmlns:")) {
+                prefix = name.substring(6);
+                frame.uris.add(value);
+                frame.prefixes.add(prefix);
+            } else {
+                frame.attributes.add(node);
+            }
+        }
+    }
+
+    protected void endElement() {
+        super.endElement();
+    }
+
+    Element getCurrentElement() {
+        return (Element) getCurrentFrame().element;
+    }
+
+    protected ElementFrame getChildFrame(int currentChild) {
+        return new ElementFrame(getCurrentElement().getChildNodes().item(currentChild), getCurrentFrame());
+    }
+
+    protected int getChildCount() {
+        return getCurrentElement().getChildNodes().getLength();
+    }
+
+    protected int moveToChild(int currentChild) {
+        this.content = getCurrentElement().getChildNodes().item(currentChild);
+        if (content instanceof Text) {
+            return CHARACTERS;
+        } else if (content instanceof Element) {
+            return START_ELEMENT;
+        } else if (content instanceof CDATASection) {
+            return CDATA;
+        } else if (content instanceof Comment) {
+            return CHARACTERS;
+        } else if (content instanceof EntityReference) {
+            return ENTITY_REFERENCE;
+        }
+        throw new IllegalStateException();
+    }
+
+    public String getElementText() throws XMLStreamException {
+        frame.ended = true;
+        currentEvent = END_ELEMENT;
+        endElement();
+        String result = getContent(getCurrentElement());
+        // we should not return null according to the StAx API javadoc
+        return result != null ? result : "";
+    }
+
+    public String getNamespaceURI(String prefix) {
+        ElementFrame frame = getCurrentFrame();
+
+        while (null != frame) {
+            int index = frame.prefixes.indexOf(prefix);
+            if (index != -1) {
+                return frame.uris.get(index);
+            }
+
+            frame = frame.parent;
+        }
+
+        return null;
+    }
+
+    public String getAttributeValue(String ns, String local) {
+        Attr attr;
+        if (ns == null || ns.equals("")) {
+            attr = getCurrentElement().getAttributeNode(local);
+        } else {
+            attr = getCurrentElement().getAttributeNodeNS(ns, local);
+        }
+        if (attr != null) {
+            return attr.getValue();
+        }
+        return null;
+    }
+
+    public int getAttributeCount() {
+        return getCurrentFrame().attributes.size();
+    }
+
+    Attr getAttribute(int i) {
+        return (Attr) getCurrentFrame().attributes.get(i);
+    }
+
+    private String getLocalName(Attr attr) {
+        String name = attr.getLocalName();
+        if (name == null) {
+            name = attr.getNodeName();
+        }
+        return name;
+    }
+
+    public QName getAttributeName(int i) {
+        Attr at = getAttribute(i);
+
+        String prefix = at.getPrefix();
+        String ln = getLocalName(at);
+        // at.getNodeName();
+        String ns = at.getNamespaceURI();
+
+        if (prefix == null) {
+            return new QName(ns, ln);
+        } else {
+            return new QName(ns, ln, prefix);
+        }
+    }
+
+    public String getAttributeNamespace(int i) {
+        return getAttribute(i).getNamespaceURI();
+    }
+
+    public String getAttributeLocalName(int i) {
+        Attr attr = getAttribute(i);
+        return getLocalName(attr);
+    }
+
+    public String getAttributePrefix(int i) {
+        return getAttribute(i).getPrefix();
+    }
+
+    public String getAttributeType(int i) {
+        return "CDATA";
+    }
+
+    public String getAttributeValue(int i) {
+        return getAttribute(i).getValue();
+    }
+
+    public boolean isAttributeSpecified(int i) {
+        return getAttribute(i).getValue() != null;
+    }
+
+    public int getNamespaceCount() {
+        return getCurrentFrame().prefixes.size();
+    }
+
+    public String getNamespacePrefix(int i) {
+        return getCurrentFrame().prefixes.get(i);
+    }
+
+    public String getNamespaceURI(int i) {
+        return getCurrentFrame().uris.get(i);
+    }
+
+    public NamespaceContext getNamespaceContext() {
+        return context;
+    }
+
+    public String getText() {
+        Node node = getCurrentElement().getChildNodes().item(getCurrentFrame().currentChild);
+        return node.getNodeValue();
+    }
+
+    public char[] getTextCharacters() {
+        return getText().toCharArray();
+    }
+
+    public int getTextStart() {
+        return 0;
+    }
+
+    public int getTextLength() {
+        return getText().length();
+    }
+
+    public String getEncoding() {
+        return null;
+    }
+
+    public QName getName() {
+        Element el = getCurrentElement();
+
+        String prefix = getPrefix();
+        String ln = getLocalName();
+
+        if (prefix == null) {
+            return new QName(el.getNamespaceURI(), ln);
+        } else {
+            return new QName(el.getNamespaceURI(), ln, prefix);
+        }
+    }
+
+    public String getLocalName() {
+        String name = getCurrentElement().getLocalName();
+        // When the element has no namespaces, null is returned
+        if (name == null) {
+            name = getCurrentElement().getNodeName();
+        }
+        return name;
+    }
+
+    public String getNamespaceURI() {
+        return getCurrentElement().getNamespaceURI();
+    }
+
+    public String getPrefix() {
+        String prefix = getCurrentElement().getPrefix();
+        if (prefix == null) {
+            prefix = "";
+        }
+        return prefix;
+    }
+
+    public String getPITarget() {
+        throw new UnsupportedOperationException();
+    }
+
+    public String getPIData() {
+        throw new UnsupportedOperationException();
+    }
+
+    /**
+     * Get the trimed text content of a node or null if there is no text
+     */
+    public static String getContent(Node n) {
+        if (n == null) {
+            return null;
+        }
+        Node n1 = getChild(n, Node.TEXT_NODE);
+        if (n1 == null) {
+            return null;
+        }
+        String s1 = n1.getNodeValue();
+        return s1.trim();
+    }
+
+    /**
+     * Get the first direct child with a given type
+     */
+    public static Node getChild(Node parent, int type) {
+        Node n = parent.getFirstChild();
+        while (n != null && type != n.getNodeType()) {
+            n = n.getNextSibling();
+        }
+        if (n == null) {
+            return null;
+        }
+        return n;
+    }
+
+}

Added: servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamWriter.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamWriter.java?rev=671330&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamWriter.java (added)
+++ servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamWriter.java Tue Jun 24 13:18:54 2008
@@ -0,0 +1,218 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.servicemix.jbi.jaxp;
+
+/*
+ * This implementation comes from the XFire project
+ * https://svn.codehaus.org/xfire/trunk/xfire/xfire-core/src/main/org/codehaus/xfire/util/stax/
+ */
+
+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 ctx = new W3CNamespaceContext();
+        ctx.setElement(element);
+        this.context = ctx;
+
+        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 ctx) throws XMLStreamException {
+        this.context = ctx;
+    }
+
+    public NamespaceContext getNamespaceContext() {
+        return context;
+    }
+
+    public Object getProperty(String prop) throws IllegalArgumentException {
+        return properties.get(prop);
+    }
+}

Added: servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/jbi/jaxp/W3CNamespaceContext.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/jbi/jaxp/W3CNamespaceContext.java?rev=671330&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/jbi/jaxp/W3CNamespaceContext.java (added)
+++ servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/jbi/jaxp/W3CNamespaceContext.java Tue Jun 24 13:18:54 2008
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.servicemix.jbi.jaxp;
+
+/*
+ * This implementation comes from the XFire project
+ * https://svn.codehaus.org/xfire/trunk/xfire/xfire-core/src/main/org/codehaus/xfire/util/stax/
+ */
+
+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 ("xmlns".equals(name)) {
+                    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 node) {
+        this.currentNode = node;
+    }
+}

Added: servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/jbi/jaxp/XMLStreamHelper.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/jbi/jaxp/XMLStreamHelper.java?rev=671330&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/jbi/jaxp/XMLStreamHelper.java (added)
+++ servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/jbi/jaxp/XMLStreamHelper.java Tue Jun 24 13:18:54 2008
@@ -0,0 +1,298 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.servicemix.jbi.jaxp;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+/**
+ * Utility methods for working with an XMLStreamWriter. Maybe push this back
+ * into stax-utils project.
+ * 
+ * Code borrowed to XFire project.
+ * 
+ * @version $Revision: 1.16 $
+ */
+public final class XMLStreamHelper implements XMLStreamConstants {
+    
+    private XMLStreamHelper() {
+    }
+
+    /**
+     * Copies the reader to the writer. The start and end document methods must
+     * be handled on the writer manually.
+     * 
+     * TODO: if the namespace on the reader has been declared previously to
+     * where we are in the stream, this probably won't work.
+     * 
+     * @param reader
+     * @param writer
+     * @throws XMLStreamException
+     */
+    public static void copy(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException {
+        int read = 0; // number of elements read in
+        int event = reader.getEventType();
+
+        while (reader.hasNext()) {
+            switch (event) {
+            case XMLStreamConstants.START_ELEMENT:
+                read++;
+                writeStartElement(reader, writer);
+                break;
+            case XMLStreamConstants.END_ELEMENT:
+                writer.writeEndElement();
+                read--;
+                if (read <= 0) {
+                    return;
+                }
+                break;
+            case XMLStreamConstants.CHARACTERS:
+                writer.writeCharacters(reader.getText());
+                break;
+            case XMLStreamConstants.START_DOCUMENT:
+            case XMLStreamConstants.END_DOCUMENT:
+            case XMLStreamConstants.ATTRIBUTE:
+            case XMLStreamConstants.NAMESPACE:
+                break;
+            default:
+                break;
+            }
+            event = reader.next();
+        }
+    }
+
+    private static void writeStartElement(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException {
+        String local = reader.getLocalName();
+        String uri = reader.getNamespaceURI();
+        String prefix = reader.getPrefix();
+        if (prefix == null) {
+            prefix = "";
+        }
+        if (uri == null) {
+            uri = "";
+        }
+
+        String boundPrefix = writer.getPrefix(uri);
+        boolean writeElementNS = false;
+        if (boundPrefix == null || !prefix.equals(boundPrefix)) {
+            writeElementNS = true;
+        }
+
+        // Write out the element name
+        if (uri != null) {
+            if (prefix.length() == 0) {
+
+                writer.writeStartElement(local);
+                writer.setDefaultNamespace(uri);
+
+            } else {
+                writer.writeStartElement(prefix, local, uri);
+                writer.setPrefix(prefix, uri);
+            }
+        } else {
+            writer.writeStartElement(local);
+        }
+
+        // Write out the namespaces
+        for (int i = 0; i < reader.getNamespaceCount(); i++) {
+            String nsURI = reader.getNamespaceURI(i);
+            String nsPrefix = reader.getNamespacePrefix(i);
+            if (nsPrefix == null) {
+                nsPrefix = "";
+            }
+
+            if (nsPrefix.length() == 0) {
+                writer.writeDefaultNamespace(nsURI);
+            } else {
+                writer.writeNamespace(nsPrefix, nsURI);
+            }
+
+            if (nsURI.equals(uri) && nsPrefix.equals(prefix)) {
+                writeElementNS = false;
+            }
+        }
+
+        // Check if the namespace still needs to be written.
+        // We need this check because namespace writing works
+        // different on Woodstox and the RI.
+        if (writeElementNS) {
+            if (prefix == null || prefix.length() == 0) {
+                writer.writeDefaultNamespace(uri);
+            } else {
+                writer.writeNamespace(prefix, uri);
+            }
+        }
+
+        // Write out attributes
+        for (int i = 0; i < reader.getAttributeCount(); i++) {
+            String ns = reader.getAttributeNamespace(i);
+            String nsPrefix = reader.getAttributePrefix(i);
+            if (ns == null || ns.length() == 0) {
+                writer.writeAttribute(reader.getAttributeLocalName(i), reader.getAttributeValue(i));
+            } else if (nsPrefix == null || nsPrefix.length() == 0) {
+                writer.writeAttribute(reader.getAttributeNamespace(i), reader.getAttributeLocalName(i), reader.getAttributeValue(i));
+            } else {
+                writer.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i), reader.getAttributeLocalName(i),
+                                reader.getAttributeValue(i));
+            }
+
+        }
+    }
+
+    /**
+     * Write a start element with the specified parameters
+     * 
+     * @param writer
+     * @param uri
+     * @param local
+     * @param prefix
+     * @throws XMLStreamException
+     */
+    public static void writeStartElement(XMLStreamWriter writer, String uri, String local, String prefix) throws XMLStreamException {
+        if (prefix == null) {
+            prefix = "";
+        }
+        if (uri == null) {
+            uri = "";
+        }
+
+        String boundPrefix = writer.getPrefix(uri);
+        boolean writeElementNS = false;
+        if (boundPrefix == null || !prefix.equals(boundPrefix)) {
+            writeElementNS = true;
+        }
+
+        // Write out the element name
+        if (uri != null) {
+            if (prefix.length() == 0) {
+
+                writer.writeStartElement(local);
+                writer.setDefaultNamespace(uri);
+
+            } else {
+                writer.writeStartElement(prefix, local, uri);
+                writer.setPrefix(prefix, uri);
+            }
+        } else {
+            writer.writeStartElement(local);
+        }
+
+        // Check if the namespace still needs to be written.
+        // We need this check because namespace writing works
+        // different on Woodstox and the RI.
+        if (writeElementNS) {
+            if (prefix.length() == 0) {
+                writer.writeDefaultNamespace(uri);
+            } else {
+                writer.writeNamespace(prefix, uri);
+            }
+        }
+    }
+
+    /**
+     * Write a start element with the given QName. However, if a namespace has
+     * already been bound to a prefix, use the existing one, else default to the
+     * prefix in the QName (if specified). Else, a prefix is generated.
+     * 
+     * @param writer
+     * @param name
+     * @throws XMLStreamException
+     */
+    public static void writeStartElement(XMLStreamWriter writer, QName name) throws XMLStreamException {
+        String prefix = choosePrefix(writer, name, false);
+        writeStartElement(writer, name.getNamespaceURI(), name.getLocalPart(), prefix);
+    }
+
+    /**
+     * 
+     * @param out
+     * @param name
+     * @throws XMLStreamException
+     */
+    public static void writeTextQName(XMLStreamWriter out, QName name) throws XMLStreamException {
+        String prefix = choosePrefix(out, name, true);
+        if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
+            out.writeCharacters(name.getLocalPart());
+        } else {
+            out.writeCharacters(prefix + ":" + name.getLocalPart());
+        }
+    }
+
+    protected static String choosePrefix(XMLStreamWriter out, QName name, boolean declare) throws XMLStreamException {
+        String uri = name.getNamespaceURI();
+        // If no namespace
+        if (uri == null || XMLConstants.NULL_NS_URI.equals(uri)) {
+            if (!XMLConstants.NULL_NS_URI.equals(out.getNamespaceContext().getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX))) {
+                out.setPrefix(XMLConstants.DEFAULT_NS_PREFIX, XMLConstants.NULL_NS_URI);
+            }
+            return XMLConstants.DEFAULT_NS_PREFIX;
+            // Need to write a prefix
+        } else {
+            String defPrefix = name.getPrefix();
+            // A prefix is specified
+            if (defPrefix != null && !XMLConstants.DEFAULT_NS_PREFIX.equals(defPrefix)) {
+                // if the uri is bound to the specified prefix, good, else
+                if (!uri.equals(out.getNamespaceContext().getNamespaceURI(defPrefix))) {
+                    // if there is a prefix bound to the uri, use it
+                    if (out.getNamespaceContext().getPrefix(uri) != null) {
+                        defPrefix = out.getNamespaceContext().getPrefix(uri);
+                        // get prefix from the writer
+                    } else if (out.getPrefix(uri) != null) {
+                        defPrefix = out.getPrefix(uri);
+                        // we need to bind the prefix
+                    } else if (declare) {
+                        out.setPrefix(defPrefix, uri);
+                        out.writeNamespace(defPrefix, uri);
+                    }
+                }
+                // No prefix specified
+            } else {
+                // if there is a prefix bound to the uri, use it
+                if (out.getNamespaceContext().getPrefix(uri) != null) {
+                    defPrefix = out.getNamespaceContext().getPrefix(uri);
+                    // get prefix from the writer
+                } else if (out.getPrefix(uri) != null) {
+                    defPrefix = out.getPrefix(uri);
+                    // we need to generate a prefix
+                } else {
+                    defPrefix = getUniquePrefix(out);
+                    if (declare) {
+                        out.setPrefix(defPrefix, uri);
+                        out.writeNamespace(defPrefix, uri);
+                    }
+                }
+            }
+            return defPrefix;
+        }
+    }
+
+    protected static String getUniquePrefix(XMLStreamWriter writer) {
+        int n = 1;
+        while (true) {
+            String nsPrefix = "ns" + n;
+            if (writer.getNamespaceContext().getNamespaceURI(nsPrefix) == null) {
+                return nsPrefix;
+            }
+            n++;
+        }
+    }
+
+}

Added: servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/jbi/jaxp/package.html
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/jbi/jaxp/package.html?rev=671330&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/jbi/jaxp/package.html (added)
+++ servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/jbi/jaxp/package.html Tue Jun 24 13:18:54 2008
@@ -0,0 +1,27 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You 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.
+
+-->
+<html>
+<head>
+</head>
+<body>
+
+JAXP and XML related utility classes
+
+</body>
+</html>

Added: servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/tck/mock/MockExchangeFactory.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/tck/mock/MockExchangeFactory.java?rev=671330&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/tck/mock/MockExchangeFactory.java (added)
+++ servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/tck/mock/MockExchangeFactory.java Tue Jun 24 13:18:54 2008
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.servicemix.tck.mock;
+
+import java.net.URI;
+
+import javax.jbi.messaging.InOnly;
+import javax.jbi.messaging.InOptionalOut;
+import javax.jbi.messaging.InOut;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessageExchangeFactory;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.RobustInOnly;
+import javax.xml.namespace.QName;
+
+public class MockExchangeFactory implements MessageExchangeFactory {
+    
+    public static final URI IN_ONLY = URI.create("http://www.w3.org/2004/08/wsdl/in-only");
+    public static final URI IN_OUT = URI.create("http://www.w3.org/2004/08/wsdl/in-out");
+    public static final URI IN_OPTIONAL_OUT = URI.create("http://www.w3.org/2004/08/wsdl/in-opt-out");
+    public static final URI ROBUST_IN_ONLY = URI.create("http://www.w3.org/2004/08/wsdl/robust-in-only");
+    
+    public MessageExchange createExchange(QName serviceName, QName operationName) throws MessagingException {
+        throw new UnsupportedOperationException();
+    }
+    public MessageExchange createExchange(URI pattern) throws MessagingException {
+        String str = pattern.toString();
+        if (str.startsWith("http://www.w3.org/2006/01/wsdl/")) {
+            str = str.replace("http://www.w3.org/2006/01/wsdl/", "http://www.w3.org/2004/08/wsdl/");
+            pattern = URI.create(str);
+        }
+        MessageExchange me;
+        if (IN_ONLY.equals(pattern)) {
+            me = createInOnlyExchange();
+        } else if (IN_OUT.equals(pattern)) {
+            me = createInOutExchange();
+        } else if (IN_OPTIONAL_OUT.equals(pattern)) {
+            me = createInOptionalOutExchange();
+        } else if (ROBUST_IN_ONLY.equals(pattern)) {
+            me = createRobustInOnlyExchange();
+        } else {
+            throw new IllegalArgumentException("Unhandled pattern: " + pattern);
+        }
+        ((MockMessageExchange) me).setPattern(pattern);
+        return me;
+    }
+    public InOnly createInOnlyExchange() throws MessagingException {
+        return new MockInOnly();
+    }
+    public InOptionalOut createInOptionalOutExchange() throws MessagingException {
+        return new MockInOptionalOut();
+    }
+    public InOut createInOutExchange() throws MessagingException {
+        return new MockInOut();
+    }
+    public RobustInOnly createRobustInOnlyExchange() throws MessagingException {
+        return new MockRobustInOnly();
+    }
+    
+    public static class MockInOnly extends MockMessageExchange implements InOnly {
+    }
+    public static class MockInOut extends MockMessageExchange implements InOut {
+    }
+    public static class MockInOptionalOut extends MockMessageExchange implements InOptionalOut {
+    }
+    public static class MockRobustInOnly extends MockMessageExchange implements RobustInOnly {
+    }
+}

Added: servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/tck/mock/MockMessageExchange.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/tck/mock/MockMessageExchange.java?rev=671330&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/tck/mock/MockMessageExchange.java (added)
+++ servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/tck/mock/MockMessageExchange.java Tue Jun 24 13:18:54 2008
@@ -0,0 +1,260 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.servicemix.tck.mock;
+
+import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.Fault;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.jbi.servicedesc.ServiceEndpoint;
+import javax.xml.namespace.QName;
+
+public class MockMessageExchange implements MessageExchange {
+
+    private ServiceEndpoint endpoint;
+    private Exception error;
+    private String exchangeId;
+    private QName interfaceName;
+    private QName operation;
+    private URI pattern;
+    private QName service;
+    private MessageExchange.Role role;
+    private ExchangeStatus status;
+    private NormalizedMessage inMessage;
+    private NormalizedMessage outMessage;
+    private Fault fault;
+    private Map<String, Object> properties = new HashMap<String, Object>();
+    
+    /**
+     * @return the endpoint
+     */
+    public ServiceEndpoint getEndpoint() {
+        return endpoint;
+    }
+
+    /**
+     * @param endpoint the endpoint to set
+     */
+    public void setEndpoint(ServiceEndpoint endpoint) {
+        this.endpoint = endpoint;
+    }
+
+    /**
+     * @return the error
+     */
+    public Exception getError() {
+        return error;
+    }
+
+    /**
+     * @param error the error to set
+     */
+    public void setError(Exception error) {
+        this.error = error;
+    }
+
+    /**
+     * @return the exchangeId
+     */
+    public String getExchangeId() {
+        return exchangeId;
+    }
+
+    /**
+     * @param exchangeId the exchangeId to set
+     */
+    public void setExchangeId(String exchangeId) {
+        this.exchangeId = exchangeId;
+    }
+
+    /**
+     * @return the fault
+     */
+    public Fault getFault() {
+        return fault;
+    }
+
+    /**
+     * @param fault the fault to set
+     */
+    public void setFault(Fault fault) {
+        this.fault = fault;
+    }
+
+    /**
+     * @return the in
+     */
+    public NormalizedMessage getInMessage() {
+        return inMessage;
+    }
+
+    /**
+     * @param in the in to set
+     */
+    public void setInMessage(NormalizedMessage in) {
+        this.inMessage = in;
+    }
+
+    /**
+     * @return the interfaceName
+     */
+    public QName getInterfaceName() {
+        return interfaceName;
+    }
+
+    /**
+     * @param interfaceName the interfaceName to set
+     */
+    public void setInterfaceName(QName interfaceName) {
+        this.interfaceName = interfaceName;
+    }
+
+    /**
+     * @return the operation
+     */
+    public QName getOperation() {
+        return operation;
+    }
+
+    /**
+     * @param operation the operation to set
+     */
+    public void setOperation(QName operation) {
+        this.operation = operation;
+    }
+
+    /**
+     * @return the out
+     */
+    public NormalizedMessage getOutMessage() {
+        return outMessage;
+    }
+
+    /**
+     * @param out the out to set
+     */
+    public void setOutMessage(NormalizedMessage out) {
+        this.outMessage = out;
+    }
+
+    /**
+     * @return the pattern
+     */
+    public URI getPattern() {
+        return pattern;
+    }
+
+    /**
+     * @param pattern the pattern to set
+     */
+    public void setPattern(URI pattern) {
+        this.pattern = pattern;
+    }
+
+    /**
+     * @return the role
+     */
+    public MessageExchange.Role getRole() {
+        return role;
+    }
+
+    /**
+     * @param role the role to set
+     */
+    public void setRole(MessageExchange.Role role) {
+        this.role = role;
+    }
+
+    /**
+     * @return the service
+     */
+    public QName getService() {
+        return service;
+    }
+
+    /**
+     * @param service the service to set
+     */
+    public void setService(QName service) {
+        this.service = service;
+    }
+
+    /**
+     * @return the status
+     */
+    public ExchangeStatus getStatus() {
+        return status;
+    }
+
+    /**
+     * @param status the status to set
+     */
+    public void setStatus(ExchangeStatus status) {
+        this.status = status;
+    }
+
+    public Fault createFault() throws MessagingException {
+        return new MockFault();
+    }
+
+    public NormalizedMessage createMessage() throws MessagingException {
+        return new MockNormalizedMessage();
+    }
+
+    public NormalizedMessage getMessage(String name) {
+        if ("in".equalsIgnoreCase(name)) {
+            return getInMessage();
+        } else if ("out".equalsIgnoreCase(name)) {
+            return getOutMessage();
+        }
+        return null;
+    }
+
+    public Object getProperty(String name) {
+        return properties.get(name);
+    }
+
+    public Set getPropertyNames() {
+        return properties.keySet();
+    }
+
+    public boolean isTransacted() {
+        return false;
+    }
+
+    public void setMessage(NormalizedMessage msg, String name) throws MessagingException {
+        if ("in".equalsIgnoreCase(name)) {
+            setInMessage(msg);
+        } else if ("out".equalsIgnoreCase(name)) {
+            setOutMessage(msg);
+        }
+    }
+
+    public void setProperty(String name, Object obj) {
+        properties.put(name, obj);
+    }
+    
+    public static class MockFault extends MockNormalizedMessage implements Fault {
+    }
+
+}

Added: servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/tck/mock/MockNormalizedMessage.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/tck/mock/MockNormalizedMessage.java?rev=671330&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/tck/mock/MockNormalizedMessage.java (added)
+++ servicemix/smx3/trunk/core/servicemix-utils/src/main/java/org/apache/servicemix/tck/mock/MockNormalizedMessage.java Tue Jun 24 13:18:54 2008
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.servicemix.tck.mock;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import javax.activation.DataHandler;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.security.auth.Subject;
+import javax.xml.transform.Source;
+
+public class MockNormalizedMessage implements NormalizedMessage {
+
+    private Source content;
+    private Map<String, Object> properties = new HashMap<String, Object>();
+    private Map<String, DataHandler> attachments = new HashMap<String, DataHandler>();
+    private Subject securitySubject;
+
+    /**
+     * @return the content
+     */
+    public Source getContent() {
+        return this.content;
+    }
+    /**
+     * @param content the content to set
+     */
+    public void setContent(Source content) {
+        this.content = content;
+    }
+    /**
+     * @return the securitySubject
+     */
+    public Subject getSecuritySubject() {
+        return securitySubject;
+    }
+    /**
+     * @param securitySubject the securitySubject to set
+     */
+    public void setSecuritySubject(Subject securitySubject) {
+        this.securitySubject = securitySubject;
+    }
+    public void addAttachment(String id, DataHandler data) throws MessagingException {
+        attachments.put(id, data);
+    }
+    public DataHandler getAttachment(String id) {
+        return attachments.get(id);
+    }
+    public Set getAttachmentNames() {
+        return attachments.keySet();
+    }
+    public Object getProperty(String name) {
+        return properties.get(name);
+    }
+    public Set getPropertyNames() {
+        return properties.keySet();
+    }
+    public void removeAttachment(String id) throws MessagingException {
+        attachments.remove(id);
+    }
+    public void setProperty(String name, Object value) {
+        properties.put(name, value);
+    }
+    
+}

Added: servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/expression/ExpressionEditorTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/expression/ExpressionEditorTest.java?rev=671330&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/expression/ExpressionEditorTest.java (added)
+++ servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/expression/ExpressionEditorTest.java Tue Jun 24 13:18:54 2008
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.servicemix.expression;
+
+import javax.jbi.messaging.MessagingException;
+
+import junit.framework.TestCase;
+
+import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
+import org.springframework.context.ApplicationContext;
+
+public class ExpressionEditorTest extends TestCase {
+
+    private ApplicationContext context;
+
+    @Override
+    protected void setUp() throws Exception {
+        context = new ClassPathXmlApplicationContext("/org/apache/servicemix/expression/expression-editor.xml");
+    }
+
+    public void testSetAsTextString() throws MessagingException {
+        ExpressionTestSupport support = (ExpressionTestSupport) context.getBean("ExpressionTestSupport");
+        assertTrue(support.getExpression() instanceof ConstantExpression);
+        assertEquals("test", support.getExpression().evaluate(null, null));
+    }
+
+}

Added: servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/expression/ExpressionTestSupport.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/expression/ExpressionTestSupport.java?rev=671330&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/expression/ExpressionTestSupport.java (added)
+++ servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/expression/ExpressionTestSupport.java Tue Jun 24 13:18:54 2008
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.servicemix.expression;
+
+public class ExpressionTestSupport {
+
+    private Expression expression;
+
+    public Expression getExpression() {
+        return expression;
+    }
+
+    public void setExpression(Expression expression) {
+        this.expression = expression;
+    }
+}

Added: servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/expression/JAXPXPathExpressionTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/expression/JAXPXPathExpressionTest.java?rev=671330&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/expression/JAXPXPathExpressionTest.java (added)
+++ servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/expression/JAXPXPathExpressionTest.java Tue Jun 24 13:18:54 2008
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.servicemix.expression;
+
+import org.apache.servicemix.jbi.jaxp.SimpleNamespaceContext;
+import org.apache.xalan.extensions.XPathFunctionResolverImpl;
+
+
+/**
+ * @version $Revision: 564607 $
+ */
+public class JAXPXPathExpressionTest extends XPathExpressionTest {
+
+    /**
+     * Note that this test only works on Java 5
+     *
+     * @throws Exception
+     */
+    public void testXPathUsingJAXP() throws Exception {
+        boolean test = false;
+
+        try {
+            Class.forName("java.lang.annotation.AnnotationTypeMismatchException");
+            test = true;
+        } catch (ClassNotFoundException doNothing) {
+            // Expected if not java 5
+        }
+
+        if (test) {
+            assertExpression(new JAXPStringXPathExpression("/foo/bar/@xyz"), "cheese", "<foo><bar xyz='cheese'/></foo>");
+            assertExpression(new JAXPStringXPathExpression("$name"), "James", "<foo><bar xyz='cheese'/></foo>");
+        }
+    }
+
+    public void testUsingJavaExtensions() throws Exception {
+        JAXPStringXPathExpression exp = new JAXPStringXPathExpression();
+        exp.setXPath("java:org.apache.servicemix.expression.JAXPXPathExpressionTest.func(string(/header/value))");
+        SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
+        namespaceContext.add("java", "http://xml.apache.org/xalan/java");
+        exp.setNamespaceContext(namespaceContext);
+        exp.setFunctionResolver(new XPathFunctionResolverImpl());
+        assertExpression(exp, "modified12", "<header><value>12</value></header>");
+    }
+
+    public static String func(String s) {
+        return "modified" + s;
+    }
+
+}

Added: servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/expression/JAXPXPathXStreamExpressionTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/expression/JAXPXPathXStreamExpressionTest.java?rev=671330&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/expression/JAXPXPathXStreamExpressionTest.java (added)
+++ servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/expression/JAXPXPathXStreamExpressionTest.java Tue Jun 24 13:18:54 2008
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.servicemix.expression;
+
+import java.util.HashMap;
+import java.util.Map;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+
+import junit.framework.TestCase;
+
+import com.thoughtworks.xstream.XStream;
+
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.tck.mock.MockMessageExchange;
+
+
+/**
+ * @version $Revision: 659786 $
+ */
+public class JAXPXPathXStreamExpressionTest extends TestCase {
+    XStream xStream = new XStream();
+    
+    public void testMap() throws Exception {
+        JAXPXPathXStreamExpression exp = new JAXPXPathXStreamExpression();
+        Map<String, Object> params = new HashMap<String, Object>();
+        params.put("key1", "value1");
+        exp.setXPath("/header/map");
+        assertExpression(exp, params, "<header>" + xStream.toXML(params) + "</header>");
+    }
+
+    protected void assertExpression(Expression expression, Object expected, String xml) throws MessagingException {
+        MessageExchange exchange = new MockMessageExchange();
+        NormalizedMessage message = exchange.createMessage();
+        message.setContent(new StringSource(xml));
+        Object value = expression.evaluate(exchange, message);
+        assertEquals("Expression: " + expression, expected, value);
+    }
+
+}

Added: servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/expression/XPathExpressionTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/expression/XPathExpressionTest.java?rev=671330&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/expression/XPathExpressionTest.java (added)
+++ servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/expression/XPathExpressionTest.java Tue Jun 24 13:18:54 2008
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.servicemix.expression;
+
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+
+import junit.framework.TestCase;
+
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.tck.mock.MockMessageExchange;
+
+/**
+ * @version $Revision: 564607 $
+ */
+public class XPathExpressionTest extends TestCase {
+
+    public void testXPathUsingJaxen() throws Exception {
+        assertExpression(new JaxenStringXPathExpression("foo/bar"), "cheese", "<foo><bar>cheese</bar></foo>");
+        assertExpression(new JaxenStringXPathExpression("foo/bar/@xyz"), "cheese", "<foo><bar xyz='cheese'/></foo>");
+        assertExpression(new JaxenStringXPathExpression("$name"), "James", "<foo><bar xyz='cheese'/></foo>");
+        assertExpression(new JaxenStringXPathExpression("foo/bar/text()"), "cheese", "<foo><bar>cheese</bar></foo>");
+    }
+
+    public void testXPathUsingXMLBeans() throws Exception {
+        assertExpression(new XMLBeansStringXPathExpression("foo/bar"), "cheese", "<foo><bar>cheese</bar></foo>");
+        assertExpression(new XMLBeansStringXPathExpression("foo/bar/@xyz"), "cheese", "<foo><bar xyz='cheese'/></foo>");
+
+        // These are way too complex for XMLBeans! :)
+        //assertExpression(new XMLBeansStringXPathExpression("$name"), "James", "<foo><bar xyz='cheese'/></foo>");
+        //assertExpression(new XMLBeansStringXPathExpression("foo/bar/text()"), "cheese", "<foo><bar>cheese</bar></foo>");
+    }
+
+    protected void assertExpression(Expression expression, String expected, String xml) throws MessagingException {
+        MessageExchange exchange = new MockMessageExchange();
+        NormalizedMessage message = exchange.createMessage();
+        message.setProperty("name", "James");
+        message.setContent(new StringSource(xml));
+        Object value = expression.evaluate(exchange, message);
+        assertEquals("Expression: " + expression, expected, value);
+    }
+
+}

Added: servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/jbi/jaxp/AbstractStreamReaderTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/jbi/jaxp/AbstractStreamReaderTest.java?rev=671330&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/jbi/jaxp/AbstractStreamReaderTest.java (added)
+++ servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/jbi/jaxp/AbstractStreamReaderTest.java Tue Jun 24 13:18:54 2008
@@ -0,0 +1,154 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.servicemix.jbi.jaxp;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamReader;
+
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
+ * @since Oct 26, 2004
+ */
+public abstract class AbstractStreamReaderTest extends TestCase {
+    
+    public void testSingleElement(XMLStreamReader reader) throws Exception {
+        assertTrue(reader.hasNext());
+        assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
+        assertTrue(reader.hasNext());
+        assertEquals("root", reader.getLocalName());
+        assertEquals(1, reader.getNamespaceCount());
+        assertEquals("", reader.getNamespacePrefix(0));
+        assertEquals("urn:test", reader.getNamespaceURI(0));
+
+        assertTrue(reader.hasNext());
+        assertEquals(XMLStreamReader.END_ELEMENT, reader.next());
+    }
+
+    public void testTextChild(XMLStreamReader reader) throws Exception {
+        assertTrue(reader.hasNext());
+        assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
+
+        assertEquals(1, reader.getNamespaceCount());
+        assertEquals("", reader.getNamespacePrefix(0));
+        assertEquals("urn:test", reader.getNamespaceURI(0));
+
+        assertTrue(reader.hasNext());
+        assertEquals(XMLStreamReader.CHARACTERS, reader.next());
+
+        assertEquals("Hello World", reader.getText());
+
+        assertTrue(reader.hasNext());
+        assertEquals(XMLStreamReader.END_ELEMENT, reader.next());
+    }
+
+    public void testMixedContent(XMLStreamReader reader) throws Exception {
+        assertTrue(reader.hasNext());
+        assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
+
+        assertEquals("root", reader.getLocalName());
+        assertEquals(1, reader.getNamespaceCount());
+        assertEquals("", reader.getNamespacePrefix(0));
+        assertEquals("urn:test", reader.getNamespaceURI(0));
+
+        assertTrue(reader.hasNext());
+        assertEquals(XMLStreamReader.CHARACTERS, reader.next());
+        assertEquals("Hello World", reader.getText());
+
+        assertTrue(reader.hasNext());
+        assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
+        assertEquals("element", reader.getLocalName());
+
+        assertTrue(reader.hasNext());
+        assertEquals(XMLStreamReader.END_ELEMENT, reader.next());
+
+        assertTrue(reader.hasNext());
+        assertEquals(XMLStreamReader.CHARACTERS, reader.next());
+        assertEquals(" more text", reader.getText());
+
+        assertTrue(reader.hasNext());
+        assertEquals(XMLStreamReader.END_ELEMENT, reader.next());
+    }
+
+    public void testAttributes(XMLStreamReader reader) throws Exception {
+        assertTrue(reader.hasNext());
+        assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
+
+        assertTrue(reader.hasNext());
+        assertEquals(XMLStreamReader.ATTRIBUTE, reader.next());
+
+        // first attribute
+        assertEquals(2, reader.getAttributeCount());
+        assertTrue(reader.getAttributePrefix(0) == null || reader.getAttributePrefix(0).equals(""));
+        assertEquals("att1", reader.getAttributeLocalName(0));
+        assertTrue(reader.getAttributeNamespace(0) == null || reader.getAttributeNamespace(0).equals(""));
+        assertEquals("value1", reader.getAttributeValue(0));
+        assertEquals("value1", reader.getAttributeValue("", "att1"));
+
+        QName q = reader.getAttributeName(0);
+        assertEquals("", q.getNamespaceURI());
+        assertEquals("", q.getPrefix());
+        assertEquals("att1", q.getLocalPart());
+
+        // second attribute
+        assertEquals("p", reader.getAttributePrefix(1));
+        assertEquals("att2", reader.getAttributeLocalName(1));
+        assertEquals("urn:test2", reader.getAttributeNamespace(1));
+        assertEquals("value2", reader.getAttributeValue(1));
+        assertEquals("value2", reader.getAttributeValue("urn:test2", "att2"));
+
+        q = reader.getAttributeName(1);
+        assertEquals("urn:test2", q.getNamespaceURI());
+        assertEquals("p", q.getPrefix());
+        assertEquals("att2", q.getLocalPart());
+
+        assertTrue(reader.hasNext());
+        assertEquals(XMLStreamReader.ATTRIBUTE, reader.next());
+
+        assertEquals(2, reader.getNamespaceCount());
+        assertEquals("", reader.getNamespacePrefix(0));
+        assertEquals("urn:test", reader.getNamespaceURI(0));
+        assertEquals("p", reader.getNamespacePrefix(1));
+        assertEquals("urn:test2", reader.getNamespaceURI(1));
+
+        assertTrue(reader.hasNext());
+        assertEquals(XMLStreamReader.END_ELEMENT, reader.next());
+    }
+
+    public void testElementChild(XMLStreamReader reader) throws Exception {
+        assertTrue(reader.hasNext());
+        assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
+
+        assertEquals("root", reader.getLocalName());
+        assertEquals("urn:test", reader.getNamespaceURI());
+        assertEquals("", reader.getPrefix());
+
+        assertTrue(reader.hasNext());
+        assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
+
+        assertEquals("child", reader.getLocalName());
+        assertEquals("urn:test2", reader.getNamespaceURI());
+        assertEquals("a", reader.getPrefix());
+
+        assertTrue(reader.hasNext());
+        assertEquals(XMLStreamReader.END_ELEMENT, reader.next());
+
+        assertTrue(reader.hasNext());
+        assertEquals(XMLStreamReader.END_ELEMENT, reader.next());
+    }
+}

Added: servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/jbi/jaxp/FragmentStreamReaderTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/jbi/jaxp/FragmentStreamReaderTest.java?rev=671330&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/jbi/jaxp/FragmentStreamReaderTest.java (added)
+++ servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/jbi/jaxp/FragmentStreamReaderTest.java Tue Jun 24 13:18:54 2008
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.servicemix.jbi.jaxp;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.StringWriter;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.Document;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class FragmentStreamReaderTest extends TestCase {
+
+    private static final Log LOG = LogFactory.getLog(FragmentStreamReaderTest.class);
+
+    public void testStaxSource() throws Exception {
+        InputStream is = getClass().getResourceAsStream("test.xml");
+        XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(is);
+        xsr = new ExtendedXMLStreamReader(xsr);
+        xsr.nextTag();
+        LOG.info(xsr.getName());
+        xsr.nextTag();
+        LOG.info(xsr.getName());
+        XMLStreamReader fsr = new FragmentStreamReader(xsr);
+        StaxSource ss = new StaxSource(fsr);
+        StringWriter buffer = new StringWriter();
+        Transformer transformer = TransformerFactory.newInstance().newTransformer();
+        transformer.transform(ss, new StreamResult(buffer));
+        LOG.info(buffer.toString());
+        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+        dbf.setNamespaceAware(true);
+        Document doc = dbf.newDocumentBuilder().parse(new ByteArrayInputStream(buffer.toString().getBytes()));
+        StringWriter buffer2 = new StringWriter();
+        transformer.transform(new DOMSource(doc), new StreamResult(buffer2));
+        LOG.info(buffer2.toString());
+    }
+
+}

Added: servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/jbi/jaxp/SourceTransformerTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/jbi/jaxp/SourceTransformerTest.java?rev=671330&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/jbi/jaxp/SourceTransformerTest.java (added)
+++ servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/jbi/jaxp/SourceTransformerTest.java Tue Jun 24 13:18:54 2008
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.servicemix.jbi.jaxp;
+
+import javax.xml.namespace.QName;
+import javax.xml.transform.dom.DOMSource;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+import junit.framework.TestCase;
+
+public class SourceTransformerTest extends TestCase {
+
+    private SourceTransformer transformer = new SourceTransformer();
+    
+    /*
+     * Test method for 'org.apache.servicemix.jbi.jaxp.SourceTransformer.toDOMNode(Source)'
+     */
+    public void testToDOMNodeSource() throws Exception {
+        Node node = transformer.toDOMNode(new StringSource(
+                "<definition xmlns:tns='http://foo.bar.com'><value>tns:bar</value></definition>"));
+        
+        assertNotNull(node);
+        assertTrue(node instanceof Document);
+        Document doc = (Document) node;
+        Element e = (Element) doc.getDocumentElement().getFirstChild();
+        QName q = createQName(e, e.getFirstChild().getNodeValue());
+        assertEquals("http://foo.bar.com", q.getNamespaceURI());
+    }
+    
+    public void testToDOMSourceFromStream() throws Exception {
+        DOMSource domsource = transformer.toDOMSourceFromStream(new StringSource(
+            "<definition xmlns:tns='http://foo.bar.com'><value>J�rgen</value></definition>"));
+        assertNotNull(domsource);
+    }
+
+    /**
+     * Creates a QName instance from the given namespace context for the given qualifiedName
+     *
+     * @param element       the element to use as the namespace context
+     * @param qualifiedName the fully qualified name
+     * @return the QName which matches the qualifiedName
+     */
+    public static QName createQName(Element element, String qualifiedName) {
+        int index = qualifiedName.indexOf(':');
+        if (index >= 0) {
+            String prefix = qualifiedName.substring(0, index);
+            String localName = qualifiedName.substring(index + 1);
+            String uri = recursiveGetAttributeValue(element, "xmlns:" + prefix);
+            return new QName(uri, localName, prefix);
+        } else {
+            String uri = recursiveGetAttributeValue(element, "xmlns");
+            if (uri != null) {
+                return new QName(uri, qualifiedName);
+            }
+            return new QName(qualifiedName);
+        }
+    }
+
+    /**
+     * Recursive method to find a given attribute value
+     */
+    public static String recursiveGetAttributeValue(Element element, String attributeName) {
+        String answer = null;
+        try {
+            answer = element.getAttribute(attributeName);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        if (answer == null || answer.length() == 0) {
+            Node parentNode = element.getParentNode();
+            if (parentNode instanceof Element) {
+                return recursiveGetAttributeValue((Element) parentNode, attributeName);
+            }
+        }
+        return answer;
+    }
+
+}

Added: servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java?rev=671330&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java (added)
+++ servicemix/smx3/trunk/core/servicemix-utils/src/test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java Tue Jun 24 13:18:54 2008
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.servicemix.jbi.jaxp;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.StringWriter;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class StaxSourceTest extends TestCase {
+
+    private static final Log LOG = LogFactory.getLog(StaxSourceTest.class);
+
+    public void testStaxSourceOnStream() throws Exception {
+        InputStream is = getClass().getResourceAsStream("test.xml");
+        XMLInputFactory factory = XMLInputFactory.newInstance();
+        factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);
+        XMLStreamReader xsr = factory.createXMLStreamReader(is);
+        StaxSource ss = new StaxSource(xsr);
+        StringWriter buffer = new StringWriter();
+        Transformer transformer = TransformerFactory.newInstance().newTransformer();
+        transformer.transform(ss, new StreamResult(buffer));
+        LOG.info(buffer.toString());
+
+        /*
+         * Attribute ordering is not preserved, so we can not compare the
+         * strings
+         * 
+         * is = getClass().getResourceAsStream("test.xml");
+         * ByteArrayOutputStream baos = new ByteArrayOutputStream();
+         * FileUtil.copyInputStream(is, baos);
+         * compare(baos.toString().replaceAll("\r", ""),
+         * buffer.toString().replaceAll("\r", ""));
+         */
+
+        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+        dbf.setNamespaceAware(true);
+        Document doc = dbf.newDocumentBuilder().parse(new ByteArrayInputStream(buffer.toString().getBytes()));
+        checkDomResult(doc);
+
+        StringWriter buffer2 = new StringWriter();
+        transformer.transform(new DOMSource(doc), new StreamResult(buffer2));
+        LOG.info(buffer2.toString());
+    }
+
+    public void testStaxSourceOnDOM() throws Exception {
+        InputStream is = getClass().getResourceAsStream("test.xml");
+        XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(is);
+        StaxSource ss = new StaxSource(xsr);
+        Transformer transformer = TransformerFactory.newInstance().newTransformer();
+        DOMResult result = new DOMResult();
+        transformer.transform(ss, result);
+        assertNotNull(result.getNode());
+        checkDomResult((Document) result.getNode());
+    }
+
+    public void testStaxToDOM() throws Exception {
+        InputStream is = getClass().getResourceAsStream("test.xml");
+        XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(is);
+        StaxSource ss = new StaxSource(xsr);
+        DOMSource src = new SourceTransformer().toDOMSource(ss);
+        assertNotNull(src);
+        assertNotNull(src.getNode());
+        checkDomResult((Document) src.getNode());
+    }
+
+    public void testEncoding() throws Exception {
+        final String msg = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><hello>���</hello>";
+        StringSource src = new StringSource(msg);
+        DOMSource dom = new SourceTransformer().toDOMSource(src);
+        StreamSource stream = new SourceTransformer().toStreamSource(dom);
+        LOG.info(new SourceTransformer().toString(stream));
+        SAXSource sax = new SourceTransformer().toSAXSource(dom);
+        LOG.info(new SourceTransformer().toString(sax));
+    }
+
+    protected void checkDomResult(Document doc) {
+        // Whitespace only elements must be preserved
+        NodeList l = doc.getElementsByTagName("child4");
+        assertEquals(1, l.getLength());
+        assertEquals(1, l.item(0).getChildNodes().getLength());
+        Text txt = (Text) l.item(0).getFirstChild();
+        assertEquals("   ", txt.getTextContent());
+
+        // Check long string
+        l = doc.getDocumentElement().getElementsByTagName("long");
+        assertEquals(1, l.getLength());
+        assertEquals(1, l.item(0).getChildNodes().getLength());
+        txt = (Text) l.item(0).getFirstChild();
+        StringBuffer expected = new StringBuffer();
+        for (int i = 0; i < 4; i++) {
+            for (int j = 0; j < 10; j++) {
+                for (int k = 0; k < 10; k++) {
+                    expected.append((char) ('0' + j));
+                    expected.append((char) ('0' + k));
+                    if (k != 9) {
+                        expected.append(' ');
+                    }
+                }
+                expected.append("\n");
+            }
+        }
+        assertEquals(expected.toString(), txt.getTextContent());
+    }
+
+    protected void compare(String s1, String s2) {
+        char[] c1 = s1.toCharArray();
+        char[] c2 = s2.toCharArray();
+        for (int i = 0; i < c1.length; i++) {
+            if (c1[i] != c2[i]) {
+                fail("Expected '" + (int) c2[i] + "' but found '" + (int) c1[i] + "' at index " + i + ". Expected '" + build(c2, i)
+                                + "' but found '" + build(c1, i) + "'.");
+            }
+        }
+    }
+
+    protected String build(char[] c, int i) {
+        int min = Math.max(0, i - 10);
+        int cnt = Math.min(20, c.length - min);
+        return new String(c, min, cnt);
+    }
+
+}



Mime
View raw message