From 24fd07435707c25741ebda81a34e37f61e6780f7 Mon Sep 17 00:00:00 2001 From: lresende Date: Sat, 17 Jan 2009 01:46:57 +0000 Subject: Merging changes to fix the StAX namespace context from 1.x brach revision 734056 git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@735205 13f79535-47bb-0310-9956-ffa450edef68 --- .../processor/TuscanyNamespaceContext.java | 215 ++++++++++----------- .../processor/TuscanyXMLStreamReader.java | 145 +++++++------- .../processor/ValidatingXMLStreamReader.java | 11 +- 3 files changed, 178 insertions(+), 193 deletions(-) (limited to 'branches/sca-java-1.3.3/modules/contribution/src') diff --git a/branches/sca-java-1.3.3/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyNamespaceContext.java b/branches/sca-java-1.3.3/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyNamespaceContext.java index 8b1e40cee9..ec64ec2b3d 100644 --- a/branches/sca-java-1.3.3/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyNamespaceContext.java +++ b/branches/sca-java-1.3.3/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyNamespaceContext.java @@ -1,124 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.apache.tuscany.sca.contribution.processor; -import java.util.List; import java.util.ArrayList; +import java.util.Collections; import java.util.Iterator; +import java.util.List; import java.util.Stack; -import javax.xml.XMLConstants; import javax.xml.namespace.NamespaceContext; -@SuppressWarnings("unused") public class TuscanyNamespaceContext implements NamespaceContext { - private Stack>> context = null; - - public TuscanyNamespaceContext(Stack>> context){ - this.context = context; - } - - public String getNamespaceURI(String prefix) { - if (prefix == null) { - throw new IllegalArgumentException(); - } - return (String) getResult("getNSUri",prefix); - } - - public String getPrefix(String namespaceURI) { - if (namespaceURI == null) { - throw new IllegalArgumentException(); - } - return (String) getResult("getPrefix",namespaceURI); - } - - @SuppressWarnings("unchecked") - public Iterator getPrefixes(String namespaceURI) { - if (namespaceURI == null) { - throw new IllegalArgumentException(); - } - - Iterator iterator = new Itr((Iterator) getResult("getPrefixes",namespaceURI)); - return iterator; - } - - /* - * Generic method to Iterate through the Stack and return required result(s) - */ - private Object getResult(String operation,String arg){ - - ArrayList> contextList = null; - Iterator prefItr = null; - Iterator uriItr = null; - - List list = new ArrayList();; - - String toCompare = null; - - String tempPrefix = null; - String tempUri = null ; - - for(int i = context.size()-1; i>=0;i--){ - contextList = context.get(i); - prefItr = ((ArrayList)contextList.get(0)).iterator(); - uriItr = ((ArrayList)contextList.get(1)).iterator(); - for(int j = 0;uriItr.hasNext();j++){ - tempPrefix = (String) prefItr.next(); - tempUri = (String) uriItr.next(); - if(operation.equalsIgnoreCase("getNSUri")){ - toCompare = tempPrefix; - } - else if(operation.equalsIgnoreCase("getPrefix")){ - toCompare = tempUri; - } - else if(operation.equalsIgnoreCase("getPrefixes")){ - toCompare = tempUri; - } - if(toCompare != null && arg.equalsIgnoreCase(toCompare)){ - if(operation.equalsIgnoreCase("getNSUri")){ - return tempUri; - } - else if(operation.equalsIgnoreCase("getPrefix")){ - return tempPrefix; - } - else if(operation.equalsIgnoreCase("getPrefixes")){ - list.add(tempPrefix); - - } - - } - } - } - - if(operation.equalsIgnoreCase("getPrefixes")){ - return list.iterator(); - } - - return null; - } - - /* - * Custom implementation of the Iterator interface to override the behavior of the remove() method. - * The iterator should not be modifiable and invocation of the remove() method should throw UnsupportedOperationException. - */ - private class Itr implements Iterator{ - Iterator iterator = null; - - Itr(Iterator inputItr){ - this.iterator = inputItr; - } - - public boolean hasNext() { - return this.iterator.hasNext(); - } - - public E next() { - return (E) this.iterator.next(); - } - - public void remove() { - throw new UnsupportedOperationException(); - } - } //end of class Itr - - -} //end of Class + private Stack[]> context = null; + + public TuscanyNamespaceContext(Stack[]> context) { + this.context = context; + } + + public String getNamespaceURI(String prefix) { + if (prefix == null) { + throw new IllegalArgumentException(); + } + return (String)getResult("getNSUri", prefix); + } + + public String getPrefix(String namespaceURI) { + if (namespaceURI == null) { + throw new IllegalArgumentException(); + } + return (String)getResult("getPrefix", namespaceURI); + } + + @SuppressWarnings("unchecked") + public Iterator getPrefixes(String namespaceURI) { + if (namespaceURI == null) { + throw new IllegalArgumentException(); + } + + Iterator iterator = (Iterator)getResult("getPrefixes", namespaceURI); + return iterator; + } + + /* + * Generic method to Iterate through the Stack and return required result(s) + */ + private Object getResult(String operation, String arg) { + + List[] contextList = null; + Iterator prefItr = null; + Iterator uriItr = null; + + List list = new ArrayList(); + + String toCompare = null; + + String tempPrefix = null; + String tempUri = null; + + for (int i = context.size() - 1; i >= 0; i--) { + contextList = context.get(i); + prefItr = contextList[0].iterator(); + uriItr = contextList[1].iterator(); + while (uriItr.hasNext()) { + tempPrefix = prefItr.next(); + tempUri = uriItr.next(); + if (operation.equalsIgnoreCase("getNSUri")) { + toCompare = tempPrefix; + } else if (operation.equalsIgnoreCase("getPrefix")) { + toCompare = tempUri; + } else if (operation.equalsIgnoreCase("getPrefixes")) { + toCompare = tempUri; + } + if (toCompare != null && arg.equalsIgnoreCase(toCompare)) { + if (operation.equalsIgnoreCase("getNSUri")) { + return tempUri; + } else if (operation.equalsIgnoreCase("getPrefix")) { + return tempPrefix; + } else if (operation.equalsIgnoreCase("getPrefixes")) { + list.add(tempPrefix); + } + + } + } + } + + if (operation.equalsIgnoreCase("getPrefixes")) { + return Collections.unmodifiableList(list).iterator(); + } + + return null; + } + +} //end of Class \ No newline at end of file diff --git a/branches/sca-java-1.3.3/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyXMLStreamReader.java b/branches/sca-java-1.3.3/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyXMLStreamReader.java index 7aa6e518f4..3fd7bded68 100644 --- a/branches/sca-java-1.3.3/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyXMLStreamReader.java +++ b/branches/sca-java-1.3.3/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyXMLStreamReader.java @@ -1,4 +1,5 @@ package org.apache.tuscany.sca.contribution.processor; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -21,92 +22,88 @@ import java.util.ArrayList; import java.util.List; import java.util.Stack; +import javax.xml.XMLConstants; import javax.xml.namespace.NamespaceContext; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.util.StreamReaderDelegate; -import javax.xml.XMLConstants; /* * Custom implementaion of the XMLStreamReader to keep track of the namespace context for each element */ -public class TuscanyXMLStreamReader extends StreamReaderDelegate implements - XMLStreamReader { +public class TuscanyXMLStreamReader extends StreamReaderDelegate implements XMLStreamReader { + + Stack[]> context = new Stack[]>(); + + List[] contextList; + List prefixList; + List uriList; + + public TuscanyXMLStreamReader(XMLStreamReader reader) { + super(reader); + } + + public void pushContext() throws XMLStreamException { + contextList = new List[2]; + prefixList = new ArrayList(); + uriList = new ArrayList(); + int namespaceCount = this.getNamespaceCount(); + for (int i = 0; i < namespaceCount; i++) { + prefixList.add(checkString(this.getNamespacePrefix(i))); + uriList.add(this.getNamespaceURI(i)); + } + contextList[0] = prefixList; + contextList[1] = uriList; + context.push(contextList); + } + + private String checkString(String namespacePrefix) { + if (namespacePrefix == null) { + return XMLConstants.DEFAULT_NS_PREFIX; + } else { + return namespacePrefix; + } + } - Stack>> context = new Stack>>(); - - List contextList; - List prefixList; - List uriList; + public void popContext() throws XMLStreamException { + context.pop(); + } - public TuscanyXMLStreamReader(XMLStreamReader reader) { - super(reader); - } + /* + * Overriding the next() method to perform PUSH and POP operations + * for the NamespaceContext for the current element + */ - public void pushContext() throws XMLStreamException { - contextList = new ArrayList>(); - prefixList = new ArrayList(); - uriList = new ArrayList(); - int namespaceCount = this.getNamespaceCount(); - if (namespaceCount == 0) { - prefixList.add(null); - uriList.add(null); - } - for (int i = 0; i < namespaceCount; i++) { - prefixList.add(checkString(this.getNamespacePrefix(i))); - uriList.add(this.getNamespaceURI(i)); - } - contextList.add(prefixList); - contextList.add(uriList); - context.push((ArrayList) contextList); - } + @Override + public int next() throws XMLStreamException { + // POP the context if the element ends + if (this.getEventType() == END_ELEMENT) { + popContext(); + } - private String checkString(String namespacePrefix) { - if (namespacePrefix == null) { - return XMLConstants.DEFAULT_NS_PREFIX; - } else { - return namespacePrefix; - } - } + //get the next event + int nextEvent = super.next(); + //PUSH the events info onto the Stack + if (nextEvent == START_ELEMENT) { + pushContext(); + } + return nextEvent; + } - public void popContext() throws XMLStreamException { - context.pop(); - } + @Override + public int nextTag() throws XMLStreamException { + int event = super.nextTag(); + if (event == START_ELEMENT) { + pushContext(); + } + if (event == END_ELEMENT) { + popContext(); + } + return event; + } - /* - * Overriding the next() method to perform PUSH and POP operations - * for the NamespaceContext for the current element - */ - - @Override - public int next() throws XMLStreamException { - // POP the context if the element ends - if (this.getEventType() == END_ELEMENT) { - popContext(); - } - - //get the next event - int nextEvent = super.next(); - //PUSH the events info onto the Stack - if (nextEvent == START_ELEMENT) { - pushContext(); - } - return nextEvent; - } - - @Override - public int nextTag() throws XMLStreamException { - if (this.getEventType() == START_ELEMENT) { - pushContext(); - } - if (this.getEventType() == END_ELEMENT) { - popContext(); - } - return super.nextTag(); - } - - @Override - public NamespaceContext getNamespaceContext(){ - return new TuscanyNamespaceContext((Stack)context.clone()); - } + @Override + public NamespaceContext getNamespaceContext() { + return new TuscanyNamespaceContext((Stack[]>)context.clone()); + } } diff --git a/branches/sca-java-1.3.3/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ValidatingXMLStreamReader.java b/branches/sca-java-1.3.3/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ValidatingXMLStreamReader.java index 3251ff37e1..912869916f 100644 --- a/branches/sca-java-1.3.3/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ValidatingXMLStreamReader.java +++ b/branches/sca-java-1.3.3/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ValidatingXMLStreamReader.java @@ -27,20 +27,19 @@ import javax.xml.stream.Location; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; -import javax.xml.stream.util.StreamReaderDelegate; import javax.xml.validation.Schema; import javax.xml.validation.ValidatorHandler; +import org.apache.tuscany.sca.assembly.builder.impl.ProblemImpl; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.Problem; +import org.apache.tuscany.sca.monitor.Problem.Severity; import org.xml.sax.Attributes; import org.xml.sax.ErrorHandler; import org.xml.sax.Locator; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.AttributesImpl; -import org.apache.tuscany.sca.assembly.builder.impl.ProblemImpl; -import org.apache.tuscany.sca.monitor.Monitor; -import org.apache.tuscany.sca.monitor.Problem; -import org.apache.tuscany.sca.monitor.Problem.Severity; /** * @@ -201,6 +200,7 @@ class ValidatingXMLStreamReader extends TuscanyXMLStreamReader implements XMLStr case XMLStreamConstants.START_ELEMENT: level++; handleStartElement(); + pushContext(); return event; case XMLStreamConstants.PROCESSING_INSTRUCTION: handler.processingInstruction(super.getPITarget(), super.getPIData()); @@ -214,6 +214,7 @@ class ValidatingXMLStreamReader extends TuscanyXMLStreamReader implements XMLStr case XMLStreamConstants.END_ELEMENT: handleEndElement(); level--; + popContext(); return event; case XMLStreamConstants.END_DOCUMENT: handler.endDocument(); -- cgit v1.2.3