summaryrefslogtreecommitdiffstats
path: root/java/sca/modules/contribution/src
diff options
context:
space:
mode:
authorrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2009-03-25 23:40:38 +0000
committerrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2009-03-25 23:40:38 +0000
commit1eafab3193550a62edf9a9c7c60c119796a9e7e8 (patch)
tree3b9da17022bb79565b4e24748ce8c34e87821cee /java/sca/modules/contribution/src
parent6b71f86309fc1c20aaac34c205fe783664a48477 (diff)
Match the document using wildcards against the artifact URI
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@758464 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/contribution/src')
-rw-r--r--java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultURLArtifactProcessorExtensionPoint.java204
-rw-r--r--java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXArtifactProcessor.java141
-rw-r--r--java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXAttributeProcessor.java197
-rw-r--r--java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleURLArtifactProcessor.java3
-rw-r--r--java/sca/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/processor/URLartifactProcessorExtensionPointTestCase.java15
5 files changed, 348 insertions, 212 deletions
diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultURLArtifactProcessorExtensionPoint.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultURLArtifactProcessorExtensionPoint.java
index 86206d4ab2..99b5b3d8b0 100644
--- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultURLArtifactProcessorExtensionPoint.java
+++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultURLArtifactProcessorExtensionPoint.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.processor;
@@ -24,6 +24,7 @@ import java.net.URI;
import java.net.URL;
import java.util.Map;
import java.util.Set;
+import java.util.regex.Pattern;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
@@ -39,16 +40,14 @@ import org.apache.tuscany.sca.monitor.MonitorFactory;
import org.apache.tuscany.sca.monitor.Problem;
import org.apache.tuscany.sca.monitor.Problem.Severity;
-
/**
* The default implementation of a URL artifact processor extension point.
- *
+ *
* @version $Rev$ $Date$
*/
-public class DefaultURLArtifactProcessorExtensionPoint
- extends DefaultArtifactProcessorExtensionPoint<URLArtifactProcessor>
- implements URLArtifactProcessorExtensionPoint {
-
+public class DefaultURLArtifactProcessorExtensionPoint extends
+ DefaultArtifactProcessorExtensionPoint<URLArtifactProcessor> implements URLArtifactProcessorExtensionPoint {
+
private ExtensionPointRegistry extensionPoints;
private StAXArtifactProcessor<?> staxProcessor;
private boolean loaded;
@@ -64,54 +63,132 @@ public class DefaultURLArtifactProcessorExtensionPoint
XMLOutputFactory outputFactory = modelFactories.getFactory(XMLOutputFactory.class);
UtilityExtensionPoint utilities = this.extensionPoints.getExtensionPoint(UtilityExtensionPoint.class);
MonitorFactory monitorFactory = utilities.getUtility(MonitorFactory.class);
- if (monitorFactory != null)
- this.monitor = monitorFactory.createMonitor();
- StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class);
+ if (monitorFactory != null)
+ this.monitor = monitorFactory.createMonitor();
+ StAXArtifactProcessorExtensionPoint staxProcessors =
+ extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class);
staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, outputFactory, this.monitor);
}
-
+
/**
* 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-validation-messages", Severity.ERROR, model, message, ex);
- monitor.problem(problem);
- }
+ Problem problem =
+ monitor.createProblem(this.getClass().getName(),
+ "contribution-validation-messages",
+ Severity.ERROR,
+ model,
+ message,
+ ex);
+ monitor.problem(problem);
+ }
}
-
+
public void addArtifactProcessor(URLArtifactProcessor artifactProcessor) {
if (artifactProcessor.getArtifactType() != null) {
- processorsByArtifactType.put((Object)artifactProcessor.getArtifactType(), artifactProcessor);
+ Pattern pattern = Pattern.compile(wildcard2regex(artifactProcessor.getArtifactType()));
+ processorsByArtifactType.put(pattern, artifactProcessor);
}
if (artifactProcessor.getModelType() != null) {
processorsByModelType.put(artifactProcessor.getModelType(), artifactProcessor);
}
}
-
+
public void removeArtifactProcessor(URLArtifactProcessor artifactProcessor) {
if (artifactProcessor.getArtifactType() != null) {
+ String regex = wildcard2regex(artifactProcessor.getArtifactType());
+ for (Object key : processorsByArtifactType.keySet()) {
+ if ((key instanceof Pattern) && ((Pattern)key).pattern().equals(regex)) {
+ processorsByArtifactType.remove(key);
+ }
+ }
processorsByArtifactType.remove((Object)artifactProcessor.getArtifactType());
}
if (artifactProcessor.getModelType() != null) {
processorsByModelType.remove(artifactProcessor.getModelType());
}
}
-
+
@Override
public URLArtifactProcessor getProcessor(Class<?> modelType) {
loadProcessors();
return super.getProcessor(modelType);
}
-
+
@Override
public URLArtifactProcessor getProcessor(Object artifactType) {
loadProcessors();
- return super.getProcessor(artifactType);
+ URLArtifactProcessor processor = null;
+ String uri = (String)artifactType;
+ if (uri.endsWith("/")) {
+ // Ignore directories
+ return null;
+ }
+ if (!uri.startsWith("/")) {
+ uri = "/" + uri;
+ }
+ for (Object key : processorsByArtifactType.keySet()) {
+ Pattern pattern = (Pattern)key;
+ if (pattern.matcher(uri).matches()) {
+ return processorsByArtifactType.get(key);
+ }
+ }
+ return processor;
+ }
+
+ private String wildcard2regex(String wildcard) {
+ if (wildcard.startsWith(".")) {
+ // File extension
+ wildcard = "**/*" + wildcard;
+ } else if (wildcard.indexOf('/') == -1) {
+ // file name
+ wildcard = "**/" + wildcard;
+ } else if (!(wildcard.startsWith("/") || wildcard.startsWith("**"))) {
+ wildcard = '/' + wildcard;
+ }
+ StringBuffer regex = new StringBuffer();
+ char[] chars = wildcard.toCharArray();
+ for (int i = 0; i < chars.length; i++) {
+ switch (chars[i]) {
+ case '*':
+ if (i < chars.length - 1 && chars[i + 1] == '*') {
+ regex.append(".*");
+ i++; // Skip next *
+ } else {
+ regex.append("[^/]*");
+ }
+ break;
+ case '?':
+ regex.append("[^/]");
+ break;
+ case '\\':
+ case '|':
+ case '(':
+ case ')':
+ // case '[':
+ // case ']':
+ // case '{':
+ // case '}':
+ case '^':
+ case '$':
+ case '+':
+ case '.':
+ case '<':
+ case '>':
+ regex.append("\\").append(chars[i]);
+ break;
+ default:
+ regex.append(chars[i]);
+ break;
+ }
+ }
+ return regex.toString();
}
/**
@@ -122,27 +199,29 @@ public class DefaultURLArtifactProcessorExtensionPoint
return;
// Get the processor service declarations
- Set<ServiceDeclaration> processorDeclarations;
+ Set<ServiceDeclaration> processorDeclarations;
try {
- processorDeclarations = ServiceDiscovery.getInstance().getServiceDeclarations(URLArtifactProcessor.class.getName());
+ processorDeclarations =
+ ServiceDiscovery.getInstance().getServiceDeclarations(URLArtifactProcessor.class.getName());
} catch (IOException e) {
- IllegalStateException ie = new IllegalStateException(e);
- error("IllegalStateException", staxProcessor, ie);
+ IllegalStateException ie = new IllegalStateException(e);
+ error("IllegalStateException", staxProcessor, ie);
throw ie;
}
-
- for (ServiceDeclaration processorDeclaration: processorDeclarations) {
+
+ for (ServiceDeclaration processorDeclaration : processorDeclarations) {
Map<String, String> attributes = processorDeclaration.getAttributes();
// Load a URL artifact processor
String artifactType = attributes.get("type");
String modelTypeName = attributes.get("model");
-
+
// Create a processor wrapper and register it
- URLArtifactProcessor processor = new LazyURLArtifactProcessor(artifactType, modelTypeName,
- processorDeclaration, extensionPoints, staxProcessor, monitor);
+ URLArtifactProcessor processor =
+ new LazyURLArtifactProcessor(artifactType, modelTypeName, processorDeclaration, extensionPoints,
+ staxProcessor, monitor);
addArtifactProcessor(processor);
}
-
+
loaded = true;
}
@@ -160,13 +239,13 @@ public class DefaultURLArtifactProcessorExtensionPoint
private Class<?> modelType;
private StAXArtifactProcessor<?> staxProcessor;
private Monitor monitor;
-
- LazyURLArtifactProcessor(String artifactType,
- String modelTypeName,
- ServiceDeclaration processorDeclaration,
- ExtensionPointRegistry extensionPoints,
- StAXArtifactProcessor<?> staxProcessor,
- Monitor monitor) {
+
+ LazyURLArtifactProcessor(String artifactType,
+ String modelTypeName,
+ ServiceDeclaration processorDeclaration,
+ ExtensionPointRegistry extensionPoints,
+ StAXArtifactProcessor<?> staxProcessor,
+ Monitor monitor) {
this.artifactType = artifactType;
this.modelTypeName = modelTypeName;
this.processorDeclaration = processorDeclaration;
@@ -178,35 +257,50 @@ public class DefaultURLArtifactProcessorExtensionPoint
public String getArtifactType() {
return artifactType;
}
-
+
private void error(String message, Object model, Exception ex) {
if (monitor != null) {
- Problem problem = monitor.createProblem(this.getClass().getName(), "contribution-validation-messages", Severity.ERROR, model, message, ex);
- monitor.problem(problem);
- }
+ Problem problem =
+ monitor.createProblem(this.getClass().getName(),
+ "contribution-validation-messages",
+ Severity.ERROR,
+ model,
+ message,
+ ex);
+ monitor.problem(problem);
+ }
}
-
+
@SuppressWarnings("unchecked")
private URLArtifactProcessor getProcessor() {
if (processor == null) {
try {
- FactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class);
- Class<URLArtifactProcessor> processorClass = (Class<URLArtifactProcessor>)processorDeclaration.loadClass();
+ FactoryExtensionPoint modelFactories =
+ extensionPoints.getExtensionPoint(FactoryExtensionPoint.class);
+ Class<URLArtifactProcessor> processorClass =
+ (Class<URLArtifactProcessor>)processorDeclaration.loadClass();
try {
- Constructor<URLArtifactProcessor> constructor = processorClass.getConstructor(FactoryExtensionPoint.class, Monitor.class);
+ Constructor<URLArtifactProcessor> constructor =
+ processorClass.getConstructor(FactoryExtensionPoint.class, Monitor.class);
processor = constructor.newInstance(modelFactories, monitor);
} catch (NoSuchMethodException e) {
try {
- Constructor<URLArtifactProcessor> constructor = processorClass.getConstructor(FactoryExtensionPoint.class, StAXArtifactProcessor.class, Monitor.class);
+ Constructor<URLArtifactProcessor> constructor =
+ processorClass.getConstructor(FactoryExtensionPoint.class,
+ StAXArtifactProcessor.class,
+ Monitor.class);
processor = constructor.newInstance(modelFactories, staxProcessor, monitor);
} catch (NoSuchMethodException e2) {
- Constructor<URLArtifactProcessor> constructor = processorClass.getConstructor(ExtensionPointRegistry.class, StAXArtifactProcessor.class, Monitor.class);
+ Constructor<URLArtifactProcessor> constructor =
+ processorClass.getConstructor(ExtensionPointRegistry.class,
+ StAXArtifactProcessor.class,
+ Monitor.class);
processor = constructor.newInstance(extensionPoints, staxProcessor, monitor);
}
}
} catch (Exception e) {
- IllegalStateException ie = new IllegalStateException(e);
- error("IllegalStateException", processor, ie);
+ IllegalStateException ie = new IllegalStateException(e);
+ error("IllegalStateException", processor, ie);
throw ie;
}
}
@@ -216,14 +310,14 @@ public class DefaultURLArtifactProcessorExtensionPoint
public Object read(URL contributionURL, URI artifactURI, URL artifactURL) throws ContributionReadException {
return getProcessor().read(contributionURL, artifactURI, artifactURL);
}
-
+
public Class<?> getModelType() {
if (modelTypeName != null && modelType == null) {
try {
modelType = processorDeclaration.loadClass(modelTypeName);
} catch (ClassNotFoundException e) {
- IllegalStateException ie = new IllegalStateException(e);
- error("IllegalStateException", processorDeclaration, ie);
+ IllegalStateException ie = new IllegalStateException(e);
+ error("IllegalStateException", processorDeclaration, ie);
throw ie;
}
}
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 7578a24ea8..35793899c0 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
@@ -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.processor;
@@ -41,16 +41,16 @@ import org.apache.tuscany.sca.monitor.Problem.Severity;
/**
* Implementation of an extensible StAX artifact processor.
- *
+ *
* Takes a StAXArtifactProcessorExtensionPoint and delegates to the proper
* StAXArtifactProcessor by element QName
- *
+ *
* @version $Rev$ $Date$
*/
public class ExtensibleStAXArtifactProcessor implements StAXArtifactProcessor<Object> {
- private static final Logger logger = Logger.getLogger(ExtensibleStAXArtifactProcessor.class.getName());
-
- private static final QName ANY_ELEMENT = new QName(Constants.XMLSCHEMA_NS, "anyElement");
+ private static final Logger logger = Logger.getLogger(ExtensibleStAXArtifactProcessor.class.getName());
+
+ private static final QName ANY_ELEMENT = new QName(Constants.XMLSCHEMA_NS, "anyElement");
private XMLInputFactory inputFactory;
private XMLOutputFactory outputFactory;
@@ -63,10 +63,10 @@ public class ExtensibleStAXArtifactProcessor implements StAXArtifactProcessor<Ob
* @param inputFactory
* @param outputFactory
*/
- public ExtensibleStAXArtifactProcessor(StAXArtifactProcessorExtensionPoint processors,
- XMLInputFactory inputFactory,
- XMLOutputFactory outputFactory,
- Monitor monitor) {
+ public ExtensibleStAXArtifactProcessor(StAXArtifactProcessorExtensionPoint processors,
+ XMLInputFactory inputFactory,
+ XMLOutputFactory outputFactory,
+ Monitor monitor) {
super();
this.processors = processors;
this.inputFactory = inputFactory;
@@ -76,52 +76,69 @@ public class ExtensibleStAXArtifactProcessor implements StAXArtifactProcessor<Ob
}
this.monitor = monitor;
}
-
+
/**
* Report a warning.
- *
+ *
* @param problems
* @param message
* @param model
*/
- private void warning(String message, Object model, Object... messageParameters) {
- if (monitor != null) {
- Problem problem = monitor.createProblem(this.getClass().getName(), "contribution-validation-messages", Severity.WARNING, model, message, (Object[])messageParameters);
- monitor.problem(problem);
- }
- }
-
- /**
- * 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-validation-messages", Severity.ERROR, model, message, (Object[])messageParameters);
- monitor.problem(problem);
- }
- }
-
+ private void warning(String message, Object model, Object... messageParameters) {
+ if (monitor != null) {
+ Problem problem =
+ monitor.createProblem(this.getClass().getName(),
+ "contribution-validation-messages",
+ Severity.WARNING,
+ model,
+ message,
+ (Object[])messageParameters);
+ monitor.problem(problem);
+ }
+ }
+
/**
- * Report a exception.
- *
+ * Report a error.
+ *
* @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-validation-messages", Severity.ERROR, model, message, ex);
- monitor.problem(problem);
- }
- }
+ private void error(String message, Object model, Object... messageParameters) {
+ if (monitor != null) {
+ Problem problem =
+ monitor.createProblem(this.getClass().getName(),
+ "contribution-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-validation-messages",
+ Severity.ERROR,
+ model,
+ message,
+ ex);
+ monitor.problem(problem);
+ }
+ }
public Object read(XMLStreamReader source) throws ContributionReadException, XMLStreamException {
-
+
// Delegate to the processor associated with the element QName
int event = source.getEventType();
if (event == XMLStreamConstants.START_DOCUMENT) {
@@ -130,25 +147,25 @@ public class ExtensibleStAXArtifactProcessor implements StAXArtifactProcessor<Ob
QName name = source.getName();
StAXArtifactProcessor<?> processor = (StAXArtifactProcessor<?>)processors.getProcessor(name);
if (processor == null) {
- Location location = source.getLocation();
- if (logger.isLoggable(Level.WARNING)) {
+ Location location = source.getLocation();
+ if (logger.isLoggable(Level.WARNING)) {
logger.warning("Element " + name + " cannot be processed. (" + location + ")");
}
warning("ElementCannotBeProcessed", processors, name, location);
StAXArtifactProcessor anyElementProcessor = processors.getProcessor(ANY_ELEMENT);
- if(anyElementProcessor != null) {
- return anyElementProcessor.read(source);
+ if (anyElementProcessor != null) {
+ return anyElementProcessor.read(source);
} else {
- return null;
+ return null;
}
}
return processor.read(source);
}
-
+
@SuppressWarnings("unchecked")
public void write(Object model, XMLStreamWriter outputSource) throws ContributionWriteException, XMLStreamException {
-
+
// Delegate to the processor associated with the model type
if (model != null) {
StAXArtifactProcessor processor = processors.getProcessor(model.getClass());
@@ -158,15 +175,17 @@ public class ExtensibleStAXArtifactProcessor implements StAXArtifactProcessor<Ob
if (logger.isLoggable(Level.WARNING)) {
logger.warning("No StAX processor is configured to handle " + model.getClass());
}
- warning("NoStaxProcessor", processors, model.getClass());
+ if (!XMLStreamReader.class.isInstance(model)) {
+ warning("NoStaxProcessor", processors, model.getClass());
+ }
StAXArtifactProcessor anyElementProcessor = processors.getProcessor(ANY_ELEMENT);
- if(anyElementProcessor != null) {
- anyElementProcessor.write(model, outputSource);
+ if (anyElementProcessor != null) {
+ anyElementProcessor.write(model, outputSource);
}
}
}
}
-
+
@SuppressWarnings("unchecked")
public void resolve(Object model, ModelResolver resolver) throws ContributionResolveException {
@@ -178,7 +197,7 @@ public class ExtensibleStAXArtifactProcessor implements StAXArtifactProcessor<Ob
}
}
}
-
+
/**
* Read a model from an InputStream.
* @param is The artifact InputStream
@@ -198,8 +217,8 @@ public class ExtensibleStAXArtifactProcessor implements StAXArtifactProcessor<Ob
if (type.isInstance(mo)) {
return type.cast(mo);
} else {
- error("UnrecognizedElementException", reader, name);
- UnrecognizedElementException e = new UnrecognizedElementException(name);
+ error("UnrecognizedElementException", reader, name);
+ UnrecognizedElementException e = new UnrecognizedElementException(name);
throw e;
}
} catch (ContributionReadException e) {
@@ -242,8 +261,8 @@ public class ExtensibleStAXArtifactProcessor implements StAXArtifactProcessor<Ob
writer.flush();
writer.close();
} catch (XMLStreamException e) {
- ContributionWriteException cw = new ContributionWriteException(e);
- error("ContributionWriteException", outputFactory, cw);
+ ContributionWriteException cw = new ContributionWriteException(e);
+ error("ContributionWriteException", outputFactory, cw);
throw cw;
}
}
@@ -251,7 +270,7 @@ public class ExtensibleStAXArtifactProcessor implements StAXArtifactProcessor<Ob
public QName getArtifactType() {
return null;
}
-
+
public Class<Object> getModelType() {
return null;
}
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 c336cb3431..9a7ba07261 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
@@ -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.processor;
@@ -32,6 +32,7 @@ import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
+import org.apache.tuscany.sca.assembly.Extension;
import org.apache.tuscany.sca.contribution.Constants;
import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
import org.apache.tuscany.sca.monitor.Monitor;
@@ -40,19 +41,18 @@ import org.apache.tuscany.sca.monitor.Problem.Severity;
/**
* Implementation of an extensible StAX attribute processor.
- *
+ *
* Takes a StAXAttributeProcessorExtensionPoint and delegates to the proper
* StAXAttributeProcessor by attribute QName
- *
+ *
* @version $Rev$ $Date$
*/
-public class ExtensibleStAXAttributeProcessor
- implements StAXAttributeProcessor<Object> {
+public class ExtensibleStAXAttributeProcessor implements StAXAttributeProcessor<Object> {
+
+ private static final Logger logger = Logger.getLogger(ExtensibleStAXAttributeProcessor.class.getName());
- private static final Logger logger = Logger.getLogger(ExtensibleStAXAttributeProcessor.class.getName());
-
private static final QName ANY_ATTRIBUTE = new QName(Constants.XMLSCHEMA_NS, "anyAttribute");
-
+
private XMLInputFactory inputFactory;
private XMLOutputFactory outputFactory;
private StAXAttributeProcessorExtensionPoint processors;
@@ -64,10 +64,10 @@ public class ExtensibleStAXAttributeProcessor
* @param inputFactory
* @param outputFactory
*/
- public ExtensibleStAXAttributeProcessor(StAXAttributeProcessorExtensionPoint processors,
- XMLInputFactory inputFactory,
- XMLOutputFactory outputFactory,
- Monitor monitor) {
+ public ExtensibleStAXAttributeProcessor(StAXAttributeProcessorExtensionPoint processors,
+ XMLInputFactory inputFactory,
+ XMLOutputFactory outputFactory,
+ Monitor monitor) {
super();
this.processors = processors;
this.inputFactory = inputFactory;
@@ -77,51 +77,69 @@ public class ExtensibleStAXAttributeProcessor
}
this.monitor = monitor;
}
-
+
/**
* Report a warning.
- *
+ *
* @param problems
* @param message
* @param model
*/
- private void warning(String message, Object model, Object... messageParameters) {
- if (monitor != null) {
- Problem problem = monitor.createProblem(this.getClass().getName(), "contribution-validation-messages", Severity.WARNING, model, message, (Object[])messageParameters);
- monitor.problem(problem);
- }
- }
-
- /**
- * 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-validation-messages", Severity.ERROR, model, message, (Object[])messageParameters);
- monitor.problem(problem);
- }
- }
-
+ private void warning(String message, Object model, Object... messageParameters) {
+ if (monitor != null) {
+ Problem problem =
+ monitor.createProblem(this.getClass().getName(),
+ "contribution-validation-messages",
+ Severity.WARNING,
+ model,
+ message,
+ (Object[])messageParameters);
+ monitor.problem(problem);
+ }
+ }
+
/**
- * Report a exception.
- *
+ * Report a error.
+ *
* @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-validation-messages", Severity.ERROR, model, message, ex);
- monitor.problem(problem);
- }
- }
+ private void error(String message, Object model, Object... messageParameters) {
+ if (monitor != null) {
+ Problem problem =
+ monitor.createProblem(this.getClass().getName(),
+ "contribution-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-validation-messages",
+ Severity.ERROR,
+ model,
+ message,
+ ex);
+ monitor.problem(problem);
+ }
+ }
- public Object read(QName attributeName, XMLStreamReader source) throws ContributionReadException, XMLStreamException {
+ public Object read(QName attributeName, XMLStreamReader source) throws ContributionReadException,
+ XMLStreamException {
// Delegate to the processor associated with the attribute QName
int event = source.getEventType();
if (event == XMLStreamConstants.START_DOCUMENT) {
@@ -129,79 +147,78 @@ public class ExtensibleStAXAttributeProcessor
}
StAXAttributeProcessor<?> processor = null;
-
+
//lookup for registered attribute processors
processor = (StAXAttributeProcessor<?>)processors.getProcessor(attributeName);
if (processor == null) {
- Location location = source.getLocation();
- if (logger.isLoggable(Level.WARNING)) {
+ Location location = source.getLocation();
+ if (logger.isLoggable(Level.WARNING)) {
logger.warning("Attribute " + attributeName + " cannot be processed. (" + location + ")");
}
- warning("AttributeCannotBeProcessed", processors, attributeName, location);
+ warning("AttributeCannotBeProcessed", processors, attributeName, location);
} else {
- return processor.read(attributeName, source);
+ return processor.read(attributeName, source);
}
-
-
+
//handle extension attributes without processors
processor = (StAXAttributeProcessor<?>)processors.getProcessor(ANY_ATTRIBUTE);
if (processor == null) {
- Location location = source.getLocation();
- if (logger.isLoggable(Level.WARNING)) {
+ Location location = source.getLocation();
+ if (logger.isLoggable(Level.WARNING)) {
logger.warning("Could not find Default Attribute processor !");
}
- warning("DefaultAttributeProcessorNotAvailable", processors, ANY_ATTRIBUTE, location);
- }
-
+ warning("DefaultAttributeProcessorNotAvailable", processors, ANY_ATTRIBUTE, location);
+ }
+
return processor == null ? null : processor.read(attributeName, source);
}
-
+
@SuppressWarnings("unchecked")
public void write(Object model, XMLStreamWriter outputSource) throws ContributionWriteException, XMLStreamException {
- if(model == null) {
- return;
- }
-
+ if (model == null) {
+ return;
+ }
+
// Delegate to the processor associated with the model type
- StAXAttributeProcessor processor = processors.getProcessor(model.getClass());
- if(processor == null) {
- if (logger.isLoggable(Level.WARNING)) {
- logger.warning("No StAX processor is configured to handle " + model.getClass());
- }
- warning("NoStaxProcessor", processors, model.getClass());
- } else {
- processor.write(model, outputSource);
- return;
- }
-
- //handle extension attributes without processors
+ StAXAttributeProcessor processor = processors.getProcessor(model.getClass());
+ if (processor == null) {
+ if (!Extension.class.isInstance(model)) {
+ if (logger.isLoggable(Level.WARNING)) {
+ logger.warning("No StAX processor is configured to handle " + model.getClass());
+ }
+ warning("NoStaxProcessor", processors, model.getClass());
+ }
+ } else {
+ processor.write(model, outputSource);
+ return;
+ }
+
+ //handle extension attributes without processors
processor = (StAXAttributeProcessor<?>)processors.getProcessor(ANY_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());
+ 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;
+ processor.write(model, outputSource);
+ return;
}
}
-
-
-
+
@SuppressWarnings("unchecked")
public void resolve(Object model, ModelResolver resolver) throws ContributionResolveException {
// Delegate to the processor associated with the model type
if (model != null) {
- StAXAttributeProcessor processor = processors.getProcessor(model.getClass());
+ StAXAttributeProcessor processor = processors.getProcessor(model.getClass());
if (processor != null) {
processor.resolve(model, resolver);
}
}
}
-
+
/**
* Read a model from an InputStream.
* @param is The artifact InputStream
@@ -226,8 +243,8 @@ public class ExtensibleStAXAttributeProcessor
writer.flush();
writer.close();
} catch (XMLStreamException e) {
- ContributionWriteException cw = new ContributionWriteException(e);
- error("ContributionWriteException", outputFactory, cw);
+ ContributionWriteException cw = new ContributionWriteException(e);
+ error("ContributionWriteException", outputFactory, cw);
throw cw;
}
}
@@ -235,7 +252,7 @@ public class ExtensibleStAXAttributeProcessor
public QName getArtifactType() {
return null;
}
-
+
public Class<Object> getModelType() {
return null;
}
diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleURLArtifactProcessor.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleURLArtifactProcessor.java
index d867667eae..7b4a7cff3a 100644
--- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleURLArtifactProcessor.java
+++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleURLArtifactProcessor.java
@@ -81,6 +81,8 @@ public class ExtensibleURLArtifactProcessor implements URLArtifactProcessor<Obje
// Register the URI as the artifact type starts with /
processor = (URLArtifactProcessor<Object>)processors.getProcessor(uri);
}
+
+ /*
if (processor == null) {
// Delegate to the processor associated with file extension
String fileName = getFileName(sourceURL);
@@ -101,6 +103,7 @@ public class ExtensibleURLArtifactProcessor implements URLArtifactProcessor<Obje
processor = (URLArtifactProcessor<Object>)processors.getProcessor(extension);
}
}
+ */
if (processor == null) {
return null;
diff --git a/java/sca/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/processor/URLartifactProcessorExtensionPointTestCase.java b/java/sca/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/processor/URLartifactProcessorExtensionPointTestCase.java
index 3b99cc0312..3a18e2b987 100644
--- a/java/sca/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/processor/URLartifactProcessorExtensionPointTestCase.java
+++ b/java/sca/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/processor/URLartifactProcessorExtensionPointTestCase.java
@@ -6,20 +6,21 @@
* 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.processor;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import java.net.URI;
import java.net.URL;
@@ -33,7 +34,7 @@ import org.junit.Test;
/**
* URL Artifact Processor Extension Point test case
* Verifies the right registration and lookup for processors that handle filename and file types
- *
+ *
* @version $Rev$ $Date$
*/
public class URLartifactProcessorExtensionPointTestCase {
@@ -50,13 +51,15 @@ public class URLartifactProcessorExtensionPointTestCase {
@Test
public final void testFileTypeProcessor() {
- assertNotNull(artifactProcessors.getProcessor(".m1"));
+ assertNotNull(artifactProcessors.getProcessor("dir1/file1.m1"));
+ assertNotNull(artifactProcessors.getProcessor("file1.m1"));
}
@Test
public final void testFileNameProcessor() {
assertNotNull(artifactProcessors.getProcessor("file.m2"));
-
+ assertNotNull(artifactProcessors.getProcessor("dir1/file.m2"));
+ assertNull(artifactProcessors.getProcessor("onefile.m2"));
}
/**