From d0a4ad6413e434ae7ef1e50a6c6c1d70402753d8 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Mon, 29 Sep 2008 01:16:27 +0000 Subject: Merged from trunk. TUSCANY-2538 - Moving Default element processor to contribution-xml and various other minor fixes. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@699943 13f79535-47bb-0310-9956-ffa450edef68 --- .../assembly/xml/AnyElementReadWriteTestCase.java | 71 +++++ .../processor/xml/AnyAttributeProcessor.java | 68 +++++ .../processor/xml/AnyElementProcessor.java | 310 +++++++++++++++++++++ ...ca.contribution.processor.StAXArtifactProcessor | 1 + .../processor/ExtensibleStAXArtifactProcessor.java | 21 +- .../ExtensibleStAXAttributeProcessor.java | 23 ++ ....processor.StAXAttributeProcessorExtensionPoint | 18 -- .../contribution-validation-messages.properties | 3 + 8 files changed, 493 insertions(+), 22 deletions(-) create mode 100644 branches/sca-equinox/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/AnyElementReadWriteTestCase.java create mode 100644 branches/sca-equinox/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyAttributeProcessor.java create mode 100644 branches/sca-equinox/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyElementProcessor.java delete mode 100644 branches/sca-equinox/modules/contribution/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessorExtensionPoint (limited to 'branches/sca-equinox/modules') diff --git a/branches/sca-equinox/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/AnyElementReadWriteTestCase.java b/branches/sca-equinox/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/AnyElementReadWriteTestCase.java new file mode 100644 index 0000000000..8516c4a028 --- /dev/null +++ b/branches/sca-equinox/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/AnyElementReadWriteTestCase.java @@ -0,0 +1,71 @@ +/* + * 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 java.io.ByteArrayOutputStream; +import java.io.InputStream; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamReader; + +import junit.framework.TestCase; + +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.Test; + +public class AnyElementReadWriteTestCase extends TestCase { + + private XMLInputFactory inputFactory; + String XML = ""; + private ExtensibleStAXArtifactProcessor staxProcessor; + + @Override + 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); + } + + @Override + 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/branches/sca-equinox/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyAttributeProcessor.java b/branches/sca-equinox/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyAttributeProcessor.java new file mode 100644 index 0000000000..f36f57aab1 --- /dev/null +++ b/branches/sca-equinox/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyAttributeProcessor.java @@ -0,0 +1,68 @@ +/* + * 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.xml; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.BaseStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.contribution.service.ContributionWriteException; +import org.apache.tuscany.sca.monitor.Monitor; + +/** + * A Policy Processor used for testing. + * + * @version $Rev$ $Date$ + */ +public class AnyAttributeProcessor extends BaseStAXArtifactProcessor implements StAXAttributeProcessor { + private static final QName ANY_ATTRIBUTE = new QName("http://www.w3.org/2001/XMLSchema", "anyAttribute"); + + public AnyAttributeProcessor(ModelFactoryExtensionPoint modelFactories, Monitor monitor) { + + } + + public QName getArtifactType() { + return ANY_ATTRIBUTE; + } + + public Class getModelType() { + return String.class; + } + + public String read(QName attributeName, XMLStreamReader reader) throws ContributionReadException, XMLStreamException { + return reader.getAttributeValue(attributeName.getNamespaceURI(), attributeName.getLocalPart()); + } + + public void write(String value, XMLStreamWriter writer) throws ContributionWriteException, XMLStreamException { + writer.setPrefix(ANY_ATTRIBUTE.getPrefix(), ANY_ATTRIBUTE.getNamespaceURI()); + writer.writeAttribute(ANY_ATTRIBUTE.getLocalPart(), value); + } + + + public void resolve(String arg0, ModelResolver arg1) throws ContributionResolveException { + + } +} diff --git a/branches/sca-equinox/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyElementProcessor.java b/branches/sca-equinox/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyElementProcessor.java new file mode 100644 index 0000000000..088b8bc58c --- /dev/null +++ b/branches/sca-equinox/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyElementProcessor.java @@ -0,0 +1,310 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.contribution.processor.xml; + +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 javax.xml.namespace.QName; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.assembly.builder.impl.ProblemImpl; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +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 { + 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(ModelFactoryExtensionPoint modelFactories, Monitor monitor) { + documentBuilderFactory = modelFactories.getFactory(DocumentBuilderFactory.class); + this.monitor = monitor; + } + + + public QName getArtifactType() { + return ANY_ELEMENT; + } + + public Class getModelType() { + return Object.class; + } + + /** + * Reads the contetns of the unknown elements and generates the DOM + * @param reader + * @param name + * @return + * @throws XMLStreamException + */ + public Object read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException { + + int event = reader.getEventType(); + int level = 0; + ArrayList elementList = new ArrayList(); + 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); + } + } + + 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); + } + } + + /** + * Method to create an empty document + * @return + */ + private Document createDocument() { + try { + document = documentBuilderFactory.newDocumentBuilder().newDocument(); + return document; + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } + return null; + } + + /** + * 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 { - +public class ExtensibleStAXArtifactProcessor implements StAXArtifactProcessor { private static final Logger logger = Logger.getLogger(ExtensibleStAXArtifactProcessor.class.getName()); + + private static final QName ANY_ELEMENT = new QName("http://www.w3.org/2001/XMLSchema", "anyElement"); + private XMLInputFactory inputFactory; private XMLOutputFactory outputFactory; private StAXArtifactProcessorExtensionPoint processors; @@ -138,7 +140,13 @@ public class ExtensibleStAXArtifactProcessor logger.warning("Element " + name + " cannot be processed. (" + location + ")"); } warning("ElementCannotBeProcessed", processors, name, location); - return null; + + StAXArtifactProcessor anyElementProcessor = processors.getProcessor(ANY_ELEMENT); + if (anyElementProcessor != null) { + return anyElementProcessor.read(source); + } else { + return null; + } } return processor.read(source); } @@ -156,6 +164,11 @@ public class ExtensibleStAXArtifactProcessor logger.warning("No StAX processor is configured to handle " + model.getClass()); } warning("NoStaxProcessor", processors, model.getClass()); + + StAXArtifactProcessor anyElementProcessor = processors.getProcessor(ANY_ELEMENT); + if (anyElementProcessor != null) { + anyElementProcessor.write(model, outputSource); + } } } } diff --git a/branches/sca-equinox/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXAttributeProcessor.java b/branches/sca-equinox/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXAttributeProcessor.java index 3329f0bf1c..29c2af7513 100644 --- a/branches/sca-equinox/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXAttributeProcessor.java +++ b/branches/sca-equinox/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXAttributeProcessor.java @@ -147,6 +147,17 @@ public class ExtensibleStAXAttributeProcessor return processor.read(attributeName, source); } + + //handle extension attributes without processors + processor = (StAXAttributeProcessor)processors.getProcessor(UNKNOWN_ATTRIBUTE); + if (processor == null) { + Location location = source.getLocation(); + if (logger.isLoggable(Level.WARNING)) { + logger.warning("Could not find Default Attribute processor !"); + } + warning("DefaultAttributeProcessorNotAvailable", processors, UNKNOWN_ATTRIBUTE, location); + } + return processor == null ? null : processor.read(attributeName, source); } @@ -168,6 +179,18 @@ public class ExtensibleStAXAttributeProcessor processor.write(model, outputSource); return; } + + //handle extension attributes without processors + processor = (StAXAttributeProcessor)processors.getProcessor(UNKNOWN_ATTRIBUTE); + if(processor == null) { + if (logger.isLoggable(Level.WARNING)) { + logger.warning("No Default StAX processor is configured to handle " + model.getClass()); + } + warning("NoDefaultStaxProcessor", processors, model.getClass()); + } else { + processor.write(model, outputSource); + return; + } } diff --git a/branches/sca-equinox/modules/contribution/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessorExtensionPoint b/branches/sca-equinox/modules/contribution/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessorExtensionPoint deleted file mode 100644 index 607725bcfe..0000000000 --- a/branches/sca-equinox/modules/contribution/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessorExtensionPoint +++ /dev/null @@ -1,18 +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. - -org.apache.tuscany.sca.contribution.processor.DefaultStAXAttributeProcessorExtensionPoint diff --git a/branches/sca-equinox/modules/contribution/src/main/resources/contribution-validation-messages.properties b/branches/sca-equinox/modules/contribution/src/main/resources/contribution-validation-messages.properties index bd650fe457..4f9f85048e 100644 --- a/branches/sca-equinox/modules/contribution/src/main/resources/contribution-validation-messages.properties +++ b/branches/sca-equinox/modules/contribution/src/main/resources/contribution-validation-messages.properties @@ -23,9 +23,12 @@ SchemaFatalError = XMLSchema validation fatal error occured in: {0} ,line = {1}, SchemaWarning = XMLSchema validation warning occured in: {0} ,line = {1}, column = {2}, Message = {3} UnsupportedPackageTypeException = Unsupported contribution package type: {0} ElementCannotBeProcessed = Element {0} cannot be processed. ({1}) +AttributeCannotBeProcessed = Attribute {0} cannot be processed. ({1}) NoStaxProcessor = No StAX processor is configured to handle {0} ContributionWriteException = ContributionWriteException occured due to : ContributionReadException = ContributionReadException occured due to : UnrecognizedElementException = Unrecognized Element : {0} IllegalArgumentException = Invalid qname: {0} PrivilegedActionException = PrivilegedActionException occured due to : +AttributeCannotBeProcessed = Attribute {0} cannot be processed. ({1}) + -- cgit v1.2.3