You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2007/03/17 06:56:54 UTC
svn commit: r519244 [2/5] - in
/incubator/tuscany/java/sca/services/databinding/databinding-framework: ./
src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/
src/main/java/org/apache/tuscany/ src/main/java/org/apache/tuscany/data...
Added: incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/BeanUtil.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/BeanUtil.java?view=auto&rev=519244
==============================================================================
--- incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/BeanUtil.java (added)
+++ incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/BeanUtil.java Fri Mar 16 22:56:48 2007
@@ -0,0 +1,196 @@
+/*
+ * 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.databinding.xml;
+
+import java.beans.BeanInfo;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+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.XMLStreamReader;
+
+import org.apache.tuscany.spi.databinding.extension.SimpleTypeMapperExtension;
+import org.apache.tuscany.spi.model.TypeInfo;
+
+public final class BeanUtil {
+ private static final Object[] NULL = (Object[])null;
+ private static int nsCount = 1;
+
+ private static final SimpleTypeMapperExtension MAPPER = new SimpleTypeMapperExtension();
+
+ private BeanUtil() {
+ }
+
+ private static boolean isSimpleType(Class javaType) {
+ return MAPPER.getXMLType(javaType) != null;
+ }
+
+ private static String getStringValue(Object o) {
+ if (o == null) {
+ return null;
+ }
+ TypeInfo info = MAPPER.getXMLType(o.getClass());
+ if (info != null) {
+ return MAPPER.toXMLLiteral(info.getQName(), o, null);
+ } else {
+ return String.valueOf(o);
+ }
+ }
+
+ /**
+ * To Serilize Bean object this method is used, this will create an object
+ * array using given bean object
+ *
+ * @param beanObject
+ * @param beanName
+ */
+ public static XMLStreamReader getXMLStreamReader(Object beanObject, QName beanName) {
+ try {
+ ClassLoader cl = beanObject.getClass().getClassLoader();
+ if (cl == null) {
+ cl = ClassLoader.getSystemClassLoader();
+ }
+ String beanNS = beanName.getNamespaceURI();
+ String beanPrefix = beanName.getPrefix();
+ BeanInfo beanInfo = Introspector.getBeanInfo(beanObject.getClass());
+ PropertyDescriptor[] propDescs = beanInfo.getPropertyDescriptors();
+ Map<String, PropertyDescriptor> propertMap = new HashMap<String, PropertyDescriptor>();
+ for (int i = 0; i < propDescs.length; i++) {
+ PropertyDescriptor propDesc = propDescs[i];
+ propertMap.put(propDesc.getName(), propDesc);
+ }
+ List<String> properties = new ArrayList<String>(propertMap.keySet());
+ Collections.sort(properties);
+ List<NamedProperty> props = new ArrayList<NamedProperty>();
+ for (int i = 0; i < properties.size(); i++) {
+ String property = properties.get(i);
+ PropertyDescriptor propDesc = (PropertyDescriptor)propertMap.get(property);
+ if (propDesc == null) {
+ // JAM does bad thing so I need to add this
+ continue;
+ }
+ Class ptype = propDesc.getPropertyType();
+ if ("class".equals(property)) {
+ continue;
+ }
+ if (isSimpleType(ptype)) {
+ Object value = propDesc.getReadMethod().invoke(beanObject, NULL);
+ NamedProperty prop =
+ new NamedProperty(new QName(beanNS, property, beanPrefix), getStringValue(value));
+ props.add(prop);
+ } else if (ptype.isArray()) {
+ if (isSimpleType(ptype.getComponentType())) {
+ Object value = propDesc.getReadMethod().invoke(beanObject, NULL);
+ if (value != null) {
+ int i1 = Array.getLength(value);
+ for (int j = 0; j < i1; j++) {
+ Object o = Array.get(value, j);
+ NamedProperty prop =
+ new NamedProperty(new QName(beanNS, property, beanPrefix), getStringValue(o));
+ props.add(prop);
+ }
+ } else {
+ NamedProperty prop = new NamedProperty(new QName(beanNS, property, beanPrefix), value);
+ props.add(prop);
+ }
+
+ } else {
+ Object value[] = (Object[])propDesc.getReadMethod().invoke(beanObject, NULL);
+ if (value != null) {
+ for (int j = 0; j < value.length; j++) {
+ Object o = value[j];
+ NamedProperty prop =
+ new NamedProperty(new QName(beanNS, property, beanPrefix), getStringValue(o));
+ props.add(prop);
+ }
+ } else {
+ NamedProperty prop = new NamedProperty(new QName(beanNS, property, beanPrefix), value);
+ props.add(prop);
+ }
+ }
+ } else if (Collection.class.isAssignableFrom(ptype)) {
+ Object value = propDesc.getReadMethod().invoke(beanObject, NULL);
+ Collection objList = (Collection)value;
+ if (objList != null && objList.size() > 0) {
+ // this was given error , when the array.size = 0
+ // and if the array contain simple type , then the
+ // ADBPullParser asked
+ // PullParser from That simpel type
+ for (Iterator j = objList.iterator(); j.hasNext();) {
+ Object o = j.next();
+ if (isSimpleType(o.getClass())) {
+ NamedProperty prop =
+ new NamedProperty(new QName(beanNS, property, beanPrefix), getStringValue(o));
+ props.add(prop);
+ } else {
+ NamedProperty prop = new NamedProperty(new QName(beanNS, property, beanPrefix), o);
+ props.add(prop);
+ }
+ }
+
+ } else {
+ NamedProperty prop = new NamedProperty(new QName(beanNS, property, beanPrefix), value);
+ props.add(prop);
+ }
+ } else {
+ Object value = propDesc.getReadMethod().invoke(beanObject, NULL);
+ NamedProperty prop = new NamedProperty(new QName(beanNS, property, beanPrefix), value);
+ props.add(prop);
+ }
+ }
+ NamedProperty[] elements = new NamedProperty[props.size()];
+ props.toArray(elements);
+ return new XMLFragmentStreamReaderImpl(beanName, elements, null);
+ } catch (Exception e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ /**
+ * to get the pull parser for a given bean object , generate the wrpper
+ * element using class name
+ *
+ * @param beanObject
+ */
+ public static XMLStreamReader getXMLStreamReader(Object beanObject) {
+ String className = beanObject.getClass().getName();
+ if (className.indexOf(".") > 0) {
+ className = className.substring(className.lastIndexOf('.') + 1, className.length());
+ }
+ return getXMLStreamReader(beanObject, new QName(className));
+ }
+
+ /**
+ * increments the namespace counter and returns a new prefix
+ *
+ * @return unique prefix
+ */
+ public static String getUniquePrefix() {
+ return "s" + nsCount++;
+ }
+
+}
Propchange: incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/BeanUtil.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/BeanUtil.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/DOMDataBinding.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/DOMDataBinding.java?view=diff&rev=519244&r1=519083&r2=519244
==============================================================================
--- incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/DOMDataBinding.java (original)
+++ incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/DOMDataBinding.java Fri Mar 16 22:56:48 2007
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.tuscany.core.databinding.xml;
+package org.apache.tuscany.databinding.xml;
import org.w3c.dom.Node;
@@ -31,9 +31,10 @@
*/
public class DOMDataBinding extends DataBindingExtension {
public static final String NAME = Node.class.getName();
+ public static final String[] ALIASES = new String[] {"dom"};
public DOMDataBinding() {
- super(Node.class);
+ super(NAME, ALIASES, Node.class);
}
@Override
@@ -44,13 +45,8 @@
public Object copy(Object source) {
if (Node.class.isAssignableFrom(source.getClass())) {
Node nodeSource = (Node) source;
- Node2String strTransformer = new Node2String();
- String stringCopy = strTransformer.transform(nodeSource, null);
-
- String2Node nodeTransformer = new String2Node();
- return nodeTransformer.transform(stringCopy, null);
+ return nodeSource.cloneNode(true);
}
-
return super.copy(source);
}
}
Modified: incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/DOMWrapperHandler.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/DOMWrapperHandler.java?view=diff&rev=519244&r1=519083&r2=519244
==============================================================================
--- incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/DOMWrapperHandler.java (original)
+++ incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/DOMWrapperHandler.java Fri Mar 16 22:56:48 2007
@@ -17,7 +17,10 @@
* under the License.
*/
-package org.apache.tuscany.core.databinding.xml;
+package org.apache.tuscany.databinding.xml;
+
+import java.util.ArrayList;
+import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.parsers.ParserConfigurationException;
@@ -27,7 +30,6 @@
import org.apache.tuscany.spi.databinding.WrapperHandler;
import org.apache.tuscany.spi.databinding.extension.DOMHelper;
import org.apache.tuscany.spi.model.ElementInfo;
-
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@@ -50,31 +52,27 @@
return DOMHelper.createElement(document, name);
}
- public Object getChild(Node wrapper, int i, ElementInfo element) {
- int index = 0;
- NodeList nodes = wrapper.getChildNodes();
- for (int j = 0; j < nodes.getLength(); j++) {
- Node node = nodes.item(j);
- if (node.getNodeType() != Node.ELEMENT_NODE) {
- continue;
- }
- if (index != i) {
- index++;
- } else {
- QName name = DOMHelper.getQName(node);
- if (name.equals(element.getQName())) {
- return node;
- }
- }
- }
- return null;
- }
-
public void setChild(Node wrapper, int i, ElementInfo childElement, Object value) {
Node node = (Node) value;
if (node.getNodeType() == Node.DOCUMENT_NODE) {
node = ((Document) node).getDocumentElement();
}
wrapper.appendChild(wrapper.getOwnerDocument().importNode(node, true));
+ }
+
+ public List getChildren(Node wrapper) {
+ assert wrapper != null;
+ if (wrapper.getNodeType() == Node.DOCUMENT_NODE) {
+ wrapper = ((Document) wrapper).getDocumentElement();
+ }
+ List<Node> elements = new ArrayList<Node>();
+ NodeList nodes = wrapper.getChildNodes();
+ for (int j = 0; j < nodes.getLength(); j++) {
+ Node node = nodes.item(j);
+ if (node.getNodeType() == Node.ELEMENT_NODE) {
+ elements.add(node);
+ }
+ }
+ return elements;
}
}
Modified: incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/DOMXMLStreamReader.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/DOMXMLStreamReader.java?view=diff&rev=519244&r1=519083&r2=519244
==============================================================================
--- incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/DOMXMLStreamReader.java (original)
+++ incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/DOMXMLStreamReader.java Fri Mar 16 22:56:48 2007
@@ -16,19 +16,15 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.tuscany.core.databinding.xml;
+package org.apache.tuscany.databinding.xml;
+
+import static javax.xml.XMLConstants.DEFAULT_NS_PREFIX;
+import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
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.XMLConstants;
-import javax.xml.namespace.NamespaceContext;
+
import javax.xml.namespace.QName;
-import javax.xml.stream.Location;
-import javax.xml.stream.XMLStreamException;
import org.w3c.dom.Attr;
import org.w3c.dom.CharacterData;
@@ -38,1378 +34,96 @@
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
-import org.apache.tuscany.core.databinding.xml.StAXHelper.XMLFragmentStreamReader;
-
-public class DOMXMLStreamReader implements XMLFragmentStreamReader {
- protected static class DelegatingNamespaceContext implements NamespaceContext {
- private int counter;
-
- private NamespaceContext parent;
-
- private Map<String, String> prefixToNamespaceMapping = new HashMap<String, String>();
-
- public DelegatingNamespaceContext(NamespaceContext parent) {
- super();
- this.parent = parent;
-
- prefixToNamespaceMapping.put("xml", "http://www.w3.org/XML/1998/namespace");
- prefixToNamespaceMapping.put("xmlns", "http://www.w3.org/2000/xmlns/");
- prefixToNamespaceMapping.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");
- }
-
- public synchronized QName createQName(String nsURI, String name) {
- String prefix = nsURI != null ? (String) getPrefix(nsURI) : null;
- if (prefix == null && nsURI != null && !nsURI.equals("")) {
- prefix = "p" + (counter++);
- }
- if (prefix == null) {
- prefix = "";
- }
- if (nsURI != null) {
- prefixToNamespaceMapping.put(prefix, nsURI);
- }
- return new QName(nsURI, name, prefix);
- }
-
- public String getNamespaceURI(String prefix) {
- if (prefix == null) {
- throw new IllegalArgumentException("Prefix is null");
- }
-
- String ns = (String) prefixToNamespaceMapping.get(prefix);
- if (ns != null) {
- return ns;
- } else if (parent != null) {
- return parent.getNamespaceURI(prefix);
- } else {
- return null;
- }
- }
-
- public String getPrefix(String nsURI) {
- if (nsURI == null) {
- throw new IllegalArgumentException("Namespace is null");
- }
- for (Map.Entry<String, String> entry1 : prefixToNamespaceMapping.entrySet()) {
- Map.Entry entry = entry1;
- if (entry.getValue().equals(nsURI)) {
- return (String) entry.getKey();
- }
- }
- if (parent != null) {
- return parent.getPrefix(nsURI);
- } else {
- return null;
- }
- }
-
- public Iterator getPrefixes(String nsURI) {
- List<String> prefixList = new ArrayList<String>();
- for (Map.Entry<String, String> entry : prefixToNamespaceMapping.entrySet()) {
- if (entry.getValue().equals(nsURI)) {
- prefixList.add(entry.getKey());
- }
- }
- if (parent != null) {
- for (Iterator i = parent.getPrefixes(nsURI); i.hasNext();) {
- prefixList.add((String) i.next());
- }
- }
- return prefixList.iterator();
- }
-
- public void registerMapping(String prefix, String nsURI) {
- prefixToNamespaceMapping.put(prefix, nsURI);
- }
-
- public void removeMapping(String prefix) {
- prefixToNamespaceMapping.remove(prefix);
- }
-
- public void setParent(NamespaceContext parent) {
- this.parent = parent;
- }
- }
-
- protected static class NameValuePair implements Map.Entry {
- private Object key;
-
- private Object value;
-
- public NameValuePair(Object key, Object value) {
- this.key = key;
- this.value = value;
- }
-
- public Object getKey() {
- return key;
- }
-
- public Object getValue() {
- return value;
- }
-
- public Object setValue(Object value) {
- Object v = this.value;
- this.value = value;
- return v;
- }
-
- }
-
- protected static class SimpleElementStreamReader implements XMLFragmentStreamReader {
-
- private static final int END_ELEMENT_STATE = 2;
-
- private static final int START_ELEMENT_STATE = 0;
-
- private static final int START_ELEMENT_STATE_WITH_NULL = 3;
-
- private static final int TEXT_STATE = 1;
-
- private static final QName XSI_NIL_QNAME =
- new QName("http://www.w3.org/2001/XMLSchema-instance", "nil", "xsi");
-
- private QName name;
-
- private DelegatingNamespaceContext namespaceContext = new DelegatingNamespaceContext(null);
-
- private int state = START_ELEMENT_STATE;
-
- private String value;
-
- public SimpleElementStreamReader(QName name, String value) {
- this.name = name;
- this.value = value;
- if (value == null) {
- state = START_ELEMENT_STATE_WITH_NULL;
- }
- }
-
- public void close() throws XMLStreamException {
- // Do nothing - we've nothing to free here
- }
-
- public int getAttributeCount() {
- if (state == START_ELEMENT_STATE_WITH_NULL) {
- return 1;
- }
- if (state == START_ELEMENT_STATE) {
- return 0;
- } else {
- throw new IllegalStateException();
- }
-
- }
-
- public String getAttributeLocalName(int i) {
- if (state == START_ELEMENT_STATE_WITH_NULL && i == 0) {
- return XSI_NIL_QNAME.getLocalPart();
- }
- if (state == START_ELEMENT_STATE) {
- return null;
- } else {
- throw new IllegalStateException();
- }
- }
-
- public QName getAttributeName(int i) {
- if (state == START_ELEMENT_STATE_WITH_NULL && i == 0) {
- return XSI_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 XSI_NIL_QNAME.getNamespaceURI();
- }
- 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 XSI_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 "true";
- }
- if (state == START_ELEMENT_STATE) {
- return null;
- } else {
- throw new IllegalStateException();
- }
- }
-
- public String getAttributeValue(String string, String string1) {
- if (state == TEXT_STATE) {
- // todo something
- return null;
- } else {
- return null;
- }
-
- }
-
- public String getCharacterEncodingScheme() {
- return null;
- }
-
- 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 String getEncoding() {
- return "UTF-8";
- }
-
- public int getEventType() {
- switch (state) {
- case START_ELEMENT_STATE:
- case START_ELEMENT_STATE_WITH_NULL:
- 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 getLocalName() {
- if (state != TEXT_STATE) {
- return name.getLocalPart();
- } else {
- return null;
- }
- }
-
- 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() {
- if (state != TEXT_STATE) {
- return name;
- } else {
- return null;
- }
- }
-
- public NamespaceContext getNamespaceContext() {
- return this.namespaceContext;
- }
-
- 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 XSI_NIL_QNAME.getPrefix();
- } else {
- return null;
- }
- }
-
- public String getNamespaceURI() {
- if (state != TEXT_STATE) {
- return name.getNamespaceURI();
- } else {
- return null;
- }
-
- }
-
- public String getNamespaceURI(int i) {
- if (state == START_ELEMENT_STATE_WITH_NULL && isXsiNamespacePresent() && i == 0) {
- return XSI_NIL_QNAME.getNamespaceURI();
- } else {
- return null;
- }
- }
-
- public String getNamespaceURI(String prefix) {
- return namespaceContext.getNamespaceURI(prefix);
- }
-
- public String getPIData() {
- return null;
- }
-
- public String getPITarget() {
- return null;
- }
-
- public String getPrefix() {
- if (state != TEXT_STATE) {
- return name.getPrefix();
- } else {
- return null;
- }
- }
-
- public Object getProperty(String key) throws IllegalArgumentException {
- return null;
- }
-
- 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 getTextLength() {
- if (state == TEXT_STATE) {
- return value.length();
- } else {
- throw new IllegalStateException();
- }
-
- }
-
- public int getTextStart() {
- if (state == TEXT_STATE) {
- return 0;
- } else {
- throw new IllegalStateException();
- }
- }
-
- public String getVersion() {
- return null; // todo 1.0 ?
- }
-
- public boolean hasName() {
- return state != TEXT_STATE;
-
- }
-
- public boolean hasNext() throws XMLStreamException {
- return state != END_ELEMENT_STATE;
- }
-
- public boolean hasText() {
- return state == TEXT_STATE;
- }
-
- public void init() {
- // just add the current elements namespace and prefix to the this
- // elements nscontext
- registerNamespace(name.getPrefix(), name.getNamespaceURI());
-
- }
-
- public boolean isAttributeSpecified(int i) {
- return false; // no attribs here
- }
-
- public boolean isCharacters() {
- return state == TEXT_STATE;
- }
-
- public boolean isEndElement() {
- return state == END_ELEMENT_STATE;
- }
-
- public boolean isEndOfFragment() {
- return state == END_ELEMENT_STATE;
- }
-
- public boolean isStandalone() {
- return false;
- }
-
- public boolean isStartElement() {
- return state == START_ELEMENT_STATE || state == START_ELEMENT_STATE_WITH_NULL;
- }
-
- public boolean isWhiteSpace() {
- return false; // no whitespaces here
- }
-
- /**
- * Test whether the xsi namespace is present
- *
- * @return
- */
- private boolean isXsiNamespacePresent() {
- return namespaceContext.getNamespaceURI(XSI_NIL_QNAME.getPrefix()) != null;
- }
-
- public int next() throws XMLStreamException {
- switch (state) {
- case START_ELEMENT_STATE:
- state = TEXT_STATE;
- return CHARACTERS;
- case START_ELEMENT_STATE_WITH_NULL:
- state = END_ELEMENT_STATE;
- return END_ELEMENT;
- 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 int nextTag() throws XMLStreamException {
- return 0; // todo
- }
-
- /**
- * @param prefix
- * @param uri
- */
- private void registerNamespace(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.registerMapping(prefix, uri);
- }
- }
-
- public void require(int i, String string, String string1) throws XMLStreamException {
- // not implemented
- }
-
- public void setParentNamespaceContext(NamespaceContext nsContext) {
- this.namespaceContext.setParent(nsContext);
- }
-
- public boolean standaloneSet() {
- return false;
- }
-
- }
-
- private static final int DELEGATED_STATE = 2;
-
- private static final int END_ELEMENT_STATE = 1;
-
- // states for this pullparser - it can only have three states
- private static final int START_ELEMENT_STATE = 0;
-
- private static final int TEXT_STATE = 3;
-
- private Map.Entry[] attributes;
-
- // reference to the child reader
- private XMLFragmentStreamReader childReader;
-
- // current property index
- private int currentPropertyIndex;
-
- private Map<String, String> declaredNamespaceMap = new HashMap<String, String>();
-
- private QName elementQName;
-
- // we always create a new namespace context
- private DelegatingNamespaceContext namespaceContext = new DelegatingNamespaceContext(null);
-
- private Map.Entry[] properties;
-
+public class DOMXMLStreamReader extends XMLFragmentStreamReaderImpl {
private Element rootElement;
- private String rootElementName;
-
- private String rootElementURI;
-
- // integer field that keeps the state of this
- // parser.
- private int state = START_ELEMENT_STATE;
-
public DOMXMLStreamReader(Node node) {
+ super(null);
switch (node.getNodeType()) {
case Node.DOCUMENT_NODE:
- this.rootElement = ((Document) node).getDocumentElement();
+ this.rootElement = ((Document)node).getDocumentElement();
break;
case Node.ELEMENT_NODE:
- this.rootElement = (Element) node;
+ this.rootElement = (Element)node;
break;
default:
throw new IllegalArgumentException("Illegal Node");
}
- this.rootElementName = rootElement.getLocalName();
- this.rootElementURI = rootElement.getNamespaceURI();
-
- declaredNamespaceMap.put("xml", "http://www.w3.org/XML/1998/namespace");
- declaredNamespaceMap.put("xmlns", "http://www.w3.org/2000/xmlns/");
- declaredNamespaceMap.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");
-
- populateProperties();
- }
-
- /*
- * 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!
- */
- protected DOMXMLStreamReader(QName elementQName, Map.Entry[] properties, Map.Entry[] attributes) {
- // validate the lengths, since both the arrays are supposed
- // to have
- this.properties = properties;
- this.elementQName = elementQName;
- this.attributes = attributes;
-
- }
-
- public void close() throws XMLStreamException {
- // do nothing here - we have no resources to free
- }
-
- public int getAttributeCount() {
- return (state == DELEGATED_STATE) ? childReader.getAttributeCount()
- : ((attributes != null) && (state == START_ELEMENT_STATE) ? attributes.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
- * @return
- */
- public QName getAttributeName(int i) {
- if (state == DELEGATED_STATE) {
- return childReader.getAttributeName(i);
- } else if (state == START_ELEMENT_STATE) {
- if (attributes == null) {
- return null;
- } else {
- if ((i >= (attributes.length)) || i < 0) { // out of range
- return null;
- } else {
- // get the attribute pointer
- Object attribPointer = attributes[i].getKey();
- // case one - attrib name is null
- // this should be the pointer to the OMAttribute then
- if (attribPointer instanceof String) {
- return new QName((String) attribPointer);
- } else if (attribPointer instanceof QName) {
- return (QName) 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 (attributes == null) {
- return null;
- } else {
- if ((i >= (attributes.length)) || i < 0) { // out of range
- return null;
- } else {
- // get the attribute pointer
- Object attribPointer = attributes[i].getKey();
- Object omAttribObj = attributes[i].getValue();
- // case one - attrib name is null
- // this should be the pointer to the OMAttribute then
- if (attribPointer instanceof String) {
- return (String) omAttribObj;
- } 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
- *
- * @return
- * @throws XMLStreamException
- */
- public String getElementText() throws XMLStreamException {
- if (state == DELEGATED_STATE) {
- return childReader.getElementText();
- } else {
- return null;
- }
-
- }
-
- 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;
- }
- }
-
- // /////////////////////////////////////////////////////////////////////////
- // / attribute handling
- // /////////////////////////////////////////////////////////////////////////
-
- public int getEventType() {
- if (state == START_ELEMENT_STATE) {
- return START_ELEMENT;
- } else if (state == END_ELEMENT_STATE) {
- return END_ELEMENT;
- } 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();
- }
- }
-
- /**
- * @return
- */
- 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
- * @return
- */
- 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();
- }
- }
-
- 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();
- }
-
- }
-
- // /////////////////////////////////////////////////////////////////////////
- // //////////// end of attribute handling
- // /////////////////////////////////////////////////////////////////////////
-
- // //////////////////////////////////////////////////////////////////////////
- // //////////// namespace handling
- // //////////////////////////////////////////////////////////////////////////
-
- 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 {
- return elementQName.getPrefix();
- }
- }
-
- /**
- * @param key
- * @return
- * @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;
- }
-
- }
-
- // /////////////////////////////////////////////////////////////////////////
- // /////// end of namespace handling
- // /////////////////////////////////////////////////////////////////////////
-
- public String getText() {
- if (state == DELEGATED_STATE) {
- return childReader.getText();
- } else if (state == TEXT_STATE) {
- return (String) properties[currentPropertyIndex - 1].getValue();
- } else {
- throw new IllegalStateException();
- }
- }
-
- public char[] getTextCharacters() {
- if (state == DELEGATED_STATE) {
- return childReader.getTextCharacters();
- } else if (state == TEXT_STATE) {
- return properties[currentPropertyIndex - 1].getValue() == null ? new char[0]
- : ((String) properties[currentPropertyIndex - 1].getValue()).toCharArray();
- } else {
- throw new IllegalStateException();
- }
- }
-
- 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) {
- // todo - implement this
- return 0;
- } else {
- throw new IllegalStateException();
- }
- }
-
- public int getTextLength() {
- if (state == DELEGATED_STATE) {
- return childReader.getTextLength();
- } else if (state == TEXT_STATE) {
- return 0; // assume text always starts at 0
- } 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;
- }
- }
-
- /**
- * @return
- * @throws XMLStreamException
- */
- public boolean hasNext() throws XMLStreamException {
- if (state == DELEGATED_STATE) {
- if (childReader.isEndOfFragment()) {
- // 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
- *
- * @return
- */
- 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 seperately 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();
- }
-
- public boolean isEndElement() {
- if (state == START_ELEMENT_STATE) {
- return false;
- } else if (state == END_ELEMENT_STATE) {
- return true;
- }
- return childReader.isEndElement();
- }
-
- /**
- * are we done ?
- *
- * @return
- */
- public boolean isEndOfFragment() {
- return state == END_ELEMENT_STATE;
- }
-
- 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 hastable and take that into an array
- *
- * @return
- */
- private String[] makePrefixArray() {
- String[] prefixes =
- (String[]) declaredNamespaceMap.keySet().toArray(new String[declaredNamespaceMap.size()]);
- Arrays.sort(prefixes);
- return prefixes;
- }
-
- public int next() throws XMLStreamException {
- return updateStatus();
- }
-
- /**
- * todo implement this
- *
- * @return
- * @throws XMLStreamException
- */
- public int nextTag() throws XMLStreamException {
- return 0;
- }
-
- // /////////////////////////////////////////////////////////////////////////
- // / Other utility methods
- // ////////////////////////////////////////////////////////////////////////
-
- /**
- * Populates a namespace context
- */
- private void populateNamespaceContext() {
-
- // first add the current element namespace to the namespace context
- // declare it if not found
- registerNamespace(elementQName.getPrefix(), elementQName.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
-
- if (attributes != null) {
- for (int i = 0; i < attributes.length; i++) { // jump in two
- Object attribName = attributes[i].getKey();
- if (attribName instanceof String) {
- // ignore this case - Nothing to do
- } else if (attribName instanceof QName) {
- QName attribQName = (QName) attribName;
- registerNamespace(attribQName.getPrefix(), attribQName.getNamespaceURI());
-
- }
- }
- }
-
- }
-
- public final void populateProperties() {
- if (properties != null) {
- return;
- }
- if (elementQName == null) {
- elementQName = namespaceContext.createQName(this.rootElementURI, this.rootElementName);
- } else {
- elementQName =
- namespaceContext.createQName(elementQName.getNamespaceURI(), elementQName.getLocalPart());
- }
-
- List<Object> elementList = new ArrayList<Object>();
- List<Object> attributeList = new ArrayList<Object>();
+ String ns = rootElement.getNamespaceURI();
+ String prefix = rootElement.getPrefix();
+ String name = rootElement.getLocalName();
+ elementQName = new QName(ns == null ? "" : ns, name, prefix == null ? "" : prefix);
+ }
+
+ @Override
+ protected final NamedProperty[] getAttributes() {
+ if (attributes == null) {
+ List<NamedProperty> attributeList = new ArrayList<NamedProperty>();
+ NamedNodeMap nodeMap = rootElement.getAttributes();
+ for (int i = 0; i < nodeMap.getLength(); i++) {
+ Attr attr = (Attr)nodeMap.item(i);
+ String ns = attr.getNamespaceURI();
+ String prefix = attr.getPrefix();
+ if (!XMLNS_ATTRIBUTE_NS_URI.equals(ns)) {
+ QName attrName = new QName(ns == null ? "" : ns, attr.getLocalName(), prefix != null ? prefix : "");
+ NamedProperty pair = new NamedProperty(attrName, attr.getValue());
+ attributeList.add(pair);
+ }
+ }
+ attributes = new NamedProperty[attributeList.size()];
+ attributeList.toArray(attributes);
+ }
+ return attributes;
+ }
+
+ @Override
+ protected QName[] getNamespaces() {
+ List<QName> nsList = new ArrayList<QName>();
NamedNodeMap nodeMap = rootElement.getAttributes();
for (int i = 0; i < nodeMap.getLength(); i++) {
- Attr attr = (Attr) nodeMap.item(i);
- if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(attr.getNamespaceURI())) {
- // Skip xmlns:xxx
- if (!attr.getName().equals(attr.getLocalName())) {
- // Skip xmlns="..."
- registerNamespace(attr.getLocalName(), attr.getValue());
- } else {
- registerNamespace(XMLConstants.DEFAULT_NS_PREFIX, attr.getValue());
+ Attr attr = (Attr)nodeMap.item(i);
+ String ns = attr.getNamespaceURI();
+ if (XMLNS_ATTRIBUTE_NS_URI.equals(ns)) {
+ String prefix = attr.getPrefix();
+ if (prefix == null) {
+ // xmlns="http://ns"
+ nsList.add(new QName(attr.getValue(), "", DEFAULT_NS_PREFIX));
+ } else {
+ // xmlns:ns="http://ns"
+ nsList.add(new QName(attr.getValue(), "", attr.getLocalName()));
}
- continue;
- }
- QName attrName = new QName(attr.getNamespaceURI(), attr.getLocalName());
- NameValuePair pair = new NameValuePair(attrName, attr.getValue());
- attributeList.add(pair);
- }
- NodeList nodeList = rootElement.getChildNodes();
- for (int i = 0; i < nodeList.getLength(); i++) {
- Node node = nodeList.item(i);
- switch (node.getNodeType()) {
- case Node.TEXT_NODE:
- case Node.CDATA_SECTION_NODE:
- NameValuePair pair = new NameValuePair(ELEMENT_TEXT, ((CharacterData) node).getData());
- elementList.add(pair);
- break;
-
- case Node.ELEMENT_NODE:
- Element element = (Element) node;
- QName elementName = new QName(element.getNamespaceURI(), element.getLocalName());
- pair = new NameValuePair(elementName, new DOMXMLStreamReader(element));
- elementList.add(pair);
- break;
}
}
- properties = elementList.toArray(new Map.Entry[elementList.size()]);
- attributes = attributeList.toArray(new Map.Entry[attributeList.size()]);
- }
-
- /**
- * 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
- Object propPointer = properties[currentPropertyIndex].getKey();
- QName propertyQName = null;
- boolean textFound = false;
- if (propPointer == null) {
- throw new XMLStreamException("property key cannot be null!");
- } else if (propPointer instanceof String) {
- // propPointer being a String has a special case
- // that is it can be a the special constant ELEMENT_TEXT that
- // says this text event
- if (ELEMENT_TEXT.equals(propPointer)) {
- textFound = true;
- } else {
- propertyQName = new QName((String) propPointer);
- }
- } else if (propPointer instanceof QName) {
- propertyQName = (QName) propPointer;
- } else {
- // oops - we've no idea what kind of key this is
- throw new XMLStreamException("unidentified property key!!!" + propPointer);
- }
-
- // ok! we got the key. Now look at the value
- Object propertyValue = properties[currentPropertyIndex].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;
- currentPropertyIndex++;
- return CHARACTERS;
- } else if (propertyValue == null || propertyValue instanceof String) {
- // strings are handled by the NameValuePairStreamReader
- childReader = new SimpleElementStreamReader(propertyQName, (String) propertyValue);
- childReader.setParentNamespaceContext(this.namespaceContext);
- childReader.init();
- } else if (propertyValue instanceof DOMXMLStreamReader) {
- // ADBbean has it's own method to get a reader
- XMLFragmentStreamReader reader = (DOMXMLStreamReader) propertyValue;
- // we know for sure that this is an ADB XMLStreamreader.
- // However we need to make sure that it is compatible
- childReader = reader;
- childReader.setParentNamespaceContext(this.namespaceContext);
- childReader.init();
- } else {
- // all special possiblilities has been tried! Let's treat
- // the thing as a bean and try generating events from it
- throw new UnsupportedOperationException("Not supported");
- // childReader = new
- // WrappingXMLStreamReader(BeanUtil.getPullParser(propertyValue,
- // propertyQName));
- // we cannot register the namespace context here
- }
-
- // set the state here
- state = DELEGATED_STATE;
- // we are done with the delegation
- // increment the property index
- currentPropertyIndex++;
- return childReader.getEventType();
- }
-
- /**
- * @param prefix
- * @param uri
- */
- private void registerNamespace(String prefix, String uri) {
- if (!uri.equals(namespaceContext.getNamespaceURI(prefix))) {
- namespaceContext.registerMapping(prefix, uri);
- declaredNamespaceMap.put(prefix, uri);
- }
- }
-
- public void require(int i, String string, String string1) throws XMLStreamException {
- throw new UnsupportedOperationException();
- }
-
- /**
- * add the namespace context
- */
-
- public void setParentNamespaceContext(NamespaceContext nsContext) {
- // register the namespace context passed in to this
- this.namespaceContext.setParent(nsContext);
-
- }
-
- public boolean standaloneSet() {
- return true;
- }
-
- /**
- * By far this should be the most important method in this class this method changes the state of the parser
- * according to the change in the
- */
- private int updateStatus() 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 (properties == null || properties.length == 0) {
- // no properties - move to the end element state
- // straightaway
- 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();
-
+ QName[] nss = new QName[nsList.size()];
+ nsList.toArray(nss);
+ return nss;
+ }
+
+ @Override
+ protected NamedProperty[] getElements() {
+ if (elements == null) {
+ List<NamedProperty> elementList = new ArrayList<NamedProperty>();
+ NodeList nodeList = rootElement.getChildNodes();
+ for (int i = 0; i < nodeList.getLength(); i++) {
+ Node node = nodeList.item(i);
+ switch (node.getNodeType()) {
+ case Node.TEXT_NODE:
+ case Node.CDATA_SECTION_NODE:
+ NamedProperty pair = new NamedProperty(ELEMENT_TEXT, ((CharacterData)node).getData());
+ elementList.add(pair);
+ break;
+
+ case Node.ELEMENT_NODE:
+ Element element = (Element)node;
+ QName elementName = new QName(element.getNamespaceURI(), element.getLocalName());
+ pair = new NamedProperty(elementName, new DOMXMLStreamReader(element));
+ elementList.add(pair);
+ break;
}
- 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.isEndOfFragment()) {
- // we've reached the end!
- if (currentPropertyIndex > (properties.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 (currentPropertyIndex > (properties.length - 1)) {
- state = END_ELEMENT_STATE;
- returnEvent = END_ELEMENT;
- } else {
- returnEvent = processProperties();
- }
- break;
+ }
+ elements = new NamedProperty[elementList.size()];
+ elementList.toArray(elements);
}
- return returnEvent;
+ return elements;
}
-
}
Added: incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/DelegatingNamespaceContext.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/DelegatingNamespaceContext.java?view=auto&rev=519244
==============================================================================
--- incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/DelegatingNamespaceContext.java (added)
+++ incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/DelegatingNamespaceContext.java Fri Mar 16 22:56:48 2007
@@ -0,0 +1,310 @@
+/*
+ * 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.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<String> prefixStack = new FastStack<String>();
+
+ // 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<String> uriStack = new FastStack<String>();
+
+ /**
+ * 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 (String)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 (String)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 = (String[])uriStack.toArray(new String[uriStack.size()]);
+ List<String> tempList = new ArrayList<String>();
+ 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 <code>ArrayList</code> instead of a
+ * <code>Vector</code>, 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.
+ * <p>
+ * The removal order of an <code>ArrayStack</code> is based on insertion order: The most recently added element is
+ * removed first. The iteration order is <i>not</i> 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.
+ * <p>
+ * Unlike <code>Stack</code>, <code>ArrayStack</code> accepts null entries.
+ */
+ public static class FastStack<T> extends ArrayList<T> {
+
+ /** Ensure serialization compatibility */
+ private static final long serialVersionUID = 2130079159931574599L;
+
+ /**
+ * Constructs a new empty <code>ArrayStack</code>. The initial size is controlled by <code>ArrayList</code>
+ * and is currently 10.
+ */
+ public FastStack() {
+ super();
+ }
+
+ /**
+ * Constructs a new empty <code>ArrayStack</code> 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 <code>true</code> if this stack is currently empty.
+ * <p>
+ * This method exists for compatibility with <code>java.util.Stack</code>. New users of this class should use
+ * <code>isEmpty</code> 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
+ * <code>add</code>.
+ *
+ * @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);
+ }
+
+ }
+
+}
Propchange: incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/DelegatingNamespaceContext.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/DelegatingNamespaceContext.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/InputSource2Node.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/InputSource2Node.java?view=diff&rev=519244&r1=519083&r2=519244
==============================================================================
--- incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/InputSource2Node.java (original)
+++ incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/InputSource2Node.java Fri Mar 16 22:56:48 2007
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.tuscany.core.databinding.xml;
+package org.apache.tuscany.databinding.xml;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMResult;
Modified: incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/InputSource2SAX.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/InputSource2SAX.java?view=diff&rev=519244&r1=519083&r2=519244
==============================================================================
--- incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/InputSource2SAX.java (original)
+++ incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/InputSource2SAX.java Fri Mar 16 22:56:48 2007
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.tuscany.core.databinding.xml;
+package org.apache.tuscany.databinding.xml;
import org.apache.tuscany.spi.databinding.PushTransformer;
import org.apache.tuscany.spi.databinding.TransformationContext;
Modified: incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/InputStream2Node.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/InputStream2Node.java?view=diff&rev=519244&r1=519083&r2=519244
==============================================================================
--- incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/InputStream2Node.java (original)
+++ incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/InputStream2Node.java Fri Mar 16 22:56:48 2007
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.tuscany.core.databinding.xml;
+package org.apache.tuscany.databinding.xml;
import java.io.InputStream;
Modified: incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/InputStream2SAX.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/InputStream2SAX.java?view=diff&rev=519244&r1=519083&r2=519244
==============================================================================
--- incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/InputStream2SAX.java (original)
+++ incubator/tuscany/java/sca/services/databinding/databinding-framework/src/main/java/org/apache/tuscany/databinding/xml/InputStream2SAX.java Fri Mar 16 22:56:48 2007
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.tuscany.core.databinding.xml;
+package org.apache.tuscany.databinding.xml;
import java.io.InputStream;
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org