diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2008-09-15 17:57:26 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2008-09-15 17:57:26 +0000 |
commit | 56ae68a329e4333c753e4cb5df7cd49e550ddc43 (patch) | |
tree | 78b06364b4578d4a2cf7c5787b58f9e992a59ee8 /branches/sca-java-1.3.2/modules/assembly-xml/src | |
parent | db0b393ee18e88e7f4b97d901828afdd7669a551 (diff) |
TUSCANY-2463 - Merging attribute extension support from trunk
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@695565 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'branches/sca-java-1.3.2/modules/assembly-xml/src')
6 files changed, 384 insertions, 16 deletions
diff --git a/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java b/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java index ae33a4da16..06b208540a 100644 --- a/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java +++ b/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java @@ -49,6 +49,7 @@ import org.apache.tuscany.sca.assembly.Composite; import org.apache.tuscany.sca.assembly.ConfiguredOperation; import org.apache.tuscany.sca.assembly.ConstrainingType; import org.apache.tuscany.sca.assembly.Contract; +import org.apache.tuscany.sca.assembly.Extensible; import org.apache.tuscany.sca.assembly.Implementation; import org.apache.tuscany.sca.assembly.Multiplicity; import org.apache.tuscany.sca.assembly.OperationsConfigurator; @@ -58,9 +59,11 @@ import org.apache.tuscany.sca.assembly.builder.impl.ProblemImpl; import org.apache.tuscany.sca.contribution.ContributionFactory; import org.apache.tuscany.sca.contribution.processor.BaseStAXArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +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.interfacedef.InterfaceContract; import org.apache.tuscany.sca.monitor.Monitor; import org.apache.tuscany.sca.monitor.Problem; @@ -832,6 +835,45 @@ abstract class BaseAssemblyProcessor extends BaseStAXArtifactProcessor implement } } + /** + * + * @param reader + * @param elementName + * @param estensibleElement + * @param extensionAttributeProcessor + * @throws ContributionReadException + * @throws XMLStreamException + */ + protected void readExtendedAttributes(XMLStreamReader reader, QName elementName, Extensible estensibleElement, StAXAttributeProcessor extensionAttributeProcessor) throws ContributionReadException, XMLStreamException { + for (int a = 0; a < reader.getAttributeCount(); a++) { + QName attributeName = reader.getAttributeName(a); + if( attributeName.getNamespaceURI() != null && attributeName.getNamespaceURI().length() > 0) { + if( ! elementName.getNamespaceURI().equals(attributeName.getNamespaceURI()) ) { + String attributeExtension = (String) extensionAttributeProcessor.read(attributeName, reader); + estensibleElement.getExtensions().add(attributeExtension); + } + } + } + } + + + /** + * + * @param attributeModel + * @param writer + * @param extensibleElement + * @param extensionAttributeProcessor + * @throws ContributionWriteException + * @throws XMLStreamException + */ + protected void writeExtendedAttributes(XMLStreamWriter writer, Extensible extensibleElement, StAXAttributeProcessor extensionAttributeProcessor) throws ContributionWriteException, XMLStreamException { + for(Object o : extensibleElement.getExtensions()) { + //FIXME How to identify it's a extended attribute ? + if(o instanceof String) { + extensionAttributeProcessor.write(o, writer); + } + } + } /*protected void validatePolicySets(PolicySetAttachPoint policySetAttachPoint) throws ContributionResolveException { diff --git a/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeProcessor.java b/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeProcessor.java index 73a64702e4..9727b89aab 100644 --- a/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeProcessor.java +++ b/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeProcessor.java @@ -38,6 +38,7 @@ import org.apache.tuscany.sca.assembly.Reference; import org.apache.tuscany.sca.assembly.Service; import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +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; @@ -45,10 +46,10 @@ import org.apache.tuscany.sca.contribution.service.ContributionWriteException; import org.apache.tuscany.sca.interfacedef.InterfaceContract; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.interfacedef.impl.OperationImpl; +import org.apache.tuscany.sca.monitor.Monitor; import org.apache.tuscany.sca.policy.IntentAttachPoint; import org.apache.tuscany.sca.policy.PolicyFactory; import org.apache.tuscany.sca.policy.PolicySetAttachPoint; -import org.apache.tuscany.sca.monitor.Monitor; import org.w3c.dom.Document; /** @@ -65,7 +66,7 @@ public class ComponentTypeProcessor extends BaseAssemblyProcessor implements StA * @param registry */ public ComponentTypeProcessor(AssemblyFactory factory, PolicyFactory policyFactory, - StAXArtifactProcessor extensionProcessor, Monitor monitor) { + StAXArtifactProcessor extensionProcessor, StAXAttributeProcessor extensionAttributeProcessor, Monitor monitor) { super(factory, policyFactory, extensionProcessor, monitor); } @@ -77,6 +78,7 @@ public class ComponentTypeProcessor extends BaseAssemblyProcessor implements StA */ public ComponentTypeProcessor(ModelFactoryExtensionPoint modelFactories, StAXArtifactProcessor extensionProcessor, + StAXAttributeProcessor extensionAttributeProcessor, Monitor monitor) { super(modelFactories.getFactory(AssemblyFactory.class), modelFactories.getFactory(PolicyFactory.class), extensionProcessor, monitor); diff --git a/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeProcessor.java b/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeProcessor.java index c1a8bb5b86..09ec7664ce 100644 --- a/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeProcessor.java +++ b/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeProcessor.java @@ -59,6 +59,7 @@ import org.apache.tuscany.sca.contribution.Artifact; import org.apache.tuscany.sca.contribution.ContributionFactory; import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessor; import org.apache.tuscany.sca.contribution.resolver.ModelResolver; import org.apache.tuscany.sca.contribution.resolver.ResolverExtension; import org.apache.tuscany.sca.contribution.service.ContributionReadException; @@ -88,16 +89,25 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt // FIXME: to be refactored private XPathFactory xPathFactory = XPathFactory.newInstance(); + protected StAXAttributeProcessor<Object> extensionAttributeProcessor; + /** * Construct a new composite processor * * @param extensionPoints * @param extensionProcessor */ - public CompositeProcessor(ExtensionPointRegistry extensionPoints, - StAXArtifactProcessor extensionProcessor, - Monitor monitor) { - this(modelFactories(extensionPoints), extensionProcessor, monitor(extensionPoints)); + public CompositeProcessor(ExtensionPointRegistry extensionPoints, + StAXArtifactProcessor extensionProcessor, + StAXAttributeProcessor extensionAttributeProcessor, + Monitor monitor) { + + this(modelFactories(extensionPoints), + extensionProcessor, + extensionAttributeProcessor, + monitor(extensionPoints)); + + this.extensionAttributeProcessor = extensionAttributeProcessor; } /** @@ -108,12 +118,18 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt * @param monitor */ private CompositeProcessor(ModelFactoryExtensionPoint modelFactories, - StAXArtifactProcessor extensionProcessor, - Monitor monitor) { - super(modelFactories.getFactory(ContributionFactory.class), + StAXArtifactProcessor extensionProcessor, + StAXAttributeProcessor extensionAttributeProcessor, + Monitor monitor) { + + super(modelFactories.getFactory(ContributionFactory.class), modelFactories.getFactory(AssemblyFactory.class), modelFactories.getFactory(PolicyFactory.class), - extensionProcessor, monitor); + extensionProcessor, + monitor); + + this.extensionAttributeProcessor = extensionAttributeProcessor; + } /** @@ -125,10 +141,11 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt * @param extensionProcessor */ public CompositeProcessor(ContributionFactory contributionFactory, - AssemblyFactory assemblyFactory, - PolicyFactory policyFactory, - StAXArtifactProcessor extensionProcessor, - Monitor monitor) { + AssemblyFactory assemblyFactory, + PolicyFactory policyFactory, + StAXArtifactProcessor extensionProcessor, + StAXAttributeProcessor extensionAttributeProcessor, + Monitor monitor) { super(contributionFactory, assemblyFactory, policyFactory, extensionProcessor, monitor); } @@ -169,6 +186,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt if(isSet(reader, AUTOWIRE)) { composite.setAutowire(getBoolean(reader, AUTOWIRE)); } + + //handle extension attributes + this.readExtendedAttributes(reader, name, composite, extensionAttributeProcessor); + composite.setLocal(getBoolean(reader, LOCAL)); composite.setConstrainingType(readConstrainingType(reader)); policyProcessor.readPolicies(composite, reader); @@ -189,6 +210,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt componentService = assemblyFactory.createComponentService(); contract = componentService; componentService.setName(getString(reader, NAME)); + + //handle extension attributes + this.readExtendedAttributes(reader, name, componentService, extensionAttributeProcessor); + component.getServices().add(componentService); policyProcessor.readPolicies(contract, reader); } else { @@ -222,6 +247,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt compositeService.setPromotedService(promotedService); } + //handle extension attributes + this.readExtendedAttributes(reader, name, compositeService, extensionAttributeProcessor); + composite.getServices().add(compositeService); policyProcessor.readPolicies(contract, reader); } @@ -238,6 +266,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt } readTargets(componentReference, reader); componentReference.setWiredByImpl(getBoolean(reader, WIRED_BY_IMPL)); + + //handle extension attributes + this.readExtendedAttributes(reader, name, componentReference, extensionAttributeProcessor); + component.getReferences().add(componentReference); policyProcessor.readPolicies(contract, reader); } else { @@ -258,7 +290,11 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt } } compositeReference.setWiredByImpl(getBoolean(reader, WIRED_BY_IMPL)); - composite.getReferences().add(compositeReference); + + //handle extension attributes + this.readExtendedAttributes(reader, name, compositeReference, extensionAttributeProcessor); + + composite.getReferences().add(compositeReference); policyProcessor.readPolicies(contract, reader); } @@ -298,6 +334,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt } } componentProperty.setFile(getString(reader, FILE)); + + //handle extension attributes + this.readExtendedAttributes(reader, name, componentProperty, extensionAttributeProcessor); + policyProcessor.readPolicies(property, reader); readAbstractProperty(componentProperty, reader); @@ -338,6 +378,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt if (isSet(reader, URI)) { component.setURI(getString(reader, URI)); } + + //handle extension attributes + this.readExtendedAttributes(reader, name, component, extensionAttributeProcessor); + component.setConstrainingType(readConstrainingType(reader)); composite.getComponents().add(component); policyProcessor.readPolicies(component, reader); @@ -356,6 +400,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt target.setName(getString(reader, TARGET)); wire.setTarget(target); + //handle extension attributes + this.readExtendedAttributes(reader, name, wire, extensionAttributeProcessor); + composite.getWires().add(wire); policyProcessor.readPolicies(wire, reader); @@ -364,6 +411,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt // Read a <callback> callback = assemblyFactory.createCallback(); contract.setCallback(callback); + + //handle extension attributes + this.readExtendedAttributes(reader, name, callback, extensionAttributeProcessor); + policyProcessor.readPolicies(callback, reader); } else if (OPERATION_QNAME.equals(name)) { @@ -396,6 +447,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt Composite implementation = assemblyFactory.createComposite(); implementation.setName(getQName(reader, NAME)); implementation.setUnresolved(true); + + //handle extension attributes + this.readExtendedAttributes(reader, name, implementation, extensionAttributeProcessor); + component.setImplementation(implementation); policyProcessor.readPolicies(implementation, reader); } else { @@ -527,6 +582,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt new XAttr(NAME, composite.getName().getLocalPart()), new XAttr(AUTOWIRE, composite.getAutowire()), policyProcessor.writePolicies(composite)); + + //write extended attributes + this.writeExtendedAttributes(writer, composite, extensionAttributeProcessor); // Write <include> elements for (Composite include : composite.getIncludes()) { @@ -535,6 +593,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt INCLUDE, new XAttr(NAME, include.getName()), new XAttr(URI, uri)); + + //write extended attributes + this.writeExtendedAttributes(writer, include, extensionAttributeProcessor); + writeEnd(writer); } @@ -556,6 +618,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt writeStart(writer, SERVICE, new XAttr(NAME, service.getName()), new XAttr(PROMOTE, promote), policyProcessor.writePolicies(service)); + //write extended attributes + this.writeExtendedAttributes(writer, service, extensionAttributeProcessor); + + // Write service interface extensionProcessor.write(service.getInterfaceContract(), writer); @@ -570,6 +636,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt writeStart(writer, CALLBACK, policyProcessor.writePolicies(callback)); + //write extended attributes + this.writeExtendedAttributes(writer, callback, extensionAttributeProcessor); + // Write callback bindings for (Binding binding : callback.getBindings()) { extensionProcessor.write(binding, writer); @@ -598,10 +667,17 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt new XAttr(AUTOWIRE, component.getAutowire()), policyProcessor.writePolicies(component)); + //write extended attributes + this.writeExtendedAttributes(writer, component, extensionAttributeProcessor); + // Write the component implementation Implementation implementation = component.getImplementation(); if (implementation instanceof Composite) { writeStart(writer, IMPLEMENTATION_COMPOSITE, new XAttr(NAME, ((Composite)implementation).getName())); + + //write extended attributes + this.writeExtendedAttributes(writer, (Composite)implementation, extensionAttributeProcessor); + writeEnd(writer); } else { extensionProcessor.write(component.getImplementation(), writer); @@ -612,6 +688,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt writeStart(writer, SERVICE, new XAttr(NAME, service.getName()), policyProcessor.writePolicies(service)); + //write extended attributes + this.writeExtendedAttributes(writer, service, extensionAttributeProcessor); + // Write service interface extensionProcessor.write(service.getInterfaceContract(), writer); @@ -624,7 +703,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt if (service.getCallback() != null) { Callback callback = service.getCallback(); writeStart(writer, CALLBACK, policyProcessor.writePolicies(callback)); - + + //write extended attributes + this.writeExtendedAttributes(writer, callback, extensionAttributeProcessor); + // Write bindings for (Binding binding : callback.getBindings()) { extensionProcessor.write(binding, writer); @@ -653,6 +735,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt writeTargets(reference), policyProcessor.writePolicies(reference)); + //write extended attributes + this.writeExtendedAttributes(writer, reference, extensionAttributeProcessor); + // Write reference interface extensionProcessor.write(reference.getInterfaceContract(), writer); @@ -666,6 +751,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt Callback callback = reference.getCallback(); writeStart(writer, CALLBACK, policyProcessor.writePolicies(callback)); + //write extended attributes + this.writeExtendedAttributes(writer, callback, extensionAttributeProcessor); + // Write callback bindings for (Binding binding : callback.getBindings()) { extensionProcessor.write(binding, writer); @@ -700,6 +788,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt new XAttr(FILE, property.getFile()), policyProcessor.writePolicies(property)); + //write extended attributes + this.writeExtendedAttributes(writer, property, extensionAttributeProcessor); + // Write property value writePropertyValue(property.getValue(), property.getXSDElement(), property.getXSDType(), writer); @@ -729,6 +820,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt new XAttr(PROMOTE, promote), policyProcessor.writePolicies(reference)); + //write extended attributes + this.writeExtendedAttributes(writer, reference, extensionAttributeProcessor); + // Write reference interface extensionProcessor.write(reference.getInterfaceContract(), writer); @@ -742,6 +836,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt Callback callback = reference.getCallback(); writeStart(writer, CALLBACK); + //write extended attributes + this.writeExtendedAttributes(writer, callback, extensionAttributeProcessor); + // Write callback bindings for (Binding binding : callback.getBindings()) { extensionProcessor.write(binding, writer); @@ -774,6 +871,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt new XAttr(ELEMENT, property.getXSDElement()), policyProcessor.writePolicies(property)); + //write extended attributes + this.writeExtendedAttributes(writer, property, extensionAttributeProcessor); + // Write property value writePropertyValue(property.getValue(), property.getXSDElement(), property.getXSDType(), writer); @@ -790,6 +890,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt writeStart(writer, WIRE, new XAttr(SOURCE, wire.getSource().getName()), new XAttr(TARGET, wire .getTarget().getName())); + //write extended attributes + this.writeExtendedAttributes(writer, wire, extensionAttributeProcessor); + // Write extensions for (Object extension : wire.getExtensions()) { extensionProcessor.write(extension, writer); diff --git a/branches/sca-java-1.3.2/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAttributeTestCase.java b/branches/sca-java-1.3.2/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAttributeTestCase.java new file mode 100644 index 0000000000..437b8928b1 --- /dev/null +++ b/branches/sca-java-1.3.2/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAttributeTestCase.java @@ -0,0 +1,106 @@ +/* + * 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.namespace.QName; +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.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessorExtensionPoint; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; + +/** + * Test reading SCA XML assemblies. + * + * @version $Rev$ $Date$ + */ +public class ReadWriteAttributeTestCase extends TestCase { + + private XMLInputFactory inputFactory; + private ExtensibleStAXArtifactProcessor staxProcessor; + + private static final QName ATTRIBUTE = new QName("http://test", "customAttribute"); + + private static final 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\" customAttribute=\"customValue\">" + + "<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\" />" + + "</composite>"; + + @Override + public void setUp() throws Exception { + ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + inputFactory = XMLInputFactory.newInstance(); + StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + + StAXAttributeProcessorExtensionPoint staxAttributeProcessors = extensionPoints.getExtensionPoint(StAXAttributeProcessorExtensionPoint.class); + staxAttributeProcessors.addArtifactProcessor(new TestAttributeProcessor()); + + + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, XMLInputFactory.newInstance(), XMLOutputFactory.newInstance(), null); + } + + @Override + public void tearDown() throws Exception { + + } + + public void testReadComposite() throws Exception { + InputStream is = getClass().getResourceAsStream("CalculatorExtended.composite"); + XMLStreamReader reader = inputFactory.createXMLStreamReader(is); + Composite composite = (Composite) staxProcessor.read(reader); + assertNotNull(composite); + is.close(); + } + + public void testWriteComposite() throws Exception { + InputStream is = getClass().getResourceAsStream("CalculatorExtended.composite"); + XMLStreamReader reader = inputFactory.createXMLStreamReader(is); + Composite composite = (Composite) staxProcessor.read(reader); + assertNotNull(composite); + is.close(); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + staxProcessor.write(composite, bos); + + assertEquals(XML, bos.toString()); + } +} diff --git a/branches/sca-java-1.3.2/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/TestAttributeProcessor.java b/branches/sca-java-1.3.2/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/TestAttributeProcessor.java new file mode 100644 index 0000000000..691145a30e --- /dev/null +++ b/branches/sca-java-1.3.2/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/TestAttributeProcessor.java @@ -0,0 +1,61 @@ +/* + * 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 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.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; + +/** + * A Policy Processor used for testing. + * + * @version $Rev$ $Date$ + */ +public class TestAttributeProcessor extends BaseStAXArtifactProcessor implements StAXAttributeProcessor<String> { + private static final QName ATTRIBUTE = new QName("http://test", "customAttribute"); + + public QName getArtifactType() { + return ATTRIBUTE; + } + + 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(ATTRIBUTE.getPrefix(), ATTRIBUTE.getNamespaceURI()); + writer.writeAttribute(ATTRIBUTE.getLocalPart(), value); + } + + public Class<String> getModelType() { + return String.class; + } + + public void resolve(String arg0, ModelResolver arg1) throws ContributionResolveException { + + } +} diff --git a/branches/sca-java-1.3.2/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/CalculatorExtended.composite b/branches/sca-java-1.3.2/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/CalculatorExtended.composite new file mode 100644 index 0000000000..3c69d0ed67 --- /dev/null +++ b/branches/sca-java-1.3.2/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/CalculatorExtended.composite @@ -0,0 +1,54 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * 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. +--> +<composite xmlns="http://www.osoa.org/xmlns/sca/1.0" + xmlns:calc="http://calc" + xmlns:test="http://test" + targetNamespace="http://calc" + name="Calculator"> + + <service name="CalculatorService" promote="CalculatorServiceComponent"> + <interface.java interface="calculator.CalculatorService"/> + </service> + + <component name="CalculatorServiceComponent" test:customAttribute="customValue"> + <implementation.java class="calculator.CalculatorServiceImpl"/> + <reference name="addService" target="AddServiceComponent"/> + <reference name="subtractService" target="SubtractServiceComponent"/> + <reference name="multiplyService" target="MultiplyServiceComponent"/> + <reference name="divideService" target="DivideServiceComponent"/> + </component> + + <component name="AddServiceComponent"> + <implementation.java class="calculator.AddServiceImpl"/> + </component> + + <component name="SubtractServiceComponent"> + <implementation.java class="calculator.SubtractServiceImpl"/> + </component> + + <component name="MultiplyServiceComponent"> + <implementation.java class="calculator.MultiplyServiceImpl"/> + </component> + + <component name="DivideServiceComponent"> + <implementation.java class="calculator.DivideServiceImpl"/> + </component> + +</composite> |