From 8ec974b5e3303e80dd96d752214790dc1ec21708 Mon Sep 17 00:00:00 2001 From: rfeng Date: Fri, 7 Aug 2009 06:56:48 +0000 Subject: Refactor the databinding module to use common-xml git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@801895 13f79535-47bb-0310-9956-ffa450edef68 --- .../databinding/xml/BeanXMLStreamReaderImpl.java | 3 + .../sca/databinding/xml/DOMXMLStreamReader.java | 36 - .../sca/databinding/xml/DOMXmlNodeImpl.java | 156 ---- .../xml/DelegatingNamespaceContext.java | 310 -------- .../xml/NameValueArrayStreamReader.java | 403 ---------- .../databinding/xml/NameValuePairStreamReader.java | 347 --------- .../tuscany/sca/databinding/xml/NamedProperty.java | 59 -- .../databinding/xml/NilElementStreamReader.java | 279 ------- .../sca/databinding/xml/Node2XMLStreamReader.java | 1 + .../tuscany/sca/databinding/xml/SAX2DOM.java | 249 ------ .../tuscany/sca/databinding/xml/SAX2DOMPipe.java | 21 +- .../sca/databinding/xml/SimpleXmlNodeImpl.java | 112 --- .../sca/databinding/xml/StAX2SAXAdapter.java | 256 ------ .../tuscany/sca/databinding/xml/StAXHelper.java | 86 --- .../databinding/xml/String2XMLStreamReader.java | 11 +- .../databinding/xml/WrappingXMLStreamReader.java | 100 --- .../databinding/xml/XMLDocumentStreamReader.java | 482 ------------ .../databinding/xml/XMLFragmentStreamReader.java | 53 -- .../xml/XMLFragmentStreamReaderImpl.java | 857 --------------------- .../sca/databinding/xml/XMLStreamReader2Node.java | 11 +- .../sca/databinding/xml/XMLStreamReader2SAX.java | 13 +- .../databinding/xml/XMLStreamReader2String.java | 11 +- .../sca/databinding/xml/XMLStreamSerializer.java | 287 ------- .../tuscany/sca/databinding/xml/XMLStreamable.java | 37 - .../tuscany/sca/databinding/xml/XmlNode.java | 69 -- .../sca/databinding/xml/XmlNodeIterator.java | 355 --------- .../databinding/xml/XmlTreeStreamReaderImpl.java | 531 ------------- .../DataBindingRegistryImplTestCase.java.fixme | 103 --- .../sca/databinding/impl/MediatorImplTestCase.java | 9 +- .../TransformerRegistryImplTestCase.java.fixme | 109 --- .../impl/XMLDocumentStreamReaderTestCase.java | 16 +- .../xml/BeanXMLStreamReaderTestCase.java | 3 +- .../sca/databinding/xml/DOM2StAXTestCase.java | 18 +- .../xml/JavaBean2XMLStreamReaderTestCase.java | 3 +- .../xml/PushTransformationTestCase.java | 9 +- .../sca/databinding/xml/StAXHelperTestCase.java | 49 -- .../xml/XmlTreeStreamReaderTestCase.java | 198 ----- 37 files changed, 91 insertions(+), 5561 deletions(-) delete mode 100644 java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DOMXMLStreamReader.java delete mode 100644 java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DOMXmlNodeImpl.java delete mode 100644 java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DelegatingNamespaceContext.java delete mode 100644 java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/NameValueArrayStreamReader.java delete mode 100644 java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/NameValuePairStreamReader.java delete mode 100644 java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/NamedProperty.java delete mode 100644 java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/NilElementStreamReader.java delete mode 100644 java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/SAX2DOM.java delete mode 100644 java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/SimpleXmlNodeImpl.java delete mode 100644 java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/StAX2SAXAdapter.java delete mode 100644 java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/StAXHelper.java delete mode 100644 java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/WrappingXMLStreamReader.java delete mode 100644 java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLDocumentStreamReader.java delete mode 100644 java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLFragmentStreamReader.java delete mode 100644 java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLFragmentStreamReaderImpl.java delete mode 100644 java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLStreamSerializer.java delete mode 100644 java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLStreamable.java delete mode 100644 java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XmlNode.java delete mode 100644 java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XmlNodeIterator.java delete mode 100644 java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XmlTreeStreamReaderImpl.java delete mode 100644 java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/DataBindingRegistryImplTestCase.java.fixme delete mode 100644 java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/TransformerRegistryImplTestCase.java.fixme delete mode 100644 java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/StAXHelperTestCase.java delete mode 100644 java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/XmlTreeStreamReaderTestCase.java (limited to 'java/sca/modules/databinding/src') diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/BeanXMLStreamReaderImpl.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/BeanXMLStreamReaderImpl.java index 7a130eb2af..ed59911a0b 100644 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/BeanXMLStreamReaderImpl.java +++ b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/BeanXMLStreamReaderImpl.java @@ -37,6 +37,9 @@ import java.util.Map; import javax.xml.namespace.QName; +import org.apache.tuscany.sca.common.xml.stax.reader.SimpleXmlNodeImpl; +import org.apache.tuscany.sca.common.xml.stax.reader.XmlNode; +import org.apache.tuscany.sca.common.xml.stax.reader.XmlTreeStreamReaderImpl; import org.apache.tuscany.sca.databinding.SimpleTypeMapper; import org.apache.tuscany.sca.databinding.impl.SimpleTypeMapperImpl; import org.apache.tuscany.sca.interfacedef.util.TypeInfo; diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DOMXMLStreamReader.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DOMXMLStreamReader.java deleted file mode 100644 index 8c7a742cdd..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DOMXMLStreamReader.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import org.w3c.dom.Node; - -public class DOMXMLStreamReader extends XmlTreeStreamReaderImpl { - - public DOMXMLStreamReader(Node node) { - super(new DOMXmlNodeImpl(node)); - switch (node.getNodeType()) { - case Node.DOCUMENT_NODE: - break; - case Node.ELEMENT_NODE: - break; - default: - throw new IllegalArgumentException("Illegal node type: " + node); - } - } -} diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DOMXmlNodeImpl.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DOMXmlNodeImpl.java deleted file mode 100644 index 507c141c33..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DOMXmlNodeImpl.java +++ /dev/null @@ -1,156 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import javax.xml.namespace.QName; - -import org.w3c.dom.Attr; -import org.w3c.dom.Document; -import org.w3c.dom.NamedNodeMap; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - -/** - * @version $Rev$ $Date$ - */ -public class DOMXmlNodeImpl implements XmlNode { - private Node node; - private Map namespaces; - private Type type; - - /** - * @param element - */ - public DOMXmlNodeImpl(Node element) { - super(); - if (element.getNodeType() == Node.DOCUMENT_NODE) { - this.node = ((Document)element).getDocumentElement(); - } else { - this.node = element; - } - switch (node.getNodeType()) { - case Node.CDATA_SECTION_NODE: - this.type = Type.CHARACTERS; - break; - case Node.ELEMENT_NODE: - this.type = Type.ELEMENT; - break; - case Node.TEXT_NODE: - this.type = Type.CHARACTERS; - break; - } - } - - /** - * @see org.apache.tuscany.sca.databinding.xml.XmlNode#attributes() - */ - public List attributes() { - if (type != Type.ELEMENT) { - return null; - } - NamedNodeMap attrs = node.getAttributes(); - List xmlAttrs = new ArrayList(); - for (int i = 0; i < attrs.getLength(); i++) { - Attr attr = (Attr)attrs.item(i); - if (!attr.getName().equals("xmlns") && !attr.getName().startsWith("xmlns:")) { - xmlAttrs.add(new SimpleXmlNodeImpl(getQName(attr), attr.getValue(), XmlNode.Type.ATTRIBUTE)); - } - } - return xmlAttrs; - } - - /** - * @see org.apache.tuscany.sca.databinding.xml.XmlNode#children() - */ - public Iterator children() { - if (type != Type.ELEMENT) { - return null; - } - NodeList nodes = node.getChildNodes(); - List xmlNodes = new ArrayList(); - for (int i = 0; i < nodes.getLength(); i++) { - Node child = (Node)nodes.item(i); - int nodeType = child.getNodeType(); - if (nodeType == Node.ELEMENT_NODE || nodeType == Node.TEXT_NODE || nodeType == Node.CDATA_SECTION_NODE) { - xmlNodes.add(new DOMXmlNodeImpl(child)); - } - } - return xmlNodes.iterator(); - } - - /** - * @see org.apache.tuscany.sca.databinding.xml.XmlNode#getName() - */ - public QName getName() { - return getQName(node); - } - - private static QName getQName(Node node) { - int type = node.getNodeType(); - if (type == Node.ELEMENT_NODE || type == Node.ATTRIBUTE_NODE) { - String ns = node.getNamespaceURI(); - String prefix = node.getPrefix(); - String localName = node.getLocalName(); - return new QName(ns == null ? "" : ns, localName, prefix == null ? "" : prefix); - } - return null; - } - - /** - * @see org.apache.tuscany.sca.databinding.xml.XmlNode#getValue() - */ - public String getValue() { - return node.getNodeValue(); - } - - /** - * @see org.apache.tuscany.sca.databinding.xml.XmlNode#namespaces() - */ - public Map namespaces() { - if (type != Type.ELEMENT) { - return null; - } - if (namespaces == null) { - namespaces = new HashMap(); - NamedNodeMap attrs = node.getAttributes(); - for (int i = 0; i < attrs.getLength(); i++) { - Attr attr = (Attr)attrs.item(i); - if ("xmlns".equals(attr.getPrefix())) { - namespaces.put(attr.getLocalName(), attr.getValue()); - } - if ("xmlns".equals(attr.getName())) { - namespaces.put("", attr.getValue()); - } - } - } - return namespaces; - } - - public Type getType() { - return type; - } - -} diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DelegatingNamespaceContext.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DelegatingNamespaceContext.java deleted file mode 100644 index 7f49a50e1e..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DelegatingNamespaceContext.java +++ /dev/null @@ -1,310 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import java.util.ArrayList; -import java.util.EmptyStackException; -import java.util.Iterator; -import java.util.List; - -import javax.xml.namespace.NamespaceContext; - -public class DelegatingNamespaceContext implements NamespaceContext { - private static int count; - - private class WrappingIterator implements Iterator { - - private Iterator containedIterator; - - public WrappingIterator(Iterator containedIterator) { - this.containedIterator = containedIterator; - } - - public Iterator getContainedIterator() { - return containedIterator; - } - - public boolean hasNext() { - return containedIterator.hasNext(); - } - - public Object next() { - return containedIterator.next(); - } - - /** - * As per the contract on the API of Namespace context the returned iterator should be immutable - */ - public void remove() { - throw new UnsupportedOperationException(); - } - - public void setContainedIterator(Iterator containedIterator) { - this.containedIterator = containedIterator; - } - } - - private NamespaceContext parentNsContext; - - private FastStack prefixStack = new FastStack(); - - // Keep two ArrayLists for the prefixes and namespaces. They should be in - // sync - // since the index of the entry will be used to relate them - // use the minimum initial capacity to let things handle memory better - - private FastStack uriStack = new FastStack(); - - /** - * Generates a unique namespace prefix that is not in the scope of the NamespaceContext - * - * @return string - */ - public String generateUniquePrefix() { - String prefix = "p" + count++; - // null should be returned if the prefix is not bound! - while (getNamespaceURI(prefix) != null) { - prefix = "p" + count++; - } - - return prefix; - } - - public String getNamespaceURI(String prefix) { - // do the corrections as per the Javadoc - int index = prefixStack.search(prefix); - if (index != -1) { - return uriStack.get(index); - } - if (parentNsContext != null) { - return parentNsContext.getPrefix(prefix); - } - return null; - } - - public NamespaceContext getParentNsContext() { - return parentNsContext; - } - - public String getPrefix(String uri) { - // do the corrections as per the Javadoc - int index = uriStack.search(uri); - if (index != -1) { - return prefixStack.get(index); - } - - if (parentNsContext != null) { - return parentNsContext.getPrefix(uri); - } - return null; - } - - public Iterator getPrefixes(String uri) { - // create an ArrayList that contains the relevant prefixes - String[] uris = uriStack.toArray(new String[uriStack.size()]); - List tempList = new ArrayList(); - for (int i = uris.length - 1; i >= 0; i--) { - if (uris[i].equals(uri)) { - tempList.add(prefixStack.get(i)); - // we assume that array conversion preserves the order - } - } - // by now all the relevant prefixes are collected - // make a new iterator and provide a wrapper iterator to - // obey the contract on the API - return new WrappingIterator(tempList.iterator()); - } - - /** - * Pop a namespace - */ - public void popNamespace() { - prefixStack.pop(); - uriStack.pop(); - } - - /** - * Register a namespace in this context - * - * @param prefix - * @param uri - */ - public void pushNamespace(String prefix, String uri) { - prefixStack.push(prefix); - uriStack.push(uri); - - } - - public void setParentNsContext(NamespaceContext parentNsContext) { - this.parentNsContext = parentNsContext; - } - - /** - * An implementation of the {@link java.util.Stack} API that is based on an ArrayList instead of a - * Vector, so it is not synchronized to protect against multi-threaded access. The implementation is - * therefore operates faster in environments where you do not need to worry about multiple thread contention. - *

- * The removal order of an ArrayStack is based on insertion order: The most recently added element is - * removed first. The iteration order is not the same as the removal order. The iterator returns elements - * from the bottom up, whereas the {@link #remove()} method removes them from the top down. - *

- * Unlike Stack, ArrayStack accepts null entries. - */ - public static class FastStack extends ArrayList { - - /** Ensure Serialization compatibility */ - private static final long serialVersionUID = 2130079159931574599L; - - /** - * Constructs a new empty ArrayStack. The initial size is controlled by ArrayList - * and is currently 10. - */ - public FastStack() { - super(); - } - - /** - * Constructs a new empty ArrayStack with an initial size. - * - * @param initialSize the initial size to use - * @throws IllegalArgumentException if the specified initial size is negative - */ - public FastStack(int initialSize) { - super(initialSize); - } - - /** - * Return true if this stack is currently empty. - *

- * This method exists for compatibility with java.util.Stack. New users of this class should use - * isEmpty instead. - * - * @return true if the stack is currently empty - */ - public boolean empty() { - return isEmpty(); - } - - /** - * Returns the top item off of this stack without removing it. - * - * @return the top item on the stack - * @throws EmptyStackException if the stack is empty - */ - public T peek() throws EmptyStackException { - int n = size(); - if (n <= 0) { - throw new EmptyStackException(); - } else { - return get(n - 1); - } - } - - /** - * Returns the n'th item down (zero-relative) from the top of this stack without removing it. - * - * @param n the number of items down to go - * @return the n'th item on the stack, zero relative - * @throws EmptyStackException if there are not enough items on the stack to satisfy this request - */ - public T peek(int n) throws EmptyStackException { - int m = (size() - n) - 1; - if (m < 0) { - throw new EmptyStackException(); - } else { - return get(m); - } - } - - /** - * Pops the top item off of this stack and return it. - * - * @return the top item on the stack - * @throws EmptyStackException if the stack is empty - */ - public T pop() throws EmptyStackException { - int n = size(); - if (n <= 0) { - throw new EmptyStackException(); - } else { - return remove(n - 1); - } - } - - /** - * Pushes a new item onto the top of this stack. The pushed item is also returned. This is equivalent to calling - * add. - * - * @param item the item to be added - * @return the item just pushed - */ - public Object push(T item) { - add(item); - return item; - } - - /** - * Returns the top-most index for the object in the stack - * - * @param object the object to be searched for - * @return top-most index, or -1 if not found - */ - public int search(T object) { - int i = size() - 1; // Current index - while (i >= 0) { - T current = get(i); - if ((object == null && current == null) || (object != null && object.equals(current))) { - return i; - } - i--; - } - return -1; - } - - /** - * Returns the element on the top of the stack. - * - * @return the element on the top of the stack - * @throws EmptyStackException if the stack is empty - */ - public T get() { - int size = size(); - if (size == 0) { - throw new EmptyStackException(); - } - return get(size - 1); - } - - /** - * Removes the element on the top of the stack. - * - * @return the removed element - * @throws EmptyStackException if the stack is empty - */ - public T remove() { - int size = size(); - if (size == 0) { - throw new EmptyStackException(); - } - return remove(size - 1); - } - - } - -} diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/NameValueArrayStreamReader.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/NameValueArrayStreamReader.java deleted file mode 100644 index 5a31d9db91..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/NameValueArrayStreamReader.java +++ /dev/null @@ -1,403 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import javax.xml.namespace.NamespaceContext; -import javax.xml.namespace.QName; -import javax.xml.stream.Location; -import javax.xml.stream.XMLStreamException; - -public class NameValueArrayStreamReader implements XMLFragmentStreamReader { - - private static final int START_ELEMENT_STATE = 0; - private static final int TEXT_STATE = 1; - private static final int END_ELEMENT_STATE = 2; - private static final int FINAL_END_ELEMENT_STATE = 3; - private static final int START_ELEMENT_STATE_WITH_NULL = 4; - - private DelegatingNamespaceContext namespaceContext = new DelegatingNamespaceContext(); - // the index of the array - private int arrayIndex; - - private QName name; - private String[] values; - - // start element is the default state - private int state = START_ELEMENT_STATE; - - public NameValueArrayStreamReader(QName name, String[] values) { - this.name = name; - this.values = values; - } - - public void setParentNamespaceContext(NamespaceContext nsContext) { - this.namespaceContext.setParentNsContext(nsContext); - } - - public void init() { - // TODO what if the QName namespace has not been declared - } - - public Object getProperty(String string) throws IllegalArgumentException { - return null; - } - - /** - * @throws XMLStreamException - */ - public int next() throws XMLStreamException { - switch (state) { - case START_ELEMENT_STATE: - if (values.length > 0) { - state = TEXT_STATE; - return CHARACTERS; - } else { - state = FINAL_END_ELEMENT_STATE; - return END_ELEMENT; - } - - case START_ELEMENT_STATE_WITH_NULL: - if (arrayIndex == (values.length - 1)) { - state = FINAL_END_ELEMENT_STATE; - } else { - state = END_ELEMENT_STATE; - } - return END_ELEMENT; - case FINAL_END_ELEMENT_STATE: - // oops, not supposed to happen! - throw new XMLStreamException("end already reached!"); - case END_ELEMENT_STATE: - // we've to have more values since this is not the - // last value - // increment the counter - arrayIndex++; - if (values[arrayIndex] == null) { - state = START_ELEMENT_STATE_WITH_NULL; - } else { - state = START_ELEMENT_STATE; - } - return START_ELEMENT; - case TEXT_STATE: - if (arrayIndex == (values.length - 1)) { - state = FINAL_END_ELEMENT_STATE; - return END_ELEMENT; - } else { - state = END_ELEMENT_STATE; - return END_ELEMENT; - } - - default: - throw new XMLStreamException("unknown event type!"); - } - } - - public void require(int i, String string, String string1) throws XMLStreamException { - // nothing done here - } - - public String getElementText() throws XMLStreamException { - return null; // not implemented - } - - public int nextTag() throws XMLStreamException { - return 0; // not implemented - } - - public String getAttributeValue(String string, String string1) { - if (state == TEXT_STATE) { - // TODO something - return null; - } else { - return null; - } - - } - - public int getAttributeCount() { - if (state == START_ELEMENT_STATE_WITH_NULL) { - return 1; - } - if (state == START_ELEMENT_STATE) { - return 0; - } else { - throw new IllegalStateException(); - } - - } - - public QName getAttributeName(int i) { - if (state == START_ELEMENT_STATE_WITH_NULL && i == 0) { - return NIL_QNAME; - } - if (state == START_ELEMENT_STATE) { - return null; - } else { - throw new IllegalStateException(); - } - } - - public String getAttributeNamespace(int i) { - if (state == START_ELEMENT_STATE_WITH_NULL && i == 0) { - return NIL_QNAME.getNamespaceURI(); - } - if (state == START_ELEMENT_STATE) { - return null; - } else { - throw new IllegalStateException(); - } - } - - public String getAttributeLocalName(int i) { - if (state == START_ELEMENT_STATE_WITH_NULL && i == 0) { - return NIL_QNAME.getLocalPart(); - } - if (state == START_ELEMENT_STATE) { - return null; - } else { - throw new IllegalStateException(); - } - } - - public String getAttributePrefix(int i) { - if (state == START_ELEMENT_STATE_WITH_NULL && i == 0) { - return NIL_QNAME.getPrefix(); - } - if (state == START_ELEMENT_STATE) { - return null; - } else { - throw new IllegalStateException(); - } - } - - public String getAttributeType(int i) { - return null; // not implemented - } - - public String getAttributeValue(int i) { - if (state == START_ELEMENT_STATE_WITH_NULL && i == 0) { - return NIL_VALUE_TRUE; - } - if (state == START_ELEMENT_STATE) { - return null; - } else { - throw new IllegalStateException(); - } - } - - public boolean isAttributeSpecified(int i) { - return false; // not supported - } - - public int getNamespaceCount() { - if (state == START_ELEMENT_STATE_WITH_NULL && isXsiNamespacePresent()) { - return 1; - } else { - return 0; - } - - } - - public String getNamespacePrefix(int i) { - if (state == START_ELEMENT_STATE_WITH_NULL && isXsiNamespacePresent() && i == 0) { - return NIL_QNAME.getPrefix(); - } else { - return null; - } - } - - public String getNamespaceURI(int i) { - if (state == START_ELEMENT_STATE_WITH_NULL && isXsiNamespacePresent() && i == 0) { - return NIL_QNAME.getNamespaceURI(); - } else { - return null; - } - } - - public NamespaceContext getNamespaceContext() { - return this.namespaceContext; - } - - public boolean isDone() { - return state == FINAL_END_ELEMENT_STATE; - } - - public int getEventType() { - switch (state) { - case START_ELEMENT_STATE: - return START_ELEMENT; - case END_ELEMENT_STATE: - return END_ELEMENT; - case TEXT_STATE: - return CHARACTERS; - case FINAL_END_ELEMENT_STATE: - return END_ELEMENT; - default: - throw new UnsupportedOperationException(); - // we've no idea what this is!!!!! - } - - } - - public String getText() { - if (state == TEXT_STATE) { - return values[arrayIndex]; - } else { - throw new IllegalStateException(); - } - } - - public char[] getTextCharacters() { - if (state == TEXT_STATE) { - return values[arrayIndex].toCharArray(); - } else { - throw new IllegalStateException(); - } - } - - public int getTextCharacters(int i, char[] chars, int i1, int i2) throws XMLStreamException { - // not implemented - throw new UnsupportedOperationException(); - } - - public int getTextStart() { - if (state == TEXT_STATE) { - return 0; - } else { - throw new IllegalStateException(); - } - } - - public int getTextLength() { - if (state == TEXT_STATE) { - return values[arrayIndex].length(); - } else { - throw new IllegalStateException(); - } - - } - - public String getEncoding() { - return null; - } - - public boolean hasText() { - return state == TEXT_STATE; - } - - public Location getLocation() { - return null; // not supported - } - - public QName getName() { - if (state != TEXT_STATE) { - return name; - } else { - return null; - } - } - - public String getLocalName() { - if (state != TEXT_STATE) { - return name.getLocalPart(); - } else { - return null; - } - } - - public boolean hasName() { - return state != TEXT_STATE; - - } - - public String getNamespaceURI() { - if (state != TEXT_STATE) { - return name.getNamespaceURI(); - } else { - return null; - } - - } - - public String getPrefix() { - if (state != TEXT_STATE) { - return name.getPrefix(); - } else { - return null; - } - } - - public String getVersion() { - return null; // TODO 1.0 ? - } - - public boolean isStandalone() { - return false; - } - - public boolean standaloneSet() { - return false; - } - - public String getCharacterEncodingScheme() { - return null; - } - - public String getPITarget() { - return null; - } - - public String getPIData() { - return null; - } - - public boolean hasNext() throws XMLStreamException { - return state != FINAL_END_ELEMENT_STATE; - } - - public void close() throws XMLStreamException { - // Do nothing - we've nothing to free here - } - - public String getNamespaceURI(String prefix) { - return namespaceContext.getNamespaceURI(prefix); - } - - public boolean isStartElement() { - return state == START_ELEMENT_STATE; - } - - public boolean isEndElement() { - return state == END_ELEMENT_STATE; - } - - public boolean isCharacters() { - return state == TEXT_STATE; - } - - public boolean isWhiteSpace() { - return false; // no whitespaces here - } - - /** - * Test whether the xsi namespace is present - */ - private boolean isXsiNamespacePresent() { - return namespaceContext.getNamespaceURI(NIL_QNAME.getPrefix()) != null; - } -} diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/NameValuePairStreamReader.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/NameValuePairStreamReader.java deleted file mode 100644 index fdc3c4139b..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/NameValuePairStreamReader.java +++ /dev/null @@ -1,347 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import javax.xml.namespace.NamespaceContext; -import javax.xml.namespace.QName; -import javax.xml.stream.Location; -import javax.xml.stream.XMLStreamException; - - -public class NameValuePairStreamReader implements XMLFragmentStreamReader { - - private static final int START_ELEMENT_STATE = 0; - private static final int TEXT_STATE = 1; - private static final int END_ELEMENT_STATE = 2; - - private DelegatingNamespaceContext namespaceContext = new DelegatingNamespaceContext(); - - private QName name; - private String value; - - private int state = START_ELEMENT_STATE; - // initiate at the start element state - - // keeps track whether the namespace is declared - // false by default - private boolean nsDeclared; - - public NameValuePairStreamReader(QName name, String value) { - this.name = name; - this.value = value; - } - - public Object getProperty(String key) throws IllegalArgumentException { - return null; - } - - public int next() throws XMLStreamException { - // no need to handle null here. it should have been handled - // already - switch (state) { - case START_ELEMENT_STATE: - state = TEXT_STATE; - return CHARACTERS; - case END_ELEMENT_STATE: - // oops, not supposed to happen! - throw new XMLStreamException("end already reached!"); - case TEXT_STATE: - state = END_ELEMENT_STATE; - return END_ELEMENT; - default: - throw new XMLStreamException("unknown event type!"); - } - } - - public void require(int i, String string, String string1) throws XMLStreamException { - // not implemented - } - - public String getElementText() throws XMLStreamException { - if (state == START_ELEMENT) { - // move to the end state and return the value - state = END_ELEMENT_STATE; - return value; - } else { - throw new XMLStreamException(); - } - - } - - public int nextTag() throws XMLStreamException { - return 0; // TODO - } - - public boolean hasNext() throws XMLStreamException { - return state != END_ELEMENT_STATE; - } - - public void close() throws XMLStreamException { - // Do nothing - we've nothing to free here - } - - public String getNamespaceURI(String prefix) { - return namespaceContext.getNamespaceURI(prefix); - } - - public boolean isStartElement() { - return state == START_ELEMENT_STATE; - } - - public boolean isEndElement() { - return state == END_ELEMENT_STATE; - } - - public boolean isCharacters() { - return state == TEXT_STATE; - } - - public boolean isWhiteSpace() { - return false; // no whitespaces here - } - - public String getAttributeValue(String string, String string1) { - return null; - } - - public int getAttributeCount() { - return 0; - } - - public QName getAttributeName(int i) { - return null; - } - - public String getAttributeNamespace(int i) { - return null; - } - - public String getAttributeLocalName(int i) { - return null; - } - - public String getAttributePrefix(int i) { - return null; - } - - public String getAttributeType(int i) { - return null; - } - - public String getAttributeValue(int i) { - return null; - } - - public boolean isAttributeSpecified(int i) { - return false; // no attributes here - } - - public int getNamespaceCount() { - return nsDeclared ? 1 : 0; - } - - public String getNamespacePrefix(int i) { - return (nsDeclared && i == 0) ? name.getPrefix() : null; - } - - public String getNamespaceURI(int i) { - return (nsDeclared && i == 0) ? name.getNamespaceURI() : null; - } - - public NamespaceContext getNamespaceContext() { - return this.namespaceContext; - } - - public int getEventType() { - switch (state) { - case START_ELEMENT_STATE: - return START_ELEMENT; - case END_ELEMENT_STATE: - return END_ELEMENT; - case TEXT_STATE: - return CHARACTERS; - default: - throw new UnsupportedOperationException(); - // we've no idea what this is!!!!! - } - - } - - public String getText() { - if (state == TEXT_STATE) { - return value; - } else { - throw new IllegalStateException(); - } - } - - public char[] getTextCharacters() { - if (state == TEXT_STATE) { - return value.toCharArray(); - } else { - throw new IllegalStateException(); - } - } - - public int getTextCharacters(int i, char[] chars, int i1, int i2) throws XMLStreamException { - // not implemented - throw new UnsupportedOperationException(); - } - - public int getTextStart() { - if (state == TEXT_STATE) { - return 0; - } else { - throw new IllegalStateException(); - } - } - - public int getTextLength() { - if (state == TEXT_STATE) { - return value.length(); - } else { - throw new IllegalStateException(); - } - - } - - public String getEncoding() { - return null; - } - - public boolean hasText() { - return state == TEXT_STATE; - } - - public Location getLocation() { - return new Location() { - public int getLineNumber() { - return 0; - } - - public int getColumnNumber() { - return 0; - } - - public int getCharacterOffset() { - return 0; - } - - public String getPublicId() { - return null; - } - - public String getSystemId() { - return null; - } - }; - } - - public QName getName() { - if (state != TEXT_STATE) { - return name; - } else { - return null; - } - } - - public String getLocalName() { - if (state != TEXT_STATE) { - return name.getLocalPart(); - } else { - return null; - } - } - - public boolean hasName() { - return state != TEXT_STATE; - - } - - public String getNamespaceURI() { - if (state != TEXT_STATE) { - return name.getNamespaceURI(); - } else { - return null; - } - - } - - public String getPrefix() { - if (state != TEXT_STATE) { - return name.getPrefix(); - } else { - return null; - } - } - - public String getVersion() { - return null; // TODO 1.0 ? - } - - public boolean isStandalone() { - return false; - } - - public boolean standaloneSet() { - return false; - } - - public String getCharacterEncodingScheme() { - return null; - } - - public String getPITarget() { - return null; - } - - public String getPIData() { - return null; - } - - public boolean isDone() { - return state == END_ELEMENT_STATE; - } - - public void setParentNamespaceContext(NamespaceContext nsContext) { - this.namespaceContext.setParentNsContext(nsContext); - } - - public void init() { - // just add the current elements namespace and prefix to the this - // elements nscontext - addToNsMap(name.getPrefix(), name.getNamespaceURI()); - - } - - /** - * @param prefix - * @param uri - */ - private void addToNsMap(String prefix, String uri) { - // TODO - need to fix this up to cater for cases where - // namespaces are having no prefixes - if (!uri.equals(namespaceContext.getNamespaceURI(prefix))) { - // this namespace is not there. Need to declare it - namespaceContext.pushNamespace(prefix, uri); - nsDeclared = true; - } - } - -} diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/NamedProperty.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/NamedProperty.java deleted file mode 100644 index 04d7d65a0d..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/NamedProperty.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import java.util.Map; - -import javax.xml.namespace.QName; - -/** - * A named property - * - * @version $Rev$ $Date$ - */ -public class NamedProperty implements Map.Entry { - private QName key; - - private Object value; - - public NamedProperty(QName key, Object value) { - this.key = key; - this.value = value; - } - - public NamedProperty(String key, Object value) { - this.key = new QName(key); - this.value = value; - } - - public QName getKey() { - return key; - } - - public Object getValue() { - return value; - } - - public Object setValue(Object value) { - Object v = this.value; - this.value = value; - return v; - } -} diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/NilElementStreamReader.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/NilElementStreamReader.java deleted file mode 100644 index abc4bf3785..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/NilElementStreamReader.java +++ /dev/null @@ -1,279 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import javax.xml.namespace.NamespaceContext; -import javax.xml.namespace.QName; -import javax.xml.stream.Location; -import javax.xml.stream.XMLStreamException; - -public class NilElementStreamReader implements XMLFragmentStreamReader { - - private static final int END_ELEMENT_STATE = 2; - - private static final int START_ELEMENT_STATE = 1; - private int currentState = START_ELEMENT; - - private QName elementQName; - - public NilElementStreamReader(QName elementQName) { - this.elementQName = elementQName; - } - - public void setParentNamespaceContext(NamespaceContext nsContext) { - // NOOP - } - - public void close() throws XMLStreamException { - // do nothing - } - - public int getAttributeCount() { - return 1; - } - - public String getAttributeLocalName(int i) { - return (i == 0) ? NIL_QNAME.getLocalPart() : null; - } - - public QName getAttributeName(int i) { - return (i == 0) ? NIL_QNAME : null; - } - - public String getAttributeNamespace(int i) { - return (i == 0) ? NIL_QNAME.getNamespaceURI() : null; - } - - public String getAttributePrefix(int i) { - return (i == 0) ? NIL_QNAME.getPrefix() : null; - } - - public String getAttributeType(int i) { - throw new UnsupportedOperationException(); - } - - public String getAttributeValue(int i) { - return (i == 0) ? NIL_VALUE_TRUE : null; - } - - public String getAttributeValue(String string, String string1) { - if (string == null && NIL_QNAME.getLocalPart().equals(string1)) { - return NIL_VALUE_TRUE; - } - return null; - } - - public String getCharacterEncodingScheme() { - throw new UnsupportedOperationException(); - } - - public String getElementText() throws XMLStreamException { - return null; - } - - public String getEncoding() { - return null; - } - - public int getEventType() { - int returnEvent = START_DOCUMENT; - switch (currentState) { - case START_ELEMENT_STATE: - returnEvent = START_ELEMENT; - break; - case END_ELEMENT_STATE: - returnEvent = END_ELEMENT; - break; - } - return returnEvent; - } - - public String getLocalName() { - return elementQName.getLocalPart(); - } - - public Location getLocation() { - return new Location() { - public int getCharacterOffset() { - return 0; - } - - public int getColumnNumber() { - return 0; - } - - public int getLineNumber() { - return 0; - } - - public String getPublicId() { - return null; - } - - public String getSystemId() { - return null; - } - }; - } - - public QName getName() { - return elementQName; - } - - public NamespaceContext getNamespaceContext() { - throw new UnsupportedOperationException(); - } - - public int getNamespaceCount() { - return 0; - } - - public String getNamespacePrefix(int i) { - return null; - } - - public String getNamespaceURI() { - return elementQName.getNamespaceURI(); - } - - public String getNamespaceURI(int i) { - return null; - } - - public String getNamespaceURI(String string) { - if (elementQName.getPrefix() != null && elementQName.getPrefix().equals(string)) { - return elementQName.getNamespaceURI(); - } else { - return null; - } - } - - public String getPIData() { - throw new UnsupportedOperationException(); - } - - public String getPITarget() { - throw new UnsupportedOperationException(); - } - - public String getPrefix() { - return elementQName.getPrefix(); - } - - public Object getProperty(String key) throws IllegalArgumentException { - // since optimization is a global property - // we've to implement it everywhere - return null; - } - - public String getText() { - return null; - } - - public char[] getTextCharacters() { - return new char[0]; - } - - public int getTextCharacters(int i, char[] chars, int i1, int i2) throws XMLStreamException { - return 0; - } - - public int getTextLength() { - return 0; - } - - public int getTextStart() { - return 0; - } - - public String getVersion() { - throw new UnsupportedOperationException(); - } - - public boolean hasName() { - return true; - } - - public boolean hasNext() throws XMLStreamException { - return currentState != END_ELEMENT_STATE; - - } - - public boolean hasText() { - return false; - } - - public void init() { - // NOOP - } - - public boolean isAttributeSpecified(int i) { - return i == 0; - } - - public boolean isCharacters() { - return false; - } - - public boolean isDone() { - return currentState == END_ELEMENT_STATE; - } - - public boolean isEndElement() { - return currentState == END_ELEMENT_STATE; - } - - public boolean isStandalone() { - throw new UnsupportedOperationException(); - } - - public boolean isStartElement() { - return currentState == START_ELEMENT_STATE; - } - - public boolean isWhiteSpace() { - return false; - } - - public int next() throws XMLStreamException { - int returnEvent = START_DOCUMENT; - switch (currentState) { - case START_ELEMENT_STATE: - currentState = END_ELEMENT_STATE; - returnEvent = END_ELEMENT; - break; - case END_ELEMENT_STATE: - throw new XMLStreamException("parser completed!"); - - } - return returnEvent; - } - - public int nextTag() throws XMLStreamException { - throw new UnsupportedOperationException(); - } - - public void require(int i, String string, String string1) throws XMLStreamException { - // nothing - } - - public boolean standaloneSet() { - throw new UnsupportedOperationException(); - } -} diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Node2XMLStreamReader.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Node2XMLStreamReader.java index 1e8d14a151..3d89a26cab 100644 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Node2XMLStreamReader.java +++ b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Node2XMLStreamReader.java @@ -20,6 +20,7 @@ package org.apache.tuscany.sca.databinding.xml; import javax.xml.stream.XMLStreamReader; +import org.apache.tuscany.sca.common.xml.stax.reader.DOMXMLStreamReader; import org.apache.tuscany.sca.databinding.PullTransformer; import org.apache.tuscany.sca.databinding.TransformationContext; import org.apache.tuscany.sca.databinding.TransformationException; diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/SAX2DOM.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/SAX2DOM.java deleted file mode 100644 index 02d871959d..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/SAX2DOM.java +++ /dev/null @@ -1,249 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -import javax.xml.parsers.ParserConfigurationException; - -import org.apache.tuscany.sca.databinding.impl.DOMHelper; -import org.w3c.dom.Comment; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.ProcessingInstruction; -import org.w3c.dom.Text; -import org.xml.sax.Attributes; -import org.xml.sax.ContentHandler; -import org.xml.sax.Locator; -import org.xml.sax.SAXException; -import org.xml.sax.ext.LexicalHandler; - -/** - * SAX2DOM adapter - * - * @version $Rev$ $Date$ - */ -public class SAX2DOM implements ContentHandler, LexicalHandler { - public static final String EMPTYSTRING = ""; - public static final String XML_PREFIX = "xml"; - public static final String XMLNS_PREFIX = "xmlns"; - public static final String XMLNS_STRING = "xmlns:"; - public static final String XMLNS_URI = "http://www.w3.org/2000/xmlns/"; - - private Node root; - - private Document document; - - private Node nextSibling; - - private Stack nodeStk = new Stack(); - - private List namespaceDecls; - - private Node lastSibling; - - public SAX2DOM() throws ParserConfigurationException { - this.document = DOMHelper.newDocument(); - this.root = document; - } - - public SAX2DOM(Node root, Node nextSibling) throws ParserConfigurationException { - this.root = root; - if (root instanceof Document) { - this.document = (Document)root; - } else if (root != null) { - this.document = root.getOwnerDocument(); - } else { - this.document = DOMHelper.newDocument(); - this.root = document; - } - - this.nextSibling = nextSibling; - } - - public SAX2DOM(Node root) throws ParserConfigurationException { - this(root, null); - } - - public Node getDOM() { - return root; - } - - public void characters(char[] ch, int start, int length) { - final Node last = nodeStk.peek(); - - // No text nodes can be children of root (DOM006 exception) - if (last != document) { - final String text = new String(ch, start, length); - if (lastSibling != null && lastSibling.getNodeType() == Node.TEXT_NODE) { - ((Text)lastSibling).appendData(text); - } else if (last == root && nextSibling != null) { - lastSibling = last.insertBefore(document.createTextNode(text), nextSibling); - } else { - lastSibling = last.appendChild(document.createTextNode(text)); - } - - } - } - - public void startDocument() { - nodeStk.push(root); - } - - public void endDocument() { - nodeStk.pop(); - } - - public void startElement(String namespace, String localName, String qName, Attributes attrs) { - final Element tmp = document.createElementNS(namespace, qName); - - // Add namespace declarations first - if (namespaceDecls != null) { - final int nDecls = namespaceDecls.size(); - for (int i = 0; i < nDecls; i++) { - final String prefix = namespaceDecls.get(i++); - - if (prefix == null || prefix.equals(EMPTYSTRING)) { - tmp.setAttributeNS(XMLNS_URI, XMLNS_PREFIX, namespaceDecls.get(i)); - } else { - tmp.setAttributeNS(XMLNS_URI, XMLNS_STRING + prefix, namespaceDecls.get(i)); - } - } - namespaceDecls.clear(); - } - - // Add attributes to element - final int nattrs = attrs.getLength(); - for (int i = 0; i < nattrs; i++) { - if (attrs.getLocalName(i) == null) { - tmp.setAttribute(attrs.getQName(i), attrs.getValue(i)); - } else { - tmp.setAttributeNS(attrs.getURI(i), attrs.getQName(i), attrs.getValue(i)); - } - } - - // Append this new node onto current stack node - Node last = nodeStk.peek(); - - // If the SAX2DOM is created with a non-null next sibling node, - // insert the result nodes before the next sibling under the root. - if (last == root && nextSibling != null) { - last.insertBefore(tmp, nextSibling); - } else { - last.appendChild(tmp); - } - - // Push this node onto stack - nodeStk.push(tmp); - lastSibling = null; - } - - public void endElement(String namespace, String localName, String qName) { - nodeStk.pop(); - lastSibling = null; - } - - public void startPrefixMapping(String prefix, String uri) { - if (namespaceDecls == null) { - namespaceDecls = new ArrayList(2); - } - namespaceDecls.add(prefix); - namespaceDecls.add(uri); - } - - public void endPrefixMapping(String prefix) { - // do nothing - } - - /** - * This class is only used internally so this method should never be called. - */ - public void ignorableWhitespace(char[] ch, int start, int length) { - } - - /** - * adds processing instruction node to DOM. - */ - public void processingInstruction(String target, String data) { - final Node last = nodeStk.peek(); - ProcessingInstruction pi = document.createProcessingInstruction(target, data); - if (pi != null) { - if (last == root && nextSibling != null) { - last.insertBefore(pi, nextSibling); - } else { - last.appendChild(pi); - } - - lastSibling = pi; - } - } - - /** - * This class is only used internally so this method should never be called. - */ - public void setDocumentLocator(Locator locator) { - } - - /** - * This class is only used internally so this method should never be called. - */ - public void skippedEntity(String name) { - } - - /** - * Lexical Handler method to create comment node in DOM tree. - */ - public void comment(char[] ch, int start, int length) { - final Node last = nodeStk.peek(); - Comment comment = document.createComment(new String(ch, start, length)); - if (comment != null) { - if (last == root && nextSibling != null) { - last.insertBefore(comment, nextSibling); - } else { - last.appendChild(comment); - } - - lastSibling = comment; - } - } - - // Lexical Handler methods- not implemented - public void startCDATA() { - } - - public void endCDATA() { - } - - public void startEntity(java.lang.String name) { - } - - public void endDTD() { - } - - public void endEntity(String name) { - } - - public void startDTD(String name, String publicId, String systemId) throws SAXException { - } - -} diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/SAX2DOMPipe.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/SAX2DOMPipe.java index cb9d538389..b6b3c05462 100644 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/SAX2DOMPipe.java +++ b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/SAX2DOMPipe.java @@ -18,8 +18,8 @@ */ package org.apache.tuscany.sca.databinding.xml; -import javax.xml.parsers.ParserConfigurationException; - +import org.apache.tuscany.sca.common.xml.dom.DOMHelper; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.DataPipe; import org.apache.tuscany.sca.databinding.DataPipeTransformer; import org.apache.tuscany.sca.databinding.impl.BaseTransformer; @@ -27,12 +27,13 @@ import org.w3c.dom.Node; import org.xml.sax.ContentHandler; public class SAX2DOMPipe extends BaseTransformer implements DataPipeTransformer { - + private DOMHelper domHelper; /** * */ - public SAX2DOMPipe() { + public SAX2DOMPipe(ExtensionPointRegistry registry) { super(); + domHelper = DOMHelper.getInstance(registry); } /** @@ -57,20 +58,16 @@ public class SAX2DOMPipe extends BaseTransformer implement return 30; } - private static class Pipe implements DataPipe { - private SAX2DOM pipe; + private class Pipe implements DataPipe { + private DOMHelper.NodeContentHandler pipe; public Pipe() { super(); - try { - this.pipe = new SAX2DOM(); - } catch (ParserConfigurationException e) { - throw new IllegalArgumentException(e); - } + this.pipe = domHelper.createContentHandler(null); } public Node getResult() { - return pipe.getDOM(); + return pipe.getNode(); } public ContentHandler getSink() { diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/SimpleXmlNodeImpl.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/SimpleXmlNodeImpl.java deleted file mode 100644 index e041f85214..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/SimpleXmlNodeImpl.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import javax.xml.namespace.QName; - -/** - * @version $Rev$ $Date$ - */ -public class SimpleXmlNodeImpl implements XmlNode { - private static final String XSI_PREFIX = "xsi"; - private static final String XSI_NS = "http://www.w3.org/2001/XMLSchema-instance"; - private static final QName XSI_NIL = new QName(XSI_NS, "nil", XSI_PREFIX); - private static final Map NS_MAP = new HashMap(); - static { - NS_MAP.put(XSI_PREFIX, XSI_NS); - } - - protected Type type; - protected QName name; - protected Object value; - - public SimpleXmlNodeImpl(QName name, Object value) { - this(name, value, name != null ? Type.ELEMENT : Type.CHARACTERS); - } - - public SimpleXmlNodeImpl(QName name, Object value, Type type) { - super(); - this.type = type; - this.name = name; - this.value = value; - } - - /** - * @see org.apache.tuscany.sca.databinding.xml.XmlNode#attributes() - */ - public List attributes() { - if (type == Type.ELEMENT && value == null) { - // Nil element - XmlNode attr = new SimpleXmlNodeImpl(XSI_NIL, "true"); - return Arrays.asList(attr); - } - return null; - } - - /** - * @see org.apache.tuscany.sca.databinding.xml.XmlNode#children() - */ - public Iterator children() { - if (type == Type.ELEMENT && value != null) { - // Nil element - XmlNode node = new SimpleXmlNodeImpl(null, value); - return Arrays.asList(node).iterator(); - } - return null; - } - - /** - * @see org.apache.tuscany.sca.databinding.xml.XmlNode#getName() - */ - public QName getName() { - return name; - } - - /** - * @see org.apache.tuscany.sca.databinding.xml.XmlNode#getValue() - */ - public String getValue() { - return value == null ? null : String.valueOf(value); - } - - /** - * @see org.apache.tuscany.sca.databinding.xml.XmlNode#namespaces() - */ - public Map namespaces() { - if (type == Type.ELEMENT && value == null) { - return NS_MAP; - } - return null; - } - - public Type getType() { - return type; - } - - public void setType(Type type) { - this.type = type; - } -} diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/StAX2SAXAdapter.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/StAX2SAXAdapter.java deleted file mode 100644 index 6257681091..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/StAX2SAXAdapter.java +++ /dev/null @@ -1,256 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import javax.xml.namespace.QName; -import javax.xml.stream.Location; -import javax.xml.stream.XMLStreamConstants; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; - -import org.xml.sax.Attributes; -import org.xml.sax.ContentHandler; -import org.xml.sax.Locator; -import org.xml.sax.SAXException; -import org.xml.sax.helpers.AttributesImpl; - -/** - * Adapter that converts from StAX to SAX event streams. Currently the following - * SAX events are not generated: - *

    - *
  • ignorableWhitespace
  • - *
  • skippedEntity
  • - *
      - * Also the following StAX events are not mapped: - *
        - *
      • CDATA
      • - *
      • COMMENT
      • - *
      • DTD
      • - *
      • ENTITY_DECLARATION
      • - *
      • ENTITY_REFERENCE
      • - *
      • NOTATION_DECLARATION
      • - *
      • SPACE
      • - *
      - * StAX ATTRIBUTE events are ignored but the equivalent attributes (derived from - * the START_ELEMENT event) are supplied in the SAX startElement event's - * Attributes parameter. If the adaptor is configured to pass namespace prefixes - * then namespace information will also be included in the Attributes; StAX - * NAMESPACE events are ignored.

      Another issue is namespace processing. If - * the reader is positioned at a sub-node, we cannot capture all the in-scope - * namespace bindings. Therefore we cannot re-create a proper SAX event stream - * from a StAX parser.

      For example

      <a:root xmlns:a="foo" - * xmlns:b="bar"><b:sub>a:foo</b:sub></a:root>

      And if - * you are handed a parser at <b:sub>, then your SAX events should look - * like:

      <b:sub xmlns:a="foo" xmlns:b="bar">a:foo</b:sub>

      - * not:

      <b:sub>a:foo</b:sub>

      Proposal: we change the - * receiver of SAX events (SDOXMLResourceImpl) so that it uses NamespaceContext - * to resolve prefix (as opposed to record start/endPrefixMappings and use it - * for resolution.) - * - * @version $Rev$ $Date$ - */ -public class StAX2SAXAdapter { - private final boolean namespacePrefixes; - - /** - * Construct a new StAX to SAX adapter that will convert a StAX event stream - * into a SAX event stream. - * - * @param namespacePrefixes whether xmlns attributes should be included in - * startElement events; - */ - public StAX2SAXAdapter(boolean namespacePrefixes) { - this.namespacePrefixes = namespacePrefixes; - } - - /** - * Pull events from the StAX stream and dispatch to the SAX ContentHandler. - * The StAX stream would typically be located on a START_DOCUMENT or - * START_ELEMENT event and when this method returns it will be located on - * the associated END_DOCUMENT or END_ELEMENT event. Behaviour with other - * start events is undefined. - * - * @param reader StAX event source to read - * @param handler SAX ContentHandler for processing events - * @throws XMLStreamException if there was a problem reading the stream - * @throws SAXException passed through from the ContentHandler - */ - public void parse(XMLStreamReader reader, ContentHandler handler) throws XMLStreamException, SAXException { - handler.setDocumentLocator(new LocatorAdaptor(reader.getLocation())); - - // remembers the nest level of elements to know when we are done - int level = 0; - int event = reader.getEventType(); - while (true) { - switch (event) { - case XMLStreamConstants.START_DOCUMENT: - level++; - handler.startDocument(); - break; - case XMLStreamConstants.START_ELEMENT: - level++; - handleStartElement(reader, handler); - break; - case XMLStreamConstants.PROCESSING_INSTRUCTION: - handler.processingInstruction(reader.getPITarget(), reader.getPIData()); - break; - case XMLStreamConstants.CHARACTERS: - handler.characters(reader.getTextCharacters(), reader.getTextStart(), reader - .getTextLength()); - break; - case XMLStreamConstants.END_ELEMENT: - handleEndElement(reader, handler); - level--; - if (level == 0) { - return; - } - break; - case XMLStreamConstants.END_DOCUMENT: - handler.endDocument(); - return; - /* - * uncomment to handle all events rather than just mapped - * ones // StAX events that are not mapped to SAX case - * XMLStreamConstants.COMMENT: case - * XMLStreamConstants.SPACE: case - * XMLStreamConstants.ENTITY_REFERENCE: case - * XMLStreamConstants.DTD: case XMLStreamConstants.CDATA: - * case XMLStreamConstants.NOTATION_DECLARATION: case - * XMLStreamConstants.ENTITY_DECLARATION: break; // StAX - * events handled in START_ELEMENT case - * XMLStreamConstants.ATTRIBUTE: case - * XMLStreamConstants.NAMESPACE: break; default: throw new - * AssertionError("Unknown StAX event: " + event); - */ - } - event = reader.next(); - } - } - - private void handleStartElement(XMLStreamReader reader, ContentHandler handler) throws SAXException { - // send startPrefixMapping events immediately before startElement event - int nsCount = reader.getNamespaceCount(); - for (int i = 0; i < nsCount; i++) { - String prefix = reader.getNamespacePrefix(i); - if (prefix == null) { // true for default namespace - prefix = ""; - } - handler.startPrefixMapping(prefix, reader.getNamespaceURI(i)); - } - - // fire startElement - QName qname = reader.getName(); - String prefix = qname.getPrefix(); - String rawname; - if (prefix == null || prefix.length() == 0) { - rawname = qname.getLocalPart(); - } else { - rawname = prefix + ':' + qname.getLocalPart(); - } - Attributes attrs = getAttributes(reader); - handler.startElement(qname.getNamespaceURI(), qname.getLocalPart(), rawname, attrs); - } - - private static void handleEndElement(XMLStreamReader reader, ContentHandler handler) throws SAXException { - // fire endElement - QName qname = reader.getName(); - handler.endElement(qname.getNamespaceURI(), qname.getLocalPart(), qname.toString()); - - // send endPrefixMapping events immediately after endElement event - // we send them in the opposite order to that returned but this is not - // actually required by SAX - int nsCount = reader.getNamespaceCount(); - for (int i = nsCount - 1; i >= 0; i--) { - String prefix = reader.getNamespacePrefix(i); - if (prefix == null) { // true for default namespace - prefix = ""; - } - handler.endPrefixMapping(prefix); - } - } - - /** - * Get the attributes associated with the current START_ELEMENT event. - * - * @return the StAX attributes converted to org.xml.sax.Attributes - */ - private Attributes getAttributes(XMLStreamReader reader) { - assert reader.getEventType() == XMLStreamConstants.START_ELEMENT; - - AttributesImpl attrs = new AttributesImpl(); - - // add namespace declarations if required - if (namespacePrefixes) { - for (int i = 0; i < reader.getNamespaceCount(); i++) { - String prefix = reader.getNamespacePrefix(i); - String uri = reader.getNamespaceURI(i); - attrs.addAttribute(null, prefix, "xmlns:" + prefix, "CDATA", uri); - } - } - - // Regular attributes - for (int i = 0; i < reader.getAttributeCount(); i++) { - String uri = reader.getAttributeNamespace(i); - if (uri == null) { - uri = ""; - } - String localName = reader.getAttributeLocalName(i); - String prefix = reader.getAttributePrefix(i); - String qname; - if (prefix == null || prefix.length() == 0) { - qname = localName; - } else { - qname = prefix + ':' + localName; - } - String type = reader.getAttributeType(i); - String value = reader.getAttributeValue(i); - - attrs.addAttribute(uri, localName, qname, type, value); - } - - return attrs; - } - - /** - * Adaptor for mapping Locator information. - */ - private static final class LocatorAdaptor implements Locator { - private final Location location; - - private LocatorAdaptor(Location location) { - this.location = location; - } - - public int getColumnNumber() { - return location == null ? 0 : location.getColumnNumber(); - } - - public int getLineNumber() { - return location == null ? 0 : location.getLineNumber(); - } - - public String getPublicId() { - return location == null ? "" : location.getPublicId(); - } - - public String getSystemId() { - return location == null ? "" : location.getSystemId(); - } - } -} diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/StAXHelper.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/StAXHelper.java deleted file mode 100644 index 103ecbd7d1..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/StAXHelper.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import java.io.InputStream; -import java.io.OutputStream; -import java.io.Reader; -import java.io.StringReader; -import java.io.StringWriter; -import java.io.Writer; - -import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLOutputFactory; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; -import javax.xml.stream.XMLStreamWriter; -import javax.xml.transform.Source; - - -public final class StAXHelper { - private static final XMLInputFactory INPUT_FACTORY = XMLInputFactory.newInstance(); - private static final XMLOutputFactory OUTPUT_FACTORY = XMLOutputFactory.newInstance(); - - private StAXHelper() { - } - - public static XMLStreamReader createXMLStreamReader(InputStream inputStream) throws XMLStreamException { - return INPUT_FACTORY.createXMLStreamReader(inputStream); - } - - public static XMLStreamReader createXMLStreamReader(Reader reader) throws XMLStreamException { - return INPUT_FACTORY.createXMLStreamReader(reader); - } - - public static XMLStreamReader createXMLStreamReader(Source source) throws XMLStreamException { - return INPUT_FACTORY.createXMLStreamReader(source); - } - - public static XMLStreamReader createXMLStreamReader(String string) throws XMLStreamException { - StringReader reader = new StringReader(string); - return createXMLStreamReader(reader); - } - - public static String save(XMLStreamReader reader) throws XMLStreamException { - StringWriter writer = new StringWriter(); - save(reader, writer); - return writer.toString(); - } - - public static void save(XMLStreamReader reader, OutputStream outputStream) throws XMLStreamException { - XMLStreamSerializer serializer = new XMLStreamSerializer(); - XMLStreamWriter streamWriter = OUTPUT_FACTORY.createXMLStreamWriter(outputStream); - serializer.serialize(reader, streamWriter); - streamWriter.flush(); - } - - public static void save(XMLStreamReader reader, Writer writer) throws XMLStreamException { - XMLStreamSerializer serializer = new XMLStreamSerializer(); - XMLStreamWriter streamWriter = OUTPUT_FACTORY.createXMLStreamWriter(writer); - serializer.serialize(reader, streamWriter); - streamWriter.flush(); - } - - public static void save(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { - XMLStreamSerializer serializer = new XMLStreamSerializer(); - serializer.serialize(reader, writer); - writer.flush(); - } - -} diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/String2XMLStreamReader.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/String2XMLStreamReader.java index a7364006d0..ac46bfd3d9 100644 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/String2XMLStreamReader.java +++ b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/String2XMLStreamReader.java @@ -21,6 +21,8 @@ package org.apache.tuscany.sca.databinding.xml; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; +import org.apache.tuscany.sca.common.xml.stax.StAXHelper; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.PullTransformer; import org.apache.tuscany.sca.databinding.TransformationContext; import org.apache.tuscany.sca.databinding.TransformationException; @@ -28,10 +30,15 @@ import org.apache.tuscany.sca.databinding.impl.BaseTransformer; public class String2XMLStreamReader extends BaseTransformer implements PullTransformer { - + private StAXHelper helper; + + public String2XMLStreamReader(ExtensionPointRegistry registry) { + helper = StAXHelper.getInstance(registry); + } + public XMLStreamReader transform(String source, TransformationContext context) { try { - return StAXHelper.createXMLStreamReader(source); + return helper.createXMLStreamReader(source); } catch (XMLStreamException e) { throw new TransformationException(e); } diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/WrappingXMLStreamReader.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/WrappingXMLStreamReader.java deleted file mode 100644 index d73e495798..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/WrappingXMLStreamReader.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import javax.xml.namespace.NamespaceContext; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; -import javax.xml.stream.util.StreamReaderDelegate; - -public class WrappingXMLStreamReader extends StreamReaderDelegate implements XMLFragmentStreamReader { - - private boolean done; - private int level; - - public WrappingXMLStreamReader(XMLStreamReader realReader) throws XMLStreamException { - super(realReader); - if (realReader == null) { - throw new UnsupportedOperationException("Reader cannot be null"); - } - - if (realReader instanceof XMLFragmentStreamReader) { - ((XMLFragmentStreamReader)realReader).init(); - } - - if (realReader.getEventType() == START_DOCUMENT) { - // Position to the 1st element - realReader.nextTag(); - } - if (realReader.getEventType() != START_ELEMENT) { - throw new IllegalStateException("The reader is not positioned at START_DOCUMENT or START_ELEMENT"); - } - this.done = false; - this.level = 1; - } - - @Override - public boolean hasNext() throws XMLStreamException { - return !done && super.hasNext(); - } - - @Override - public int next() throws XMLStreamException { - if (!hasNext()) { - throw new IllegalStateException("No more events"); - } - int event = super.next(); - if (!super.hasNext()) { - done = true; - } - if (event == START_ELEMENT) { - level++; - } else if (event == END_ELEMENT) { - level--; - if (level == 0) { - done = true; - } - } - return event; - } - - @Override - public int nextTag() throws XMLStreamException { - int event = 0; - while (true) { - event = next(); - if (event == START_ELEMENT || event == END_ELEMENT) { - return event; - } - } - } - - public void setParentNamespaceContext(NamespaceContext nsContext) { - // nothing to do here - } - - public void init() { - // Nothing to do here - } - - public boolean isDone() { - return done; - } - -} diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLDocumentStreamReader.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLDocumentStreamReader.java deleted file mode 100644 index a7088eada3..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLDocumentStreamReader.java +++ /dev/null @@ -1,482 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import java.util.NoSuchElementException; - -import javax.xml.namespace.NamespaceContext; -import javax.xml.namespace.QName; -import javax.xml.stream.Location; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; - -/** - * This class is derived from Apache Axis2 class - * org.apache.axis2.util.StreamWrapper. It's used wrap a XMLStreamReader to - * create a XMLStreamReader representing a document and it will produce - * START_DOCUMENT, END_DOCUMENT events. - * - * @version $Rev$ $Date$ - */ -public class XMLDocumentStreamReader implements XMLStreamReader { - private static final int STATE_COMPLETE_AT_NEXT = 2; // The wrapper - // will produce - // END_DOCUMENT - - private static final int STATE_COMPLETED = 3; // Done - - private static final int STATE_INIT = 0; // The wrapper will produce - // START_DOCUMENT - - private static final int STATE_SWITCHED = 1; // The real reader will - // produce events - - private XMLStreamReader realReader; - private boolean fragment; - private int level = 1; - - private int state = STATE_INIT; - - public XMLDocumentStreamReader(XMLStreamReader realReader) { - if (realReader == null) { - throw new UnsupportedOperationException("Reader cannot be null"); - } - - this.realReader = realReader; - - if (realReader instanceof XMLFragmentStreamReader) { - ((XMLFragmentStreamReader)realReader).init(); - } - - // If the real reader is positioned at START_DOCUMENT, always use - // the real reader - if (realReader.getEventType() == START_DOCUMENT) { - fragment = false; - state = STATE_SWITCHED; - } - } - - public void close() throws XMLStreamException { - realReader.close(); - } - - public int getAttributeCount() { - if (isDelegating()) { - return realReader.getAttributeCount(); - } else { - throw new IllegalStateException(); - } - } - - public String getAttributeLocalName(int i) { - if (isDelegating()) { - return realReader.getAttributeLocalName(i); - } else { - throw new IllegalStateException(); - } - } - - public QName getAttributeName(int i) { - if (isDelegating()) { - return realReader.getAttributeName(i); - } else { - throw new IllegalStateException(); - } - } - - public String getAttributeNamespace(int i) { - if (isDelegating()) { - return realReader.getAttributeNamespace(i); - } else { - throw new IllegalStateException(); - } - } - - public String getAttributePrefix(int i) { - if (isDelegating()) { - return realReader.getAttributePrefix(i); - } else { - throw new IllegalStateException(); - } - } - - public String getAttributeType(int i) { - if (isDelegating()) { - return realReader.getAttributeType(i); - } else { - throw new IllegalStateException(); - } - } - - public String getAttributeValue(int i) { - if (isDelegating()) { - return realReader.getAttributeValue(i); - } else { - throw new IllegalStateException(); - } - } - - public String getAttributeValue(String s, String s1) { - if (isDelegating()) { - return realReader.getAttributeValue(s, s1); - } else { - throw new IllegalStateException(); - } - } - - public String getCharacterEncodingScheme() { - return realReader.getCharacterEncodingScheme(); - } - - public String getElementText() throws XMLStreamException { - if (isDelegating()) { - return realReader.getElementText(); - } else { - throw new XMLStreamException(); - } - } - - public String getEncoding() { - return realReader.getEncoding(); - } - - public int getEventType() { - int event = -1; - switch (state) { - case STATE_SWITCHED: - case STATE_COMPLETE_AT_NEXT: - event = realReader.getEventType(); - break; - case STATE_INIT: - event = START_DOCUMENT; - break; - case STATE_COMPLETED: - event = END_DOCUMENT; - break; - } - return event; - } - - public String getLocalName() { - if (isDelegating()) { - return realReader.getLocalName(); - } else { - throw new IllegalStateException(); - } - } - - public Location getLocation() { - if (isDelegating()) { - return realReader.getLocation(); - } else { - return null; - } - } - - public QName getName() { - if (isDelegating()) { - return realReader.getName(); - } else { - throw new IllegalStateException(); - } - } - - public NamespaceContext getNamespaceContext() { - return realReader.getNamespaceContext(); - } - - public int getNamespaceCount() { - if (isDelegating()) { - return realReader.getNamespaceCount(); - } else { - throw new IllegalStateException(); - } - } - - public String getNamespacePrefix(int i) { - if (isDelegating()) { - return realReader.getNamespacePrefix(i); - } else { - throw new IllegalStateException(); - } - } - - public String getNamespaceURI() { - if (isDelegating()) { - return realReader.getNamespaceURI(); - } else { - throw new IllegalStateException(); - } - } - - public String getNamespaceURI(int i) { - if (isDelegating()) { - return realReader.getNamespaceURI(i); - } else { - throw new IllegalStateException(); - } - } - - public String getNamespaceURI(String s) { - if (isDelegating()) { - return realReader.getNamespaceURI(s); - } else { - throw new IllegalStateException(); - } - } - - public String getPIData() { - if (isDelegating()) { - return realReader.getPIData(); - } else { - throw new IllegalStateException(); - } - } - - public String getPITarget() { - if (isDelegating()) { - return realReader.getPITarget(); - } else { - throw new IllegalStateException(); - } - } - - public String getPrefix() { - if (isDelegating()) { - return realReader.getPrefix(); - } else { - throw new IllegalStateException(); - } - } - - public Object getProperty(String s) throws IllegalArgumentException { - if (isDelegating()) { - return realReader.getProperty(s); - } else { - throw new IllegalArgumentException(); - } - } - - public String getText() { - if (isDelegating()) { - return realReader.getText(); - } else { - throw new IllegalStateException(); - } - } - - public char[] getTextCharacters() { - if (isDelegating()) { - return realReader.getTextCharacters(); - } else { - throw new IllegalStateException(); - } - } - - public int getTextCharacters(int i, char[] chars, int i1, int i2) throws XMLStreamException { - if (isDelegating()) { - return realReader.getTextCharacters(i, chars, i1, i2); - } else { - throw new IllegalStateException(); - } - } - - public int getTextLength() { - if (isDelegating()) { - return realReader.getTextLength(); - } else { - throw new IllegalStateException(); - } - } - - public int getTextStart() { - if (isDelegating()) { - return realReader.getTextStart(); - } else { - throw new IllegalStateException(); - } - } - - public String getVersion() { - if (isDelegating()) { - return realReader.getVersion(); - } else { - return null; - } - } - - public boolean hasName() { - if (isDelegating()) { - return realReader.hasName(); - } else { - return false; - } - } - - public boolean hasNext() throws XMLStreamException { - if (state == STATE_COMPLETE_AT_NEXT) { - return true; - } else if (state == STATE_COMPLETED) { - return false; - } else if (state == STATE_SWITCHED) { - return realReader.hasNext(); - } else { - return true; - } - } - - public boolean hasText() { - if (isDelegating()) { - return realReader.hasText(); - } else { - return false; - } - } - - public boolean isAttributeSpecified(int i) { - if (isDelegating()) { - return realReader.isAttributeSpecified(i); - } else { - return false; - } - } - - public boolean isCharacters() { - if (isDelegating()) { - return realReader.isCharacters(); - } else { - return false; - } - } - - private boolean isDelegating() { - return state == STATE_SWITCHED || state == STATE_COMPLETE_AT_NEXT; - } - - public boolean isEndElement() { - if (isDelegating()) { - return realReader.isEndElement(); - } else { - return false; - } - } - - public boolean isStandalone() { - if (isDelegating()) { - return realReader.isStandalone(); - } else { - return false; - } - } - - public boolean isStartElement() { - if (isDelegating()) { - return realReader.isStartElement(); - } else { - return false; - } - } - - public boolean isWhiteSpace() { - if (isDelegating()) { - return realReader.isWhiteSpace(); - } else { - return false; - } - } - - public int next() throws XMLStreamException { - int returnEvent; - - switch (state) { - case STATE_SWITCHED: - returnEvent = realReader.next(); - if (returnEvent == END_DOCUMENT) { - state = STATE_COMPLETED; - } else if (!realReader.hasNext()) { - state = STATE_COMPLETE_AT_NEXT; - } - if (fragment && returnEvent == END_ELEMENT) { - level--; - if (level == 0) { - // We are now at the end of the top-level element in the fragment - state = STATE_COMPLETE_AT_NEXT; - } - } - if (fragment && returnEvent == START_ELEMENT) { - level++; - } - break; - case STATE_INIT: - state = STATE_SWITCHED; - returnEvent = realReader.getEventType(); - if (returnEvent == START_ELEMENT) { - // The real reader is positioned at the top-level element in the fragment - level = 0; - fragment = true; - } - break; - case STATE_COMPLETE_AT_NEXT: - state = STATE_COMPLETED; - returnEvent = END_DOCUMENT; - break; - case STATE_COMPLETED: - // oops - no way we can go beyond this - throw new NoSuchElementException("End of stream has reached."); - default: - throw new UnsupportedOperationException(); - } - - return returnEvent; - } - - public int nextTag() throws XMLStreamException { - if (isDelegating()) { - int returnEvent = realReader.nextTag(); - if (fragment && returnEvent == END_ELEMENT) { - level--; - if (level == 0) { - // We are now at the end of the top-level element in the fragment - state = STATE_COMPLETE_AT_NEXT; - } - } - if (fragment && returnEvent == START_ELEMENT) { - level++; - } - return returnEvent; - } else { - throw new XMLStreamException(); - } - } - - public void require(int i, String s, String s1) throws XMLStreamException { - if (isDelegating()) { - realReader.require(i, s, s1); - } - } - - public boolean standaloneSet() { - if (isDelegating()) { - return realReader.standaloneSet(); - } else { - return false; - } - } -} diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLFragmentStreamReader.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLFragmentStreamReader.java deleted file mode 100644 index a42d6acc9a..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLFragmentStreamReader.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import javax.xml.namespace.NamespaceContext; -import javax.xml.namespace.QName; -import javax.xml.stream.XMLStreamReader; - -public interface XMLFragmentStreamReader extends XMLStreamReader { - QName NIL_QNAME = new QName("http://www.w3.org/2001/XMLSchema-instance", "nil", "xsi"); - String NIL_VALUE_TRUE = "true"; - - /** - * this will help to handle Text within the current element. user should - * pass the element text to the property list as this ELEMENT_TEXT as the - * key. This key deliberately has a space in it so that it is not a valid - * XML name - */ - String ELEMENT_TEXT = "Element Text"; - - /** - * Extra method to query the state of the pullparser - */ - boolean isDone(); - - /** - * add the parent namespace context to this parser - */ - void setParentNamespaceContext(NamespaceContext nsContext); - - /** - * Initiate the parser - this will do whatever the needed tasks to initiate - * the parser and must be called before attempting any specific parsing - * using this parser - */ - void init(); -} diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLFragmentStreamReaderImpl.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLFragmentStreamReaderImpl.java deleted file mode 100644 index f1084cf2a3..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLFragmentStreamReaderImpl.java +++ /dev/null @@ -1,857 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -import javax.xml.namespace.NamespaceContext; -import javax.xml.namespace.QName; -import javax.xml.stream.Location; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; - -/** - * This is the new implementation of the XMLFramentStreamReader. The approach - * here is simple When the pull parser needs to generate events for a particular - * name-value(s) pair it always hands over (delegates) the task to another pull - * parser which knows how to deal with it The common types of name value pairs - * we'll come across are - *

        - *
      • String name/QName name - String value - *
      • String name/QName name - String[] value - *
      • QName name/String name - XMLStreamReader value - *
      • QName name/String name - XMLStreamable value - *
      • QName name/String name - Java bean - *
      • QName name/String name - Datahandler - * - *
      - *

      As for the attributes, these are the possible combinations in the array - *

        - *
      • String name/QName name - String value - *
      - * Note that certain array methods have been deliberately removed to avoid - * complications. The generated code will take the trouble to lay the elements - * of the array in the correct order

      Hence there will be a parser impl - * that knows how to handle these types, and this parent parser will always - * delegate these tasks to the child pullparasers in effect this is one huge - * state machine that has only a few states and delegates things down to the - * child parsers whenever possible

      - * - * @version $Rev$ $Date$ - */ -public class XMLFragmentStreamReaderImpl implements XMLFragmentStreamReader { - - private static final int DELEGATED_STATE = 2; - private static final int END_ELEMENT_STATE = 1; - // states for this pullparser - it can only have four states - private static final int START_ELEMENT_STATE = 0; - private static final int TEXT_STATE = 3; - - protected NamedProperty[] attributes; - - // reference to the child reader - protected XMLFragmentStreamReader childReader; - // current property index - // initialized at zero - protected int index; - protected Map declaredNamespaceMap = new HashMap(); - protected QName elementQName; - - // we always create a new namespace context - protected DelegatingNamespaceContext namespaceContext = new DelegatingNamespaceContext(); - - protected NamedProperty[] elements; - - // integer field that keeps the state of this - // parser. - protected int state = START_ELEMENT_STATE; - - /* - * we need to pass in a namespace context since when delegated, we've no - * idea of the current namespace context. So it needs to be passed on here! - */ - public XMLFragmentStreamReaderImpl(QName elementQName, NamedProperty[] elements, NamedProperty[] attributes) { - // validate the lengths, since both the arrays are supposed - // to have - this.elements = elements == null ? new NamedProperty[0] : elements; - this.elementQName = elementQName; - this.attributes = attributes == null ? new NamedProperty[0] : attributes; - } - - protected XMLFragmentStreamReaderImpl(QName elementQName) { - this.elementQName = elementQName; - } - - /** - * add the namespace context - */ - - public void setParentNamespaceContext(NamespaceContext nsContext) { - // register the namespace context passed in to this - this.namespaceContext.setParentNsContext(nsContext); - - } - - protected NamedProperty[] getElements() { - return elements; - } - - protected NamedProperty[] getAttributes() { - return attributes; - } - - protected QName[] getNamespaces() { - return new QName[0]; - } - - /** - * @param prefix - * @param uri - */ - protected void addToNsMap(String prefix, String uri) { - if (!uri.equals(namespaceContext.getNamespaceURI(prefix))) { - namespaceContext.pushNamespace(prefix, uri); - declaredNamespaceMap.put(prefix, uri); - } - } - - public void close() throws XMLStreamException { - // do nothing here - we have no resources to free - } - - public int getAttributeCount() { - return (state == DELEGATED_STATE) ? childReader.getAttributeCount() : (state == START_ELEMENT_STATE - ? getAttributes().length : 0); - } - - public String getAttributeLocalName(int i) { - if (state == DELEGATED_STATE) { - return childReader.getAttributeLocalName(i); - } else if (state == START_ELEMENT_STATE) { - QName name = getAttributeName(i); - if (name == null) { - return null; - } else { - return name.getLocalPart(); - } - } else { - throw new IllegalStateException(); - } - } - - /** - * @param i - */ - public QName getAttributeName(int i) { - if (state == DELEGATED_STATE) { - return childReader.getAttributeName(i); - } else if (state == START_ELEMENT_STATE) { - if ((i >= (getAttributes().length)) || i < 0) { // out of range - return null; - } else { - // get the attribute pointer - QName attribPointer = getAttributes()[i].getKey(); - // case one - attrib name is null - // this should be the pointer to the OMAttribute then - if (attribPointer == null) { - throw new UnsupportedOperationException(); - } else if (attribPointer instanceof QName) { - return attribPointer; - } else { - return null; - } - } - } else { - throw new IllegalStateException(); // as per the API contract - } - - } - - public String getAttributeNamespace(int i) { - if (state == DELEGATED_STATE) { - return childReader.getAttributeNamespace(i); - } else if (state == START_ELEMENT_STATE) { - QName name = getAttributeName(i); - if (name == null) { - return null; - } else { - return name.getNamespaceURI(); - } - } else { - throw new IllegalStateException(); - } - } - - public String getAttributePrefix(int i) { - if (state == DELEGATED_STATE) { - return childReader.getAttributePrefix(i); - } else if (state == START_ELEMENT_STATE) { - QName name = getAttributeName(i); - if (name == null) { - return null; - } else { - return name.getPrefix(); - } - } else { - throw new IllegalStateException(); - } - } - - public String getAttributeType(int i) { - return null; // not supported - } - - public String getAttributeValue(int i) { - if (state == DELEGATED_STATE) { - return childReader.getAttributeValue(i); - } else if (state == START_ELEMENT_STATE) { - if ((i >= (getAttributes().length)) || i < 0) { // out of range - return null; - } else { - // get the attribute pointer - QName attribPointer = getAttributes()[i].getKey(); - Object omAttribObj = getAttributes()[i].getValue(); - // case one - attrib name is null - // this should be the pointer to the OMAttribute then - if (attribPointer == null) { - throw new UnsupportedOperationException(); - } else if (attribPointer instanceof QName) { - return (String)omAttribObj; - } else { - return null; - } - } - } else { - throw new IllegalStateException(); - } - - } - - public String getAttributeValue(String nsUri, String localName) { - - int attribCount = getAttributeCount(); - String returnValue = null; - QName attribQualifiedName; - for (int i = 0; i < attribCount; i++) { - attribQualifiedName = getAttributeName(i); - if (nsUri == null) { - if (localName.equals(attribQualifiedName.getLocalPart())) { - returnValue = getAttributeValue(i); - break; - } - } else { - if (localName.equals(attribQualifiedName.getLocalPart()) && nsUri.equals(attribQualifiedName - .getNamespaceURI())) { - returnValue = getAttributeValue(i); - break; - } - } - - } - - return returnValue; - } - - public String getCharacterEncodingScheme() { - return null; // TODO - should we return something for this ? - } - - /** - * TODO implement the right contract for this - * - * @throws XMLStreamException - */ - public String getElementText() throws XMLStreamException { - if (state == DELEGATED_STATE) { - return childReader.getElementText(); - } else { - return null; - } - - } - - // ///////////////////////////////////////////////////////////////////////// - // / attribute handling - // ///////////////////////////////////////////////////////////////////////// - - public String getEncoding() { - if (state == DELEGATED_STATE) { - return childReader.getEncoding(); - } else { - // we've no idea what the encoding is going to be in this case - // perhaps we ought to return some constant here, which the user - // might - // have access to change! - return null; - } - } - - public int getEventType() { - if (state == START_ELEMENT_STATE) { - return START_ELEMENT; - } else if (state == END_ELEMENT_STATE) { - return END_ELEMENT; - } else if (state == TEXT_STATE) { - return CHARACTERS; - } else { // this is the delegated state - return childReader.getEventType(); - } - } - - public String getLocalName() { - if (state == DELEGATED_STATE) { - return childReader.getLocalName(); - } else if (state != TEXT_STATE) { - return elementQName.getLocalPart(); - } else { - throw new IllegalStateException(); - } - } - - /** - */ - public Location getLocation() { - // return a default location - return new Location() { - public int getCharacterOffset() { - return 0; - } - - public int getColumnNumber() { - return 0; - } - - public int getLineNumber() { - return 0; - } - - public String getPublicId() { - return null; - } - - public String getSystemId() { - return null; - } - }; - } - - public QName getName() { - if (state == DELEGATED_STATE) { - return childReader.getName(); - } else if (state != TEXT_STATE) { - return elementQName; - } else { - throw new IllegalStateException(); - } - - } - - public NamespaceContext getNamespaceContext() { - if (state == DELEGATED_STATE) { - return childReader.getNamespaceContext(); - } else { - return namespaceContext; - } - - } - - public int getNamespaceCount() { - if (state == DELEGATED_STATE) { - return childReader.getNamespaceCount(); - } else { - return declaredNamespaceMap.size(); - } - } - - /** - * @param i - */ - public String getNamespacePrefix(int i) { - if (state == DELEGATED_STATE) { - return childReader.getNamespacePrefix(i); - } else if (state != TEXT_STATE) { - // order the prefixes - String[] prefixes = makePrefixArray(); - if ((i >= prefixes.length) || (i < 0)) { - return null; - } else { - return prefixes[i]; - } - - } else { - throw new IllegalStateException(); - } - - } - - public String getNamespaceURI() { - if (state == DELEGATED_STATE) { - return childReader.getNamespaceURI(); - } else if (state == TEXT_STATE) { - return null; - } else { - return elementQName.getNamespaceURI(); - } - } - - // ///////////////////////////////////////////////////////////////////////// - // //////////// end of attribute handling - // ///////////////////////////////////////////////////////////////////////// - - // ////////////////////////////////////////////////////////////////////////// - // //////////// namespace handling - // ////////////////////////////////////////////////////////////////////////// - - public String getNamespaceURI(int i) { - if (state == DELEGATED_STATE) { - return childReader.getNamespaceURI(i); - } else if (state != TEXT_STATE) { - String namespacePrefix = getNamespacePrefix(i); - return namespacePrefix == null ? null : (String)declaredNamespaceMap.get(namespacePrefix); - } else { - throw new IllegalStateException(); - } - - } - - public String getNamespaceURI(String prefix) { - return namespaceContext.getNamespaceURI(prefix); - } - - public String getPIData() { - throw new UnsupportedOperationException("Yet to be implemented !!"); - } - - public String getPITarget() { - throw new UnsupportedOperationException("Yet to be implemented !!"); - } - - public String getPrefix() { - if (state == DELEGATED_STATE) { - return childReader.getPrefix(); - } else if (state == TEXT_STATE) { - return null; - } else { - String prefix = elementQName.getPrefix(); - return "".equals(prefix) ? null : prefix; - } - } - - // ///////////////////////////////////////////////////////////////////////// - // /////// end of namespace handling - // ///////////////////////////////////////////////////////////////////////// - - /** - * @param key - * @throws IllegalArgumentException - */ - public Object getProperty(String key) throws IllegalArgumentException { - if (state == START_ELEMENT_STATE || state == END_ELEMENT_STATE) { - return null; - } else if (state == TEXT_STATE) { - return null; - } else if (state == DELEGATED_STATE) { - return childReader.getProperty(key); - } else { - return null; - } - - } - - public String getText() { - if (state == DELEGATED_STATE) { - return childReader.getText(); - } else if (state == TEXT_STATE) { - return (String)getElements()[index - 1].getValue(); - } else { - throw new IllegalStateException(); - } - } - - public char[] getTextCharacters() { - if (state == DELEGATED_STATE) { - return childReader.getTextCharacters(); - } else if (state == TEXT_STATE) { - return getElements()[index - 1].getValue() == null ? new char[0] : ((String)getElements()[index - 1] - .getValue()).toCharArray(); - } else { - throw new IllegalStateException(); - } - } - - private int copy(int sourceStart, char[] target, int targetStart, int length) { - char[] source = getTextCharacters(); - if (sourceStart > source.length) { - throw new IndexOutOfBoundsException("source start > source length"); - } - int sourceLen = source.length - sourceStart; - if (length > sourceLen) { - length = sourceLen; - } - System.arraycopy(source, sourceStart, target, targetStart, length); - return sourceLen; - } - - public int getTextCharacters(int i, char[] chars, int i1, int i2) throws XMLStreamException { - if (state == DELEGATED_STATE) { - return childReader.getTextCharacters(i, chars, i1, i2); - } else if (state == TEXT_STATE) { - return copy(i, chars, i1, i2); - } else { - throw new IllegalStateException(); - } - } - - public int getTextLength() { - if (state == DELEGATED_STATE) { - return childReader.getTextLength(); - } else if (state == TEXT_STATE) { - return getTextCharacters().length; - } else { - throw new IllegalStateException(); - } - } - - public int getTextStart() { - if (state == DELEGATED_STATE) { - return childReader.getTextStart(); - } else if (state == TEXT_STATE) { - return 0; // assume text always starts at 0 - } else { - throw new IllegalStateException(); - } - } - - public String getVersion() { - return null; - } - - public boolean hasName() { - // since this parser always has a name, the hasName - // has to return true if we are still navigating this element - // if not we should ask the child reader for it. - if (state == DELEGATED_STATE) { - return childReader.hasName(); - } else { - return state != TEXT_STATE; - } - } - - /** - * @throws XMLStreamException - */ - public boolean hasNext() throws XMLStreamException { - if (state == DELEGATED_STATE) { - if (childReader.isDone()) { - // the child reader is done. We shouldn't be getting the - // hasNext result from the child pullparser then - return true; - } else { - return childReader.hasNext(); - } - } else { - return state == START_ELEMENT_STATE || state == TEXT_STATE; - - } - } - - /** - * check the validity of this implementation - */ - public boolean hasText() { - if (state == DELEGATED_STATE) { - return childReader.hasText(); - } else { - return state == TEXT_STATE; - } - - } - - /** - * we need to split out the calling to the populate namespaces separately - * since this needs to be done *after* setting the parent namespace context. - * We cannot assume it will happen at construction! - */ - public void init() { - // here we have an extra issue to attend to. we need to look at the - // prefixes and URIs (the combination) and populate a HashMap of - // namespaces. The HashMap of namespaces will be used to serve the - // namespace context - - populateNamespaceContext(); - } - - public boolean isAttributeSpecified(int i) { - return false; // not supported - } - - public boolean isCharacters() { - if (state == START_ELEMENT_STATE || state == END_ELEMENT_STATE) { - return false; - } - return childReader.isCharacters(); - } - - /** - * are we done ? - */ - public boolean isDone() { - return state == END_ELEMENT_STATE; - } - - public boolean isEndElement() { - if (state == START_ELEMENT_STATE) { - return false; - } else if (state == END_ELEMENT_STATE) { - return true; - } - return childReader.isEndElement(); - } - - public boolean isStandalone() { - return true; - } - - public boolean isStartElement() { - if (state == START_ELEMENT_STATE) { - return true; - } else if (state == END_ELEMENT_STATE) { - return false; - } - return childReader.isStartElement(); - } - - public boolean isWhiteSpace() { - if (state == START_ELEMENT_STATE || state == END_ELEMENT_STATE) { - return false; - } - return childReader.isWhiteSpace(); - } - - /** - * Get the prefix list from the HashTable and take that into an array - */ - private String[] makePrefixArray() { - String[] prefixes = declaredNamespaceMap.keySet().toArray(new String[declaredNamespaceMap.size()]); - Arrays.sort(prefixes); - return prefixes; - } - - /** - * By far this should be the most important method in this class this method - * changes the state of the parser - */ - public int next() throws XMLStreamException { - int returnEvent = -1; // invalid state is the default state - switch (state) { - case START_ELEMENT_STATE: - // current element is start element. We should be looking at the - // property list and making a pullparser for the property value - if (getElements() == null || getElements().length == 0) { - // no properties - move to the end element state - // straight away - state = END_ELEMENT_STATE; - returnEvent = END_ELEMENT; - } else { - // there are properties. now we should delegate this task to - // a - // child reader depending on the property type - returnEvent = processProperties(); - - } - break; - case END_ELEMENT_STATE: - // we've reached the end element already. If the user tries to - // push - // further ahead then it is an exception - throw new XMLStreamException("Trying to go beyond the end of the pullparser"); - - case DELEGATED_STATE: - if (childReader.isDone()) { - // we've reached the end! - if (index > (getElements().length - 1)) { - state = END_ELEMENT_STATE; - returnEvent = END_ELEMENT; - } else { - returnEvent = processProperties(); - } - } else { - returnEvent = childReader.next(); - } - break; - - case TEXT_STATE: - // if there are any more event we should be delegating to - // processProperties. if not we just return an end element - if (index > (getElements().length - 1)) { - state = END_ELEMENT_STATE; - returnEvent = END_ELEMENT; - } else { - returnEvent = processProperties(); - } - break; - } - return returnEvent; - } - - // ///////////////////////////////////////////////////////////////////////// - // / Other utility methods - // //////////////////////////////////////////////////////////////////////// - - /** - * TODO implement this - * - * @throws XMLStreamException - */ - public int nextTag() throws XMLStreamException { - return 0; - } - - /** - * Populates a namespace context - */ - private void populateNamespaceContext() { - - // first add the current element namespace to the namespace context - // declare it if not found - addToNsMap(elementQName.getPrefix(), elementQName.getNamespaceURI()); - - for (QName n : getNamespaces()) { - addToNsMap(n.getPrefix(), n.getNamespaceURI()); - } - - // traverse through the attributes and populate the namespace context - // the attrib list can be of many combinations - // the valid combinations are - // String - String - // QName - QName - // null - OMAttribute - - for (int i = 0; i < getAttributes().length; i++) { // jump in two - QName attrQName = getAttributes()[i].getKey(); - if (!"".equals(attrQName.getNamespaceURI())) { - addToNsMap(attrQName.getPrefix(), attrQName.getNamespaceURI()); - } - } - } - - /** - * A convenient method to reuse the properties - * - * @return event to be thrown - * @throws XMLStreamException - */ - private int processProperties() throws XMLStreamException { - // move to the next property depending on the current property - // index - QName propertyQName = getElements()[index].getKey(); - boolean textFound = false; - if (propertyQName == null) { - throw new XMLStreamException("property key cannot be null!"); - } else if (ELEMENT_TEXT.equals(propertyQName.getLocalPart())) { - // propPointer being a String has a special case - // that is it can be a the special constant ELEMENT_TEXT that - // says this text event - textFound = true; - } - - // OK! we got the key. Now look at the value - Object propertyValue = getElements()[index].getValue(); - // cater for the special case now - if (textFound) { - // no delegation here - make the parser null and immediately - // return with the event characters - childReader = null; - state = TEXT_STATE; - ++index; - return CHARACTERS; - } else if (propertyValue == null) { - // if the value is null we delegate the work to a nullable - // parser - childReader = new NilElementStreamReader(propertyQName); - childReader.setParentNamespaceContext(this.namespaceContext); - childReader.init(); - } else if (propertyValue instanceof String) { - // strings are handled by the NameValuePairStreamReader - childReader = new NameValuePairStreamReader(propertyQName, (String)propertyValue); - childReader.setParentNamespaceContext(this.namespaceContext); - childReader.init(); - } else if (propertyValue instanceof String[]) { - // string[] are handled by the NameValueArrayStreamReader - // if the array is empty - skip it - if (((String[])propertyValue).length == 0) { - // advance the index - ++index; - return processProperties(); - } else { - childReader = new NameValueArrayStreamReader(propertyQName, (String[])propertyValue); - childReader.setParentNamespaceContext(this.namespaceContext); - childReader.init(); - } - - } else if (propertyValue instanceof XMLStreamable) { - // ADBbean has it's own method to get a reader - XMLStreamReader reader = ((XMLStreamable)propertyValue).getXMLStreamReader(propertyQName); - // we know for sure that this is an ADB XMLStreamreader. - // However we need to make sure that it is compatible - if (reader instanceof XMLFragmentStreamReader) { - childReader = (XMLFragmentStreamReader)reader; - childReader.setParentNamespaceContext(this.namespaceContext); - childReader.init(); - } else { - // wrap it to make compatible - childReader = new WrappingXMLStreamReader(reader); - } - } else if (propertyValue instanceof XMLStreamReader) { - XMLStreamReader reader = (XMLStreamReader)propertyValue; - if (reader instanceof XMLFragmentStreamReader) { - childReader = (XMLFragmentStreamReader)reader; - childReader.setParentNamespaceContext(this.namespaceContext); - childReader.init(); - } else { - // wrap it to make compatible - childReader = new WrappingXMLStreamReader(reader); - } - - } else { - // all special possibilities has been tried! Let's treat - // the thing as a bean and try generating events from it - childReader = new WrappingXMLStreamReader(new BeanXMLStreamReaderImpl(propertyQName, propertyValue)); - // we cannot register the namespace context here - } - - // set the state here - state = DELEGATED_STATE; - // we are done with the delegation - // increment the property index - ++index; - return childReader.getEventType(); - } - - public void require(int i, String string, String string1) throws XMLStreamException { - throw new UnsupportedOperationException(); - } - - public boolean standaloneSet() { - return true; - } - -} diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLStreamReader2Node.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLStreamReader2Node.java index e2d0416aec..a180e21ab8 100644 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLStreamReader2Node.java +++ b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLStreamReader2Node.java @@ -20,6 +20,7 @@ package org.apache.tuscany.sca.databinding.xml; import javax.xml.stream.XMLStreamReader; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.DataPipe; import org.apache.tuscany.sca.databinding.PullTransformer; import org.apache.tuscany.sca.databinding.TransformationContext; @@ -38,11 +39,17 @@ import org.xml.sax.ContentHandler; public class XMLStreamReader2Node extends BaseTransformer implements PullTransformer { - private XMLStreamReader2SAX stax2sax = new XMLStreamReader2SAX(); + private XMLStreamReader2SAX stax2sax; + private SAX2DOMPipe sax2domPipe; + + public XMLStreamReader2Node(ExtensionPointRegistry registry) { + stax2sax = new XMLStreamReader2SAX(registry); + sax2domPipe = new SAX2DOMPipe(registry); + } public Node transform(XMLStreamReader source, TransformationContext context) { try { - DataPipe pipe = new SAX2DOMPipe().newInstance(); + DataPipe pipe = sax2domPipe.newInstance(); stax2sax.transform(source, pipe.getSink(), context); Node node = pipe.getResult(); source.close(); diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLStreamReader2SAX.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLStreamReader2SAX.java index 9b39f039b1..a45c563293 100644 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLStreamReader2SAX.java +++ b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLStreamReader2SAX.java @@ -20,6 +20,8 @@ package org.apache.tuscany.sca.databinding.xml; import javax.xml.stream.XMLStreamReader; +import org.apache.tuscany.sca.common.xml.stax.StAXHelper; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.PushTransformer; import org.apache.tuscany.sca.databinding.TransformationContext; import org.apache.tuscany.sca.databinding.TransformationException; @@ -33,7 +35,13 @@ import org.xml.sax.ContentHandler; */ public class XMLStreamReader2SAX extends BaseTransformer implements PushTransformer { - + + private StAXHelper helper; + + public XMLStreamReader2SAX(ExtensionPointRegistry registry) { + helper = StAXHelper.getInstance(registry); + } + @Override protected Class getTargetType() { return ContentHandler.class; @@ -58,9 +66,8 @@ public class XMLStreamReader2SAX extends BaseTransformer implements PullTransformer { - + private StAXHelper helper; + + public XMLStreamReader2String(ExtensionPointRegistry registry) { + helper = StAXHelper.getInstance(registry); + } + public String transform(XMLStreamReader source, TransformationContext context) { try { - String str = StAXHelper.save(source); + String str = helper.saveAsString(source); source.close(); return str; } catch (XMLStreamException e) { diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLStreamSerializer.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLStreamSerializer.java deleted file mode 100644 index 920ebb511c..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLStreamSerializer.java +++ /dev/null @@ -1,287 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import javax.xml.namespace.NamespaceContext; -import javax.xml.stream.XMLStreamConstants; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; -import javax.xml.stream.XMLStreamWriter; - -/** - * The XMLStreamSerializer pulls events from the XMLStreamReader and dumps into the XMLStreamWriter - * - * @version $Rev$ $Date$ - */ -public class XMLStreamSerializer implements XMLStreamConstants { - public static final String NAMESPACE_PREFIX = "ns"; - private static int namespaceSuffix; - - /* - * The behavior of the Serializer is such that it returns when it encounters the starting element for the second - * time. The depth variable tracks the depth of the Serializer and tells it when to return. Note that it is assumed - * that this Serialization starts on an Element. - */ - - /** - * Field depth - */ - private int depth; - - /** - * Generates a unique namespace prefix that is not in the scope of the NamespaceContext - * - * @param nsCtxt - * @return string - */ - private String generateUniquePrefix(NamespaceContext nsCtxt) { - String prefix = NAMESPACE_PREFIX + namespaceSuffix++; - // null should be returned if the prefix is not bound! - while (nsCtxt.getNamespaceURI(prefix) != null) { - prefix = NAMESPACE_PREFIX + namespaceSuffix++; - } - - return prefix; - } - - /** - * Method serialize. - * - * @param node - * @param writer - * @throws XMLStreamException - */ - public void serialize(XMLStreamReader node, XMLStreamWriter writer) throws XMLStreamException { - serializeNode(node, writer); - } - - /** - * @param reader - * @param writer - * @throws XMLStreamException - */ - protected void serializeAttributes(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { - int count = reader.getAttributeCount(); - String prefix; - String namespaceName; - String writerPrefix; - for (int i = 0; i < count; i++) { - prefix = reader.getAttributePrefix(i); - namespaceName = reader.getAttributeNamespace(i); - /* - * Due to parser implementations returning null as the namespace URI (for the empty namespace) we need to - * make sure that we deal with a namespace name that is not null. The best way to work around this issue is - * to set the namespace URI to "" if it is null - */ - if (namespaceName == null) { - namespaceName = ""; - } - - writerPrefix = writer.getPrefix(namespaceName); - - if (!"".equals(namespaceName)) { - // prefix has already being declared but this particular - // attrib has a - // no prefix attached. So use the prefix provided by the - // writer - if (writerPrefix != null && (prefix == null || prefix.equals(""))) { - writer.writeAttribute(writerPrefix, namespaceName, reader.getAttributeLocalName(i), reader - .getAttributeValue(i)); - - // writer prefix is available but different from the - // current - // prefix of the attrib. We should be declaring the new - // prefix - // as a namespace declaration - } else if (prefix != null && !"".equals(prefix) && !prefix.equals(writerPrefix)) { - writer.writeNamespace(prefix, namespaceName); - writer.writeAttribute(prefix, namespaceName, reader.getAttributeLocalName(i), reader - .getAttributeValue(i)); - - // prefix is null (or empty), but the namespace name is - // valid! it has not - // being written previously also. So we need to generate - // a prefix - // here - } else if (prefix == null || prefix.equals("")) { - prefix = generateUniquePrefix(writer.getNamespaceContext()); - writer.writeNamespace(prefix, namespaceName); - writer.writeAttribute(prefix, namespaceName, reader.getAttributeLocalName(i), reader - .getAttributeValue(i)); - } else { - writer.writeAttribute(prefix, namespaceName, reader.getAttributeLocalName(i), reader - .getAttributeValue(i)); - } - } else { - // empty namespace is equal to no namespace! - writer.writeAttribute(reader.getAttributeLocalName(i), reader.getAttributeValue(i)); - } - - } - } - - /** - * Method serializeCData. - * - * @param reader - * @param writer - * @throws XMLStreamException - */ - protected void serializeCData(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { - writer.writeCData(reader.getText()); - } - - /** - * Method serializeComment. - * - * @param reader - * @param writer - * @throws XMLStreamException - */ - protected void serializeComment(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { - writer.writeComment(reader.getText()); - } - - /** - * @param reader - * @param writer - * @throws XMLStreamException - */ - protected void serializeElement(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { - String prefix = reader.getPrefix(); - String nameSpaceName = reader.getNamespaceURI(); - if (nameSpaceName != null) { - String writerPrefix = writer.getPrefix(nameSpaceName); - if (writerPrefix != null) { - writer.writeStartElement(nameSpaceName, reader.getLocalName()); - } else { - if (prefix != null) { - writer.writeStartElement(prefix, reader.getLocalName(), nameSpaceName); - writer.writeNamespace(prefix, nameSpaceName); - // writer.setPrefix(prefix, nameSpaceName); - } else { - // [rfeng] We need to set default NS 1st before calling writeStateElement - writer.setDefaultNamespace(nameSpaceName); - writer.writeStartElement(nameSpaceName, reader.getLocalName()); - writer.writeDefaultNamespace(nameSpaceName); - } - } - } else { - writer.writeStartElement(reader.getLocalName()); - } - - // add the namespaces - int count = reader.getNamespaceCount(); - String namespacePrefix; - for (int i = 0; i < count; i++) { - namespacePrefix = reader.getNamespacePrefix(i); - // [rfeng] The following is commented out to allow to default ns - // if (namespacePrefix != null && namespacePrefix.length() == 0) { - // continue; - // } - - serializeNamespace(namespacePrefix, reader.getNamespaceURI(i), writer); - } - - // add attributes - serializeAttributes(reader, writer); - - } - - /** - * Method serializeEndElement. - * - * @param writer - * @throws XMLStreamException - */ - protected void serializeEndElement(XMLStreamWriter writer) throws XMLStreamException { - writer.writeEndElement(); - } - - /** - * Method serializeNamespace. - * - * @param prefix - * @param uri - * @param writer - * @throws XMLStreamException - */ - private void serializeNamespace(String prefix, String uri, XMLStreamWriter writer) throws XMLStreamException { - String prefix1 = writer.getPrefix(uri); - if (prefix1 == null) { - writer.writeNamespace(prefix, uri); - // writer.setPrefix(prefix, uri); - } - } - - /** - * Method serializeNode. - * - * @param reader - * @param writer - * @throws XMLStreamException - */ - protected void serializeNode(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { - while (true) { - int event = reader.getEventType(); - if (event == START_ELEMENT) { - serializeElement(reader, writer); - depth++; - } else if (event == ATTRIBUTE) { - serializeAttributes(reader, writer); - } else if (event == CHARACTERS) { - serializeText(reader, writer); - } else if (event == COMMENT) { - serializeComment(reader, writer); - } else if (event == CDATA) { - serializeCData(reader, writer); - } else if (event == END_ELEMENT) { - serializeEndElement(writer); - depth--; - } else if (event == START_DOCUMENT) { - depth++; // if a start document is found then increment - writer.writeStartDocument(); - // the depth - } else if (event == END_DOCUMENT) { - if (depth != 0) { - depth--; // for the end document - reduce the depth - } - writer.writeEndDocument(); - } - if (depth == 0) { - break; - } - if (reader.hasNext()) { - reader.next(); - } else { - break; - } - } - } - - /** - * @param reader - * @param writer - * @throws XMLStreamException - */ - protected void serializeText(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { - writer.writeCharacters(reader.getText()); - } -} diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLStreamable.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLStreamable.java deleted file mode 100644 index f32f93d098..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLStreamable.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import javax.xml.namespace.QName; -import javax.xml.stream.XMLStreamReader; - -/** - * An interface represents data that can be read using StAX streaming - * - * @version $Rev$ $Date$ - */ -public interface XMLStreamable { - /** - * Get the XMLStreamReader for StAX processing - * - * @param rootElementName the name of the element to be generated - * @return Returns a pull parser. - */ - XMLStreamReader getXMLStreamReader(QName rootElementName); -} diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XmlNode.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XmlNode.java deleted file mode 100644 index 86ebd627f7..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XmlNode.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import javax.xml.namespace.QName; - -/** - * @version $Rev$ $Date$ - */ -public interface XmlNode { - enum Type {ELEMENT, ATTRIBUTE, CHARACTERS, READER}; - /** - * Returns the children of the receiver as an Iterator. - */ - Iterator children(); - - /** - * Returns the attributes of the element as an List. Namespace declarations - * should be excluded. - * - * @return - */ - List attributes(); - - /** - * Returns a map of prefix to namespace URI - * @return - */ - Map namespaces(); - - /** - * Return the QName of the element. If it's for a text node, the name is null. - * @return - */ - QName getName(); - - /** - * Return the text value of the leaf element - * @return - */ - T getValue(); - - /** - * Return the type of the XML node - * @return - */ - Type getType(); -} diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XmlNodeIterator.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XmlNodeIterator.java deleted file mode 100644 index d1632befe1..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XmlNodeIterator.java +++ /dev/null @@ -1,355 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import java.util.ArrayList; -import java.util.EmptyStackException; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import javax.xml.namespace.NamespaceContext; - -/** - * @version $Rev$ $Date$ - */ -public class XmlNodeIterator implements Iterator { - public static final int START = 0; - public static final int END = 1; - - protected FastStack stack; - protected int state; - protected NamespaceContextImpl nsContext; - - public XmlNodeIterator(XmlNode rootNode) { - super(); - List v = new ArrayList(1); - v.add(rootNode); - stack = new FastStack(); - Iterator i = v.iterator(); - stack.push(new ElementHolder(null, i)); - this.state = START; - this.nsContext = new NamespaceContextImpl(null); - } - - public boolean hasNext() { - return !(stack.empty() || (state == END && stack.peek().parent == null)); - } - - public XmlNode next() { - this.state = START; - ElementHolder element = stack.peek(); - Iterator it = element.children; - if (it == null || (!it.hasNext())) { - // End of the children, return END event of parent - stack.pop(); - this.state = END; - this.nsContext = (NamespaceContextImpl)nsContext.getParent(); - return element.parent; - } - XmlNode node = it.next(); - stack.push(new ElementHolder(node, node.children())); - this.nsContext = new NamespaceContextImpl(this.nsContext); - populateNamespaces(node); - return node; - } - - public void remove() { - throw new UnsupportedOperationException(); - } - - public int getState() { - return state; - } - - public NamespaceContext getNamespaceContext() { - return nsContext; - } - - private void populateNamespaces(XmlNode element) { - if (element.getName() != null) { - if (element.namespaces() != null) { - for (Map.Entry e : element.namespaces().entrySet()) { - nsContext.register(e.getKey(), e.getValue()); - } - } - } - } - - private static class ElementHolder { - private XmlNode parent; - private Iterator children; - - public ElementHolder(XmlNode parent, Iterator children) { - this.parent = parent; - this.children = children; - } - } - - private static class NamespaceContextImpl implements NamespaceContext { - private NamespaceContext parent; - private Map map = new HashMap(); - - /** - * @param parent - */ - public NamespaceContextImpl(NamespaceContext parent) { - super(); - this.parent = parent; - if (parent == null) { - map.put("xml", "http://www.w3.org/XML/1998/namespace"); - map.put("xmlns", "http://www.w3.org/2000/xmlns/"); - } - } - - public String getNamespaceURI(String prefix) { - if (prefix == null) { - throw new IllegalArgumentException("Prefix is null"); - } - - String ns = (String)map.get(prefix); - if (ns != null) { - return ns; - } - if (parent != null) { - return parent.getNamespaceURI(prefix); - } - return null; - } - - public String getPrefix(String nsURI) { - if (nsURI == null) - throw new IllegalArgumentException("Namespace is null"); - for (Iterator> i = map.entrySet().iterator(); i.hasNext();) { - Map.Entry entry = i.next(); - if (entry.getValue().equals(nsURI)) { - return entry.getKey(); - } - } - if (parent != null) { - return parent.getPrefix(nsURI); - } - return null; - } - - public Iterator getPrefixes(String nsURI) { - List prefixList = new ArrayList(); - for (Iterator> i = map.entrySet().iterator(); i.hasNext();) { - Map.Entry entry = i.next(); - if (entry.getValue().equals(nsURI)) { - prefixList.add(entry.getKey()); - } - } - final Iterator currentIterator = prefixList.iterator(); - final Iterator parentIterator = parent != null ? null : parent.getPrefixes(nsURI); - return new Iterator() { - - public boolean hasNext() { - return currentIterator.hasNext() || (parentIterator != null && parentIterator.hasNext()); - } - - public Object next() { - if (!hasNext()) { - throw new IllegalStateException("End of iterator has reached"); - } - return currentIterator.hasNext() ? currentIterator.next() : parentIterator.next(); - } - - public void remove() { - throw new UnsupportedOperationException(); - } - - }; - - } - - public void register(String prefix, String ns) { - map.put(prefix, ns); - } - - public NamespaceContext getParent() { - return parent; - } - - @Override - public String toString() { - StringBuffer sb = new StringBuffer(map.toString()); - if (parent != null) { - sb.append("\nParent: "); - sb.append(parent); - } - return sb.toString(); - } - } - - /** - * An implementation of the {@link java.util.Stack} API that is based on an ArrayList instead of a - * Vector, so it is not synchronized to protect against multi-threaded access. The implementation is - * therefore operates faster in environments where you do not need to worry about multiple thread contention. - *

      - * The removal order of an ArrayStack is based on insertion order: The most recently added element is - * removed first. The iteration order is not the same as the removal order. The iterator returns elements - * from the bottom up, whereas the {@link #remove()} method removes them from the top down. - *

      - * Unlike Stack, ArrayStack accepts null entries. - */ - public static class FastStack extends ArrayList { - - /** Ensure Serialization compatibility */ - private static final long serialVersionUID = 2130079159931574599L; - - /** - * Constructs a new empty ArrayStack. The initial size is controlled by ArrayList - * and is currently 10. - */ - public FastStack() { - super(); - } - - /** - * Constructs a new empty ArrayStack with an initial size. - * - * @param initialSize the initial size to use - * @throws IllegalArgumentException if the specified initial size is negative - */ - public FastStack(int initialSize) { - super(initialSize); - } - - /** - * Return true if this stack is currently empty. - *

      - * This method exists for compatibility with java.util.Stack. New users of this class should use - * isEmpty instead. - * - * @return true if the stack is currently empty - */ - public boolean empty() { - return isEmpty(); - } - - /** - * Returns the top item off of this stack without removing it. - * - * @return the top item on the stack - * @throws EmptyStackException if the stack is empty - */ - public T peek() throws EmptyStackException { - int n = size(); - if (n <= 0) { - throw new EmptyStackException(); - } else { - return get(n - 1); - } - } - - /** - * Returns the n'th item down (zero-relative) from the top of this stack without removing it. - * - * @param n the number of items down to go - * @return the n'th item on the stack, zero relative - * @throws EmptyStackException if there are not enough items on the stack to satisfy this request - */ - public T peek(int n) throws EmptyStackException { - int m = (size() - n) - 1; - if (m < 0) { - throw new EmptyStackException(); - } else { - return get(m); - } - } - - /** - * Pops the top item off of this stack and return it. - * - * @return the top item on the stack - * @throws EmptyStackException if the stack is empty - */ - public T pop() throws EmptyStackException { - int n = size(); - if (n <= 0) { - throw new EmptyStackException(); - } else { - return remove(n - 1); - } - } - - /** - * Pushes a new item onto the top of this stack. The pushed item is also returned. This is equivalent to calling - * add. - * - * @param item the item to be added - * @return the item just pushed - */ - public Object push(T item) { - add(item); - return item; - } - - /** - * Returns the top-most index for the object in the stack - * - * @param object the object to be searched for - * @return top-most index, or -1 if not found - */ - public int search(T object) { - int i = size() - 1; // Current index - while (i >= 0) { - T current = get(i); - if ((object == null && current == null) || (object != null && object.equals(current))) { - return i; - } - i--; - } - return -1; - } - - /** - * Returns the element on the top of the stack. - * - * @return the element on the top of the stack - * @throws EmptyStackException if the stack is empty - */ - public T get() { - int size = size(); - if (size == 0) { - throw new EmptyStackException(); - } - return get(size - 1); - } - - /** - * Removes the element on the top of the stack. - * - * @return the removed element - * @throws EmptyStackException if the stack is empty - */ - public T remove() { - int size = size(); - if (size == 0) { - throw new EmptyStackException(); - } - return remove(size - 1); - } - - } - -} diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XmlTreeStreamReaderImpl.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XmlTreeStreamReaderImpl.java deleted file mode 100644 index f4bc357c7d..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XmlTreeStreamReaderImpl.java +++ /dev/null @@ -1,531 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; - -import javax.xml.namespace.NamespaceContext; -import javax.xml.namespace.QName; -import javax.xml.stream.Location; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; - -/** - * - * @version $Rev$ $Date$ - */ -public class XmlTreeStreamReaderImpl implements XMLStreamReader { - - protected int state; - protected XmlNodeIterator iterator; - protected XmlNode current; - - protected XMLStreamReader reader; - - /* - * we need to pass in a namespace context since when delegated, we've no - * idea of the current namespace context. So it needs to be passed on here! - */ - public XmlTreeStreamReaderImpl(XmlNode root) { - this.iterator = new XmlNodeIterator(root); - this.current = null; - this.state = START_DOCUMENT; - this.reader = null; - } - - public void close() throws XMLStreamException { - if (reader != null) { - reader.close(); - } - } - - private void checkElementState() { - if (getEventType() != START_ELEMENT && getEventType() != END_ELEMENT) { - throw new IllegalStateException(); - } - } - - private List getAttributes() { - if (current != null && current.attributes() != null) { - return current.attributes(); - } else { - return Collections.emptyList(); - } - } - - public int getAttributeCount() { - checkElementState(); - if (reader != null) { - return reader.getAttributeCount(); - } - return getAttributes().size(); - } - - public String getAttributeLocalName(int i) { - checkElementState(); - if (reader != null) { - return reader.getAttributeLocalName(i); - } - return getAttributes().get(i).getName().getLocalPart(); - } - - /** - * @param i - */ - public QName getAttributeName(int i) { - checkElementState(); - if (reader != null) { - return reader.getAttributeName(i); - } - return getAttributes().get(i).getName(); - } - - public String getAttributeNamespace(int i) { - checkElementState(); - if (reader != null) { - return reader.getAttributeNamespace(i); - } - return getAttributes().get(i).getName().getNamespaceURI(); - } - - public String getAttributePrefix(int i) { - checkElementState(); - if (reader != null) { - return reader.getAttributePrefix(i); - } - return getAttributes().get(i).getName().getPrefix(); - } - - public String getAttributeType(int i) { - if (reader != null) { - return reader.getAttributeType(i); - } - return null; // not supported - } - - public String getAttributeValue(int i) { - checkElementState(); - if (reader != null) { - return reader.getAttributeValue(i); - } - return getAttributes().get(i).getValue(); - } - - public String getAttributeValue(String nsUri, String localName) { - checkElementState(); - if (reader != null) { - return reader.getAttributeValue(nsUri, localName); - } - int count = getAttributeCount(); - String value = null; - QName attrQName; - for (int i = 0; i < count; i++) { - attrQName = getAttributeName(i); - if (nsUri == null) { - if (localName.equals(attrQName.getLocalPart())) { - value = getAttributeValue(i); - break; - } - } else { - if (localName.equals(attrQName.getLocalPart()) && nsUri.equals(attrQName.getNamespaceURI())) { - value = getAttributeValue(i); - break; - } - } - - } - - return value; - } - - public String getCharacterEncodingScheme() { - if (reader != null) { - return reader.getCharacterEncodingScheme(); - } - return "UTF-8"; - } - - public String getElementText() throws XMLStreamException { - checkElementState(); - if (reader != null) { - return reader.getElementText(); - } - return current.getValue(); - } - - public String getEncoding() { - if (reader != null) { - return reader.getEncoding(); - } - return "UTF-8"; - } - - public int getEventType() { - return state; - } - - public String getLocalName() { - checkElementState(); - if (reader != null) { - return reader.getLocalName(); - } - return current.getName().getLocalPart(); - } - - /** - */ - public Location getLocation() { - if (reader != null) { - return reader.getLocation(); - } - // return a default location - return new Location() { - public int getCharacterOffset() { - return 0; - } - - public int getColumnNumber() { - return 0; - } - - public int getLineNumber() { - return 0; - } - - public String getPublicId() { - return null; - } - - public String getSystemId() { - return null; - } - }; - } - - public QName getName() { - checkElementState(); - if (reader != null) { - return reader.getName(); - } - return current.getName(); - } - - public NamespaceContext getNamespaceContext() { - if (reader != null) { - return reader.getNamespaceContext(); - } - return iterator.getNamespaceContext(); - } - - private Map getNamespaces() { - if (current != null && current.namespaces() != null) { - return current.namespaces(); - } else { - return Collections.emptyMap(); - } - } - - public int getNamespaceCount() { - checkElementState(); - if (reader != null) { - return reader.getNamespaceCount(); - } - return getNamespaces().size(); - } - - /** - * @param i - */ - public String getNamespacePrefix(int i) { - checkElementState(); - if (reader != null) { - return reader.getNamespacePrefix(i); - } - return new ArrayList>(getNamespaces().entrySet()).get(i).getKey(); - } - - public String getNamespaceURI() { - checkElementState(); - if (reader != null) { - return reader.getNamespaceURI(); - } - return current.getName().getNamespaceURI(); - } - - public String getNamespaceURI(int i) { - checkElementState(); - if (reader != null) { - return reader.getNamespaceURI(i); - } - return new ArrayList>(getNamespaces().entrySet()).get(i).getValue(); - } - - public String getNamespaceURI(String prefix) { - if (reader != null) { - return reader.getNamespaceURI(prefix); - } - return getNamespaceContext().getNamespaceURI(prefix); - } - - public String getPIData() { - if (reader != null) { - return reader.getPIData(); - } - throw new UnsupportedOperationException("Yet to be implemented !!"); - } - - public String getPITarget() { - if (reader != null) { - return reader.getPITarget(); - } - throw new UnsupportedOperationException("Yet to be implemented !!"); - } - - public String getPrefix() { - if (reader != null) { - return reader.getPrefix(); - } - if (state == START_ELEMENT || state == END_ELEMENT) { - String prefix = current.getName().getPrefix(); - return "".equals(prefix) ? null : prefix; - } else if (state == START_DOCUMENT) { - return null; - } else { - throw new IllegalStateException("State==" + state); - } - } - - /** - * @param key - * @throws IllegalArgumentException - */ - public Object getProperty(String key) throws IllegalArgumentException { - if (reader != null) { - return reader.getProperty(key); - } - return null; - } - - public String getText() { - if (reader != null) { - return reader.getText(); - } - return current.getValue(); - } - - public char[] getTextCharacters() { - if (reader != null) { - return reader.getTextCharacters(); - } - String value = current.getValue(); - return value == null ? new char[0] : value.toCharArray(); - } - - private int copy(int sourceStart, char[] target, int targetStart, int length) { - char[] source = getTextCharacters(); - if (sourceStart > source.length) { - throw new IndexOutOfBoundsException("source start > source length"); - } - int sourceLen = source.length - sourceStart; - if (length > sourceLen) { - length = sourceLen; - } - System.arraycopy(source, sourceStart, target, targetStart, length); - return sourceLen; - } - - public int getTextCharacters(int i, char[] chars, int i1, int i2) throws XMLStreamException { - if (reader != null) { - return reader.getTextCharacters(i, chars, i1, i2); - } - return copy(i, chars, i1, i2); - } - - public int getTextLength() { - if (reader != null) { - return reader.getTextLength(); - } - return getTextCharacters().length; - } - - public int getTextStart() { - if (reader != null) { - return reader.getTextStart(); - } - return 0; - } - - public String getVersion() { - return "1.0"; - } - - public boolean hasName() { - if (reader != null) { - return reader.hasName(); - } - return current.getName() != null; - } - - /** - * @throws XMLStreamException - */ - public boolean hasNext() throws XMLStreamException { - return iterator.hasNext() || state != END_DOCUMENT || (reader != null && reader.hasNext()); - } - - public boolean hasText() { - if (reader != null) { - return reader.hasText(); - } - return current.getType() == XmlNode.Type.CHARACTERS; - } - - public boolean isAttributeSpecified(int i) { - if (reader != null) { - return reader.isAttributeSpecified(i); - } - return false; // not supported - } - - public boolean isCharacters() { - if (reader != null) { - return reader.isCharacters(); - } - return current.getType() == XmlNode.Type.CHARACTERS; - } - - public boolean isEndElement() { - if (reader != null) { - return reader.isEndElement(); - } - return getEventType() == END_ELEMENT; - } - - public boolean isStandalone() { - return true; - } - - public boolean isStartElement() { - if (reader != null) { - return reader.isStartElement(); - } - return getEventType() == START_ELEMENT; - } - - public boolean isWhiteSpace() { - if (reader != null) { - return reader.isWhiteSpace(); - } - return false; - } - - /** - * By far this should be the most important method in this class this method - * changes the state of the parser - */ - public int next() throws XMLStreamException { - if (!hasNext()) { - throw new IllegalStateException("No more events"); - } - if (reader != null) { - if (!reader.hasNext()) { - this.reader = null; - } else { - // Go to the delegation mode - state = reader.next(); - return state; - } - } - if (!iterator.hasNext()) { - state = END_DOCUMENT; - current = null; - return state; - } - current = iterator.next(); - XmlNode.Type type = current.getType(); - - int itState = iterator.getState(); - if (itState == XmlNodeIterator.END) { - if (type == XmlNode.Type.ELEMENT) { - state = END_ELEMENT; - } else { - // Ignore the pop - state = next(); - } - } - if (itState == XmlNodeIterator.START) { - if (type == XmlNode.Type.ELEMENT) { - state = START_ELEMENT; - } else if (type == XmlNode.Type.CHARACTERS) { - state = CHARACTERS; - } else if (type == XmlNode.Type.READER) { - XMLStreamReader value = current.getValue(); - this.reader = new WrappingXMLStreamReader(value); - state = reader.getEventType(); - return state; - } - } - return state; - } - - /** - * TODO implement this - * - * @throws XMLStreamException - */ - public int nextTag() throws XMLStreamException { - while (true) { - int event = next(); - if (event == START_ELEMENT || event == END_ELEMENT) { - return event; - } - } - } - - public void require(int i, String ns, String localPart) throws XMLStreamException { - if (reader != null) { - reader.require(i, ns, localPart); - return; - } - int event = getEventType(); - if (event != i) { - throw new IllegalStateException("Event type is " + event + " (!=" + i + ")"); - } - QName name = getName(); - String ns1 = name.getNamespaceURI(); - String localName1 = name.getLocalPart(); - - if (ns != null && !ns.equals(ns1)) { - throw new IllegalStateException("Namespace URI is " + ns1 + " (!=" + ns + ")"); - } - - if (localPart != null && !localPart.equals(localName1)) { - throw new IllegalStateException("Local name is " + localName1 + " (!=" + localPart + ")"); - } - - } - - public boolean standaloneSet() { - return true; - } - -} diff --git a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/DataBindingRegistryImplTestCase.java.fixme b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/DataBindingRegistryImplTestCase.java.fixme deleted file mode 100644 index db210683dd..0000000000 --- a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/DataBindingRegistryImplTestCase.java.fixme +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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.tuscany.sca.databinding.impl; - -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; - -import javax.xml.stream.XMLStreamReader; - -import junit.framework.TestCase; - -import org.apache.tuscany.sca.databinding.DataBinding; -import org.apache.tuscany.sca.databinding.DataBindingExtensionPoint; -import org.apache.tuscany.sca.databinding.DefaultDataBindingExtensionPoint; -import org.apache.tuscany.sca.interfacedef.DataType; -import org.apache.tuscany.sca.interfacedef.Operation; -import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl; -import org.easymock.EasyMock; -import org.xml.sax.ContentHandler; - -/** - * - * @version $Rev$ $Date$ - */ -public class DataBindingRegistryImplTestCase extends TestCase { - private DataBindingExtensionPoint registry; - - /** - * @see junit.framework.TestCase#setUp() - */ - @Override - protected void setUp() throws Exception { - super.setUp(); - registry = new DefaultDataBindingExtensionPoint(); - } - - @SuppressWarnings("unchecked") - public void testRegistry() { - DataBinding db1 = createMock(DataBinding.class); - expect(db1.getAliases()).andReturn(new String[] {"db1"}).anyTimes(); - expect(db1.getName()).andReturn(ContentHandler.class.getName()).anyTimes(); - DataType dataType1 = new DataTypeImpl(ContentHandler.class, ContentHandler.class); - expect(db1.introspect(dataType1, null)).andReturn(true); - expect(db1.introspect(EasyMock.not(EasyMock.same(dataType1)), (Operation) EasyMock.isNull())) - .andReturn(false).anyTimes(); - replay(db1); - - registry.addDataBinding(db1); - - DataBinding db2 = createMock(DataBinding.class); - expect(db2.getAliases()).andReturn(new String[] {"db2"}).anyTimes(); - expect(db2.getName()).andReturn(XMLStreamReader.class.getName()).anyTimes(); - DataType dataType2 = new DataTypeImpl(XMLStreamReader.class, XMLStreamReader.class); - expect(db2.introspect(dataType2, null)).andReturn(true); - expect(db2.introspect(EasyMock.not(EasyMock.same(dataType2)), (Operation) EasyMock.isNull())) - .andReturn(false).anyTimes(); - replay(db2); - - registry.addDataBinding(db2); - - // Lookup by name - String name = db1.getName(); - DataBinding db3 = registry.getDataBinding(name); - assertSame(db1, db3); - - // Look up by alias - DataBinding db5 = registry.getDataBinding("db1"); - assertSame(db1, db5); - - DataType dt = new DataTypeImpl(ContentHandler.class, null); - registry.introspectType(dt, null); - assertEquals(dataType1.getLogical(), ContentHandler.class); - //FIXME does not match with dynamically loaded databindings - //assertTrue(dt.getDataBinding().equalsIgnoreCase("java.lang.Object")); - - registry.removeDataBinding(name); - DataBinding db4 = registry.getDataBinding(name); - assertNull(db4); - - dt = new DataTypeImpl(null, String.class, null); - registry.introspectType(dt, null); - //FIXME does not match with dynamically loaded databindings - //assertEquals("java.lang.Object", dt.getDataBinding()); - } - -} diff --git a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/MediatorImplTestCase.java b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/MediatorImplTestCase.java index 0be7320f3e..a74a8e5550 100644 --- a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/MediatorImplTestCase.java +++ b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/MediatorImplTestCase.java @@ -21,6 +21,8 @@ package org.apache.tuscany.sca.databinding.impl; import java.io.StringWriter; import java.io.Writer; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.DataBindingExtensionPoint; import org.apache.tuscany.sca.databinding.DefaultDataBindingExtensionPoint; import org.apache.tuscany.sca.databinding.DefaultTransformerExtensionPoint; @@ -79,11 +81,12 @@ public class MediatorImplTestCase { @Before public void setUp() throws Exception { - DataBindingExtensionPoint dataBindingRegistry = new DefaultDataBindingExtensionPoint(); - TransformerExtensionPoint registry = new DefaultTransformerExtensionPoint(null); + ExtensionPointRegistry extensionPointRegistry = new DefaultExtensionPointRegistry(); + DataBindingExtensionPoint dataBindingRegistry = new DefaultDataBindingExtensionPoint(extensionPointRegistry); + TransformerExtensionPoint registry = new DefaultTransformerExtensionPoint(extensionPointRegistry); registry.addTransformer(new String2SAX(), true); - registry.addTransformer(new SAX2DOMPipe(), true); + registry.addTransformer(new SAX2DOMPipe(extensionPointRegistry), true); registry.addTransformer(new Node2String(), true); registry.addTransformer(new Node2Writer(), true); diff --git a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/TransformerRegistryImplTestCase.java.fixme b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/TransformerRegistryImplTestCase.java.fixme deleted file mode 100644 index 31c3fd4f00..0000000000 --- a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/TransformerRegistryImplTestCase.java.fixme +++ /dev/null @@ -1,109 +0,0 @@ -/* - * 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.tuscany.sca.databinding.impl; - -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; - -import java.util.List; - -import junit.framework.Assert; -import junit.framework.TestCase; - -import org.apache.tuscany.sca.databinding.DefaultTransformerExtensionPoint; -import org.apache.tuscany.sca.databinding.Transformer; -import org.apache.tuscany.sca.databinding.TransformerExtensionPoint; - -/** - * - * @version $Rev$ $Date$ - */ -public class TransformerRegistryImplTestCase extends TestCase { - private TransformerExtensionPoint registry; - - /** - * @see junit.framework.TestCase#setUp() - */ - @Override - protected void setUp() throws Exception { - super.setUp(); - registry = new DefaultTransformerExtensionPoint(); - } - - public void testRegisterTransformer1() { - Transformer transformer = createMock(Transformer.class); - registry.addTransformer("a", "b", 10, transformer, true); - Transformer t = registry.getTransformer("a", "b"); - Assert.assertSame(t, transformer); - } - - public void testRegisterTransformerTransformer() { - Transformer transformer = createMock(Transformer.class); - expect(transformer.getSourceDataBinding()).andReturn("a"); - expect(transformer.getTargetDataBinding()).andReturn("b"); - expect(transformer.getWeight()).andReturn(10); - replay(transformer); - registry.addTransformer(transformer, true); - Transformer t = registry.getTransformer("a", "b"); - Assert.assertSame(t, transformer); - } - - public void testUnregisterTransformer() { - Transformer transformer = createMock(Transformer.class); - registry.addTransformer("a", "b", 10, transformer, true); - boolean result = registry.removeTransformer("a", "b"); - Assert.assertTrue(result); - Transformer t = registry.getTransformer("a", "b"); - Assert.assertNull(t); - } - - public void testGetTransformerChain() { - Transformer t1 = createMock(Transformer.class); - expect(t1.getSourceDataBinding()).andReturn("a"); - expect(t1.getTargetDataBinding()).andReturn("b"); - expect(t1.getWeight()).andReturn(10); - replay(t1); - Transformer t2 = createMock(Transformer.class); - expect(t2.getSourceDataBinding()).andReturn("b"); - expect(t2.getTargetDataBinding()).andReturn("c"); - expect(t2.getWeight()).andReturn(20); - replay(t2); - - Transformer t3 = createMock(Transformer.class); - expect(t3.getSourceDataBinding()).andReturn("a"); - expect(t3.getTargetDataBinding()).andReturn("c"); - expect(t3.getWeight()).andReturn(120); - replay(t3); - - registry.addTransformer(t1, true); - registry.addTransformer(t2, true); - registry.addTransformer(t3, true); - - List l1 = registry.getTransformerChain("a", "b"); - Assert.assertTrue(l1.size() == 1 && l1.get(0) == t1); - List l2 = registry.getTransformerChain("a", "c"); - Assert.assertTrue(l2.size() == 1 && l2.get(0) == t3); - List l3 = registry.getTransformerChain("a", "d"); - Assert.assertNull(l3); - - } - -} diff --git a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/XMLDocumentStreamReaderTestCase.java b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/XMLDocumentStreamReaderTestCase.java index ea6f34d6fb..e240592adf 100644 --- a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/XMLDocumentStreamReaderTestCase.java +++ b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/XMLDocumentStreamReaderTestCase.java @@ -22,11 +22,13 @@ package org.apache.tuscany.sca.databinding.impl; import java.io.StringReader; import javax.xml.namespace.QName; -import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamReader; -import org.apache.tuscany.sca.databinding.xml.XMLDocumentStreamReader; +import org.apache.tuscany.sca.common.xml.stax.StAXHelper; +import org.apache.tuscany.sca.common.xml.stax.reader.XMLDocumentStreamReader; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.xml.XMLStreamReader2String; import org.junit.Assert; @@ -38,13 +40,15 @@ public class XMLDocumentStreamReaderTestCase { @org.junit.Test public void testReader() throws Exception { - XMLInputFactory factory = XMLInputFactory.newInstance(); - XMLStreamReader r1 = factory.createXMLStreamReader(new StringReader(xml)); + ExtensionPointRegistry extensionPointRegistry = new DefaultExtensionPointRegistry(); + + StAXHelper helper = StAXHelper.getInstance(extensionPointRegistry); + XMLStreamReader r1 = helper.createXMLStreamReader(new StringReader(xml)); XMLDocumentStreamReader r2 = new XMLDocumentStreamReader(r1); - XMLStreamReader2String t1 = new XMLStreamReader2String(); + XMLStreamReader2String t1 = new XMLStreamReader2String(extensionPointRegistry); String result = t1.transform(r2, null); System.out.println(result); - XMLStreamReader r3 = factory.createXMLStreamReader(new StringReader(xml)); + XMLStreamReader r3 = helper.createXMLStreamReader(new StringReader(xml)); r3.nextTag(); r3.nextTag(); Assert.assertEquals(XMLStreamConstants.START_ELEMENT, r3.getEventType()); diff --git a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/BeanXMLStreamReaderTestCase.java b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/BeanXMLStreamReaderTestCase.java index aa785a5e48..95f21ff4c9 100644 --- a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/BeanXMLStreamReaderTestCase.java +++ b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/BeanXMLStreamReaderTestCase.java @@ -27,6 +27,7 @@ import java.util.Map; import javax.xml.stream.XMLStreamReader; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; import org.custommonkey.xmlunit.Diff; import org.junit.Test; @@ -68,7 +69,7 @@ public class BeanXMLStreamReaderTestCase { bean.map.put("key1", "value1"); bean.map.put("key2", "value2"); XMLStreamReader reader = new BeanXMLStreamReaderImpl(null, bean); - XMLStreamReader2String t3 = new XMLStreamReader2String(); + XMLStreamReader2String t3 = new XMLStreamReader2String(new DefaultExtensionPointRegistry()); String xml = t3.transform(reader, null); Diff diff = new Diff(XML_RESULT, xml); Diff diff1 = new Diff(XML_RESULT1, xml); diff --git a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/DOM2StAXTestCase.java b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/DOM2StAXTestCase.java index 7665eaa8ec..4da91bd783 100644 --- a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/DOM2StAXTestCase.java +++ b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/DOM2StAXTestCase.java @@ -22,7 +22,12 @@ import static org.junit.Assert.assertTrue; import javax.xml.stream.XMLStreamReader; +import org.apache.tuscany.sca.common.xml.stax.reader.DOMXmlNodeImpl; +import org.apache.tuscany.sca.common.xml.stax.reader.XmlTreeStreamReaderImpl; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.custommonkey.xmlunit.XMLAssert; +import org.junit.BeforeClass; import org.junit.Test; import org.w3c.dom.Node; @@ -64,13 +69,20 @@ public class DOM2StAXTestCase { private static final String CRAZY_XML = "" + "E4"; + private static ExtensionPointRegistry registry; + + @BeforeClass + public static void init() { + registry = new DefaultExtensionPointRegistry(); + } + @Test public void testTransformation() throws Exception { String2Node t1 = new String2Node(); Node node = t1.transform(IPO_XML, null); Node2XMLStreamReader t2 = new Node2XMLStreamReader(); XMLStreamReader reader = t2.transform(node, null); - XMLStreamReader2String t3 = new XMLStreamReader2String(); + XMLStreamReader2String t3 = new XMLStreamReader2String(registry); String xml = t3.transform(reader, null); XMLAssert.assertXMLEqual(IPO_XML, xml); // assertTrue(xml != null && xml.indexOf("1999-12-05") != -1); @@ -82,7 +94,7 @@ public class DOM2StAXTestCase { Node node = t1.transform(CRAZY_XML, null); Node2XMLStreamReader t2 = new Node2XMLStreamReader(); XMLStreamReader reader = t2.transform(node, null); - XMLStreamReader2String t3 = new XMLStreamReader2String(); + XMLStreamReader2String t3 = new XMLStreamReader2String(registry); String xml = t3.transform(reader, null); // System.out.println(xml); XMLAssert.assertXMLEqual(CRAZY_XML, xml); @@ -95,7 +107,7 @@ public class DOM2StAXTestCase { Node node = t1.transform(IPO_XML, null); DOMXmlNodeImpl element = new DOMXmlNodeImpl(node); XmlTreeStreamReaderImpl reader = new XmlTreeStreamReaderImpl(element); - XMLStreamReader2String t3 = new XMLStreamReader2String(); + XMLStreamReader2String t3 = new XMLStreamReader2String(registry); String xml = t3.transform(reader, null); XMLAssert.assertXMLEqual(IPO_XML, xml); // assertTrue(xml != null && xml.indexOf("1999-12-05") != -1); diff --git a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/JavaBean2XMLStreamReaderTestCase.java b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/JavaBean2XMLStreamReaderTestCase.java index 66cb12c19b..8919453731 100644 --- a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/JavaBean2XMLStreamReaderTestCase.java +++ b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/JavaBean2XMLStreamReaderTestCase.java @@ -20,6 +20,7 @@ package org.apache.tuscany.sca.databinding.xml; import javax.xml.stream.XMLStreamReader; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; import org.apache.tuscany.sca.databinding.javabeans.JavaBean2XMLStreamReaderTransformer; import org.custommonkey.xmlunit.XMLAssert; import org.junit.Test; @@ -45,7 +46,7 @@ public class JavaBean2XMLStreamReaderTestCase { bean.bean = new AnotherBean(); bean.bean.setName("Name"); XMLStreamReader reader = t2.transform(bean, null); - XMLStreamReader2String t3 = new XMLStreamReader2String(); + XMLStreamReader2String t3 = new XMLStreamReader2String(new DefaultExtensionPointRegistry()); String xml = t3.transform(reader, null); XMLAssert.assertXMLEqual(XML_RESULT, xml); diff --git a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/PushTransformationTestCase.java b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/PushTransformationTestCase.java index ad1bebfc8a..a8657a5be8 100644 --- a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/PushTransformationTestCase.java +++ b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/PushTransformationTestCase.java @@ -20,6 +20,8 @@ package org.apache.tuscany.sca.databinding.xml; import javax.xml.stream.XMLStreamReader; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.impl.PipedTransformer; import org.junit.Assert; import org.junit.Test; @@ -63,11 +65,12 @@ public class PushTransformationTestCase { @Test public void testTransformation() { - String2XMLStreamReader t1 = new String2XMLStreamReader(); + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + String2XMLStreamReader t1 = new String2XMLStreamReader(registry); XMLStreamReader reader = t1.transform(IPO_XML, null); - XMLStreamReader2SAX t2 = new XMLStreamReader2SAX(); + XMLStreamReader2SAX t2 = new XMLStreamReader2SAX(registry); PipedTransformer t3 = - new PipedTransformer(t2, new SAX2DOMPipe()); + new PipedTransformer(t2, new SAX2DOMPipe(registry)); Node node = t3.transform(reader, null); Assert.assertNotNull(node); Node2String t4 = new Node2String(); diff --git a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/StAXHelperTestCase.java b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/StAXHelperTestCase.java deleted file mode 100644 index 264eb1a228..0000000000 --- a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/StAXHelperTestCase.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import static org.junit.Assert.assertNotNull; - -import javax.xml.stream.XMLStreamReader; - -import org.custommonkey.xmlunit.XMLAssert; -import org.junit.Test; - -/** - * Test Case for StAXHelper - * - * @version $Rev$ $Date$ - */ -public class StAXHelperTestCase { - private static final String XML = - "" - + "" - + ""; - - @Test - public void testHelper() throws Exception { - XMLStreamReader reader = StAXHelper.createXMLStreamReader(XML); - String xml = StAXHelper.save(reader); - XMLAssert.assertXMLEqual(XML, xml); - reader = StAXHelper.createXMLStreamReader(xml); - assertNotNull(reader); - } - -} diff --git a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/XmlTreeStreamReaderTestCase.java b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/XmlTreeStreamReaderTestCase.java deleted file mode 100644 index 45ee53314e..0000000000 --- a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/XmlTreeStreamReaderTestCase.java +++ /dev/null @@ -1,198 +0,0 @@ -/* - * 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.tuscany.sca.databinding.xml; - -import java.io.StringReader; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import javax.xml.namespace.QName; -import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLStreamReader; - -import org.custommonkey.xmlunit.XMLAssert; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -/** - * @version $Rev$ $Date$ - */ -public class XmlTreeStreamReaderTestCase { - private static final String IPO_XML = - "" + "" - + " " - + " Helen Zoe" - + " 47 Eden Street" - + " Cambridge" - + " CB1 1JR" - + " " - + " " - + " Robert Smith" - + " 8 Oak Avenue" - + " Old Town" - + " PA" - + " 95819" - + " " - + " " - + " " - + " Lapis necklace" - + " 1" - + " 99.95" - + " Want this for the holidays" - + " 1999-12-05" - + " " - + " " - + ""; - - private static final String XML_RESULT = - "" + "" - + "MyText" - + "" - + " " - + " " - + "Helen Zoe 47 Eden Street " - + "Cambridge CB1 1JR " - + " Robert Smith " - + "8 Oak Avenue Old Town PA " - + "95819 " - + "Lapis necklace 1 " - + "99.95 Want this for the holidays " - + "1999-12-05 "; - private XmlNodeImpl root; - - @Before - public void setUp() throws Exception { - root = new XmlNodeImpl(); - root.name = new QName("http://ns", "e1", "p1"); - - XmlNodeImpl e11 = new XmlNodeImpl(); - e11.name = new QName("http://ns1", "e11", "p2"); - - XmlNodeImpl e12 = new XmlNodeImpl(); - e12.name = new QName("http://ns", "e12"); - - root.children.add(e11); - root.children.add(e12); - - XmlNodeImpl e121 = new XmlNodeImpl(); - e121.name = new QName("http://ns", "e121"); - e12.children.add(e121); - - XmlNodeImpl e111 = new XmlNodeImpl(); - e111.value = "MyText"; - e11.children.add(e111); - - XmlNodeImpl e13 = new XmlNodeImpl(); - e13.value = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(IPO_XML)); - root.children.add(e13); - - } - - @Test - public void testIterator() { - List elements = new ArrayList(); - XmlNodeIterator i = new XmlNodeIterator(root); - for (; i.hasNext();) { - XmlNode e = i.next(); - elements.add(e.getName()); - } - // System.out.println(elements); - QName[] names = - {new QName("http://ns", "e1"), new QName("http://ns1", "e11"), null, null, new QName("http://ns1", "e11"), - new QName("http://ns", "e12"), new QName("http://ns", "e121"), new QName("http://ns", "e121"), - new QName("http://ns", "e12"), null, null, new QName("http://ns", "e1")}; - Assert.assertEquals(Arrays.asList(names), elements); - } - - @Test - public void testReader() throws Exception { - XmlTreeStreamReaderImpl reader = new XmlTreeStreamReaderImpl(root); - XMLStreamReader2String t = new XMLStreamReader2String(); - String xml = t.transform(reader, null); - XMLAssert.assertXMLEqual(XML_RESULT, xml); - } - - private static class XmlNodeImpl implements XmlNode { - private List children = new ArrayList(); - private List attrs = new ArrayList(); - private Map namespaces = new HashMap(); - private QName name; - private Object value = "123"; - - /** - * @see org.apache.tuscany.sca.databinding.xml.XmlNode#attributes() - */ - public List attributes() { - return attrs; - } - - /** - * @see org.apache.tuscany.sca.databinding.xml.XmlNode#children() - */ - public Iterator children() { - return children.iterator(); - } - - /** - * @see org.apache.tuscany.sca.databinding.xml.XmlNode#getName() - */ - public QName getName() { - return name; - } - - /** - * @see org.apache.tuscany.sca.databinding.xml.XmlNode#getValue() - */ - public T getValue() { - return (T)value; - } - - /** - * @see org.apache.tuscany.sca.databinding.xml.XmlNode#namespaces() - */ - public Map namespaces() { - return namespaces; - } - - @Override - public String toString() { - return String.valueOf(name); - } - - public Type getType() { - if (value instanceof XMLStreamReader) { - return Type.READER; - } - return name == null ? Type.CHARACTERS : Type.ELEMENT; - } - - } -} -- cgit v1.2.3