diff options
author | slaws <slaws@13f79535-47bb-0310-9956-ffa450edef68> | 2009-07-29 15:05:28 +0000 |
---|---|---|
committer | slaws <slaws@13f79535-47bb-0310-9956-ffa450edef68> | 2009-07-29 15:05:28 +0000 |
commit | 8d4d8c42fed9b65efae67444e94230ff4ae88573 (patch) | |
tree | 0fec35c71d14fcb4a75fc687e404c3f333310ed8 /branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz | |
parent | 930785f016baa657a2a0012fe0a4cf5ba58f2c22 (diff) |
TUSCANY-3174 - Add workspace manager updates to workspace from 1.x and a new itest for the workspace manager
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@798937 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz')
10 files changed, 745 insertions, 0 deletions
diff --git a/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/DefaultImportExportXYZFactory.java b/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/DefaultImportExportXYZFactory.java new file mode 100644 index 0000000000..b9a7bf6c0c --- /dev/null +++ b/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/DefaultImportExportXYZFactory.java @@ -0,0 +1,28 @@ +/* + * 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.imprt.xyz; + +/** + * Default Import/Export model factory implementation + * + * @version $Rev$ $Date$ + */ +public class DefaultImportExportXYZFactory extends ImportExportXYZFactoryImpl implements ImportExportXYZFactory { +} diff --git a/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ExportXYZ.java b/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ExportXYZ.java new file mode 100644 index 0000000000..b099cfee11 --- /dev/null +++ b/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ExportXYZ.java @@ -0,0 +1,60 @@ +/* + * 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.imprt.xyz; + +import org.apache.tuscany.sca.contribution.Export; + + +/** + * The model of an export. + * + * @version $Rev$ $Date$ + */ +public interface ExportXYZ extends Export { + + /** + * Get the uri of the contributions + * + * @return The URI + */ + String getURI(); + + /** + * Set the uri of the contributions + * + * @param URI + */ + void setURI(String URI); + + /** + * Get anAttribute + * + * @return anAttribute + */ + String getAnAttribute(); + + /** + * Set anAttribute + * + * @param anAttribute + */ + void setAnAttribute(String anAttribute); + +} diff --git a/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ExportXYZImpl.java b/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ExportXYZImpl.java new file mode 100644 index 0000000000..6e98979b5d --- /dev/null +++ b/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ExportXYZImpl.java @@ -0,0 +1,64 @@ +/* + * 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.imprt.xyz; + +import org.apache.tuscany.sca.assembly.impl.ExtensibleImpl; +import org.apache.tuscany.sca.contribution.namespace.NamespaceExport; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; + +/** + * The model of an export + * + * @version $Rev$ $Date$ + */ +public class ExportXYZImpl extends ExtensibleImpl implements ExportXYZ { + private String URI; + private String anAttribute; + private ModelResolver modelResolver; + + protected ExportXYZImpl() { + super(); + } + + public String getURI() { + return URI; + } + + public void setURI(String URI) { + this.URI = URI; + } + + public String getAnAttribute() { + return anAttribute; + } + + public void setAnAttribute(String anAttribute) { + this.anAttribute = anAttribute; + } + + public ModelResolver getModelResolver() { + return modelResolver; + } + + public void setModelResolver(ModelResolver modelResolver) { + this.modelResolver = modelResolver; + } + +} diff --git a/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ExportXYZModelResolver.java b/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ExportXYZModelResolver.java new file mode 100644 index 0000000000..c3d2663d62 --- /dev/null +++ b/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ExportXYZModelResolver.java @@ -0,0 +1,54 @@ +/* + * 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.imprt.xyz; + +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; + +/** + * A model resolver for exports. + * + * @version $Rev$ $Date$ + */ +public class ExportXYZModelResolver implements ModelResolver { + + private ModelResolver resolver; + + public ExportXYZModelResolver(ModelResolver resolver) { + this.resolver = resolver; + } + + public void addModel(Object resolved) { + throw new IllegalStateException(); + } + + public Object removeModel(Object resolved) { + throw new IllegalStateException(); + } + + public <T> T resolveModel(Class<T> modelClass, T unresolved) { + + // Just delegate to the contribution's model resolver, namespace + // based filtering is implemented in the model specific model + // resolver, which know how to get the namespace of the particular + // type of model that they handle + return resolver.resolveModel(modelClass, unresolved); + } + +} diff --git a/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ExportXYZProcessor.java b/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ExportXYZProcessor.java new file mode 100644 index 0000000000..31d435a12a --- /dev/null +++ b/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ExportXYZProcessor.java @@ -0,0 +1,171 @@ +/* + * 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.imprt.xyz; + +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.assembly.ExtensionFactory; +import org.apache.tuscany.sca.assembly.builder.impl.ProblemImpl; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.namespace.NamespaceExport; +import org.apache.tuscany.sca.contribution.namespace.NamespaceImportExportFactory; +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.monitor.Monitor; +import org.apache.tuscany.sca.monitor.Problem; +import org.apache.tuscany.sca.monitor.Problem.Severity; + + +public class ExportXYZProcessor extends BaseStAXArtifactProcessor implements StAXArtifactProcessor<ExportXYZ> { + + private static final String NS = "http://someuri"; + private static final QName EXPORT = new QName(NS, "export.xyz"); + private static final String URI = "uri"; + private static final String AN_ATTRIBUTE = "anAttribute"; + + private final ImportExportXYZFactory factory; + private final Monitor monitor; + private final ExtensionFactory extensionFactory; + + + public ExportXYZProcessor(ModelFactoryExtensionPoint modelFactories, + Monitor monitor) { + this.factory = modelFactories.getFactory(ImportExportXYZFactory.class); + this.extensionFactory = modelFactories.getFactory(ExtensionFactory.class); + this.monitor = monitor; + } + + /** + * Report a warning. + * + * @param problems + * @param message + * @param model + */ + private void error(String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = new ProblemImpl(this.getClass().getName(), "export-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 = new ProblemImpl(this.getClass().getName(), "export-validation-messages", Severity.ERROR, model, message, ex); + monitor.problem(problem); + } + } + + public QName getArtifactType() { + return EXPORT; + } + + public Class<ExportXYZ> getModelType() { + return ExportXYZ.class; + } + + + public ExportXYZ read(XMLStreamReader reader) throws ContributionReadException { + ExportXYZ export = this.factory.createExport(); + QName element = null; + + try { + while (reader.hasNext()) { + int event = reader.getEventType(); + switch (event) { + case START_ELEMENT: + element = reader.getName(); + + // Read <export> + if (EXPORT.equals(element)) { + String ns = reader.getAttributeValue(null, URI); + if (ns == null) { + error("AttributeNameSpaceMissing", reader); + } else { + export.setURI(ns); + } + + String anAttribute = reader.getAttributeValue(null, AN_ATTRIBUTE); + export.setAnAttribute(anAttribute); + } + + break; + case XMLStreamConstants.END_ELEMENT: + if (EXPORT.equals(reader.getName())) { + return export; + } + break; + } + + // Read the next element + if (reader.hasNext()) { + reader.next(); + } + } + } + catch (XMLStreamException e) { + ContributionReadException ex = new ContributionReadException(e); + error("XMLStreamException", reader, ex); + } + + return export; + } + + public void write(ExportXYZ export, XMLStreamWriter writer) throws ContributionWriteException, XMLStreamException { + + // Write <export> + writer.writeStartElement(EXPORT.getNamespaceURI(), EXPORT.getLocalPart()); + + if (export.getURI() != null) { + writer.writeAttribute(URI, export.getURI()); + } + + if (export.getAnAttribute() != null) { + writer.writeAttribute(AN_ATTRIBUTE, export.getAnAttribute()); + } + + writer.writeEndElement(); + } + + public void resolve(ExportXYZ export, ModelResolver resolver) throws ContributionResolveException { + + if (export.getURI() != null) + // Initialize the export's resolver + export.setModelResolver(new ExportXYZModelResolver(resolver)); + } +} diff --git a/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ImportExportXYZFactory.java b/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ImportExportXYZFactory.java new file mode 100644 index 0000000000..db2bab7f99 --- /dev/null +++ b/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ImportExportXYZFactory.java @@ -0,0 +1,27 @@ +/* + * 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.imprt.xyz; + +public interface ImportExportXYZFactory { + + ImportXYZ createImport(); + + ExportXYZ createExport(); +} diff --git a/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ImportExportXYZFactoryImpl.java b/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ImportExportXYZFactoryImpl.java new file mode 100644 index 0000000000..b0a9c80d88 --- /dev/null +++ b/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ImportExportXYZFactoryImpl.java @@ -0,0 +1,32 @@ +/* + * 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.imprt.xyz; + +public class ImportExportXYZFactoryImpl implements ImportExportXYZFactory { + + public ImportXYZ createImport() { + return new ImportXYZImpl(); + } + + public ExportXYZ createExport() { + return new ExportXYZImpl(); + } + +} diff --git a/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ImportXYZ.java b/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ImportXYZ.java new file mode 100644 index 0000000000..3e883e84a2 --- /dev/null +++ b/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ImportXYZ.java @@ -0,0 +1,59 @@ +/* + * 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.imprt.xyz; + +import org.apache.tuscany.sca.contribution.Import; + +/** + * The representation of an XML namespace import. + * + * @version $Rev$ $Date$ + */ +public interface ImportXYZ extends Import { + + /** + * Get the uri of the contributions + * + * @return The URI + */ + String getURI(); + + /** + * Set the uri of the contributions + * + * @param URI + */ + void setURI(String URI); + + /** + * Get anAttribute + * + * @return anAttribute + */ + String getAnAttribute(); + + /** + * Set anAttribute + * + * @param anAttribute + */ + void setAnAttribute(String anAttribute); + +}
\ No newline at end of file diff --git a/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ImportXYZImpl.java b/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ImportXYZImpl.java new file mode 100644 index 0000000000..ef3f0653f3 --- /dev/null +++ b/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ImportXYZImpl.java @@ -0,0 +1,79 @@ +/* + * 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.imprt.xyz; + +import org.apache.tuscany.sca.assembly.impl.ExtensibleImpl; +import org.apache.tuscany.sca.contribution.Export; +import org.apache.tuscany.sca.contribution.namespace.NamespaceExport; +import org.apache.tuscany.sca.contribution.namespace.NamespaceImport; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; + +/** + * The representation of an import for the contribution + * + * @version $Rev$ $Date$ + */ +public class ImportXYZImpl extends ExtensibleImpl implements ImportXYZ { + private ModelResolver modelResolver; + + private String URI; + private String anAttribute; + + + protected ImportXYZImpl() { + super(); + } + + public String getURI() { + return URI; + } + + public void setURI(String URI) { + this.URI = URI; + } + + public String getAnAttribute() { + return anAttribute; + } + + public void setAnAttribute(String anAttribute) { + this.anAttribute = anAttribute; + } + + public ModelResolver getModelResolver() { + return modelResolver; + } + + public void setModelResolver(ModelResolver modelResolver) { + this.modelResolver = modelResolver; + } + + public boolean match(Export export) { + if (export instanceof ExportXYZ){ + return anAttribute.equals(((ExportXYZ)export).getAnAttribute()); + } + return false; + } + + @Override + public String toString() { + return String.valueOf(URI); + } +} diff --git a/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ImportXYZProcessor.java b/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ImportXYZProcessor.java new file mode 100644 index 0000000000..7083eb5821 --- /dev/null +++ b/branches/sca-java-1.5.1/itest/workspace-manager/src/main/java/org/apache/tuscany/sca/imprt/xyz/ImportXYZProcessor.java @@ -0,0 +1,171 @@ +/* + * 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.imprt.xyz; + +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.assembly.ExtensionFactory; +import org.apache.tuscany.sca.assembly.builder.impl.ProblemImpl; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.namespace.NamespaceImport; +import org.apache.tuscany.sca.contribution.namespace.NamespaceImportExportFactory; +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.monitor.Monitor; +import org.apache.tuscany.sca.monitor.Problem; +import org.apache.tuscany.sca.monitor.Problem.Severity; + +/** + * Artifact processor for Namespace import + * + * @version $Rev$ $Date$ + */ +public class ImportXYZProcessor extends BaseStAXArtifactProcessor implements StAXArtifactProcessor<ImportXYZ> { + private static final String NS = "http://someuri"; + + private static final QName IMPORT = new QName(NS, "import.xyz"); + + private static final String URI = "uri"; + private static final String AN_ATTRIBUTE = "anAttribute"; + + private final ImportExportXYZFactory factory; + private final ExtensionFactory extensionFactory; + private final Monitor monitor; + + public ImportXYZProcessor(ModelFactoryExtensionPoint modelFactories, + Monitor monitor) { + this.factory = modelFactories.getFactory(ImportExportXYZFactory.class); + this.monitor = monitor; + this.extensionFactory = modelFactories.getFactory(ExtensionFactory.class); + } + + /** + * Report a warning. + * + * @param problems + * @param message + * @param model + */ + private void error(String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = new ProblemImpl(this.getClass().getName(), "contribution-namespace-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 = new ProblemImpl(this.getClass().getName(), "contribution-namespace-validation-messages", Severity.ERROR, model, message, ex); + monitor.problem(problem); + } + } + + public QName getArtifactType() { + return IMPORT; + } + + public Class<ImportXYZ> getModelType() { + return ImportXYZ.class; + } + + public ImportXYZ read(XMLStreamReader reader) throws ContributionReadException { + ImportXYZ importXYZ= this.factory.createImport(); + QName element; + + try { + while (reader.hasNext()) { + int event = reader.getEventType(); + switch (event) { + case START_ELEMENT: + element = reader.getName(); + + // Read <import> + if (IMPORT.equals(element)) { + String ns = reader.getAttributeValue(null, URI); + if (ns == null) { + error("AttributeNameSpaceMissing", reader); + } else { + importXYZ.setURI(ns); + } + + String anAttribute = reader.getAttributeValue(null, AN_ATTRIBUTE); + if (anAttribute != null) { + importXYZ.setAnAttribute(anAttribute); + } + } + break; + case XMLStreamConstants.END_ELEMENT: + if (IMPORT.equals(reader.getName())) { + return importXYZ; + } + break; + } + + // Read the next element + if (reader.hasNext()) { + reader.next(); + } + } + } + catch (XMLStreamException e) { + ContributionReadException ex = new ContributionReadException(e); + error("XMLStreamException", reader, ex); + } + + return importXYZ; + } + + public void write(ImportXYZ importXYZ, XMLStreamWriter writer) throws ContributionWriteException, XMLStreamException { + + // Write <import> + writer.writeStartElement(IMPORT.getNamespaceURI(), IMPORT.getLocalPart()); + + if (importXYZ.getURI() != null) { + writer.writeAttribute(URI, importXYZ.getURI()); + } + if (importXYZ.getAnAttribute() != null) { + writer.writeAttribute(AN_ATTRIBUTE, importXYZ.getAnAttribute()); + } + + writer.writeEndElement(); + } + + + public void resolve(ImportXYZ model, ModelResolver resolver) throws ContributionResolveException { + } +} |