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 2006/10/03 01:25:20 UTC
svn commit: r452260 [2/4] - in /incubator/tuscany/java/sca/kernel/core/src:
main/java/org/apache/tuscany/core/builder/
main/java/org/apache/tuscany/core/databinding/impl/
main/java/org/apache/tuscany/core/databinding/xml/
test/java/org/apache/tuscany/c...
Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/DOMXMLStreamReader.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/DOMXMLStreamReader.java?view=diff&rev=452260&r1=452259&r2=452260
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/DOMXMLStreamReader.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/DOMXMLStreamReader.java Mon Oct 2 16:25:18 2006
@@ -38,1334 +38,1362 @@
import org.w3c.dom.NodeList;
public class DOMXMLStreamReader implements XMLFragmentStreamReader {
- private Element rootElement;
-
- private String rootElementURI;
-
- private String rootElementName;
-
- private Map.Entry[] properties;
+ protected static class DelegatingNamespaceContext implements NamespaceContext {
+ private int counter = 0;
- private Map.Entry[] attributes;
+ private NamespaceContext parent;
- private QName elementQName;
+ private Map<String, String> prefixToNamespaceMapping = new HashMap<String, String>();
- // we always create a new namespace context
- private DelegatingNamespaceContext namespaceContext = new DelegatingNamespaceContext(null);
+ public DelegatingNamespaceContext(NamespaceContext parent) {
+ super();
+ this.parent = parent;
- private Map<String, String> declaredNamespaceMap = new HashMap<String, String>();
+ 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");
+ }
- // states for this pullparser - it can only have three states
- private static final int START_ELEMENT_STATE = 0;
+ 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);
+ }
- private static final int END_ELEMENT_STATE = 1;
+ public String getNamespaceURI(String prefix) {
+ if (prefix == null) {
+ throw new IllegalArgumentException("Prefix is null");
+ }
- private static final int DELEGATED_STATE = 2;
+ String ns = (String)prefixToNamespaceMapping.get(prefix);
+ if (ns != null) {
+ return ns;
+ } else if (parent != null) {
+ return parent.getNamespaceURI(prefix);
+ } else {
+ return null;
+ }
+ }
- private static final int TEXT_STATE = 3;
+ 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;
+ }
+ }
- // integer field that keeps the state of this
- // parser.
- private int state = START_ELEMENT_STATE;
+ 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();
+ }
- // reference to the child reader
- private XMLFragmentStreamReader childReader;
+ public void registerMapping(String prefix, String nsURI) {
+ prefixToNamespaceMapping.put(prefix, nsURI);
+ }
- // current property index
- // initialized at zero
- private int currentPropertyIndex = 0;
+ public void removeMapping(String prefix) {
+ prefixToNamespaceMapping.remove(prefix);
+ }
- public DOMXMLStreamReader(Node node) {
- switch (node.getNodeType()) {
- case Node.DOCUMENT_NODE:
- this.rootElement = ((Document) node).getDocumentElement();
- break;
- case Node.ELEMENT_NODE:
- this.rootElement = (Element) node;
- break;
- default:
- throw new IllegalArgumentException("Illegal Node");
+ public void setParent(NamespaceContext parent) {
+ this.parent = parent;
}
- this.rootElementName = rootElement.getLocalName();
- this.rootElementURI = rootElement.getNamespaceURI();
- 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;
-
- }
+ protected static class NameValuePair implements Map.Entry {
+ private Object key;
- 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());
+ private Object value;
- List<Object> elementList = new ArrayList<Object>();
- List<Object> attributeList = new ArrayList<Object>();
- NamedNodeMap nodeMap = rootElement.getAttributes();
- for (int i = 0; i < nodeMap.getLength(); i++) {
- Attr attr = (Attr) nodeMap.item(i);
- QName attrName = new QName(attr.getNamespaceURI(), attr.getLocalName());
- NameValuePair pair = new NameValuePair(attrName, attr.getValue());
- attributeList.add(pair);
+ public NameValuePair(Object key, Object value) {
+ this.key = key;
+ this.value = value;
}
- 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;
- }
+ public Object getKey() {
+ return key;
}
- properties = elementList.toArray(new Map.Entry[elementList.size()]);
- attributes = attributeList.toArray(new Map.Entry[attributeList.size()]);
- }
- /**
- * add the namespace context
- */
+ public Object getValue() {
+ return value;
+ }
- public void setParentNamespaceContext(NamespaceContext nsContext) {
- // register the namespace context passed in to this
- this.namespaceContext.setParent(nsContext);
+ public Object setValue(Object value) {
+ Object v = this.value;
+ this.value = value;
+ return v;
+ }
}
- /**
- * 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
+ protected static class SimpleElementStreamReader implements XMLFragmentStreamReader {
- populateNamespaceContext();
- }
+ private static final int END_ELEMENT_STATE = 2;
- /**
- *
- * @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;
- }
+ private static final int START_ELEMENT_STATE = 0;
- }
+ private static final int START_ELEMENT_STATE_WITH_NULL = 3;
- public int next() throws XMLStreamException {
- return updateStatus();
- }
+ private static final int TEXT_STATE = 1;
- public void require(int i, String string, String string1) throws XMLStreamException {
- throw new UnsupportedOperationException();
- }
+ private static final QName XSI_NIL_QNAME =
+ new QName("http://www.w3.org/2001/XMLSchema-instance", "nil", "xsi");
- /**
- * 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;
- }
+ private QName name;
- }
+ private DelegatingNamespaceContext namespaceContext = new DelegatingNamespaceContext(null);
- /**
- * todo implement this
- *
- * @return
- * @throws XMLStreamException
- */
- public int nextTag() throws XMLStreamException {
- return 0;
- }
+ private int state = START_ELEMENT_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);
+ 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 here - we have no resources to free
- }
+ public void close() throws XMLStreamException {
+ // Do nothing - we've nothing to free here
+ }
- public String getNamespaceURI(String prefix) {
- return namespaceContext.getNamespaceURI(prefix);
- }
+ public int getAttributeCount() {
+ if (state == START_ELEMENT_STATE_WITH_NULL) {
+ return 1;
+ }
+ if (state == START_ELEMENT_STATE) {
+ return 0;
+ } else {
+ throw new IllegalStateException();
+ }
- public boolean isStartElement() {
- if (state == START_ELEMENT_STATE) {
- return true;
- } else if (state == END_ELEMENT_STATE) {
- return false;
}
- return childReader.isStartElement();
- }
- public boolean isEndElement() {
- if (state == START_ELEMENT_STATE) {
- return false;
- } else if (state == END_ELEMENT_STATE) {
- return true;
- }
- return childReader.isEndElement();
- }
-
- public boolean isCharacters() {
- if (state == START_ELEMENT_STATE || state == END_ELEMENT_STATE) {
- return false;
+ 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();
+ }
}
- return childReader.isCharacters();
- }
- public boolean isWhiteSpace() {
- if (state == START_ELEMENT_STATE || state == END_ELEMENT_STATE) {
- return false;
+ 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();
+ }
}
- return childReader.isWhiteSpace();
- }
- // /////////////////////////////////////////////////////////////////////////
- // / attribute handling
- // /////////////////////////////////////////////////////////////////////////
-
- 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;
- }
+ 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 {
- if (localName.equals(attribQualifiedName.getLocalPart())
- && nsUri.equals(attribQualifiedName.getNamespaceURI())) {
- returnValue = getAttributeValue(i);
- break;
- }
+ throw new IllegalStateException();
}
-
}
- return returnValue;
- }
-
- public int getAttributeCount() {
- return (state == DELEGATED_STATE) ? childReader.getAttributeCount() : ((attributes != null)
- && (state == START_ELEMENT_STATE) ? attributes.length : 0);
- }
-
- /**
- * @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) {
+ 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 {
- 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;
- }
- }
+ throw new IllegalStateException();
}
- } else {
- throw new IllegalStateException();// as per the api contract
}
- }
+ public String getAttributeType(int i) {
+ return null; // not implemented
+ }
- 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) {
+ public String getAttributeValue(int i) {
+ if (state == START_ELEMENT_STATE_WITH_NULL && i == 0) {
+ return "true";
+ }
+ if (state == START_ELEMENT_STATE) {
return null;
} else {
- return name.getNamespaceURI();
+ throw new IllegalStateException();
}
- } else {
- throw new IllegalStateException();
}
- }
- 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) {
+ public String getAttributeValue(String string, String string1) {
+ if (state == TEXT_STATE) {
+ // todo something
return null;
} else {
- return name.getLocalPart();
+ return null;
}
- } 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;
+ 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 {
- return name.getPrefix();
+ throw new XMLStreamException();
}
- } else {
- throw new IllegalStateException();
+
}
- }
- public String getAttributeType(int i) {
- return null; // not supported
- }
+ public String getEncoding() {
+ return "UTF-8";
+ }
- public String getAttributeValue(int i) {
- if (state == DELEGATED_STATE) {
- return childReader.getAttributeValue(i);
- } else if (state == START_ELEMENT_STATE) {
- if (attributes == null) {
- return null;
+ 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 {
- 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;
- }
- }
+ return null;
}
- } else {
- throw new IllegalStateException();
}
- }
+ public Location getLocation() {
+ return new Location() {
+ public int getCharacterOffset() {
+ return 0;
+ }
- public boolean isAttributeSpecified(int i) {
- return false; // not supported
- }
+ public int getColumnNumber() {
+ return 0;
+ }
- // /////////////////////////////////////////////////////////////////////////
- // //////////// end of attribute handling
- // /////////////////////////////////////////////////////////////////////////
+ public int getLineNumber() {
+ return 0;
+ }
- // //////////////////////////////////////////////////////////////////////////
- // //////////// namespace handling
- // //////////////////////////////////////////////////////////////////////////
+ public String getPublicId() {
+ return null;
+ }
- public int getNamespaceCount() {
- if (state == DELEGATED_STATE) {
- return childReader.getNamespaceCount();
- } else {
- return declaredNamespaceMap.size();
+ public String getSystemId() {
+ return null;
+ }
+ };
}
- }
- /**
- * @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)) {
+ 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 prefixes[i];
+ return 0;
}
- } else {
- throw new IllegalStateException();
}
- }
+ public String getNamespacePrefix(int i) {
+ if (state == START_ELEMENT_STATE_WITH_NULL && isXsiNamespacePresent() && i == 0) {
+ return XSI_NIL_QNAME.getPrefix();
+ } else {
+ return null;
+ }
+ }
- /**
- * 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 String getNamespaceURI() {
+ if (state != TEXT_STATE) {
+ return name.getNamespaceURI();
+ } else {
+ return null;
+ }
- 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 NamespaceContext getNamespaceContext() {
- if (state == DELEGATED_STATE) {
- return childReader.getNamespaceContext();
- } else {
- return namespaceContext;
+ public String getNamespaceURI(int i) {
+ if (state == START_ELEMENT_STATE_WITH_NULL && isXsiNamespacePresent() && i == 0) {
+ return XSI_NIL_QNAME.getNamespaceURI();
+ } else {
+ return null;
+ }
}
- }
-
- // /////////////////////////////////////////////////////////////////////////
- // /////// end of namespace 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 getNamespaceURI(String prefix) {
+ return namespaceContext.getNamespaceURI(prefix);
}
- }
-
- 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 String getPIData() {
+ return null;
}
- }
- 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 String getPITarget() {
+ return null;
}
- }
- 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 String getPrefix() {
+ if (state != TEXT_STATE) {
+ return name.getPrefix();
+ } else {
+ return null;
+ }
}
- }
- 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 Object getProperty(String key) throws IllegalArgumentException {
+ return null;
}
- }
- 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 String getText() {
+ if (state == TEXT_STATE) {
+ return value;
+ } else {
+ throw new IllegalStateException();
+ }
}
- }
- 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 char[] getTextCharacters() {
+ if (state == TEXT_STATE) {
+ return value.toCharArray();
+ } else {
+ throw new IllegalStateException();
+ }
}
- }
- /**
- * check the validity of this implementation
- *
- * @return
- */
- public boolean hasText() {
- if (state == DELEGATED_STATE) {
- return childReader.hasText();
- } else {
- return (state == TEXT_STATE);
+ public int getTextCharacters(int i, char[] chars, int i1, int i2) throws XMLStreamException {
+ // not implemented
+ throw new UnsupportedOperationException();
}
- }
- /**
- * @return
- */
- public Location getLocation() {
- // return a default location
- return new Location() {
- public int getLineNumber() {
- return 0;
+ public int getTextLength() {
+ if (state == TEXT_STATE) {
+ return value.length();
+ } else {
+ throw new IllegalStateException();
}
- public int getColumnNumber() {
- return 0;
- }
+ }
- public int getCharacterOffset() {
+ public int getTextStart() {
+ if (state == TEXT_STATE) {
return 0;
+ } else {
+ throw new IllegalStateException();
}
+ }
- public String getPublicId() {
- return null;
- }
+ public String getVersion() {
+ return null; // todo 1.0 ?
+ }
- public String getSystemId() {
- return null;
- }
- };
- }
+ public boolean hasName() {
+ return state != TEXT_STATE;
- public QName getName() {
- if (state == DELEGATED_STATE) {
- return childReader.getName();
- } else if (state != TEXT_STATE) {
- return elementQName;
- } else {
- throw new IllegalStateException();
}
- }
+ public boolean hasNext() throws XMLStreamException {
+ return state != END_ELEMENT_STATE;
+ }
- public String getLocalName() {
- if (state == DELEGATED_STATE) {
- return childReader.getLocalName();
- } else if (state != TEXT_STATE) {
- return elementQName.getLocalPart();
- } else {
- throw new IllegalStateException();
+ public boolean hasText() {
+ return state == TEXT_STATE;
}
- }
- 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);
+ public void init() {
+ // just add the current elements namespace and prefix to the this
+ // elements nscontext
+ registerNamespace(name.getPrefix(), name.getNamespaceURI());
+
}
- }
- public String getNamespaceURI() {
- if (state == DELEGATED_STATE) {
- return childReader.getNamespaceURI();
- } else if (state == TEXT_STATE) {
- return null;
- } else {
- return elementQName.getNamespaceURI();
+ public boolean isAttributeSpecified(int i) {
+ return false; // no attribs here
}
- }
- public String getPrefix() {
- if (state == DELEGATED_STATE) {
- return childReader.getPrefix();
- } else if (state == TEXT_STATE) {
- return null;
- } else {
- return elementQName.getPrefix();
+ public boolean isCharacters() {
+ return state == TEXT_STATE;
}
- }
- public String getVersion() {
- return null;
- }
+ public boolean isEndElement() {
+ return state == END_ELEMENT_STATE;
+ }
- public boolean isStandalone() {
- return true;
- }
+ public boolean isEndOfFragment() {
+ return state == END_ELEMENT_STATE;
+ }
- public boolean standaloneSet() {
- return true;
- }
+ public boolean isStandalone() {
+ return false;
+ }
- public String getCharacterEncodingScheme() {
- return null; // todo - should we return something for this ?
- }
+ public boolean isStartElement() {
+ return state == START_ELEMENT_STATE || state == START_ELEMENT_STATE_WITH_NULL;
+ }
- public String getPITarget() {
- throw new UnsupportedOperationException("Yet to be implemented !!");
- }
+ public boolean isWhiteSpace() {
+ return false; // no whitespaces here
+ }
- public String getPIData() {
- throw new UnsupportedOperationException("Yet to be implemented !!");
- }
+ /**
+ * Test whether the xsi namespace is present
+ *
+ * @return
+ */
+ private boolean isXsiNamespacePresent() {
+ return namespaceContext.getNamespaceURI(XSI_NIL_QNAME.getPrefix()) != null;
+ }
- // /////////////////////////////////////////////////////////////////////////
- // / 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 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!");
}
}
- }
-
- /**
- * @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 int nextTag() throws XMLStreamException {
+ return 0; // todo
}
- }
-
- /**
- * 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();
+ /**
+ * @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);
}
- 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;
+ public void require(int i, String string, String string1) throws XMLStreamException {
+ // not implemented
}
- return returnEvent;
- }
- /**
- * 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);
+ public void setParentNamespaceContext(NamespaceContext nsContext) {
+ this.namespaceContext.setParent(nsContext);
}
- // 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
+ public boolean standaloneSet() {
+ return false;
}
- // set the state here
- state = DELEGATED_STATE;
- // we are done with the delegation
- // increment the property index
- currentPropertyIndex++;
- return childReader.getEventType();
}
- /**
- * are we done ?
- *
- * @return
- */
- public boolean isEndOfFragment() {
- return (state == END_ELEMENT_STATE);
- }
+ private static final int DELEGATED_STATE = 2;
- protected static class NameValuePair implements Map.Entry {
- private Object key;
+ private static final int END_ELEMENT_STATE = 1;
- private Object value;
+ // states for this pullparser - it can only have three states
+ private static final int START_ELEMENT_STATE = 0;
- public NameValuePair(Object key, Object value) {
- this.key = key;
- this.value = value;
- }
+ private static final int TEXT_STATE = 3;
- public Object getKey() {
- return key;
- }
+ private Map.Entry[] attributes;
- public Object getValue() {
- return value;
- }
+ // reference to the child reader
+ private XMLFragmentStreamReader childReader;
- public Object setValue(Object value) {
- Object v = this.value;
- this.value = value;
- return v;
- }
+ // current property index
+ // initialized at zero
+ private int currentPropertyIndex = 0;
- }
+ private Map<String, String> declaredNamespaceMap = new HashMap<String, String>();
- protected static class SimpleElementStreamReader implements XMLFragmentStreamReader {
+ private QName elementQName;
- private static final int START_ELEMENT_STATE = 0;
+ // we always create a new namespace context
+ private DelegatingNamespaceContext namespaceContext = new DelegatingNamespaceContext(null);
- private static final int TEXT_STATE = 1;
+ private Map.Entry[] properties;
- private static final int END_ELEMENT_STATE = 2;
+ private Element rootElement;
- private static final int START_ELEMENT_STATE_WITH_NULL = 3;
+ private String rootElementName;
- private static final QName XSI_NIL_QNAME = new QName("http://www.w3.org/2001/XMLSchema-instance", "nil", "xsi");
+ private String rootElementURI;
- private DelegatingNamespaceContext namespaceContext = new DelegatingNamespaceContext(null);
+ // integer field that keeps the state of this
+ // parser.
+ private int state = START_ELEMENT_STATE;
- private QName name;
+ public DOMXMLStreamReader(Node node) {
+ switch (node.getNodeType()) {
+ case Node.DOCUMENT_NODE:
+ this.rootElement = ((Document)node).getDocumentElement();
+ break;
+ case Node.ELEMENT_NODE:
+ this.rootElement = (Element)node;
+ break;
+ default:
+ throw new IllegalArgumentException("Illegal Node");
+ }
+ this.rootElementName = rootElement.getLocalName();
+ this.rootElementURI = rootElement.getNamespaceURI();
+ populateProperties();
+ }
- private String value;
+ /*
+ * 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;
- private int state = START_ELEMENT_STATE;
+ }
- 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 here - we have no resources to free
+ }
- public Object getProperty(String key) throws IllegalArgumentException {
- return null;
- }
+ public int getAttributeCount() {
+ return (state == DELEGATED_STATE) ? childReader.getAttributeCount()
+ : ((attributes != null) && (state == START_ELEMENT_STATE) ? attributes.length : 0);
+ }
- 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 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();
}
+ }
- 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;
+ /**
+ * @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 {
- throw new XMLStreamException();
+ 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 int nextTag() throws XMLStreamException {
- return 0;// todo
- }
+ }
- public boolean hasNext() throws XMLStreamException {
- return (state != END_ELEMENT_STATE);
+ 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 void close() throws XMLStreamException {
- // Do nothing - we've nothing to free here
+ 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 getNamespaceURI(String prefix) {
- return namespaceContext.getNamespaceURI(prefix);
- }
+ public String getAttributeType(int i) {
+ return null; // not supported
+ }
- public boolean isStartElement() {
- return (state == START_ELEMENT_STATE || state == START_ELEMENT_STATE_WITH_NULL);
+ 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 boolean isEndElement() {
- return (state == END_ELEMENT_STATE);
- }
+ }
- public boolean isCharacters() {
- return (state == TEXT_STATE);
- }
+ 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;
+ }
+ }
- public boolean isWhiteSpace() {
- return false; // no whitespaces here
}
- public boolean isAttributeSpecified(int i) {
- return false; // no attribs here
+ 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 NamespaceContext getNamespaceContext() {
- return this.namespaceContext;
+ }
+
+ 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() {
- 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!!!!!
- }
+ // /////////////////////////////////////////////////////////////////////////
+ // / 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 getText() {
- if (state == TEXT_STATE) {
- return value;
- } else {
- throw new IllegalStateException();
- }
+ }
+
+ public String getLocalName() {
+ if (state == DELEGATED_STATE) {
+ return childReader.getLocalName();
+ } else if (state != TEXT_STATE) {
+ return elementQName.getLocalPart();
+ } else {
+ throw new IllegalStateException();
}
+ }
- public char[] getTextCharacters() {
- if (state == TEXT_STATE) {
- return value.toCharArray();
- } else {
- throw new IllegalStateException();
+ /**
+ * @return
+ */
+ public Location getLocation() {
+ // return a default location
+ return new Location() {
+ public int getCharacterOffset() {
+ return 0;
}
- }
- public int getTextCharacters(int i, char[] chars, int i1, int i2) throws XMLStreamException {
- // not implemented
- throw new UnsupportedOperationException();
- }
+ public int getColumnNumber() {
+ return 0;
+ }
- public int getTextStart() {
- if (state == TEXT_STATE) {
+ public int getLineNumber() {
return 0;
- } else {
- throw new IllegalStateException();
}
- }
- public int getTextLength() {
- if (state == TEXT_STATE) {
- return value.length();
- } else {
- throw new IllegalStateException();
+ public String getPublicId() {
+ return null;
}
- }
+ public String getSystemId() {
+ return null;
+ }
+ };
+ }
- public String getEncoding() {
- return "UTF-8";
+ public QName getName() {
+ if (state == DELEGATED_STATE) {
+ return childReader.getName();
+ } else if (state != TEXT_STATE) {
+ return elementQName;
+ } else {
+ throw new IllegalStateException();
}
- public boolean hasText() {
- return (state == TEXT_STATE);
+ }
+
+ public NamespaceContext getNamespaceContext() {
+ if (state == DELEGATED_STATE) {
+ return childReader.getNamespaceContext();
+ } else {
+ return namespaceContext;
}
- public Location getLocation() {
- return new Location() {
- public int getLineNumber() {
- return 0;
- }
+ }
- public int getColumnNumber() {
- return 0;
- }
+ public int getNamespaceCount() {
+ if (state == DELEGATED_STATE) {
+ return childReader.getNamespaceCount();
+ } else {
+ return declaredNamespaceMap.size();
+ }
+ }
- 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 {
+ /**
+ * @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;
- }
- }
-
- public String getLocalName() {
- if (state != TEXT_STATE) {
- return name.getLocalPart();
} else {
- return null;
+ return prefixes[i];
}
- }
-
- public boolean hasName() {
- return (state != TEXT_STATE);
+ } else {
+ throw new IllegalStateException();
}
- public String getNamespaceURI() {
- if (state != TEXT_STATE) {
- return name.getNamespaceURI();
- } else {
- return null;
- }
+ }
+ public String getNamespaceURI() {
+ if (state == DELEGATED_STATE) {
+ return childReader.getNamespaceURI();
+ } else if (state == TEXT_STATE) {
+ return null;
+ } else {
+ return elementQName.getNamespaceURI();
}
+ }
- public String getPrefix() {
- if (state != TEXT_STATE) {
- return name.getPrefix();
- } else {
- return null;
- }
+ 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 getVersion() {
- return null; // todo 1.0 ?
- }
+ }
- public boolean isStandalone() {
- return false;
- }
+ // /////////////////////////////////////////////////////////////////////////
+ // //////////// end of attribute handling
+ // /////////////////////////////////////////////////////////////////////////
- public boolean standaloneSet() {
- return false;
- }
+ // //////////////////////////////////////////////////////////////////////////
+ // //////////// namespace handling
+ // //////////////////////////////////////////////////////////////////////////
- public String getCharacterEncodingScheme() {
- return null;
- }
+ public String getNamespaceURI(String prefix) {
+ return namespaceContext.getNamespaceURI(prefix);
+ }
- public String getPITarget() {
- return null;
- }
+ public String getPIData() {
+ throw new UnsupportedOperationException("Yet to be implemented !!");
+ }
- public String getPIData() {
- return null;
- }
+ public String getPITarget() {
+ throw new UnsupportedOperationException("Yet to be implemented !!");
+ }
- public boolean isEndOfFragment() {
- return (state == END_ELEMENT_STATE);
+ public String getPrefix() {
+ if (state == DELEGATED_STATE) {
+ return childReader.getPrefix();
+ } else if (state == TEXT_STATE) {
+ return null;
+ } else {
+ return elementQName.getPrefix();
}
+ }
- public void setParentNamespaceContext(NamespaceContext nsContext) {
- this.namespaceContext.setParent(nsContext);
+ /**
+ * @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;
}
- public void init() {
- // just add the current elements namespace and prefix to the this
- // elements nscontext
- registerNamespace(name.getPrefix(), name.getNamespaceURI());
+ }
- }
+ // /////////////////////////////////////////////////////////////////////////
+ // /////// end of namespace handling
+ // /////////////////////////////////////////////////////////////////////////
- /**
- * @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 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 int getAttributeCount() {
- if (state == START_ELEMENT_STATE_WITH_NULL)
- return 1;
- if (state == START_ELEMENT_STATE) {
- return 0;
- } 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 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 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 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 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 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 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 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 getVersion() {
+ return null;
+ }
- public String getAttributeType(int i) {
- return null; // not implemented
+ 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;
}
+ }
- public String getAttributeValue(int i) {
- if (state == START_ELEMENT_STATE_WITH_NULL && i == 0)
- return "true";
- if (state == START_ELEMENT_STATE) {
- return null;
+ /**
+ * @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 {
- throw new IllegalStateException();
+ return childReader.hasNext();
}
+ } else {
+ return state == START_ELEMENT_STATE || state == TEXT_STATE;
+
}
+ }
- public String getAttributeValue(String string, String string1) {
- if (state == TEXT_STATE) {
- // todo something
- return null;
- } else {
- return null;
- }
-
+ /**
+ * check the validity of this implementation
+ *
+ * @return
+ */
+ public boolean hasText() {
+ if (state == DELEGATED_STATE) {
+ return childReader.hasText();
+ } else {
+ return state == TEXT_STATE;
}
+ }
- public int getNamespaceCount() {
- if (state == START_ELEMENT_STATE_WITH_NULL && isXsiNamespacePresent())
- return 1;
- else
- return 0;
+ /**
+ * 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 String getNamespacePrefix(int i) {
- if (state == START_ELEMENT_STATE_WITH_NULL && isXsiNamespacePresent() && i == 0)
- return XSI_NIL_QNAME.getPrefix();
- else
- return null;
+ public boolean isEndElement() {
+ if (state == START_ELEMENT_STATE) {
+ return false;
+ } else if (state == END_ELEMENT_STATE) {
+ return true;
}
+ return childReader.isEndElement();
+ }
- public String getNamespaceURI(int i) {
- if (state == START_ELEMENT_STATE_WITH_NULL && isXsiNamespacePresent() && i == 0)
- return XSI_NIL_QNAME.getNamespaceURI();
- else
- return null;
+ /**
+ * 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();
+ }
- /**
- * Test whether the xsi namespace is present
- *
- * @return
- */
- private boolean isXsiNamespacePresent() {
- return (namespaceContext.getNamespaceURI(XSI_NIL_QNAME.getPrefix()) != null);
+ 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;
}
- protected static class DelegatingNamespaceContext implements NamespaceContext {
- private NamespaceContext parent;
+ public int next() throws XMLStreamException {
+ return updateStatus();
+ }
- private Map<String, String> prefixToNamespaceMapping = new HashMap<String, String>();
+ /**
+ * todo implement this
+ *
+ * @return
+ * @throws XMLStreamException
+ */
+ public int nextTag() throws XMLStreamException {
+ return 0;
+ }
- public DelegatingNamespaceContext(NamespaceContext parent) {
- super();
- this.parent = parent;
+ // /////////////////////////////////////////////////////////////////////////
+ // / Other utility methods
+ // ////////////////////////////////////////////////////////////////////////
- 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");
- }
+ /**
+ * Populates a namespace context
+ */
+ private void populateNamespaceContext() {
- public String getNamespaceURI(String prefix) {
- if (prefix == null)
- throw new IllegalArgumentException("Prefix is null");
+ // first add the current element namespace to the namespace context
+ // declare it if not found
+ registerNamespace(elementQName.getPrefix(), elementQName.getNamespaceURI());
- String ns = (String) prefixToNamespaceMapping.get(prefix);
- if (ns != null)
- return ns;
- else if (parent != null)
- return parent.getNamespaceURI(prefix);
- else
- return null;
- }
+ // 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 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());
- }
+ }
+
+ 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>();
+ NamedNodeMap nodeMap = rootElement.getAttributes();
+ for (int i = 0; i < nodeMap.getLength(); i++) {
+ Attr attr = (Attr)nodeMap.item(i);
+ 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;
}
- if (parent != null) {
- for (Iterator i = parent.getPrefixes(nsURI); i.hasNext();) {
- prefixList.add((String) i.next());
- }
+ }
+ 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);
}
- return prefixList.iterator();
+ } 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);
}
- public void registerMapping(String prefix, String nsURI) {
- prefixToNamespaceMapping.put(prefix, nsURI);
+ // 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
}
- private int counter = 0;
+ // set the state here
+ state = DELEGATED_STATE;
+ // we are done with the delegation
+ // increment the property index
+ currentPropertyIndex++;
+ return childReader.getEventType();
+ }
- 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);
+ /**
+ * @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 removeMapping(String prefix) {
- prefixToNamespaceMapping.remove(prefix);
- }
+ public void require(int i, String string, String string1) throws XMLStreamException {
+ throw new UnsupportedOperationException();
+ }
- public void setParent(NamespaceContext parent) {
- this.parent = parent;
+ /**
+ * 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();
+
+ }
+ 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;
}
+ return returnEvent;
}
}
Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/InputSource2Node.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/InputSource2Node.java?view=diff&rev=452260&r1=452259&r2=452260
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/InputSource2Node.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/InputSource2Node.java Mon Oct 2 16:25:18 2006
@@ -33,17 +33,17 @@
/**
* Push DOM InputSource to Node
- *
*/
@Service(Transformer.class)
-public class InputSource2Node extends TransformerExtension<InputSource, Node> implements PullTransformer<InputSource, Node> {
- private static final Source2ResultTransformer transformer = new Source2ResultTransformer();
+public class InputSource2Node extends TransformerExtension<InputSource, Node> implements
+ PullTransformer<InputSource, Node> {
+ private static final Source2ResultTransformer TRANSFORMER = new Source2ResultTransformer();
public Node transform(InputSource source, TransformationContext context) {
try {
Source streamSource = new StreamSource(source.getCharacterStream());
DOMResult result = new DOMResult();
- transformer.transform(streamSource, result, context);
+ TRANSFORMER.transform(streamSource, result, context);
return result.getNode();
} catch (Exception e) {
throw new TransformationException(e);
Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/InputSource2SAX.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/InputSource2SAX.java?view=diff&rev=452260&r1=452259&r2=452260
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/InputSource2SAX.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/InputSource2SAX.java Mon Oct 2 16:25:18 2006
@@ -32,10 +32,11 @@
/**
* Push InputSource to SAX
- *
*/
@Service(Transformer.class)
-public class InputSource2SAX extends TransformerExtension<InputSource, ContentHandler> implements PushTransformer<InputSource, ContentHandler> {
+public class InputSource2SAX extends TransformerExtension<InputSource, ContentHandler> implements
+ PushTransformer<InputSource, ContentHandler> {
+
public void transform(InputSource source, ContentHandler target, TransformationContext context) {
try {
XMLReader reader = XMLReaderFactory.createXMLReader();
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org