diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-01-22 00:02:32 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-01-22 00:02:32 +0000 |
commit | 700c96eb31b6a088c0c3fd37b491cc2abea82cd7 (patch) | |
tree | 0bd8b788791d17ef177dd4104a21d3f4264f86c0 /java/sca/modules | |
parent | 0b3eec00056e90bf82fdc6d931c1e3d590bd9d2a (diff) |
TUSCANY-2663 - Merging latest code to support extended elements to 2.x development stream
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@736500 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
10 files changed, 377 insertions, 508 deletions
diff --git a/java/sca/modules/assembly-xml/pom.xml b/java/sca/modules/assembly-xml/pom.xml index e4738b1ac4..d044df502a 100644 --- a/java/sca/modules/assembly-xml/pom.xml +++ b/java/sca/modules/assembly-xml/pom.xml @@ -52,7 +52,13 @@ <artifactId>tuscany-contribution</artifactId> <version>2.0-SNAPSHOT</version> </dependency> - + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-contribution-xml</artifactId> + <version>2.0-SNAPSHOT</version> + </dependency> + <dependency> <groupId>org.apache.tuscany.sca</groupId> <artifactId>tuscany-contribution-namespace</artifactId> diff --git a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/AnyElementReadWriteTestCase.java b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/AnyElementReadWriteTestCase.java deleted file mode 100644 index fdafff07bd..0000000000 --- a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/AnyElementReadWriteTestCase.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tuscany.sca.assembly.xml; - -import static org.junit.Assert.assertNotNull; - -import java.io.ByteArrayOutputStream; -import java.io.InputStream; - -import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLOutputFactory; -import javax.xml.stream.XMLStreamReader; - -import org.apache.tuscany.sca.assembly.Composite; -import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; -import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; -import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; -import org.apache.tuscany.sca.core.ExtensionPointRegistry; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -public class AnyElementReadWriteTestCase { - - private XMLInputFactory inputFactory; - String XML = "<?xml version='1.0' encoding='UTF-8'?><composite xmlns=\"http://www.osoa.org/xmlns/sca/1.0\" xmlns:ns1=\"http://www.osoa.org/xmlns/sca/1.0\" targetNamespace=\"http://calc\" name=\"Calculator\"><service name=\"CalculatorService\" promote=\"CalculatorServiceComponent\"><interface.java interface=\"calculator.CalculatorService\" /></service><component name=\"CalculatorServiceComponent\"><reference name=\"addService\" multiplicity=\"0..1\" target=\"AddServiceComponent\" /><reference name=\"subtractService\" target=\"SubtractServiceComponent\" /><reference name=\"multiplyService\" target=\"MultiplyServiceComponent\" /><reference name=\"divideService\" target=\"DivideServiceComponent\" /></component><component name=\"AddServiceComponent\" /><component name=\"SubtractServiceComponent\" /><component name=\"MultiplyServiceComponent\" /><component name=\"DivideServiceComponent\" /><x:unknownElement xmlns:x=\"http://x\" uknAttr=\"attribute1\"><y:subUnknownElement1 xmlns:y=\"http://y\" uknAttr1=\"attribute2\" /><x:subUnknownElement2 /></x:unknownElement></composite>"; - private ExtensibleStAXArtifactProcessor staxProcessor; - - @Before - public void setUp() throws Exception { - ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); - inputFactory = XMLInputFactory.newInstance(); - - StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints - .getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); - staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, - inputFactory, XMLOutputFactory.newInstance(), null); - } - - @After - public void tearDown() throws Exception { - } - - /* - @Test - public void testReadWriteComposite() throws Exception { - InputStream is = getClass().getResourceAsStream("Calculator.composite"); - XMLStreamReader reader = inputFactory.createXMLStreamReader(is); - Composite composite = (Composite) staxProcessor.read(reader); - assertNotNull(composite); - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - staxProcessor.write(composite, bos); - //System.out.println(bos.toString()); - assertEquals(XML, bos.toString()); - bos.close(); - - is.close(); - } - */ - - @Test - public void testReadWriteUnknownElementComposite() throws Exception { - InputStream is = getClass().getResourceAsStream("UnknownElement.composite"); - XMLStreamReader reader = inputFactory.createXMLStreamReader(is); - Composite composite = (Composite) staxProcessor.read(reader); - assertNotNull(composite); - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - staxProcessor.write(composite, bos); - System.out.println(bos.toString()); - //assertEquals(XML, bos.toString()); - bos.close(); - - is.close(); - } - -} diff --git a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAnyElementTestCase.java b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAnyElementTestCase.java new file mode 100644 index 0000000000..37a142d2f4 --- /dev/null +++ b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAnyElementTestCase.java @@ -0,0 +1,147 @@ +/* + * 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.assembly.xml; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.StringReader; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.ValidatingXMLInputFactory; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +public class ReadWriteAnyElementTestCase { + private static final String XML_RECURSIVE_EXTENDED_ELEMENT = + "<?xml version='1.0' encoding='UTF-8'?>" + + "<composite xmlns=\"http://www.osoa.org/xmlns/sca/1.0\" xmlns:ns1=\"http://www.osoa.org/xmlns/sca/1.0\" targetNamespace=\"http://temp\" name=\"RecursiveExtendedElement\">" + + "<unknownElement>" + + "<subUnknownElement1 attribute=\"anyAttribute\" />" + + "<subUnknownElement2 />" + + "</unknownElement>" + + "</composite>"; + + private static final String XML_UNKNOWN_IMPL = + "<?xml version='1.0' encoding='UTF-8'?>" + + "<composite xmlns=\"http://www.osoa.org/xmlns/sca/1.0\" xmlns:ns1=\"http://www.osoa.org/xmlns/sca/1.0\" targetNamespace=\"http://temp\" name=\"aaaa\" autowire=\"false\">" + + "<component name=\"unknownImpl\">" + + "<implementation.unknown class=\"raymond\" />" + + "<service name=\"service\">" + + "<binding.ws />" + + "</service>" + + "</component>" + + "</composite>"; + + private static final String XML_UNKNOWN_IMPL_WITH_INVALID_ATTRIBUTE = + "<?xml version='1.0' encoding='UTF-8'?>" + + "<composite xmlns=\"http://www.osoa.org/xmlns/sca/1.0\" xmlns:ns1=\"http://www.osoa.org/xmlns/sca/1.0\" targetNamespace=\"http://temp\" name=\"aaaa\" autowire=\"false\">" + + "<component name=\"unknownImpl\">" + + "<implementation.unknown class=\"raymond\" />" + + "<service name=\"service\" requires=\"\">" + + "<binding.ws />" + + "</service>" + + "</component>" + + "</composite>"; + + + private XMLInputFactory inputFactory; + private ExtensibleStAXArtifactProcessor staxProcessor; + + @Before + public void setUp() throws Exception { + ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + FactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); + inputFactory = modelFactories.getFactory(ValidatingXMLInputFactory.class); + + StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, XMLOutputFactory.newInstance(), null); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testReadWriteExtendedRecursiveElement() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(XML_RECURSIVE_EXTENDED_ELEMENT)); + Composite composite = (Composite)staxProcessor.read(reader); + assertNotNull(composite); + reader.close(); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + staxProcessor.write(composite, bos); + + // used for debug comparison + // System.out.println(XML_RECURSIVE_EXTENDED_ELEMENT); + // System.out.println(bos.toString()); + + assertEquals(XML_RECURSIVE_EXTENDED_ELEMENT, bos.toString()); + bos.close(); + } + + @Test + public void testReadWriteUnknwonImpl() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(XML_UNKNOWN_IMPL)); + Composite composite = (Composite)staxProcessor.read(reader); + assertNotNull(composite); + reader.close(); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + staxProcessor.write(composite, bos); + + // used for debug comparison + // System.out.println(XML_UNKNOWN_IMPL); + // System.out.println(bos.toString()); + + assertEquals(XML_UNKNOWN_IMPL, bos.toString()); + bos.close(); + } + + // @Test + @Ignore() + public void testReadWriteInvalidAttribute() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(XML_UNKNOWN_IMPL_WITH_INVALID_ATTRIBUTE)); + Composite composite = (Composite)staxProcessor.read(reader); + assertNotNull(composite); + reader.close(); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + staxProcessor.write(composite, bos); + + // used for debug comparison + // System.out.println(XML_UNKNOWN_IMPL); + // System.out.println(bos.toString()); + + assertEquals(XML_UNKNOWN_IMPL, bos.toString()); + bos.close(); + } +}
\ No newline at end of file diff --git a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteUnknownElementTestCase.java b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteUnknownElementTestCase.java deleted file mode 100644 index 07070b5bed..0000000000 --- a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteUnknownElementTestCase.java +++ /dev/null @@ -1,57 +0,0 @@ -package org.apache.tuscany.sca.assembly.xml; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import java.io.ByteArrayOutputStream; -import java.io.InputStream; - -import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLOutputFactory; -import javax.xml.stream.XMLStreamReader; - -import org.apache.tuscany.sca.assembly.Composite; -import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; -import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; -import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; -import org.apache.tuscany.sca.core.ExtensionPointRegistry; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -public class ReadWriteUnknownElementTestCase { - - private XMLInputFactory inputFactory; - //String XML = "<?xml version='1.0' encoding='UTF-8'?><composite xmlns=\"http://www.osoa.org/xmlns/sca/1.0\" xmlns:ns1=\"http://www.osoa.org/xmlns/sca/1.0\" targetNamespace=\"http://calc\" name=\"Calculator\"><service name=\"CalculatorService\" promote=\"CalculatorServiceComponent\" /><component name=\"CalculatorServiceComponent\"><reference name=\"addService\" target=\"AddServiceComponent\" /><reference name=\"subtractService\" target=\"SubtractServiceComponent\" /><reference name=\"multiplyService\" target=\"MultiplyServiceComponent\" /><reference name=\"divideService\" target=\"DivideServiceComponent\" /></component><component name=\"AddServiceComponent\" /><component name=\"SubtractServiceComponent\" /><component name=\"MultiplyServiceComponent\" /><component name=\"DivideServiceComponent\" /><x:unknownElement xmlns:x=\"http://x\" uknAttr=\"attribute1\"><x:subUnknownElement1 uknAttr1=\"attribute1\" /><x:subUnknownElement2 /></x:unknownElement></composite>"; - String XML = "<?xml version='1.0' encoding='UTF-8'?><composite xmlns=\"http://www.osoa.org/xmlns/sca/1.0\" xmlns:ns1=\"http://www.osoa.org/xmlns/sca/1.0\" targetNamespace=\"http://calc\" name=\"Calculator\"><service name=\"CalculatorService\" promote=\"CalculatorServiceComponent\" /><component name=\"CalculatorServiceComponent\"><reference name=\"addService\" multiplicity=\"0..1\" target=\"AddServiceComponent\" /><reference name=\"subtractService\" target=\"SubtractServiceComponent\" /><reference name=\"multiplyService\" target=\"MultiplyServiceComponent\" /><reference name=\"divideService\" target=\"DivideServiceComponent\" /></component><component name=\"AddServiceComponent\" /><component name=\"SubtractServiceComponent\" /><component name=\"MultiplyServiceComponent\" /><component name=\"DivideServiceComponent\" /></composite>"; - private ExtensibleStAXArtifactProcessor staxProcessor; - - @Before - public void setUp() throws Exception { - ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); - inputFactory = XMLInputFactory.newInstance(); - StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); - staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, XMLInputFactory.newInstance(), XMLOutputFactory.newInstance(), null); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testReadWriteComposite() throws Exception{ - InputStream is = getClass().getResourceAsStream("Calculator.composite"); - XMLStreamReader reader = inputFactory.createXMLStreamReader(is); - Composite composite = (Composite)staxProcessor.read(reader); - assertNotNull(composite); - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - staxProcessor.write(composite, bos); - System.out.println(bos.toString()); - assertEquals(XML,bos.toString()); - bos.close(); - - is.close(); - } - -} diff --git a/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyElementProcessor.java b/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyElementProcessor.java index 38da7740cd..68c46a2301 100644 --- a/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyElementProcessor.java +++ b/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyElementProcessor.java @@ -18,50 +18,45 @@ */ package org.apache.tuscany.sca.contribution.processor.xml; +import static javax.xml.stream.XMLStreamConstants.CDATA; +import static javax.xml.stream.XMLStreamConstants.CHARACTERS; import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; import java.util.ArrayList; -import java.util.logging.Level; -import java.util.logging.Logger; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.xml.namespace.NamespaceContext; import javax.xml.namespace.QName; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; +import javax.xml.stream.XMLEventReader; +import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; +import javax.xml.stream.events.XMLEvent; +import org.apache.tuscany.sca.contribution.Constants; import org.apache.tuscany.sca.contribution.processor.ContributionReadException; import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; import org.apache.tuscany.sca.contribution.resolver.ModelResolver; import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.monitor.Monitor; -import org.apache.tuscany.sca.monitor.Problem; -import org.apache.tuscany.sca.monitor.Problem.Severity; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NamedNodeMap; -import org.w3c.dom.Node; -import org.w3c.dom.traversal.DocumentTraversal; -import org.w3c.dom.traversal.NodeFilter; -import org.w3c.dom.traversal.NodeIterator; -import org.w3c.dom.traversal.TreeWalker; public class AnyElementProcessor implements StAXArtifactProcessor<Object> { - private static final QName ANY_ELEMENT = new QName("http://www.w3.org/2001/XMLSchema", "anyElement"); - - private static final Logger logger = Logger.getLogger(AnyElementProcessor.class.getName()); - private DocumentBuilderFactory documentBuilderFactory; - private Document document; - private Monitor monitor; - - public AnyElementProcessor(FactoryExtensionPoint modelFactories, Monitor monitor) { - documentBuilderFactory = modelFactories.getFactory(DocumentBuilderFactory.class); - this.monitor = monitor; - } - + private static final QName ANY_ELEMENT = new QName(Constants.XMLSCHEMA_NS, "anyElement"); + + private XMLInputFactory xmlInputFactory; + @SuppressWarnings("unused") + private Monitor monitor; + + + public AnyElementProcessor(FactoryExtensionPoint modelFactories, Monitor monitor) { + xmlInputFactory = modelFactories.getFactory(XMLInputFactory.class); + this.monitor = monitor; + } public QName getArtifactType() { return ANY_ELEMENT; @@ -70,240 +65,97 @@ public class AnyElementProcessor implements StAXArtifactProcessor<Object> { public Class<Object> getModelType() { return Object.class; } - - /** - * Reads the contetns of the unknown elements and generates the DOM - * @param reader - * @param name - * @return - * @throws XMLStreamException - */ + + /** + * Reads the contetns of the unknown elements and generates a custom + * implementation of XMLStreamReader i.e. XMLEventsStreamReader + * + * @param reader + * @return + * @throws XMLStreamException + */ + @SuppressWarnings("unchecked") public Object read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException { - - int event = reader.getEventType(); - int level = 0; - ArrayList<String> elementList = new ArrayList<String>(); - document = createDocument(); - - QName name = reader.getName(); - - while(reader.hasNext()){ - switch(event){ - case START_ELEMENT: - elementList.add(reader.getName().getLocalPart()); - if(level == 0){ - generateDOM(reader,null); - level++; - } - else{ - generateDOM(reader,elementList.get(elementList.size()-2).toString()); - } - - break; - case END_ELEMENT: - elementList.remove(reader.getName().getLocalPart()); - } - if(reader.hasNext()){ - event = reader.next(); - } - - if(event == START_ELEMENT || event == END_ELEMENT){ - if(reader.getName().equals(name)){ - break; - } - } - } - return document; - } - /** - * Writes unknown portions back to the writer - * @param model - * @param writer - */ - public void write(Object model, XMLStreamWriter writer) { - - if( ! (model instanceof Document)) { - return; - } - - Document doc = (Document)model; - try{ - DocumentTraversal traversal = (DocumentTraversal)doc; - TreeWalker walker = traversal.createTreeWalker(doc.getDocumentElement(),NodeFilter.SHOW_ALL, null, true); - writeDOM(walker,writer); - } - catch(Exception e){ - if (logger.isLoggable(Level.SEVERE)) { - logger.log(Level.SEVERE, "Document not created "); - } - error("Document not created",document,e); - } - } + // Custom variables + String currentElement = null; + List eventsList = new ArrayList(); - public void resolve(Object arg0, ModelResolver arg1) throws ContributionResolveException { - - } - - /** - * Method to generate the DOM - * @param reader - * @param parent - * @throws Exception - */ - //private void generateDOM(String elementText, String parent) { - private void generateDOM(XMLStreamReader reader, String parent) { - try{ - String elePrefix = reader.getPrefix(); - String eleQName = reader.getLocalName(); - if (elePrefix != null && elePrefix.length() != 0) { - eleQName = elePrefix + ":" + eleQName; - } - - Element element = document.createElementNS(reader.getNamespaceURI(), eleQName); - - int attributeCount = reader.getAttributeCount(); - for(int i = 0;i < attributeCount;i++){ - String ns = reader.getAttributeNamespace(i); - String prefix = reader.getAttributePrefix(i); - String qname = reader.getAttributeLocalName(i); - String value = reader.getAttributeValue(i); - if (prefix != null && prefix.length() != 0) { - qname = prefix + ":" + qname; - } - element.setAttributeNS(ns,qname,value); - } - if(parent == null){ - if(document != null){ - document.appendChild(element); - } - else{ - if (logger.isLoggable(Level.SEVERE)) { - logger.log(Level.SEVERE, "Document not created "); - } - error("Document not created",document,element); - } - } - else{ - Node parentNode = getParentNode(document,parent); - if(parentNode != null){ - parentNode.appendChild(element); - } - else{ - if (logger.isLoggable(Level.SEVERE)) { - logger.log(Level.SEVERE, "Parent node not found"); - } - error("Parent node not found",document,parentNode.getNodeName()); - } - } - } - catch(Exception e){ - e.printStackTrace(); - if (logger.isLoggable(Level.SEVERE)) { - logger.log(Level.SEVERE, "Document not created "); - } - error("Document not created",document,e); - } - } + Map<String, NamespaceContext> eventContext = new HashMap<String, NamespaceContext>(); - /** - * Method to create an empty document - * @return - */ - private Document createDocument() { - try { - document = documentBuilderFactory.newDocumentBuilder().newDocument(); - return document; - } catch (ParserConfigurationException e) { - e.printStackTrace(); - } - return null; - } + try { + // Cast the block of unknown elements into document + XMLDocumentStreamReader docReader = new XMLDocumentStreamReader(reader); - /** - * Method to traverse the DOM structure and write the elements - * @param walker - * @param writer - * @throws XMLStreamException - */ - private void writeDOM(TreeWalker walker,XMLStreamWriter writer) throws XMLStreamException { - - Node parent = walker.getCurrentNode(); - - writer.writeStartElement(parent.getPrefix(), parent.getLocalName(), parent.getNamespaceURI()); - - NamedNodeMap attributes = parent.getAttributes(); - - for(int i = 0;i<attributes.getLength();i++){ - writer.writeAttribute(attributes.item(i).getPrefix(), attributes.item(i).getNamespaceURI(), attributes.item(i).getLocalName(), attributes.item(i).getNodeValue()); - } - - for (Node n = walker.firstChild();n != null;n = walker.nextSibling()) { - writeDOM(walker,writer); - } - writer.writeEndElement(); - - walker.setCurrentNode(parent); - } + XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(docReader); - /** - * Method to get the Parent node out of the DOM structure - * @param doc - * @param parent - * @return - */ - private Node getParentNode(Node doc,String parent) { - Node parentNode = null; - try{ - DocumentTraversal traversal = (DocumentTraversal)doc; - - CharSequence prefixChar = ":"; - NodeIterator iterator = traversal.createNodeIterator(document.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true); - for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) { - String nodeName = n.getNodeName(); - String[] str = null; - if(n.getNodeName().contains(prefixChar)){ - str = nodeName.split(":"); - nodeName = str[str.length-1]; - } - if(parent.equalsIgnoreCase(nodeName)){ - parentNode = n; - } - } - return parentNode; - } - catch(Exception e){ - e.printStackTrace(); - } - return parentNode; - } - - /** - * Marshals exceptions into the monitor - * - * @param problems - * @param message - * @param model - */ - private void error(String message, Object model, Exception ex) { - if (monitor != null) { - Problem problem = monitor.createProblem(this.getClass().getName(), "contribution-validation-messages", Severity.ERROR, model, message, ex); - monitor.problem(problem); - } + while (xmlEventReader.hasNext()) { + XMLEvent event = xmlEventReader.nextEvent(); + + // Populate the eventContext map with the current element's name + // and corresponding NamesapceContext + if (currentElement != null && !(eventContext.containsKey(currentElement))) { + eventContext.put(currentElement, reader.getNamespaceContext()); + } + + // Populate the list with the XMLEvents + eventsList.add(event); + if (event.isStartElement()) { + currentElement = reader.getName().getLocalPart(); + } + if (event.isEndDocument()) { + return new XMLEventsStreamReader(eventsList, eventContext); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + return null; } - + /** - * Report a error. + * Writes unknown portions back to the writer * - * @param problems - * @param message * @param model + * @param writer */ - private void error(String message, Object model, Object... messageParameters) { - if (monitor != null) { - Problem problem = monitor.createProblem(this.getClass().getName(), "contribution-validation-messages", Severity.ERROR, model, message, (Object[])messageParameters); - monitor.problem(problem); - } + public void write(Object model, XMLStreamWriter writer) throws XMLStreamException { + if (!(model instanceof XMLStreamReader)) { + return; + } + XMLStreamReader reader = (XMLStreamReader)model; + + int event = reader.getEventType(); + while (reader.hasNext()) { + switch (event) { + case START_ELEMENT: + + writer.writeStartElement(reader.getPrefix(), reader.getLocalName(), reader.getNamespaceURI()); + for (int i = 1; i <= reader.getAttributeCount(); i++) { + writer.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i), reader + .getAttributeLocalName(i), reader.getAttributeValue(i)); + } + break; + + case CHARACTERS: + writer.writeCharacters(reader.getText()); + break; + + case CDATA: + writer.writeCData(reader.getText()); + break; + + case END_ELEMENT: + writer.writeEndElement(); + break; + } + if (reader.hasNext()) { + event = reader.next(); + } + } + } + + public void resolve(Object model, ModelResolver resolver) throws ContributionResolveException { + } - } diff --git a/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/XMLDocumentStreamReader.java b/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/XMLDocumentStreamReader.java index 70398deaa9..7e8b3a9d9e 100644 --- a/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/XMLDocumentStreamReader.java +++ b/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/XMLDocumentStreamReader.java @@ -37,16 +37,16 @@ import javax.xml.stream.XMLStreamReader; */ public class XMLDocumentStreamReader implements XMLStreamReader { private static final int STATE_COMPLETE_AT_NEXT = 2; // The wrapper - // will produce - // END_DOCUMENT + // will produce + // END_DOCUMENT - private static final int STATE_COMPLETED = 3; // Done + private static final int STATE_COMPLETED = 3; // Done - private static final int STATE_INIT = 0; // The wrapper will produce - // START_DOCUMENT + private static final int STATE_INIT = 0; // The wrapper will produce + // START_DOCUMENT - private static final int STATE_SWITCHED = 1; // The real reader will - // produce events + private static final int STATE_SWITCHED = 1; // The real reader will + // produce events private XMLStreamReader realReader; private boolean fragment; diff --git a/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/XMLEventsStreamReader.java b/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/XMLEventsStreamReader.java index eecdee5ba6..351929d8a1 100644 --- a/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/XMLEventsStreamReader.java +++ b/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/XMLEventsStreamReader.java @@ -20,9 +20,11 @@ package org.apache.tuscany.sca.contribution.processor.xml; import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Stack; + +import java.util.Iterator; import javax.xml.namespace.NamespaceContext; import javax.xml.namespace.QName; @@ -39,12 +41,14 @@ public class XMLEventsStreamReader implements XMLStreamReader { @SuppressWarnings("unused") private ArrayList<XMLEvent> events = null; + @SuppressWarnings("unchecked") private HashMap<String, NamespaceContext> eventContext = null; private int state; private java.util.Iterator<XMLEvent> iterator; private XMLEvent current; + @SuppressWarnings("unchecked") public XMLEventsStreamReader(List<XMLEvent> events,Map<String, NamespaceContext> map) { this.events = (ArrayList<XMLEvent>) events; this.eventContext = (HashMap<String, NamespaceContext>) map; diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXArtifactProcessor.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXArtifactProcessor.java index 881ad4a681..7578a24ea8 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXArtifactProcessor.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXArtifactProcessor.java @@ -142,17 +142,6 @@ public class ExtensibleStAXArtifactProcessor implements StAXArtifactProcessor<Ob } else { return null; } - - - //FIXME Re-enable feature after complete merge is done - /* - StAXArtifactProcessor<?> anyElementProcessor = processors.getProcessor(ANY_ELEMENT); - if (anyElementProcessor != null) { - return anyElementProcessor.read(source); - } else { - return null; - } - */ } return processor.read(source); } diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyNamespaceContext.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyNamespaceContext.java index cfe98535a1..2ddba7527e 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyNamespaceContext.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyNamespaceContext.java @@ -1,93 +1,114 @@ +/* + * 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.ArrayList; -import java.util.Collections;
+import java.util.Collections; import java.util.Iterator; -import java.util.List;
+import java.util.List; import java.util.Stack; import javax.xml.namespace.NamespaceContext; +/** + * Tuscany specialized class to handle NamespaceContext + * + */ public class TuscanyNamespaceContext implements NamespaceContext { - private Stack<List<String>[]> context = null;
- - public TuscanyNamespaceContext(Stack<List<String>[]> 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<String> getPrefixes(String namespaceURI) {
- if (namespaceURI == null) {
- throw new IllegalArgumentException();
- }
- - Iterator<String> iterator = (Iterator<String>)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<String>[] contextList = null;
- Iterator<String> prefItr = null;
- Iterator<String> uriItr = null;
- - List<String> list = new ArrayList<String>();
-
- 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 + private Stack<List<String>[]> context = null; + + public TuscanyNamespaceContext(Stack<List<String>[]> 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<String> getPrefixes(String namespaceURI) { + if (namespaceURI == null) { + throw new IllegalArgumentException(); + } + + Iterator<String> iterator = (Iterator<String>)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<String>[] contextList = null; + Iterator<String> prefItr = null; + Iterator<String> uriItr = null; + + List<String> list = new ArrayList<String>(); + + 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; + } +}
\ No newline at end of file diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyXMLStreamReader.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyXMLStreamReader.java index 5a5a70fb20..3352e76a0d 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyXMLStreamReader.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/TuscanyXMLStreamReader.java @@ -1,5 +1,3 @@ -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 @@ -18,6 +16,9 @@ package org.apache.tuscany.sca.contribution.processor; * specific language governing permissions and limitations * under the License. */ + +package org.apache.tuscany.sca.contribution.processor; + import java.util.ArrayList; import java.util.List; import java.util.Stack; @@ -29,7 +30,7 @@ import javax.xml.stream.XMLStreamReader; import javax.xml.stream.util.StreamReaderDelegate; /* - * Custom implementation of the XMLStreamReader to keep track of the namespace context for each element + * Custom implementaion of the XMLStreamReader to keep track of the namespace context for each element */ public class TuscanyXMLStreamReader extends StreamReaderDelegate implements XMLStreamReader { @@ -106,4 +107,4 @@ public class TuscanyXMLStreamReader extends StreamReaderDelegate implements XMLS public NamespaceContext getNamespaceContext() { return new TuscanyNamespaceContext((Stack<List<String>[]>)context.clone()); } -} +}
\ No newline at end of file |