diff options
9 files changed, 183 insertions, 45 deletions
diff --git a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/TestReadWriteUnkonwnElement.java b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/AnyElementReadWriteTestCase.java index 3c1d9ba88e..8516c4a028 100644 --- a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/TestReadWriteUnkonwnElement.java +++ b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/AnyElementReadWriteTestCase.java @@ -1,11 +1,25 @@ +/* + * 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.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; -import java.net.URI; -import java.net.URL; -import java.util.Iterator; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLOutputFactory; @@ -15,15 +29,12 @@ 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.StAXArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; import org.apache.tuscany.sca.core.ExtensionPointRegistry; -import org.apache.tuscany.sca.definitions.SCADefinitions; import org.junit.Test; -import org.w3c.dom.Document; -public class TestReadWriteUnkonwnElement extends TestCase { +public class AnyElementReadWriteTestCase extends TestCase { 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 xmlns=\"http://www.osoa.org/xmlns/sca/1.0\" interface=\"calculator.CalculatorService\" /></service><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>"; diff --git a/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyAttributeProcessor.java b/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyAttributeProcessor.java new file mode 100644 index 0000000000..36e959ea21 --- /dev/null +++ b/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyAttributeProcessor.java @@ -0,0 +1,69 @@ +/* + * 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.Constants; +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<String> { + private static final QName ANY_ATTRIBUTE = new QName(Constants.XMLSCHEMA_NS, "anyAttribute"); + + public AnyAttributeProcessor(ModelFactoryExtensionPoint modelFactories, Monitor monitor) { + + } + + public QName getArtifactType() { + return ANY_ATTRIBUTE; + } + + public Class<String> 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/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultUnknownElementProcessor.java b/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyElementProcessor.java index d52a110c05..66bea7a4dd 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultUnknownElementProcessor.java +++ b/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyElementProcessor.java @@ -1,4 +1,22 @@ -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 + * 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; @@ -15,6 +33,12 @@ 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.Constants; +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; @@ -27,17 +51,28 @@ import org.w3c.dom.traversal.NodeFilter; import org.w3c.dom.traversal.NodeIterator; import org.w3c.dom.traversal.TreeWalker; -public class DefaultUnknownElementProcessor{ +public class AnyElementProcessor implements StAXArtifactProcessor<Object> { + private static final QName ANY_ELEMENT = new QName(Constants.XMLSCHEMA_NS, "anyElement"); + private static final Logger logger = Logger.getLogger(AnyElementProcessor.class.getName()); + private DocumentBuilderFactory documentBuilderFactory; + private Document document; private Monitor monitor; - private static final Logger logger = Logger.getLogger(DefaultUnknownElementProcessor.class.getName()); - public DefaultUnknownElementProcessor(Monitor monitor){ + public AnyElementProcessor(ModelFactoryExtensionPoint modelFactories, Monitor monitor) { + documentBuilderFactory = modelFactories.getFactory(DocumentBuilderFactory.class); this.monitor = monitor; } - private DocumentBuilderFactory documentBuilderFactory; - private Document document; + + public QName getArtifactType() { + return ANY_ELEMENT; + } + + public Class<Object> getModelType() { + return Object.class; + } + /** * Reads the contetns of the unknown elements and generates the DOM * @param reader @@ -45,12 +80,15 @@ public class DefaultUnknownElementProcessor{ * @return * @throws XMLStreamException */ - public Object read(XMLStreamReader reader, QName name) throws XMLStreamException{ + 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: @@ -105,6 +143,10 @@ public class DefaultUnknownElementProcessor{ } } + public void resolve(Object arg0, ModelResolver arg1) throws ContributionResolveException { + + } + /** * Method to generate the DOM * @param reader @@ -172,9 +214,6 @@ public class DefaultUnknownElementProcessor{ */ private Document createDocument() { try { - if (documentBuilderFactory == null) { - documentBuilderFactory = DocumentBuilderFactory.newInstance(); - } document = documentBuilderFactory.newDocumentBuilder().newDocument(); return document; } catch (ParserConfigurationException e) { diff --git a/java/sca/modules/contribution-xml/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor b/java/sca/modules/contribution-xml/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor index b73c380370..b69ea1e135 100644 --- a/java/sca/modules/contribution-xml/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor +++ b/java/sca/modules/contribution-xml/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor @@ -17,3 +17,4 @@ # Implementation class for the artifact processor extension org.apache.tuscany.sca.contribution.xml.ContributionMetadataProcessor;qname=http://www.osoa.org/xmlns/sca/1.0#contribution,model=org.apache.tuscany.sca.contribution.ContributionMetadata +org.apache.tuscany.sca.contribution.processor.xml.AnyElementProcessor;qname=http://www.w3.org/2001/XMLSchema#anyElement,model=java.lang.Object diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/Constants.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/Constants.java index 4cf20125b9..14131a9806 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/Constants.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/Constants.java @@ -25,6 +25,8 @@ package org.apache.tuscany.sca.contribution; * @version $Rev$ $Date$ */ public interface Constants { - String SCA10_NS = "http://www.osoa.org/xmlns/sca/1.0"; + String XMLSCHEMA_NS = "http://www.w3.org/2001/XMLSchema"; + + String SCA10_NS = "http://www.osoa.org/xmlns/sca/1.0"; String SCA10_TUSCANY_NS = "http://tuscany.apache.org/xmlns/sca/1.0"; } 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 ca5c36fe98..24d2a16bc8 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 @@ -34,12 +34,12 @@ 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.Constants; 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.contribution.service.UnrecognizedElementException; -import org.apache.tuscany.sca.contribution.processor.DefaultUnknownElementProcessor; import org.apache.tuscany.sca.monitor.Monitor; import org.apache.tuscany.sca.monitor.Problem; import org.apache.tuscany.sca.monitor.Problem.Severity; @@ -52,8 +52,9 @@ import org.apache.tuscany.sca.monitor.Problem.Severity; * * @version $Rev$ $Date$ */ -public class ExtensibleStAXArtifactProcessor - implements StAXArtifactProcessor<Object> { +public class ExtensibleStAXArtifactProcessor implements StAXArtifactProcessor<Object> { + + private static final QName ANY_ELEMENT = new QName(Constants.XMLSCHEMA_NS, "anyElement"); private static final Logger logger = Logger.getLogger(ExtensibleStAXArtifactProcessor.class.getName()); private XMLInputFactory inputFactory; @@ -134,14 +135,19 @@ public class ExtensibleStAXArtifactProcessor QName name = source.getName(); StAXArtifactProcessor<?> processor = (StAXArtifactProcessor<?>)processors.getProcessor(name); if (processor == null) { - DefaultUnknownElementProcessor unknownElementProcessor = new DefaultUnknownElementProcessor(monitor); Location location = source.getLocation(); if (logger.isLoggable(Level.WARNING)) { logger.warning("Element " + name + " cannot be processed. (" + location + ")"); } warning("ElementCannotBeProcessed", processors, name, location); - //return null; - return unknownElementProcessor.read(source,name); + + StAXArtifactProcessor anyElementProcessor = processors.getProcessor(ANY_ELEMENT); + if(anyElementProcessor != null) { + return anyElementProcessor.read(source); + } else { + return null; + } + } return processor.read(source); } @@ -155,12 +161,14 @@ public class ExtensibleStAXArtifactProcessor if (processor != null) { processor.write(model, outputSource); } else { - DefaultUnknownElementProcessor unknownElementProcessor = new DefaultUnknownElementProcessor(monitor); - unknownElementProcessor.write(model,outputSource); if (logger.isLoggable(Level.WARNING)) { 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/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXAttributeProcessor.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXAttributeProcessor.java index 3329f0bf1c..29c2af7513 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXAttributeProcessor.java +++ b/java/sca/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/java/sca/modules/contribution/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessorExtensionPoint b/java/sca/modules/contribution/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessorExtensionPoint deleted file mode 100644 index 607725bcfe..0000000000 --- a/java/sca/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/java/sca/modules/contribution/src/main/resources/contribution-validation-messages.properties b/java/sca/modules/contribution/src/main/resources/contribution-validation-messages.properties index bd650fe457..4f9f85048e 100644 --- a/java/sca/modules/contribution/src/main/resources/contribution-validation-messages.properties +++ b/java/sca/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}) + |