diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-04-22 22:49:55 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-04-22 22:49:55 +0000 |
commit | 023a43d56c8af3f8aa7516b9b3b03bbe0d84ef2b (patch) | |
tree | dcb91796490834c99a16628855fb82ed07976910 /java/sca/modules/contribution-xml | |
parent | b354c03aecb7f30bfd65f0ec02a81ab915ba4966 (diff) |
Merge r767683 from 1.x branch
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@767701 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/contribution-xml')
2 files changed, 96 insertions, 67 deletions
diff --git a/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataProcessor.java b/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataProcessor.java index 804af17719..7218a0fb73 100644 --- a/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataProcessor.java +++ b/java/sca/modules/contribution-xml/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataProcessor.java @@ -6,15 +6,15 @@ * 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. + * under the License. */ package org.apache.tuscany.sca.contribution.xml; @@ -39,6 +39,7 @@ import org.apache.tuscany.sca.contribution.processor.ContributionReadException; import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; import org.apache.tuscany.sca.contribution.processor.ContributionWriteException; 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.core.FactoryExtensionPoint; import org.apache.tuscany.sca.monitor.Monitor; @@ -47,55 +48,69 @@ import org.apache.tuscany.sca.monitor.Problem.Severity; /** * Processor for contribution metadata - * + * * @version $Rev$ $Date$ */ -public class ContributionMetadataProcessor extends BaseStAXArtifactProcessor implements StAXArtifactProcessor<ContributionMetadata> { +public class ContributionMetadataProcessor extends BaseStAXArtifactProcessor implements + StAXArtifactProcessor<ContributionMetadata> { private static final String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200903"; - + private static final QName CONTRIBUTION_QNAME = new QName(SCA11_NS, "contribution"); private static final QName DEPLOYABLE_QNAME = new QName(SCA11_NS, "deployable"); - + private final AssemblyFactory assemblyFactory; private final ContributionFactory contributionFactory; - private final StAXArtifactProcessor<Object> extensionProcessor; + private final StAXAttributeProcessor<Object> attributeProcessor; private Monitor monitor; - public ContributionMetadataProcessor(AssemblyFactory assemblyFactory, - ContributionFactory contributionFactory, - StAXArtifactProcessor<Object> extensionProcessor, - Monitor monitor) { - this.assemblyFactory = assemblyFactory; - this.contributionFactory = contributionFactory; + public ContributionMetadataProcessor(FactoryExtensionPoint modelFactories, + StAXArtifactProcessor<Object> extensionProcessor, + StAXAttributeProcessor<Object> attributeProcessor, + Monitor monitor) { + this.assemblyFactory = modelFactories.getFactory(AssemblyFactory.class); + this.contributionFactory = modelFactories.getFactory(ContributionFactory.class); this.extensionProcessor = extensionProcessor; + this.attributeProcessor = attributeProcessor; this.monitor = monitor; } - + /** * Report a error. - * + * * @param problems * @param message * @param model */ - private void error(String message, Object model, Object... messageParameters) { - if (monitor != null) { - Problem problem = monitor.createProblem(this.getClass().getName(), "contribution-xml-validation-messages", Severity.ERROR, model, message, (Object[])messageParameters); - monitor.problem(problem); - } - } - - public ContributionMetadataProcessor(FactoryExtensionPoint modelFactories, - StAXArtifactProcessor<Object> extensionProcessor, - Monitor monitor) { - this.assemblyFactory = modelFactories.getFactory(AssemblyFactory.class); - this.contributionFactory = modelFactories.getFactory(ContributionFactory.class); - this.extensionProcessor = extensionProcessor; - this.monitor = monitor; + private void error(String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = + monitor.createProblem(this.getClass().getName(), + "contribution-xml-validation-messages", + Severity.ERROR, + model, + message, + (Object[])messageParameters); + monitor.problem(problem); + } } - - + + /** + * Report a exception. + * + * @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-xml-validation-messages", Severity.ERROR, + model, message, ex); + monitor.problem(problem); + } + } + public QName getArtifactType() { return CONTRIBUTION_QNAME; } @@ -107,37 +122,38 @@ public class ContributionMetadataProcessor extends BaseStAXArtifactProcessor imp public ContributionMetadata read(XMLStreamReader reader) throws ContributionReadException { ContributionMetadata contribution = null; QName name = null; - + try { while (reader.hasNext()) { int event = reader.getEventType(); switch (event) { case START_ELEMENT: name = reader.getName(); - + if (CONTRIBUTION_QNAME.equals(name)) { - + // Read <contribution> contribution = this.contributionFactory.createContributionMetadata(); contribution.setUnresolved(true); - - } else if (DEPLOYABLE_QNAME.equals(name)) { - + readExtendedAttributes(reader, contribution, attributeProcessor, assemblyFactory); + + } else if (DEPLOYABLE_QNAME.equals(name)) { + // Read <deployable> QName compositeName = getQName(reader, "composite"); if (compositeName == null) { - error("AttributeCompositeMissing", reader); + error("AttributeCompositeMissing", reader); //throw new ContributionReadException("Attribute 'composite' is missing"); } else { if (contribution != null) { Composite composite = assemblyFactory.createComposite(); composite.setName(compositeName); composite.setUnresolved(true); - contribution.getDeployables().add(composite); + contribution.getDeployables().add(composite); } } } else { - + // Read an extension element Object extension = extensionProcessor.read(reader); if (extension != null && contribution != null) { @@ -145,69 +161,74 @@ public class ContributionMetadataProcessor extends BaseStAXArtifactProcessor imp contribution.getImports().add((Import)extension); } else if (extension instanceof Export) { contribution.getExports().add((Export)extension); + } else { + contribution.getExtensions().add(extension); } } } break; - + case XMLStreamConstants.END_ELEMENT: if (CONTRIBUTION_QNAME.equals(reader.getName())) { return contribution; } - break; + break; } - + //Read the next element if (reader.hasNext()) { reader.next(); } } - } - catch (XMLStreamException e) { + } catch (XMLStreamException e) { ContributionReadException ex = new ContributionReadException(e); error("XMLStreamException", reader, ex); } - + return contribution; } - public void write(ContributionMetadata contribution, XMLStreamWriter writer) throws ContributionWriteException, XMLStreamException { - + public void write(ContributionMetadata contribution, XMLStreamWriter writer) throws ContributionWriteException, + XMLStreamException { + // Write <contribution> writeStartDocument(writer, CONTRIBUTION_QNAME.getNamespaceURI(), CONTRIBUTION_QNAME.getLocalPart()); + writeExtendedAttributes(writer, contribution, attributeProcessor); // Write <import> - for (Import imp: contribution.getImports()) { + for (Import imp : contribution.getImports()) { extensionProcessor.write(imp, writer); } - + // Write <export> - for (Export export: contribution.getExports()) { + for (Export export : contribution.getExports()) { extensionProcessor.write(export, writer); } - + // Write <deployable> - for (Composite deployable: contribution.getDeployables()) { - writeStart(writer, DEPLOYABLE_QNAME.getNamespaceURI(), DEPLOYABLE_QNAME.getLocalPart(), + for (Composite deployable : contribution.getDeployables()) { + writeStart(writer, + DEPLOYABLE_QNAME.getNamespaceURI(), + DEPLOYABLE_QNAME.getLocalPart(), new XAttr("composite", deployable.getName())); writeEnd(writer); } - + writeEndDocument(writer); } public void resolve(ContributionMetadata contribution, ModelResolver resolver) throws ContributionResolveException { - + // Resolve imports and exports - for (Export export: contribution.getExports()) { + for (Export export : contribution.getExports()) { extensionProcessor.resolve(export, resolver); } - for (Import import_: contribution.getImports()) { + for (Import import_ : contribution.getImports()) { extensionProcessor.resolve(import_, resolver); } - + // Resolve deployable composites - List<Composite> deployables = contribution.getDeployables(); + List<Composite> deployables = contribution.getDeployables(); for (int i = 0, n = deployables.size(); i < n; i++) { Composite deployable = deployables.get(i); Composite resolved = (Composite)resolver.resolveModel(Composite.class, deployable); @@ -215,7 +236,11 @@ public class ContributionMetadataProcessor extends BaseStAXArtifactProcessor imp deployables.set(i, resolved); } } - + + for (Object ext : contribution.getExtensions()) { + extensionProcessor.resolve(ext, resolver); + } + contribution.setUnresolved(false); } } diff --git a/java/sca/modules/contribution-xml/src/test/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataProcessorTestCase.java b/java/sca/modules/contribution-xml/src/test/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataProcessorTestCase.java index 35e8b57136..d1c18beee5 100644 --- a/java/sca/modules/contribution-xml/src/test/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataProcessorTestCase.java +++ b/java/sca/modules/contribution-xml/src/test/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataProcessorTestCase.java @@ -6,15 +6,15 @@ * 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. + * under the License. */ package org.apache.tuscany.sca.contribution.xml; @@ -47,16 +47,18 @@ import org.junit.Test; /** * Test the contribution metadata processor. - * + * * @version $Rev$ $Date$ */ public class ContributionMetadataProcessorTestCase { private static final String VALID_XML = - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<contribution xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\" xmlns:ns=\"http://ns\">" + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<contribution xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\"" + + " xmlns:ns=\"http://ns\" ns:foo=\"extended\">" + "<deployable composite=\"ns:Composite1\"/>" + "<deployable composite=\"ns:Composite2\"/>" + + "<ns:bar x=\"1\"/>" + "</contribution>"; private static final String INVALID_XML = @@ -95,6 +97,8 @@ public class ContributionMetadataProcessorTestCase { ContributionMetadata contribution = (ContributionMetadata)staxProcessor.read(reader); assertNotNull(contribution); assertEquals(2, contribution.getDeployables().size()); + assertEquals(1, contribution.getAttributeExtensions().size()); + assertEquals(1, contribution.getExtensions().size()); } @Test |