You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by ge...@apache.org on 2008/08/25 12:57:29 UTC
svn commit: r688693 [4/5] - in /servicemix/utils/trunk: ./ src/ src/main/
src/main/java/ src/main/java/org/ src/main/java/org/apache/
src/main/java/org/apache/servicemix/
src/main/java/org/apache/servicemix/components/
src/main/java/org/apache/servicem...
Added: servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamReader.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamReader.java?rev=688693&view=auto
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamReader.java (added)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamReader.java Mon Aug 25 03:57:27 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/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamWriter.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamWriter.java?rev=688693&view=auto
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamWriter.java (added)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/W3CDOMStreamWriter.java Mon Aug 25 03:57:27 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/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/W3CNamespaceContext.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/W3CNamespaceContext.java?rev=688693&view=auto
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/W3CNamespaceContext.java (added)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/W3CNamespaceContext.java Mon Aug 25 03:57:27 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/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/XMLStreamHelper.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/XMLStreamHelper.java?rev=688693&view=auto
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/XMLStreamHelper.java (added)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/XMLStreamHelper.java Mon Aug 25 03:57:27 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/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/package.html
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/package.html?rev=688693&view=auto
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/package.html (added)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/package.html Mon Aug 25 03:57:27 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/utils/trunk/src/main/java/org/apache/servicemix/tck/mock/MockExchangeFactory.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/tck/mock/MockExchangeFactory.java?rev=688693&view=auto
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/tck/mock/MockExchangeFactory.java (added)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/tck/mock/MockExchangeFactory.java Mon Aug 25 03:57:27 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/utils/trunk/src/main/java/org/apache/servicemix/tck/mock/MockMessageExchange.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/tck/mock/MockMessageExchange.java?rev=688693&view=auto
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/tck/mock/MockMessageExchange.java (added)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/tck/mock/MockMessageExchange.java Mon Aug 25 03:57:27 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/utils/trunk/src/main/java/org/apache/servicemix/tck/mock/MockNormalizedMessage.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/tck/mock/MockNormalizedMessage.java?rev=688693&view=auto
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/tck/mock/MockNormalizedMessage.java (added)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/tck/mock/MockNormalizedMessage.java Mon Aug 25 03:57:27 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/utils/trunk/src/test/java/org/apache/servicemix/components/util/DefaultFileMarshalerTest.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/test/java/org/apache/servicemix/components/util/DefaultFileMarshalerTest.java?rev=688693&view=auto
==============================================================================
--- servicemix/utils/trunk/src/test/java/org/apache/servicemix/components/util/DefaultFileMarshalerTest.java (added)
+++ servicemix/utils/trunk/src/test/java/org/apache/servicemix/components/util/DefaultFileMarshalerTest.java Mon Aug 25 03:57:27 2008
@@ -0,0 +1,75 @@
+/*
+ * 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.components.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.nio.charset.Charset;
+
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+
+import junit.framework.TestCase;
+
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.tck.mock.MockMessageExchange;
+import org.apache.servicemix.tck.mock.MockNormalizedMessage;
+
+/**
+ * Test cases for {@link DefaultFileMarshaler}
+ */
+public class DefaultFileMarshalerTest extends TestCase {
+
+ private static final String MESSAGE = "<test>l'élève est à l'école</test>";
+ private static final SourceTransformer TRANSFORMER = new SourceTransformer();
+ private DefaultFileMarshaler marshaler = new DefaultFileMarshaler();
+
+ public void testReadExplicitEncoding() throws Exception {
+ //create a mock exchange
+ MessageExchange exchange = createMockExchange();
+
+ //have the marshaler read the message as ISO-8859-1, encoded as ISO-8859-1
+ ByteArrayInputStream stream = new ByteArrayInputStream(Charset.forName("ISO-8859-1").encode(MESSAGE).array());
+ marshaler.setEncoding("ISO-8859-1");
+ marshaler.readMessage(exchange, exchange.getMessage("in"), stream, "/any/thing/will/do");
+
+ //make sure that the end result is the same and no exception is thrown
+ assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + MESSAGE, TRANSFORMER.contentToString(exchange.getMessage("in")));
+ }
+
+ public void testWriteExplicitEncoding() throws Exception {
+ //create a mock exchange
+ MessageExchange exchange = createMockExchange();
+ exchange.getMessage("in").setContent(new StringSource(MESSAGE));
+
+ //have the marshaler read the message as ISO-8859-1, encoded as ISO-8859-1
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
+ marshaler.setEncoding("ISO-8859-1");
+ marshaler.writeMessage(exchange, exchange.getMessage("in"), stream, "/any/thing/will/do");
+
+ //make sure that the end result is the same and no exception is thrown
+ assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + MESSAGE, stream.toString("ISO-8859-1"));
+ }
+
+ private MessageExchange createMockExchange() throws MessagingException {
+ MessageExchange exchange = new MockMessageExchange();
+ exchange.setMessage(new MockNormalizedMessage(), "in");
+ return exchange;
+ }
+
+}
Added: servicemix/utils/trunk/src/test/java/org/apache/servicemix/components/util/SimpleFlatFileMarshalerTest.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/test/java/org/apache/servicemix/components/util/SimpleFlatFileMarshalerTest.java?rev=688693&view=auto
==============================================================================
--- servicemix/utils/trunk/src/test/java/org/apache/servicemix/components/util/SimpleFlatFileMarshalerTest.java (added)
+++ servicemix/utils/trunk/src/test/java/org/apache/servicemix/components/util/SimpleFlatFileMarshalerTest.java Mon Aug 25 03:57:27 2008
@@ -0,0 +1,250 @@
+/*
+ * 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.components.util;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.jbi.JBIException;
+import junit.framework.TestCase;
+
+public class SimpleFlatFileMarshalerTest extends TestCase {
+
+ public void testFixedLengthMarshalling() throws FileNotFoundException, IOException, JBIException {
+ SimpleFlatFileMarshaler marshaler = new SimpleFlatFileMarshaler();
+ marshaler.setLineFormat(SimpleFlatFileMarshaler.LINEFORMAT_FIXLENGTH);
+ marshaler.setColumnLengths(new String[] {"2", "3", "5" });
+
+ String path = "./src/test/resources/org/apache/servicemix/components/util/fixedlength.txt";
+ String result = convertLinesToString(marshaler, new FileInputStream(new File(path)), path);
+ assertNotNull(result);
+ assertTrue(result.length() > 0);
+
+ assertTrue(result.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
+ assertTrue(result.contains("<File"));
+ assertTrue(result.endsWith("</File>"));
+
+ assertTrue(countMatches(result, "<Line") == 3);
+ }
+
+ public void testFixedLengthWithColNamesMarshalling() throws FileNotFoundException, IOException, JBIException {
+ SimpleFlatFileMarshaler marshaler = new SimpleFlatFileMarshaler();
+ marshaler.setLineFormat(SimpleFlatFileMarshaler.LINEFORMAT_FIXLENGTH);
+ marshaler.setColumnLengths(new String[] {"2", "3", "5" });
+ marshaler.setColumnNames(new String[] {"First", "Second", "Third" });
+
+ String path = "./src/test/resources/org/apache/servicemix/components/util/fixedlength.txt";
+ String result = convertLinesToString(marshaler, new FileInputStream(new File(path)), path);
+ assertNotNull(result);
+ assertTrue(result.length() > 0);
+
+ assertTrue(result.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
+ assertTrue(result.contains("<File"));
+ assertTrue(result.endsWith("</File>"));
+
+ assertTrue(countMatches(result, "<Line") == 3);
+ assertTrue(countMatches(result, "<First") == 3);
+ assertTrue(countMatches(result, "<Second") == 3);
+ assertTrue(countMatches(result, "<Third") == 3);
+ }
+
+ public void testFixedLengthWithConversionMarshalling() throws FileNotFoundException, IOException, JBIException {
+ SimpleFlatFileMarshaler marshaler = new SimpleFlatFileMarshaler();
+ marshaler.setLineFormat(SimpleFlatFileMarshaler.LINEFORMAT_FIXLENGTH);
+ marshaler.setColumnLengths(new String[] {"2", "3", "5", "8" });
+ marshaler.setColumnNames(new String[] {"Number", "Text1", "Text2", "Date" });
+
+ List columnConverters = new ArrayList();
+ columnConverters.add(new NumberConverter());
+ columnConverters.add(null);
+ columnConverters.add(null);
+ columnConverters.add(new DateConverter(new SimpleDateFormat("yyyyMMdd"), new SimpleDateFormat("yyyy-MM-dd")));
+ marshaler.setColumnConverters(columnConverters);
+
+ String path = "./src/test/resources/org/apache/servicemix/components/util/fixedlength_morecomplex.txt";
+ String result = convertLinesToString(marshaler, new FileInputStream(new File(path)), path);
+ assertNotNull(result);
+ assertTrue(result.length() > 0);
+
+ assertTrue(result.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
+ assertTrue(result.contains("<File"));
+ assertTrue(result.endsWith("</File>"));
+
+ assertTrue(countMatches(result, "<Line") == 3);
+ assertTrue(countMatches(result, "<Number") == 3);
+ assertTrue(countMatches(result, "<Text1") == 3);
+ assertTrue(countMatches(result, "<Text2") == 3);
+ assertTrue(countMatches(result, "<Date") == 2);
+ }
+
+ public void testCSVMarshalling() throws FileNotFoundException, IOException, JBIException {
+ SimpleFlatFileMarshaler marshaler = new SimpleFlatFileMarshaler();
+ marshaler.setLineFormat(SimpleFlatFileMarshaler.LINEFORMAT_CSV);
+
+ String path = "./src/test/resources/org/apache/servicemix/components/util/csv_simplesample.csv";
+ String result = convertLinesToString(marshaler, new FileInputStream(new File(path)), path);
+ assertNotNull(result);
+ assertTrue(result.length() > 0);
+
+ assertTrue(result.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
+ assertTrue(result.contains("<File"));
+ assertTrue(result.endsWith("</File>"));
+
+ assertTrue(countMatches(result, "<Line") == 3);
+ }
+
+ class FileFewTimes extends InputStream {
+ byte [] buffer;
+ int pos;
+ int timesLeft;
+ public FileFewTimes(File file, int times) throws FileNotFoundException, IOException {
+ this.timesLeft = times;
+ buffer = new byte [(int)file.length()];
+ FileInputStream fs = new FileInputStream(file);
+ if (buffer.length != fs.read(buffer)) {
+ throw new IOException("Unexpected end of file " + file.getCanonicalPath());
+ }
+ }
+
+ @Override
+ public int read() throws IOException {
+ if (pos < buffer.length) {
+ return buffer[pos++];
+ }
+ if (timesLeft == 0) {
+ return -1;
+ }
+ timesLeft--;
+ pos = 0;
+ return buffer[pos++];
+ }
+ }
+
+ public void testHugeStream() throws FileNotFoundException, IOException {
+ SimpleFlatFileMarshaler marshaler = new SimpleFlatFileMarshaler();
+ marshaler.setLineFormat(SimpleFlatFileMarshaler.LINEFORMAT_CSV);
+
+ String path = "./src/test/resources/org/apache/servicemix/components/util/csv_simplesample.csv";
+ InputStream in = new FileFewTimes(new File(path), 500000);
+ InputStream out = marshaler.convertLines(null, in, path);
+ int r = 0;
+ while (r != -1) {
+ r = out.read();
+ }
+ }
+
+ private String convertLinesToString(SimpleFlatFileMarshaler marshaler, FileInputStream fileInputStream,
+ String path) throws IOException, JBIException {
+ InputStream out = marshaler.convertLines(null, fileInputStream, path);
+ StringBuilder sb = new StringBuilder();
+ for (Object string : readLines(new InputStreamReader(out))) {
+ sb.append(string);
+ }
+ return sb.toString();
+ }
+
+ /**
+ * Get the contents of a <code>Reader</code> as a list of Strings,
+ * one entry per line.
+ * <p>
+ * This method buffers the input internally, so there is no need to use a
+ * <code>BufferedReader</code>.
+ *
+ * @param input the <code>Reader</code> to read from, not null
+ * @return the list of Strings, never null
+ * @throws NullPointerException if the input is null
+ * @throws IOException if an I/O error occurs
+ * @since Commons IO 1.1
+ */
+ private static List<String> readLines(Reader input) throws IOException {
+ BufferedReader reader = new BufferedReader(input);
+ List<String> list = new ArrayList<String>();
+ String line = reader.readLine();
+ while (line != null) {
+ list.add(line);
+ line = reader.readLine();
+ }
+ return list;
+ }
+
+ /**
+ * <p>Counts how many times the substring appears in the larger String.</p>
+ *
+ * <p>A <code>null</code> or empty ("") String input returns <code>0</code>.</p>
+ *
+ * <pre>
+ * countMatches(null, *) = 0
+ * countMatches("", *) = 0
+ * countMatches("abba", null) = 0
+ * countMatches("abba", "") = 0
+ * countMatches("abba", "a") = 2
+ * countMatches("abba", "ab") = 1
+ * countMatches("abba", "xxx") = 0
+ * </pre>
+ *
+ * @param str the String to check, may be null
+ * @param sub the substring to count, may be null
+ * @return the number of occurrences, 0 if either String is <code>null</code>
+ */
+ private static int countMatches(String str, String sub) {
+ if (isEmpty(str) || isEmpty(sub)) {
+ return 0;
+ }
+ int count = 0;
+ int idx = 0;
+ for (;;) {
+ idx = str.indexOf(sub, idx);
+ if (idx == -1) {
+ break;
+ }
+ idx += sub.length();
+ count++;
+ }
+ return count;
+ }
+
+ /**
+ * <p>Checks if a String is empty ("") or null.</p>
+ *
+ * <pre>
+ * StringUtils.isEmpty(null) = true
+ * StringUtils.isEmpty("") = true
+ * StringUtils.isEmpty(" ") = false
+ * StringUtils.isEmpty("bob") = false
+ * StringUtils.isEmpty(" bob ") = false
+ * </pre>
+ *
+ * <p>NOTE: This method changed in Lang version 2.0.
+ * It no longer trims the String.
+ * That functionality is available in isBlank().</p>
+ *
+ * @param str the String to check, may be null
+ * @return <code>true</code> if the String is empty or null
+ */
+ public static boolean isEmpty(String str) {
+ return str == null || str.length() == 0;
+ }
+}
Added: servicemix/utils/trunk/src/test/java/org/apache/servicemix/expression/ExpressionEditorTest.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/test/java/org/apache/servicemix/expression/ExpressionEditorTest.java?rev=688693&view=auto
==============================================================================
--- servicemix/utils/trunk/src/test/java/org/apache/servicemix/expression/ExpressionEditorTest.java (added)
+++ servicemix/utils/trunk/src/test/java/org/apache/servicemix/expression/ExpressionEditorTest.java Mon Aug 25 03:57:27 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/utils/trunk/src/test/java/org/apache/servicemix/expression/ExpressionTestSupport.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/test/java/org/apache/servicemix/expression/ExpressionTestSupport.java?rev=688693&view=auto
==============================================================================
--- servicemix/utils/trunk/src/test/java/org/apache/servicemix/expression/ExpressionTestSupport.java (added)
+++ servicemix/utils/trunk/src/test/java/org/apache/servicemix/expression/ExpressionTestSupport.java Mon Aug 25 03:57:27 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/utils/trunk/src/test/java/org/apache/servicemix/expression/JAXPXPathExpressionTest.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/test/java/org/apache/servicemix/expression/JAXPXPathExpressionTest.java?rev=688693&view=auto
==============================================================================
--- servicemix/utils/trunk/src/test/java/org/apache/servicemix/expression/JAXPXPathExpressionTest.java (added)
+++ servicemix/utils/trunk/src/test/java/org/apache/servicemix/expression/JAXPXPathExpressionTest.java Mon Aug 25 03:57:27 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/utils/trunk/src/test/java/org/apache/servicemix/expression/JAXPXPathXStreamExpressionTest.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/test/java/org/apache/servicemix/expression/JAXPXPathXStreamExpressionTest.java?rev=688693&view=auto
==============================================================================
--- servicemix/utils/trunk/src/test/java/org/apache/servicemix/expression/JAXPXPathXStreamExpressionTest.java (added)
+++ servicemix/utils/trunk/src/test/java/org/apache/servicemix/expression/JAXPXPathXStreamExpressionTest.java Mon Aug 25 03:57:27 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/utils/trunk/src/test/java/org/apache/servicemix/expression/XPathExpressionTest.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/test/java/org/apache/servicemix/expression/XPathExpressionTest.java?rev=688693&view=auto
==============================================================================
--- servicemix/utils/trunk/src/test/java/org/apache/servicemix/expression/XPathExpressionTest.java (added)
+++ servicemix/utils/trunk/src/test/java/org/apache/servicemix/expression/XPathExpressionTest.java Mon Aug 25 03:57:27 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/utils/trunk/src/test/java/org/apache/servicemix/jbi/jaxp/AbstractStreamReaderTest.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/jaxp/AbstractStreamReaderTest.java?rev=688693&view=auto
==============================================================================
--- servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/jaxp/AbstractStreamReaderTest.java (added)
+++ servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/jaxp/AbstractStreamReaderTest.java Mon Aug 25 03:57:27 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/utils/trunk/src/test/java/org/apache/servicemix/jbi/jaxp/FragmentStreamReaderTest.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/jaxp/FragmentStreamReaderTest.java?rev=688693&view=auto
==============================================================================
--- servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/jaxp/FragmentStreamReaderTest.java (added)
+++ servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/jaxp/FragmentStreamReaderTest.java Mon Aug 25 03:57:27 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());
+ }
+
+}