diff options
Diffstat (limited to '')
22 files changed, 402 insertions, 41 deletions
diff --git a/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JSONHelper.java b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JSONHelper.java index cdcad3a472..028690a789 100644 --- a/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JSONHelper.java +++ b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/JSONHelper.java @@ -19,11 +19,14 @@ package org.apache.tuscany.sca.databinding.json; +import java.util.Collection; + import org.apache.tuscany.sca.databinding.json.jackson.JacksonHelper; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.JsonParser; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.json.JSONArray; /** * @version $Rev$ $Date$ @@ -79,19 +82,22 @@ public class JSONHelper { return json; } - public static <T> T toJSON(String json, Class<T> type) { + public static Object toJSON(String json, Class<?> type) { if (type == JSONObject.class) { try { - return type.cast(new JSONObject(json)); + return new JSONObject(json); } catch (JSONException e) { throw new IllegalArgumentException(e); } } else { if (type == null) { - type = (Class<T>)org.json.JSONObject.class; + type = org.json.JSONObject.class; } try { - return type.cast(new org.json.JSONObject(json)); + if (type == JSONArray.class || type.isArray() || Collection.class.isAssignableFrom(type)) { + return new JSONArray(json); + } + return new org.json.JSONObject(json); } catch (org.json.JSONException e) { throw new IllegalArgumentException(e); } diff --git a/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/InputStream2JSON.java b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/InputStream2JSON.java new file mode 100644 index 0000000000..57b9d55b44 --- /dev/null +++ b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/InputStream2JSON.java @@ -0,0 +1,49 @@ +/* + * 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.databinding.json.jackson; + +import java.io.InputStream; + +import org.apache.tuscany.sca.databinding.PullTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.json.JSONDataBinding; + +/** + * + */ +public class InputStream2JSON implements PullTransformer<InputStream, Object> { + + public String getSourceDataBinding() { + return "application/json" + "#" + InputStream.class.getName(); + } + + public String getTargetDataBinding() { + return JSONDataBinding.NAME; + } + + public int getWeight() { + return 10; + } + + public Object transform(InputStream source, TransformationContext context) { + return JacksonHelper.createJsonParser(source); + } + +} diff --git a/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2Object.java b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2Object.java index 5edcc6701e..3620fc117f 100644 --- a/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2Object.java +++ b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2Object.java @@ -54,8 +54,9 @@ public class JSON2Object implements PullTransformer<Object, Object> { return mapper.treeToValue((JsonNode)source, context.getTargetDataType().getPhysical()); } else if (source instanceof JsonParser) { return mapper.readValue((JsonParser)source, javaType); + } else { + return mapper.readValue(source.toString(), javaType); } - return null; } catch (Exception e) { throw new TransformationException(e); } diff --git a/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2OutputStream.java b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2OutputStream.java new file mode 100644 index 0000000000..920485a540 --- /dev/null +++ b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JSON2OutputStream.java @@ -0,0 +1,65 @@ +/* + * 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.databinding.json.jackson; + +import java.io.OutputStream; + +import org.apache.tuscany.sca.databinding.PushTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.TransformationException; +import org.apache.tuscany.sca.databinding.json.JSONDataBinding; +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.JsonParser; + +/** + * + */ +public class JSON2OutputStream implements PushTransformer<Object, OutputStream> { + + public String getSourceDataBinding() { + return JSONDataBinding.NAME; + } + + public String getTargetDataBinding() { + return "application/json" + "#" + OutputStream.class.getName(); + } + + public void transform(Object source, OutputStream sink, TransformationContext context) { + if (source == null) { + return; + } + if (source instanceof JsonNode) { + JacksonHelper.write((JsonNode)source, sink); + } else if (source instanceof JsonParser) { + JacksonHelper.write((JsonParser)source, sink); + } else { + try { + sink.write(source.toString().getBytes("UTF-8")); + } catch (Exception e) { + throw new TransformationException(e); + } + } + } + + public int getWeight() { + return 50; + } + +} diff --git a/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java index 1e3399017c..ae3bb8fd48 100644 --- a/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java +++ b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java @@ -20,13 +20,18 @@ package org.apache.tuscany.sca.databinding.json.jackson; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; import java.io.StringWriter; +import org.codehaus.jackson.JsonEncoding; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.map.AnnotationIntrospector; +import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.introspect.JacksonAnnotationIntrospector; import org.codehaus.jackson.xc.JaxbAnnotationIntrospector; @@ -41,6 +46,8 @@ public class JacksonHelper { AnnotationIntrospector secondary = new JacksonAnnotationIntrospector(); AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary); mapper.getDeserializationConfig().setAnnotationIntrospector(pair); + // [rfeng] To avoid complaints about javaClass + mapper.getDeserializationConfig().set(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.FALSE); mapper.getSerializationConfig().setAnnotationIntrospector(pair); return mapper; } @@ -79,4 +86,43 @@ public class JacksonHelper { } } + public static JsonParser createJsonParser(InputStream content) { + JsonFactory jsonFactory = new JsonFactory(); + try { + return jsonFactory.createJsonParser(content); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } + + public static JsonParser createJsonParser(Reader content) { + JsonFactory jsonFactory = new JsonFactory(); + try { + return jsonFactory.createJsonParser(content); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } + + public static void write(JsonNode node, OutputStream out) { + try { + JsonFactory jsonFactory = new JsonFactory(); + JsonGenerator generator = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8); + generator.writeTree(node); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } + + public static void write(JsonParser parser, OutputStream out) { + try { + JsonFactory jsonFactory = new JsonFactory(); + JsonGenerator generator = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8); + JsonNode node = parser.readValueAs(JsonNode.class); + generator.writeTree(node); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } + } diff --git a/sca-java-2.x/trunk/modules/databinding-json/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.PullTransformer b/sca-java-2.x/trunk/modules/databinding-json/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.PullTransformer index 800e4a43fb..3590a46c47 100644 --- a/sca-java-2.x/trunk/modules/databinding-json/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.PullTransformer +++ b/sca-java-2.x/trunk/modules/databinding-json/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.PullTransformer @@ -18,15 +18,17 @@ # Implementation classes for the transformers org.apache.tuscany.sca.databinding.json.JSON2XMLStreamReader;source=JSON,target=javax.xml.stream.XMLStreamReader,weight=5000 org.apache.tuscany.sca.databinding.json.XMLStreamReader2JSON;source=javax.xml.stream.XMLStreamReader,target=JSON,weight=5000,public=false -org.apache.tuscany.sca.databinding.json.JavaBean2JSON;source=java:complexType,target=JSON,weight=80000,public=false -org.apache.tuscany.sca.databinding.json.JavaBean2JSON;source=java:simpleType,target=JSON,weight=80000,public=false +org.apache.tuscany.sca.databinding.json.jackson.Object2JSON;source=java:complexType,target=JSON,weight=90000,public=true +org.apache.tuscany.sca.databinding.json.jackson.Object2JSON;source=java:simpleType,target=JSON,weight=90000,public=false org.apache.tuscany.sca.databinding.json.axiom.JSON2OMElement;source=JSON,target=org.apache.axiom.om.OMElement,weight=500 -org.apache.tuscany.sca.databinding.json.JavaBean2JSON;source=java:array,target=JSON,weight=80000,public=false -org.apache.tuscany.sca.databinding.json.JavaBean2JSON;source=commonj.sdo.DataObject,target=JSON,weight=80000,public=false -org.apache.tuscany.sca.databinding.json.JavaBean2JSON;source=javax.xml.bind.JAXBElement,target=JSON,weight=80000,public=false -org.apache.tuscany.sca.databinding.json.JSON2JavaBean;source=JSON,target=java:complexType,weight=80000,public=false -org.apache.tuscany.sca.databinding.json.JSON2JavaBean;source=JSON,target=java:simpleType,weight=80000,public=false -org.apache.tuscany.sca.databinding.json.JSON2JavaBean;source=JSON,target=commonj.sdo.DataObject,weight=80000,public=false -org.apache.tuscany.sca.databinding.json.JSON2JavaBean;source=JSON,target=javax.xml.bind.JAXBElement,weight=80000,public=false -org.apache.tuscany.sca.databinding.json.JSON2JavaBean;source=JSON,target=java:array,weight=80000,public=false +org.apache.tuscany.sca.databinding.json.jackson.Object2JSON;source=java:array,target=JSON,weight=90000,public=false +org.apache.tuscany.sca.databinding.json.jackson.Object2JSON;source=commonj.sdo.DataObject,target=JSON,weight=90000,public=false +org.apache.tuscany.sca.databinding.json.jackson.Object2JSON;source=javax.xml.bind.JAXBElement,target=JSON,weight=90000,public=false +org.apache.tuscany.sca.databinding.json.jackson.JSON2Object;source=JSON,target=java:complexType,weight=90000,public=false +org.apache.tuscany.sca.databinding.json.jackson.JSON2Object;source=JSON,target=java:simpleType,weight=90000,public=false +org.apache.tuscany.sca.databinding.json.jackson.JSON2Object;source=JSON,target=commonj.sdo.DataObject,weight=90000,public=false +org.apache.tuscany.sca.databinding.json.jackson.JSON2Object;source=JSON,target=javax.xml.bind.JAXBElement,weight=90000,public=false +org.apache.tuscany.sca.databinding.json.jackson.JSON2Object;source=JSON,target=java:array,weight=90000,public=false + +org.apache.tuscany.sca.databinding.json.jackson.InputStream2JSON;source=application/json#java.io.InputStream;target=JSON,weight=50,public=true diff --git a/sca-java-2.x/trunk/modules/databinding-json/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.PushTransformer b/sca-java-2.x/trunk/modules/databinding-json/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.PushTransformer new file mode 100644 index 0000000000..6fb438eec7 --- /dev/null +++ b/sca-java-2.x/trunk/modules/databinding-json/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.PushTransformer @@ -0,0 +1,34 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +org.apache.tuscany.sca.databinding.json.jackson.JSON2OutputStream;source=JSON;target=application/json#java.io.OutputStream,weight=50,public=true +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +org.apache.tuscany.sca.databinding.json.jackson.JSON2OutputStream;source=JSON;target=application/json#java.io.OutputStream,weight=50,public=true
\ No newline at end of file diff --git a/sca-java-2.x/trunk/modules/databinding-sdo/src/test/java/org/apache/tuscany/sca/databinding/sdo/DataObject2XMLStreamReaderTestCase.java b/sca-java-2.x/trunk/modules/databinding-sdo/src/test/java/org/apache/tuscany/sca/databinding/sdo/DataObject2XMLStreamReaderTestCase.java index d1b272b1b3..c2a9f46059 100644 --- a/sca-java-2.x/trunk/modules/databinding-sdo/src/test/java/org/apache/tuscany/sca/databinding/sdo/DataObject2XMLStreamReaderTestCase.java +++ b/sca-java-2.x/trunk/modules/databinding-sdo/src/test/java/org/apache/tuscany/sca/databinding/sdo/DataObject2XMLStreamReaderTestCase.java @@ -68,7 +68,7 @@ public class DataObject2XMLStreamReaderTestCase extends SDOTransformerTestCaseBa XMLStreamReader2Node t2 = new XMLStreamReader2Node(new DefaultExtensionPointRegistry()); org.w3c.dom.Node node = t2.transform(reader, context); assertNotNull(node); - Node2String t3 = new Node2String(); + Node2String t3 = new Node2String(new DefaultExtensionPointRegistry()); String xml = t3.transform(node, context); assertTrue(xml.contains("xmlns:xsi")); } diff --git a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/DataBindingContext.java b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/DataBindingContext.java new file mode 100644 index 0000000000..c77d0ad0f6 --- /dev/null +++ b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/DataBindingContext.java @@ -0,0 +1,95 @@ +/* + * 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.databinding; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import org.apache.tuscany.sca.interfacedef.Operation; + +/** + * The context for databinding processing + */ +public class DataBindingContext { + + private String contentType; + private Class<?> type; + private Type genericType; + private Annotation[] annotations; + private Operation operation; + + public DataBindingContext(Class<?> type, + Type genericType, + Annotation[] annotations, + Operation operation, + String contentType) { + super(); + this.type = type; + this.genericType = genericType; + this.annotations = annotations; + this.operation = operation; + this.contentType = contentType; + } + + public DataBindingContext(Class<?> type, Type genericType, Annotation[] annotations) { + super(); + this.type = type; + this.genericType = genericType; + this.annotations = annotations; + } + + public DataBindingContext(Class<?> type) { + super(); + this.type = type; + this.genericType = type; + } + + public Class<?> getType() { + return type; + } + + public Type getGenericType() { + return genericType; + } + + public Annotation[] getAnnotations() { + return annotations; + } + + public Operation getOperation() { + return operation; + } + + public <A extends Annotation> A getAnnotation(Class<A> type) { + if (annotations == null) { + return null; + } + for (Annotation a : annotations) { + if (a.annotationType() == type) { + return type.cast(a); + } + } + return null; + } + + public String getContentType() { + return contentType; + } +} diff --git a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/InputSource2Node.java b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/InputSource2Node.java index 5333d8047a..0bb0513011 100644 --- a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/InputSource2Node.java +++ b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/InputSource2Node.java @@ -22,6 +22,7 @@ import javax.xml.transform.Source; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.stream.StreamSource; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.BaseTransformer; import org.apache.tuscany.sca.databinding.PullTransformer; import org.apache.tuscany.sca.databinding.TransformationContext; @@ -36,8 +37,14 @@ import org.xml.sax.InputSource; */ public class InputSource2Node extends BaseTransformer<InputSource, Node> implements PullTransformer<InputSource, Node> { - private static final Source2ResultTransformer TRANSFORMER = new Source2ResultTransformer(); - + + private final Source2ResultTransformer TRANSFORMER; + + public InputSource2Node(ExtensionPointRegistry registry) { + super(); + this.TRANSFORMER = new Source2ResultTransformer(registry); + } + public Node transform(InputSource source, TransformationContext context) { try { Source streamSource = new StreamSource(source.getCharacterStream()); diff --git a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/InputStream2Node.java b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/InputStream2Node.java index 31254ff2c6..c9183c4cbf 100644 --- a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/InputStream2Node.java +++ b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/InputStream2Node.java @@ -24,6 +24,7 @@ import javax.xml.transform.Source; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.sax.SAXSource; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.BaseTransformer; import org.apache.tuscany.sca.databinding.PullTransformer; import org.apache.tuscany.sca.databinding.TransformationContext; @@ -38,8 +39,14 @@ import org.xml.sax.InputSource; */ public class InputStream2Node extends BaseTransformer<InputStream, Node> implements PullTransformer<InputStream, Node> { - private static final Source2ResultTransformer TRANSFORMER = new Source2ResultTransformer(); + private final Source2ResultTransformer TRANSFORMER; + + public InputStream2Node(ExtensionPointRegistry registry) { + super(); + this.TRANSFORMER = new Source2ResultTransformer(registry); + } + public Node transform(InputStream source, TransformationContext context) { try { Source streamSource = new SAXSource(new InputSource(source)); diff --git a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Node2OutputStream.java b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Node2OutputStream.java index bf39509e35..25ed70dda5 100644 --- a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Node2OutputStream.java +++ b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Node2OutputStream.java @@ -25,6 +25,7 @@ import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.BaseTransformer; import org.apache.tuscany.sca.databinding.PushTransformer; import org.apache.tuscany.sca.databinding.TransformationContext; @@ -38,8 +39,14 @@ import org.w3c.dom.Node; */ public class Node2OutputStream extends BaseTransformer<Node, OutputStream> implements PushTransformer<Node, OutputStream> { - private static final Source2ResultTransformer TRANSFORMER = new Source2ResultTransformer(); + private final Source2ResultTransformer TRANSFORMER; + + public Node2OutputStream(ExtensionPointRegistry registry) { + super(); + this.TRANSFORMER = new Source2ResultTransformer(registry); + } + public void transform(Node source, OutputStream writer, TransformationContext context) { try { Source domSource = new DOMSource(source); diff --git a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Node2String.java b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Node2String.java index a43354e99c..fb3023345c 100644 --- a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Node2String.java +++ b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Node2String.java @@ -20,6 +20,7 @@ package org.apache.tuscany.sca.databinding.xml; import java.io.StringWriter; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.BaseTransformer; import org.apache.tuscany.sca.databinding.PullTransformer; import org.apache.tuscany.sca.databinding.TransformationContext; @@ -32,8 +33,13 @@ import org.w3c.dom.Node; * @version $Rev$ $Date$ */ public class Node2String extends BaseTransformer<Node, String> implements PullTransformer<Node, String> { - private static final Node2Writer TRANSFORMER = new Node2Writer(); - + private final Node2Writer TRANSFORMER; + + public Node2String(ExtensionPointRegistry registry) { + super(); + this.TRANSFORMER = new Node2Writer(registry); + } + public String transform(Node source, TransformationContext context) { try { StringWriter writer = new StringWriter(); diff --git a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Node2Writer.java b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Node2Writer.java index 18561d0eeb..6c1d241ce9 100644 --- a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Node2Writer.java +++ b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Node2Writer.java @@ -25,6 +25,7 @@ import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.BaseTransformer; import org.apache.tuscany.sca.databinding.PushTransformer; import org.apache.tuscany.sca.databinding.TransformationContext; @@ -37,8 +38,14 @@ import org.w3c.dom.Node; * @version $Rev$ $Date$ */ public class Node2Writer extends BaseTransformer<Node, Writer> implements PushTransformer<Node, Writer> { - private static final Source2ResultTransformer TRANSFORMER = new Source2ResultTransformer(); + private final Source2ResultTransformer TRANSFORMER; + + public Node2Writer(ExtensionPointRegistry registry) { + super(); + this.TRANSFORMER = new Source2ResultTransformer(registry); + } + public void transform(Node source, Writer writer, TransformationContext context) { try { Source domSource = new DOMSource(source); diff --git a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Reader2Node.java b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Reader2Node.java index 31adea234d..1866f3864a 100644 --- a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Reader2Node.java +++ b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Reader2Node.java @@ -24,6 +24,7 @@ import javax.xml.transform.Source; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.stream.StreamSource; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.BaseTransformer; import org.apache.tuscany.sca.databinding.PullTransformer; import org.apache.tuscany.sca.databinding.TransformationContext; @@ -36,8 +37,13 @@ import org.w3c.dom.Node; * @version $Rev$ $Date$ */ public class Reader2Node extends BaseTransformer<Reader, Node> implements PullTransformer<Reader, Node> { - private static final Source2ResultTransformer TRANSFORMER = new Source2ResultTransformer(); - + private final Source2ResultTransformer TRANSFORMER; + + public Reader2Node(ExtensionPointRegistry registry) { + super(); + this.TRANSFORMER = new Source2ResultTransformer(registry); + } + public Node transform(Reader source, TransformationContext context) { try { Source streamSource = new StreamSource(source); diff --git a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Source2ResultTransformer.java b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Source2ResultTransformer.java index da78e6f9a6..dd69e3ea66 100644 --- a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Source2ResultTransformer.java +++ b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Source2ResultTransformer.java @@ -22,6 +22,8 @@ import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.TransformerFactory; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.databinding.BaseTransformer; import org.apache.tuscany.sca.databinding.PushTransformer; import org.apache.tuscany.sca.databinding.TransformationContext; @@ -34,11 +36,17 @@ import org.apache.tuscany.sca.databinding.TransformationException; */ public class Source2ResultTransformer extends BaseTransformer<Source, Result> implements PushTransformer<Source, Result> { - private static final TransformerFactory FACTORY = TransformerFactory.newInstance(); + private final TransformerFactory factory; + public Source2ResultTransformer(ExtensionPointRegistry registry) { + super(); + FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); + factory = factories.getFactory(TransformerFactory.class); + } + public void transform(Source source, Result result, TransformationContext context) { try { - javax.xml.transform.Transformer transformer = FACTORY.newTransformer(); + javax.xml.transform.Transformer transformer = factory.newTransformer(); transformer.transform(source, result); } catch (Exception e) { throw new TransformationException(e); diff --git a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Source2StringTransformer.java b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Source2StringTransformer.java index 5cf1867b96..0c3db2a477 100644 --- a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Source2StringTransformer.java +++ b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Source2StringTransformer.java @@ -24,6 +24,8 @@ import javax.xml.transform.Source; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.databinding.BaseTransformer; import org.apache.tuscany.sca.databinding.PullTransformer; import org.apache.tuscany.sca.databinding.TransformationContext; @@ -36,11 +38,17 @@ import org.apache.tuscany.sca.databinding.TransformationException; */ public class Source2StringTransformer extends BaseTransformer<Source, String> implements PullTransformer<Source, String> { - private static final TransformerFactory FACTORY = TransformerFactory.newInstance(); + private final TransformerFactory factory; + public Source2StringTransformer(ExtensionPointRegistry registry) { + super(); + FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); + factory = factories.getFactory(TransformerFactory.class); + } + public String transform(Source source, TransformationContext context) { try { - javax.xml.transform.Transformer transformer = FACTORY.newTransformer(); + javax.xml.transform.Transformer transformer = factory.newTransformer(); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); transformer.transform(source, result); diff --git a/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/MediatorImplTestCase.java b/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/MediatorImplTestCase.java index a74a8e5550..953e539dd1 100644 --- a/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/MediatorImplTestCase.java +++ b/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/MediatorImplTestCase.java @@ -87,8 +87,8 @@ public class MediatorImplTestCase { registry.addTransformer(new String2SAX(), true); registry.addTransformer(new SAX2DOMPipe(extensionPointRegistry), true); - registry.addTransformer(new Node2String(), true); - registry.addTransformer(new Node2Writer(), true); + registry.addTransformer(new Node2String(extensionPointRegistry), true); + registry.addTransformer(new Node2Writer(extensionPointRegistry), true); mediator = new MediatorImpl(dataBindingRegistry, registry); } diff --git a/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/DataPipeTestCase.java b/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/DataPipeTestCase.java index 682fbd4243..da18a4f89c 100644 --- a/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/DataPipeTestCase.java +++ b/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/DataPipeTestCase.java @@ -27,6 +27,7 @@ import java.io.Writer; import org.apache.tuscany.sca.common.xml.dom.DOMHelper; import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.DataPipe; import org.apache.tuscany.sca.databinding.DataPipeTransformer; import org.apache.tuscany.sca.databinding.impl.PipedTransformer; @@ -76,11 +77,12 @@ public class DataPipeTestCase { @Test public final void testPiped() throws Exception { - Node2Writer node2Writer = new Node2Writer(); + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + Node2Writer node2Writer = new Node2Writer(registry); Writer2ReaderDataPipe pipe = new Writer2ReaderDataPipe(); PipedTransformer<Node, Writer, Reader> transformer = new PipedTransformer<Node, Writer, Reader>(node2Writer, pipe); - Document document = DOMHelper.getInstance(new DefaultExtensionPointRegistry()).newDocument(); + Document document = DOMHelper.getInstance(registry).newDocument(); Element element = document.createElementNS("http://ns1", "root"); document.appendChild(element); Reader reader = transformer.transform(document, null); diff --git a/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/Node2StringTestCase.java b/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/Node2StringTestCase.java index 516b57c851..75c43dbf79 100644 --- a/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/Node2StringTestCase.java +++ b/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/Node2StringTestCase.java @@ -20,6 +20,7 @@ package org.apache.tuscany.sca.databinding.xml; import org.apache.tuscany.sca.common.xml.dom.DOMHelper; import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.junit.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -32,10 +33,11 @@ public class Node2StringTestCase { @Test public void testTransformation() throws Exception { - Document document = DOMHelper.getInstance(new DefaultExtensionPointRegistry()).newDocument(); + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + Document document = DOMHelper.getInstance(registry).newDocument(); Element element = document.createElementNS("http://ns1", "test"); document.appendChild(element); - new Node2String().transform(document, null); + new Node2String(registry).transform(document, null); } } diff --git a/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/PushTransformationTestCase.java b/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/PushTransformationTestCase.java index a8657a5be8..5027f64f01 100644 --- a/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/PushTransformationTestCase.java +++ b/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/PushTransformationTestCase.java @@ -73,7 +73,7 @@ public class PushTransformationTestCase { new PipedTransformer<XMLStreamReader, ContentHandler, Node>(t2, new SAX2DOMPipe(registry)); Node node = t3.transform(reader, null); Assert.assertNotNull(node); - Node2String t4 = new Node2String(); + Node2String t4 = new Node2String(registry); String xml = t4.transform(node, null); Assert.assertTrue(xml != null && xml.indexOf("<shipDate>1999-12-05</shipDate>") != -1); } diff --git a/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/TraxTransformerTestCase.java b/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/TraxTransformerTestCase.java index fb9895f120..6fd8ce2e8b 100644 --- a/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/TraxTransformerTestCase.java +++ b/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/TraxTransformerTestCase.java @@ -28,6 +28,8 @@ import java.io.StringWriter; import java.io.Writer; import java.net.URL; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.junit.Before; import org.junit.Test; import org.w3c.dom.Node; @@ -50,22 +52,23 @@ public class TraxTransformerTestCase { @Test public void testTransformDOM() throws IOException { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); InputStream is = url.openStream(); - InputStream2Node t1 = new InputStream2Node(); + InputStream2Node t1 = new InputStream2Node(registry); Node node = t1.transform(is, null); is.close(); Writer writer = new StringWriter(); - Node2Writer t2 = new Node2Writer(); + Node2Writer t2 = new Node2Writer(registry); t2.transform(node, writer, null); String str = writer.toString(); StringReader reader = new StringReader(str); - Reader2Node t3 = new Reader2Node(); + Reader2Node t3 = new Reader2Node(registry); node = t3.transform(reader, null); ByteArrayOutputStream os = new ByteArrayOutputStream(); - Node2OutputStream t4 = new Node2OutputStream(); + Node2OutputStream t4 = new Node2OutputStream(registry); t4.transform(node, os, null); InputSource inputSource = new InputSource(new ByteArrayInputStream(os.toByteArray())); - InputSource2Node t5 = new InputSource2Node(); + InputSource2Node t5 = new InputSource2Node(registry); node = t5.transform(inputSource, null); } |