diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-10 19:20:03 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-10 19:20:03 +0000 |
commit | 836328ea2ca7c6780307abf258503201c91e5a96 (patch) | |
tree | 1bb1b4a3af18e95389780d65ece875ba5ae93aff /sdo-java/branches/sdo-1.1.1-incubating/lib/src/main/java/org/apache/tuscany/sdo/spi | |
parent | 2a444004802173fa97afbc389fc4a6600b6d3f88 (diff) |
moving SDO branches
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@834615 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sdo-java/branches/sdo-1.1.1-incubating/lib/src/main/java/org/apache/tuscany/sdo/spi')
2 files changed, 422 insertions, 0 deletions
diff --git a/sdo-java/branches/sdo-1.1.1-incubating/lib/src/main/java/org/apache/tuscany/sdo/spi/HelperProviderBase.java b/sdo-java/branches/sdo-1.1.1-incubating/lib/src/main/java/org/apache/tuscany/sdo/spi/HelperProviderBase.java new file mode 100644 index 0000000000..375552497b --- /dev/null +++ b/sdo-java/branches/sdo-1.1.1-incubating/lib/src/main/java/org/apache/tuscany/sdo/spi/HelperProviderBase.java @@ -0,0 +1,246 @@ +/** + * + * 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.sdo.spi; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.NotSerializableException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.io.ObjectStreamException; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; + +import org.apache.tuscany.sdo.api.SDOHelper; +import org.apache.tuscany.sdo.api.SDOUtil; +import org.apache.tuscany.sdo.api.XMLStreamHelper; +import org.apache.tuscany.sdo.lib.SDOObjectInputStream; +import org.apache.tuscany.sdo.lib.SDOObjectOutputStream; + +import commonj.sdo.DataGraph; +import commonj.sdo.DataObject; +import commonj.sdo.helper.CopyHelper; +import commonj.sdo.helper.DataFactory; +import commonj.sdo.helper.DataHelper; +import commonj.sdo.helper.EqualityHelper; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.TypeHelper; +import commonj.sdo.helper.XMLDocument; +import commonj.sdo.helper.XMLHelper; +import commonj.sdo.helper.XSDHelper; +import commonj.sdo.impl.HelperProvider; +import commonj.sdo.impl.ExternalizableDelegator.Resolvable; + + +/** + * Create and manage all the default helpers + */ +public abstract class HelperProviderBase extends HelperProvider +{ + protected CopyHelper copyHelper; + + protected DataFactory dataFactory; + + protected DataHelper dataHelper; + + protected EqualityHelper equalityHelper; + + protected TypeHelper typeHelper; + + protected XMLHelper xmlHelper; + + protected XSDHelper xsdHelper; + + protected SDOHelper sdoHelper; // Tuscany extension APIs + + protected XMLStreamHelper xmlStreamHelper; + /** + * Subclasses must implement this method to initialize the above Helper instance variables + */ + protected abstract HelperContext createDefaultHelpers(); + + public HelperProviderBase() + { + defaultContext = createDefaultHelpers(); + } + + public SDOHelper sdoHelper() + { + return sdoHelper; + } + + public CopyHelper copyHelper() + { + return copyHelper; + } + + public DataFactory dataFactory() + { + return dataFactory; + } + + public DataHelper dataHelper() + { + return dataHelper; + } + + public EqualityHelper equalityHelper() + { + return equalityHelper; + } + + public TypeHelper typeHelper() + { + return typeHelper; + } + + public XMLHelper xmlHelper() + { + return xmlHelper; + } + + public XMLStreamHelper xmlStreamHelper() + { + return xmlStreamHelper; + } + public XSDHelper xsdHelper() + { + return xsdHelper; + } + + public Resolvable resolvable() + { + return new ResolvableImpl(); + } + + public Resolvable resolvable(Object target) + { + return new ResolvableImpl(target); + } + + protected class ResolvableImpl implements Resolvable + { + protected Object target; + + public ResolvableImpl(Object target) { this.target = target; } + + public ResolvableImpl() { this.target = null; } + + public void writeExternal(ObjectOutput out) throws IOException + { + if (target instanceof DataObject) + { + writeDataObject((DataObject)target, out); + } + else + { + throw new NotSerializableException(); // should never happen + } + } + + public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException + { + target = readDataObject(in); + } + + public Object readResolve() throws ObjectStreamException + { + return target; + } + + protected void writeDataObject(DataObject dataObject, ObjectOutput objectOutput) throws IOException + { + DataGraph dataGraph = dataObject.getDataGraph(); + if (dataGraph != null) + { + objectOutput.writeByte(0); + objectOutput.writeUTF(SDOUtil.getXPath(dataObject)); + objectOutput.writeObject(dataGraph); + } + else if (dataObject.getContainer() != null) + { + objectOutput.writeByte(0); + objectOutput.writeUTF(SDOUtil.getXPath(dataObject)); + objectOutput.writeObject(dataObject.getRootObject()); + } + else + { + // Root object + objectOutput.writeByte(1); + + ByteArrayOutputStream compressedByteArrayOutputStream = new ByteArrayOutputStream(); + GZIPOutputStream gzipOutputStream = new GZIPOutputStream(compressedByteArrayOutputStream); + XMLHelper xmlHelperLocal = xmlHelper; + if(objectOutput instanceof SDOObjectOutputStream) + { + xmlHelperLocal = ((SDOObjectOutputStream)objectOutput).getHelperContext().getXMLHelper(); + } + xmlHelperLocal.save(dataObject, "commonj.sdo", "dataObject", gzipOutputStream); + gzipOutputStream.close(); // Flush the contents + + byte[] byteArray = compressedByteArrayOutputStream.toByteArray(); + objectOutput.writeInt(byteArray.length); + objectOutput.write(byteArray); + } + } + + protected DataObject readDataObject(ObjectInput objectInput) throws IOException, ClassNotFoundException + { + boolean isRoot = objectInput.readByte() == 1; + if (isRoot) + { + // Root object: [rootXML] = length + XML contents + int length = objectInput.readInt(); + byte[] compressedBytes = new byte [length]; + + int index = 0; + int bytesRead; + while (index < length) { + if ((bytesRead = objectInput.read(compressedBytes, index, length-index)) == -1) { + break; + } + index += bytesRead; + } + + GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(compressedBytes)); + XMLHelper xmlHelperLocal = xmlHelper; + if (objectInput instanceof SDOObjectInputStream) + { + xmlHelperLocal = ((SDOObjectInputStream)objectInput).getHelperContext().getXMLHelper(); + } + XMLDocument doc = xmlHelperLocal.load(gzipInputStream); + gzipInputStream.close(); + + return doc.getRootObject(); + } + else + { + // Non root object: [path] [root] + String xpath = objectInput.readUTF(); + Object object = objectInput.readObject(); + + DataObject root = object instanceof DataGraph ? ((DataGraph)object).getRootObject() : (DataObject)object; + return xpath.equals("") ? root : root.getDataObject(xpath); + } + } + } + +} diff --git a/sdo-java/branches/sdo-1.1.1-incubating/lib/src/main/java/org/apache/tuscany/sdo/spi/SDOHelperBase.java b/sdo-java/branches/sdo-1.1.1-incubating/lib/src/main/java/org/apache/tuscany/sdo/spi/SDOHelperBase.java new file mode 100644 index 0000000000..ce3678d81f --- /dev/null +++ b/sdo-java/branches/sdo-1.1.1-incubating/lib/src/main/java/org/apache/tuscany/sdo/spi/SDOHelperBase.java @@ -0,0 +1,176 @@ +/** + * + * 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.sdo.spi; + +/** + * Base class for an implementation of the SDOHelper and SDOHelper.MetaDataBuilder interfaces. + */ +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.OutputStream; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.sdo.api.SDOHelper; +import org.apache.tuscany.sdo.api.SDOUtil; +import org.apache.tuscany.sdo.lib.SDOObjectInputStream; +import org.apache.tuscany.sdo.lib.SDOObjectOutputStream; + +import commonj.sdo.DataObject; +import commonj.sdo.Property; +import commonj.sdo.helper.HelperContext; + +public abstract class SDOHelperBase implements SDOHelper, SDOHelper.MetaDataBuilder +{ + public HelperContext createHelperContext() + { + return createHelperContext(false); + } + + public ObjectInputStream createObjectInputStream(InputStream inputStream, HelperContext helperContext) throws IOException + { + return new SDOObjectInputStream(inputStream, helperContext); + } + + public ObjectOutputStream createObjectOutputStream(OutputStream outputStream, HelperContext helperContext) throws IOException + { + return new SDOObjectOutputStream(outputStream, helperContext); + } + + public String getXPath(DataObject dataObject) + { + StringBuffer path = getXPath(dataObject, new StringBuffer(), dataObject); + return path.toString(); + } + + protected StringBuffer getXPath(DataObject dataObject, StringBuffer path, DataObject root) + { + DataObject container = dataObject.getContainer(); + if (container == null) + return path; + + if (container == root) + throw new IllegalStateException("There is a cycle in the containment hierarchy of " + root); + + boolean first = path.length() == 0; + Property property = dataObject.getContainmentProperty(); + if (SDOUtil.isMany(property, dataObject)) + { + List list = container.getList(property); + int pos = list.indexOf(dataObject); + path.insert(0, property.getName() + "." + pos + (first ? "" : "/")); + } + else + { + path.insert(0, property.getName() + (first ? "" : "/")); + } + + return getXPath(container, path, root); + } + + public MetaDataBuilder getMetaDataBuilder() + { + return this; + } + + //Java instance class to SDO mappings (section 8.1 of the SDO spec) + protected static Map javaToSdoMappings = new HashMap(); + static { + javaToSdoMappings.put(boolean.class, "Boolean"); + javaToSdoMappings.put(byte.class, "Byte"); + javaToSdoMappings.put(char.class, "Character"); + javaToSdoMappings.put(Date.class, "Date"); + javaToSdoMappings.put(BigDecimal.class, "Decimal"); + javaToSdoMappings.put(double.class, "Double"); + javaToSdoMappings.put(float.class, "Float"); + javaToSdoMappings.put(int.class, "Int"); + javaToSdoMappings.put(BigInteger.class, "Integer"); + javaToSdoMappings.put(long.class, "Long"); + javaToSdoMappings.put(Object.class, "Object"); + javaToSdoMappings.put(short.class, "Short"); + javaToSdoMappings.put(String.class, "String"); + javaToSdoMappings.put(Boolean.class, "BooleanObject"); + javaToSdoMappings.put(Byte.class, "ByteObject"); + javaToSdoMappings.put(Character.class, "CharacterObject"); + javaToSdoMappings.put(Double.class, "DoubleObject"); + javaToSdoMappings.put(Float.class, "FloatObject"); + javaToSdoMappings.put(Integer.class, "IntObject"); + javaToSdoMappings.put(Long.class, "LongObject"); + javaToSdoMappings.put(Short.class, "ShortObject"); + } + + //XSD to SDO mappings (section 9.4 of the SDO spec) + protected static Map xsdToSdoMappings = new HashMap(); + static { + xsdToSdoMappings.put("anySimpleType", "Object"); + xsdToSdoMappings.put("anyType", "DataObject"); + xsdToSdoMappings.put("anyURI", "URI"); + xsdToSdoMappings.put("base64Binary", "Bytes"); + xsdToSdoMappings.put("boolean", "Boolean"); + xsdToSdoMappings.put("byte", "Byte"); + xsdToSdoMappings.put("date", "YearMonthDay"); + xsdToSdoMappings.put("dateTime", "DateTime"); + xsdToSdoMappings.put("decimal", "Decimal"); + xsdToSdoMappings.put("double", "Double"); + xsdToSdoMappings.put("duration", "Duration"); + xsdToSdoMappings.put("ENTITIES", "Strings"); + xsdToSdoMappings.put("ENTITY", "String"); + xsdToSdoMappings.put("float", "Float"); + xsdToSdoMappings.put("gDay", "Day"); + xsdToSdoMappings.put("gMonth", "Month"); + xsdToSdoMappings.put("gMonthDay", "MonthDay"); + xsdToSdoMappings.put("gYear", "Year"); + xsdToSdoMappings.put("gYearMonth", "YearMonth"); + xsdToSdoMappings.put("hexBinary", "Bytes"); + xsdToSdoMappings.put("ID","String"); + xsdToSdoMappings.put("IDREF","String"); + xsdToSdoMappings.put("IDREFS","Strings"); + xsdToSdoMappings.put("int","Int"); + xsdToSdoMappings.put("integer","Integer"); + xsdToSdoMappings.put("language","String"); + xsdToSdoMappings.put("long","Long"); + xsdToSdoMappings.put("Name","String"); + xsdToSdoMappings.put("NCName","String"); + xsdToSdoMappings.put("negativeInteger","Integer"); + xsdToSdoMappings.put("NMTOKEN","String"); + xsdToSdoMappings.put("NMTOKENS","Strings"); + xsdToSdoMappings.put("nonNegativeInteger","Integer"); + xsdToSdoMappings.put("nonPositiveInteger","Integer"); + xsdToSdoMappings.put("normalizedString","String"); + xsdToSdoMappings.put("NOTATION","String"); + xsdToSdoMappings.put("positiveInteger","Integer"); + xsdToSdoMappings.put("QName","URI"); + xsdToSdoMappings.put("short","Short"); + xsdToSdoMappings.put("string","String"); + xsdToSdoMappings.put("time","Time"); + xsdToSdoMappings.put("token","String"); + xsdToSdoMappings.put("unsignedByte","Short"); + xsdToSdoMappings.put("unsignedInt","Long"); + xsdToSdoMappings.put("unsignedLong","Integer"); + xsdToSdoMappings.put("unsignedShort","Int"); + } + +} |