diff options
author | slaws <slaws@13f79535-47bb-0310-9956-ffa450edef68> | 2009-07-06 07:23:44 +0000 |
---|---|---|
committer | slaws <slaws@13f79535-47bb-0310-9956-ffa450edef68> | 2009-07-06 07:23:44 +0000 |
commit | 5b67e082ce57df77acc72cd36569a05c9a2fa347 (patch) | |
tree | 2aae54f73b2a9bbd5b8ad71f8e605e82f4595fc5 /branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org | |
parent | aafdaec972cfe3439c6fe15c599d46d3e5f13fa9 (diff) |
Branch for 1.5.1
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@791397 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org')
9 files changed, 596 insertions, 0 deletions
diff --git a/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/MetaObject.java b/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/MetaObject.java new file mode 100644 index 0000000000..4c57cfe06b --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/MetaObject.java @@ -0,0 +1,37 @@ +/* + * 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.xstream; + +/** + * Wrapper for XObject + * @version $Rev$ $Date$ + */ +public interface MetaObject { + void setName(String name); + + String getName(); + + Class<?> getType(); + + String getID(); + + XObject getInstance(); + //byte[] getByteCode(); +} diff --git a/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/MetaObjectImpl.java b/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/MetaObjectImpl.java new file mode 100644 index 0000000000..d223a4db23 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/MetaObjectImpl.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.xstream; + +import java.io.FileNotFoundException; +import java.io.IOException; + +/** + * Implementation of a wrapper for XObject. + * + * @version $Rev$ $Date$ + */ +public class MetaObjectImpl implements MetaObject { + private String obID; + private String name; + private XObject obInstance; + + //private byte[] bytecode = null; + + public MetaObjectImpl() { + this.obID = Utils.uniqueID(); + } + + public MetaObjectImpl(XObject x) { + //this.name = path; + this.obID = Utils.uniqueID(); + this.obInstance = x; + + try { + initMetaOb(this.obInstance.getClass()); + + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + private void initMetaOb(Class<?> class1) throws FileNotFoundException, IOException { + /* + File file = new File(cname); + DataInputStream cstream = new DataInputStream(new FileInputStream(file)); + bytecode = new byte[(int)file.length()]; + cstream.readFully(bytecode); + */ + name = class1.getName(); + + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public String getID() { + return obID; + } + + public Class<?> getType() { + + return this.obInstance.getClass(); + } + + public XObject getInstance() { + return this.obInstance; + } + /* + * TODO Add bytecode with Serialization + public byte[] getByteCode() { + return bytecode; + } + */ + +} diff --git a/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/Node2XObject.java b/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/Node2XObject.java new file mode 100644 index 0000000000..2b7fa9d506 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/Node2XObject.java @@ -0,0 +1,69 @@ +/* + * 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.xstream; + +import org.apache.tuscany.sca.databinding.PullTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.TransformationException; +import org.apache.tuscany.sca.databinding.impl.BaseTransformer; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +import com.thoughtworks.xstream.XStream; +import com.thoughtworks.xstream.io.xml.DomReader; + +/** + * DOM Node --> XStream transformer + * + * @version $Rev$ $Date$ + */ +public class Node2XObject extends BaseTransformer<Node, XObject> implements PullTransformer<Node, XObject> { + + public XObject transform(Node source, TransformationContext context) { + try { + DomReader in = null; + if (source instanceof Document) { + in = new DomReader((Document)source); + } else if (source instanceof Element) { + in = new DomReader((Element)source); + } + XStream xs = new XStream(); + return (XObject)xs.unmarshal(in); + } catch (Exception e) { + throw new TransformationException(e); + } + } + + @Override + public Class<Node> getSourceType() { + return Node.class; + } + + @Override + public Class<XObject> getTargetType() { + return XObject.class; + } + + @Override + public int getWeight() { + return 10; + } + +} diff --git a/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/OMElement2XObject.java b/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/OMElement2XObject.java new file mode 100644 index 0000000000..a465e92e65 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/OMElement2XObject.java @@ -0,0 +1,76 @@ +/* + * 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.xstream; + +import java.io.StringWriter; + +import javax.xml.stream.XMLStreamException; + +import org.apache.axiom.om.OMElement; +import org.apache.tuscany.sca.databinding.PullTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.impl.BaseTransformer; + +import com.thoughtworks.xstream.XStream; + +/** + * Job DataObject --> AXIOM OMElement transformer + * + * @version $Rev$ $Date$ + */ +public class OMElement2XObject extends BaseTransformer<OMElement, XObject> implements + PullTransformer<OMElement, XObject> { + + public XObject transform(OMElement source, TransformationContext context) { + + XStream xs = new XStream(); + xs.alias("xobject", MetaObjectImpl.class); + MetaObjectImpl mo; + try { + java.io.StringWriter writer = new StringWriter(); + source.serialize(writer); + String w = writer.toString(); + // FIXME: a better way to get eliminate param0 + w = w.replaceAll("param0", "xobject"); + w = w.replaceAll("xmlns=\"\"", ""); + mo = (MetaObjectImpl)xs.fromXML(w.trim()); + return mo.getInstance(); + } catch (XMLStreamException e) { + + e.printStackTrace(); + } + return null; + } + + @Override + public Class<OMElement> getSourceType() { + return OMElement.class; + } + + @Override + public Class<XObject> getTargetType() { + return XObject.class; + } + + @Override + public int getWeight() { + return 10; + } + +} diff --git a/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/Utils.java b/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/Utils.java new file mode 100644 index 0000000000..d819975910 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/Utils.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.databinding.xstream; + +import java.io.IOException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; + +/** + * Utility methods. + * + * @version $Rev$ $Date$ + */ +public class Utils { + public static String toBase64(byte[] array) { + String code = null; + code = new sun.misc.BASE64Encoder().encode(array); + return code; + } + + public static byte[] fromBase64(String bytecode) { + byte[] dec = null; + try { + dec = new sun.misc.BASE64Decoder().decodeBuffer(bytecode); + } catch (IOException e) { + e.printStackTrace(); + } + return dec; + } + + public static String uniqueID() { + try { + //Initialize SecureRandom + //This is a lengthy operation, to be done only upon + //initialization of the application + SecureRandom prng = SecureRandom.getInstance("SHA1PRNG"); + + //generate a random number + String randomNum = Integer.toString(prng.nextInt()); + + //get its digest + MessageDigest sha = MessageDigest.getInstance("SHA-1"); + byte[] result = sha.digest(randomNum.getBytes()); + return hexEncode(result); + } catch (NoSuchAlgorithmException ex) { + ex.printStackTrace(); + } + return null; + } + + private static String hexEncode(byte[] aInput) { + StringBuffer result = new StringBuffer(); + char[] digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + for (int idx = 0; idx < aInput.length; ++idx) { + byte b = aInput[idx]; + result.append(digits[(b & 0xf0) >> 4]); + result.append(digits[b & 0x0f]); + } + return result.toString(); + } + +} diff --git a/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/XObject.java b/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/XObject.java new file mode 100644 index 0000000000..21112efcf5 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/XObject.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.databinding.xstream; + +/** + * Marker interface for XStream Serializable objects + * + * @version $Rev$ $Date$ + */ +// FIXME: Why do we require a marker interface? +public interface XObject { +} diff --git a/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/XObject2Node.java b/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/XObject2Node.java new file mode 100644 index 0000000000..98e0e69f9b --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/XObject2Node.java @@ -0,0 +1,69 @@ +/* + * 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.xstream; + +import org.apache.tuscany.sca.databinding.PullTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.TransformationException; +import org.apache.tuscany.sca.databinding.impl.BaseTransformer; +import org.apache.tuscany.sca.databinding.impl.DOMHelper; +import org.w3c.dom.Document; +import org.w3c.dom.Node; + +import com.thoughtworks.xstream.XStream; +import com.thoughtworks.xstream.io.xml.DomWriter; + +/** + * XObject --> DOM Node transformer + * + * @version $Rev$ $Date$ + */ +public class XObject2Node extends BaseTransformer<XObject, Node> implements PullTransformer<XObject, Node> { + + public Node transform(XObject source, TransformationContext context) { + try { + MetaObject mj = new MetaObjectImpl(source); + Document root = DOMHelper.newDocument(); + DomWriter out = new DomWriter(DOMHelper.newDocument()); + XStream xs = new XStream(); + xs.alias("xobject", mj.getClass()); + xs.marshal(source, out); + DOMHelper.adjustElementName(context, root.getDocumentElement()); + return root; + } catch (Exception e) { + throw new TransformationException(e); + } + } + + @Override + public Class<XObject> getSourceType() { + return XObject.class; + } + + @Override + public Class<Node> getTargetType() { + return Node.class; + } + + @Override + public int getWeight() { + return 10; + } + +} diff --git a/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/XObject2OMElement.java b/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/XObject2OMElement.java new file mode 100644 index 0000000000..ff4305491a --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/XObject2OMElement.java @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.databinding.xstream; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; + +import javax.xml.namespace.QName; + +import org.apache.axiom.om.OMAbstractFactory; +import org.apache.axiom.om.OMElement; +import org.apache.axiom.om.OMFactory; +import org.apache.axiom.om.OMNamespace; +import org.apache.axiom.om.impl.builder.StAXOMBuilder; +import org.apache.tuscany.sca.databinding.PullTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.TransformationException; +import org.apache.tuscany.sca.databinding.impl.BaseTransformer; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.util.XMLType; + +import com.thoughtworks.xstream.XStream; + +/** + * XObject --> AXIOM OMElement transformer + * + * @version $Rev$ $Date$ + */ +public class XObject2OMElement extends BaseTransformer<XObject, OMElement> implements + PullTransformer<XObject, OMElement> { + + public OMElement transform(XObject source, TransformationContext context) { + MetaObject mj = new MetaObjectImpl(source); + OMElement element = null; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + XStream xs = new XStream(); + xs.alias("xobject", mj.getClass()); + xs.toXML(mj, out); + /* TODO: Maybe a base64 conversion...? or not? + * ie. new ByteArrayInputStream(Utils.toBase64(out.toByteArray()).getBytes()) + */ + try { + // what's better this one: + StAXOMBuilder builder = new StAXOMBuilder(new ByteArrayInputStream(out.toByteArray())); + // or this one: + // StaAXOMBuilder builder = new StAXOMBuilder(new ByteArrayInputStream(Utils.toBase64(out.toByteArray()).getBytes())) + element = builder.getDocumentElement(); + adjustElementName(context, element); + } catch (Exception e) { + throw new TransformationException(e); + } + return element; + + } + + private static void adjustElementName(TransformationContext context, OMElement element) { + if (context != null) { + DataType dataType = context.getTargetDataType(); + Object logical = dataType == null ? null : dataType.getLogical(); + if (!(logical instanceof XMLType)) { + return; + } + XMLType xmlType = (XMLType)logical; + if (xmlType.isElement() && !xmlType.getElementName().equals(element.getQName())) { + // FIXME:: Throw exception or switch to the new Element? + OMFactory factory = OMAbstractFactory.getOMFactory(); + QName name = xmlType.getElementName(); + OMNamespace namespace = factory.createOMNamespace(name.getNamespaceURI(), name.getPrefix()); + element.setNamespace(namespace); + element.setLocalName(name.getLocalPart()); + } + } + } + + @Override + public Class<XObject> getSourceType() { + return XObject.class; + } + + @Override + public Class<OMElement> getTargetType() { + return OMElement.class; + } + + @Override + public int getWeight() { + return 10; + } + +} diff --git a/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/XObjectDataBinding.java b/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/XObjectDataBinding.java new file mode 100644 index 0000000000..b319c7d557 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/databinding-xstream/src/main/java/org/apache/tuscany/sca/databinding/xstream/XObjectDataBinding.java @@ -0,0 +1,37 @@ +/* + * 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.xstream; + +import org.apache.tuscany.sca.databinding.impl.BaseDataBinding; + +/** + * DataBinding for Jobs + * + * @version $Rev$ $Date$ + */ +public class XObjectDataBinding extends BaseDataBinding { + + public static final String NAME = XObject.class.getName(); + + public XObjectDataBinding() { + super(NAME, XObject.class); + } + +} |