summaryrefslogtreecommitdiffstats
path: root/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper
diff options
context:
space:
mode:
Diffstat (limited to 'sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper')
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/CopyHelperImpl.java54
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/CrossScopeCopyHelperImpl.java255
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/DataFactoryImpl.java65
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/DataHelperImpl.java469
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/EqualityHelperImpl.java70
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/HelperProviderImpl.java215
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SDOAnnotations.java51
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SDOExtendedMetaDataImpl.java99
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SDOXSDEcoreBuilder.java458
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SchemaBuilder.java786
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/TypeHelperImpl.java286
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/TypeTable.java254
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java536
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLHelperImpl.java119
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLStreamHelper.java92
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLStreamHelperImpl.java129
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XSDHelperImpl.java285
17 files changed, 4223 insertions, 0 deletions
diff --git a/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/CopyHelperImpl.java b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/CopyHelperImpl.java
new file mode 100644
index 0000000000..16bafe68ca
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/CopyHelperImpl.java
@@ -0,0 +1,54 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.sdo.helper;
+
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EReference;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.emf.ecore.util.EcoreUtil.Copier;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.helper.CopyHelper;
+
+
+/**
+ * A helper for copying DataObjects.
+ */
+public class CopyHelperImpl implements CopyHelper
+{
+ public DataObject copyShallow(DataObject dataObject)
+ {
+ Copier copier = new Copier()
+ {
+ protected void copyContainment(EReference eReference, EObject eObject, EObject copyEObject)
+ {
+ }
+ };
+ EObject result = copier.copy((EObject)dataObject);
+ copier.copyReferences();
+ return (DataObject)result;
+ }
+
+ public DataObject copy(DataObject dataObject)
+ {
+ return (DataObject)EcoreUtil.copy((EObject)dataObject);
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/CrossScopeCopyHelperImpl.java b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/CrossScopeCopyHelperImpl.java
new file mode 100644
index 0000000000..b7a5534800
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/CrossScopeCopyHelperImpl.java
@@ -0,0 +1,255 @@
+/**
+ *
+ * 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.helper;
+
+import java.util.Iterator;
+import java.util.Map;
+
+import org.eclipse.emf.ecore.EAttribute;
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EReference;
+import org.eclipse.emf.ecore.EStructuralFeature;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.emf.ecore.util.FeatureMap;
+import org.eclipse.emf.ecore.util.FeatureMapUtil;
+import org.eclipse.emf.ecore.util.InternalEList;
+import org.eclipse.emf.ecore.util.EcoreUtil.Copier;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.Type;
+import commonj.sdo.helper.CopyHelper;
+import commonj.sdo.helper.TypeHelper;
+
+/**
+ * A CopyHelper implementation that creates the copy objects in a specific metadata scope.
+ * The target scope must contain a compatible version of the Types needed to create the copy objects.
+ */
+public class CrossScopeCopyHelperImpl implements CopyHelper
+{
+ protected TypeHelper scope;
+
+ public CrossScopeCopyHelperImpl(TypeHelper targetScope)
+ {
+ scope = targetScope;
+ }
+
+ public DataObject copyShallow(DataObject dataObject)
+ {
+ Copier copier = new CrossScopeCopier()
+ {
+ protected void copyContainment(EReference eReference, EObject eObject, EObject copyEObject)
+ {
+ }
+ protected void copyAttribute(EAttribute eAttribute, EObject eObject, EObject copyEObject)
+ {
+ if (eObject.eIsSet(eAttribute) && !FeatureMapUtil.isFeatureMap(eAttribute))
+ {
+ super.copyAttribute(eAttribute,eObject,copyEObject);
+ }
+ }
+ };
+ EObject result = copier.copy((EObject)dataObject);
+ copier.copyReferences();
+ return (DataObject)result;
+ }
+
+ public DataObject copy(DataObject dataObject)
+ {
+ Copier copier = new CrossScopeCopier();
+ DataObject result = (DataObject)copier.copy((EObject)dataObject);
+ copier.copyReferences();
+ return (DataObject)result;
+ }
+
+ protected class CrossScopeCopier extends EcoreUtil.Copier
+ {
+ protected boolean useOriginalReferences = false;
+
+ protected EClass getTarget(EClass eClass)
+ {
+ EClass target = (EClass)get(eClass);
+ if (target == null)
+ {
+ Type type = (Type)eClass;
+ target = (EClass)scope.getType(type.getURI(), type.getName());
+ }
+ return target;
+ }
+
+ protected EStructuralFeature getTarget(EStructuralFeature eStructuralFeature)
+ {
+ EClass eClass = getTarget(eStructuralFeature.getEContainingClass());
+ EStructuralFeature targetEf = eClass.getEStructuralFeature(eStructuralFeature.getName());
+ return targetEf;
+ }
+
+ /**
+ * This Method WILL BE REMOVED when EMF 3.0 is available
+ */
+ public void copyReferences()
+ {
+ for (Iterator i = entrySet().iterator(); i.hasNext();)
+ {
+ Map.Entry entry = (Map.Entry)i.next();
+ EObject eObject = (EObject)entry.getKey();
+ EObject copyEObject = (EObject)entry.getValue();
+ EClass eClass = eObject.eClass();
+ for (int j = 0, size = eClass.getFeatureCount(); j < size; ++j)
+ {
+ EStructuralFeature eStructuralFeature = eClass.getEStructuralFeature(j);
+ if (eStructuralFeature.isChangeable() && !eStructuralFeature.isDerived())
+ {
+ if (eStructuralFeature instanceof EReference)
+ {
+ EReference eReference = (EReference)eStructuralFeature;
+ if (!eReference.isContainment() && !eReference.isContainer())
+ {
+ copyReference(eReference, eObject, copyEObject);
+ }
+ }
+ else if (FeatureMapUtil.isFeatureMap(eStructuralFeature))
+ {
+ FeatureMap featureMap = (FeatureMap)eObject.eGet(eStructuralFeature);
+ FeatureMap copyFeatureMap = (FeatureMap)copyEObject.eGet(getTarget(eStructuralFeature));
+ int copyFeatureMapSize = copyFeatureMap.size();
+ for (int k = 0, featureMapSize = featureMap.size(); k < featureMapSize; ++k)
+ {
+ EStructuralFeature feature = featureMap.getEStructuralFeature(k);
+ if (feature instanceof EReference)
+ {
+ Object referencedEObject = featureMap.getValue(k);
+ Object copyReferencedEObject = get(referencedEObject);
+ if (copyReferencedEObject == null && referencedEObject != null)
+ {
+ EReference reference = (EReference)feature;
+ if (!useOriginalReferences || reference.isContainment() || reference.getEOpposite() != null)
+ {
+ continue;
+ }
+ copyReferencedEObject = referencedEObject;
+ }
+ // If we can't add it, it must aleady be in the list so find it and move it to the end.
+ //
+ if (!copyFeatureMap.add(feature, copyReferencedEObject))
+ {
+ for (int l = 0; l < copyFeatureMapSize; ++l)
+ {
+ if (copyFeatureMap.getEStructuralFeature(l) == feature && copyFeatureMap.getValue(l) == copyReferencedEObject)
+ {
+ copyFeatureMap.move(copyFeatureMap.size() - 1, l);
+ --copyFeatureMapSize;
+ break;
+ }
+ }
+ }
+ }
+ else
+ {
+ copyFeatureMap.add(featureMap.get(k));
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * This Method WILL BE REMOVED when EMF 3.0 is available
+ */
+ protected void copyReference(EReference eReference, EObject eObject, EObject copyEObject)
+ {
+ if (eObject.eIsSet(eReference))
+ {
+ if (eReference.isMany())
+ {
+ InternalEList source = (InternalEList)eObject.eGet(eReference);
+ InternalEList target = (InternalEList)copyEObject.eGet(getTarget(eReference));
+ if (source.isEmpty())
+ {
+ target.clear();
+ }
+ else
+ {
+ boolean isBidirectional = eReference.getEOpposite() != null;
+ int index = 0;
+ for (Iterator k = resolveProxies ? source.iterator() : source.basicIterator(); k.hasNext();)
+ {
+ Object referencedEObject = k.next();
+ Object copyReferencedEObject = get(referencedEObject);
+ if (copyReferencedEObject == null)
+ {
+ if (useOriginalReferences && !isBidirectional)
+ {
+ target.addUnique(index, referencedEObject);
+ ++index;
+ }
+ }
+ else
+ {
+ if (isBidirectional)
+ {
+ int position = target.indexOf(copyReferencedEObject);
+ if (position == -1)
+ {
+ target.addUnique(index, copyReferencedEObject);
+ }
+ else if (index != position)
+ {
+ target.move(index, copyReferencedEObject);
+ }
+ }
+ else
+ {
+ target.addUnique(index, copyReferencedEObject);
+ }
+ ++index;
+ }
+ }
+ }
+ }
+ else
+ {
+ Object referencedEObject = eObject.eGet(eReference, resolveProxies);
+ if (referencedEObject == null)
+ {
+ copyEObject.eSet(getTarget(eReference), null);
+ }
+ else
+ {
+ Object copyReferencedEObject = get(referencedEObject);
+ if (copyReferencedEObject == null)
+ {
+ if (useOriginalReferences && eReference.getEOpposite() == null)
+ {
+ copyEObject.eSet(getTarget(eReference), referencedEObject);
+ }
+ }
+ else
+ {
+ copyEObject.eSet(getTarget(eReference), copyReferencedEObject);
+ }
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/DataFactoryImpl.java b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/DataFactoryImpl.java
new file mode 100644
index 0000000000..128f63f4d7
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/DataFactoryImpl.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.sdo.helper;
+
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.Type;
+import commonj.sdo.helper.DataFactory;
+import commonj.sdo.helper.TypeHelper;
+
+/**
+ * A Factory for creating DataObjects.
+ * The created DataObjects are not connected to any other DataObjects.
+ */
+public class DataFactoryImpl implements DataFactory
+{
+ protected TypeHelper typeHelper;
+
+ public DataFactoryImpl(TypeHelper typeHelper)
+ {
+ this.typeHelper = typeHelper;
+ }
+
+ public DataObject create(String uri, String typeName)
+ {
+ Type type = typeHelper.getType(uri, typeName);
+ return create(type);
+ }
+
+ public DataObject create(Class interfaceClass)
+ {
+ //TODO more efficient implementation ... this is a really bad one!
+ Type type = typeHelper.getType(interfaceClass);
+ return create(type);
+ }
+
+ public DataObject create(Type type)
+ {
+ if (type instanceof EClass)
+ {
+ EClass eClass = (EClass)type;
+ return (DataObject)EcoreUtil.create(eClass);
+ }
+ throw new IllegalArgumentException();
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/DataHelperImpl.java b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/DataHelperImpl.java
new file mode 100644
index 0000000000..2406af1a8a
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/DataHelperImpl.java
@@ -0,0 +1,469 @@
+/**
+ *
+ * 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.helper;
+
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.Locale;
+import java.util.TimeZone;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+
+
+import commonj.sdo.helper.DataHelper;
+
+/**
+ * Data helper methods.
+ */
+public class DataHelperImpl implements DataHelper
+{
+ /**
+ * @param dateString - Must comply to the pattern of yyyy-MM-dd'T'HH:mm:ss'.'SSS'Z'
+ * @return null if dataString couldn't be parsed
+ */
+ public synchronized Date toDate(String dateString)
+ {
+ Date result = null;
+ dateString = dateString.trim();
+ boolean negative = false;
+
+ if (dateString == null)
+ {
+ return null;
+ }
+
+ // Determine if it is a negative Date, DateTime, or Duration
+ if (dateString.length() > 2 && dateString.charAt(0) == '-' && dateString.charAt(1) != '-')
+ {
+ negative = true;
+ dateString = dateString.substring(1);
+ }
+
+ // SDO Date Format ends with a Z
+
+ if (dateString.endsWith("Z"))
+ {
+ SimpleDateFormat[] SDO_DATE_FORMATS =
+ {
+ new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'SSS'Z'"),
+ new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
+ };
+
+ for (int i = 0; i < SDO_DATE_FORMATS.length; i++)
+ {
+ SDO_DATE_FORMATS[i].setTimeZone(TimeZone.getTimeZone("GMT"));
+ }
+
+ result = checkFormats(dateString, SDO_DATE_FORMATS);
+
+ // If no match, continue to try further possibilities
+
+ if (result != null)
+ {
+ if (negative)
+ return handleBCE(result);
+ else
+ return result;
+ }
+
+ }
+
+ // Duration format begins with a P
+
+ if (dateString.startsWith("P"))
+ {
+ // Remove any spaces in the dateString
+
+ String durationString = dateString.replaceAll(" ", "");
+
+ // For Duration format, there are so many possibilities due to so many optional
+ // inclusions, that we will build the format string rather than create
+ // a potential SimpleDateFormat for each possibility.
+
+ SimpleDateFormat[] DURATION_FORMATS = obtainDurationFormats(durationString);
+ result = checkFormats(durationString, DURATION_FORMATS);
+ if (result != null)
+ {
+ if (negative)
+ return handleNegative(result);
+ else
+ return result;
+ }
+ }
+
+ // Check the remaining possibilities. Note that time zone is optional for each.
+
+ SimpleDateFormat [] DATE_PATTERNS =
+ {
+ new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'SSS z"),
+ new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss z"),
+ new SimpleDateFormat("yyyy-MM-dd'T'HH:mm z"),
+ new SimpleDateFormat("'--'MM'-'dd z"),
+ new SimpleDateFormat("'--'MM z"),
+ new SimpleDateFormat("'---'dd zzzz"),
+ new SimpleDateFormat("HH:mm:ss'.'SSS z"),
+ new SimpleDateFormat("HH:mm:ss z"),
+ new SimpleDateFormat("yyyy-MM-dd z"),
+ new SimpleDateFormat("yyyy-MM z"),
+ new SimpleDateFormat("yyyy z"),
+ new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'SSS"),
+ new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"),
+ new SimpleDateFormat("yyyy-MM-dd'T'HH:mm"),
+ new SimpleDateFormat("'--'MM'-'dd"),
+ new SimpleDateFormat("'--'MM"),
+ new SimpleDateFormat("'---'dd"),
+ new SimpleDateFormat("HH:mm:ss'.'SSS"),
+ new SimpleDateFormat("HH:mm:ss"),
+ new SimpleDateFormat("yyyy-MM-dd"),
+ new SimpleDateFormat("yyyy-MM"),
+ new SimpleDateFormat("yyyy")
+ };
+
+ result = checkFormats(dateString, DATE_PATTERNS);
+ if (result != null)
+ {
+ if (negative)
+ return handleBCE(result);
+ else
+ return result;
+ }
+
+ return null;
+ }
+
+ private synchronized Date checkFormats (String dateString, SimpleDateFormat[] format_array)
+ {
+ for (int i = 0; i < format_array.length; ++i)
+ {
+ try
+ {
+ format_array[i].setLenient(false);
+ return format_array[i].parse(dateString);
+ }
+ catch (ParseException parseException)
+ {
+ }
+ }
+
+ return null;
+ }
+
+ public synchronized SimpleDateFormat[] obtainDurationFormats(String dateString)
+ {
+ String first_part, second_part;
+ StringBuffer format_buffer = new StringBuffer("'P'");
+
+ // Must divide it into two parts to distinguish between Months and Minutes
+
+ int time_index = dateString.indexOf("T");
+ if (time_index != -1)
+ {
+ first_part = dateString.substring(0, time_index + 1);
+ second_part = dateString.substring(time_index);
+ }
+ else
+ {
+ first_part = dateString;
+ second_part = null;
+ }
+
+ if (first_part.indexOf("Y") != -1)
+ format_buffer.append("yyyy'Y'");
+ if (first_part.indexOf("M") != -1)
+ format_buffer.append("MM'M'");
+ if (first_part.indexOf("D") != -1)
+ format_buffer.append("dd'D'");
+ if (time_index != -1)
+ {
+ format_buffer.append("'T'");
+
+ if (second_part.indexOf("H") != -1)
+ format_buffer.append("HH'H'");
+ if (second_part.indexOf("M") != -1)
+ format_buffer.append("mm'M'");
+ if (second_part.indexOf("S.") != -1)
+ format_buffer.append("ss'S'.S");
+ else if (second_part.indexOf("S") != -1)
+ format_buffer.append("ss'S'");
+ }
+
+ String format_string = format_buffer.toString().replaceAll("''", "");
+
+ SimpleDateFormat [] DURATION_FORMATS =
+ {
+ new SimpleDateFormat(format_string)
+ };
+
+ return DURATION_FORMATS;
+ }
+
+ // Return a negative Duration if a negative sign existed in dateString
+ public synchronized Date handleNegative(Date output)
+ {
+ return new Date(0 - output.getTime());
+ }
+
+ // Return the date in BCE if a negative sign existed in dateString
+
+ public synchronized Date handleBCE(Date output)
+ {
+ GregorianCalendar temp = new GregorianCalendar();
+ temp.setTime(output);
+ temp.set(GregorianCalendar.ERA, GregorianCalendar.BC);
+
+ return temp.getTime();
+ }
+
+ public synchronized Calendar toCalendar(String dateString)
+ {
+ if (dateString == null)
+ {
+ return null;
+ }
+
+ Date date = toDate(dateString);
+ if (date == null)
+ {
+ return null;
+ }
+
+ Calendar calendar = new GregorianCalendar();
+ calendar.setTime(date);
+
+ return calendar;
+ }
+
+ public synchronized Calendar toCalendar(String dateString, Locale locale)
+ {
+ if (dateString == null || locale == null)
+ {
+ return null;
+ }
+
+ Date date = toDate(dateString);
+ if (date == null)
+ {
+ return null;
+ }
+
+ Calendar calendar = new GregorianCalendar(locale);
+ calendar.setTime(date);
+ return calendar;
+ }
+
+ public synchronized String toDateTime(Date date)
+ {
+ if (date == null)
+ {
+ return null;
+ }
+
+ SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'SSS zz");
+
+ return f.format(date);
+ }
+
+ public synchronized String toDuration(Date date)
+ {
+ if (date == null)
+ {
+ return null;
+ }
+
+ SimpleDateFormat f = new SimpleDateFormat("'P'yyyy'Y' MM'M' dd'D' 'T' HH'H' mm'M' ss'S.'SSS");
+
+ return f.format(date);
+ }
+
+ public synchronized String toTime(Date date)
+ {
+ if (date == null)
+ {
+ return null;
+ }
+
+ SimpleDateFormat f = new SimpleDateFormat("HH:mm:ss'.'SSS zz");
+
+ return f.format(date);
+ }
+
+ public synchronized String toDay(Date date)
+ {
+ if (date == null)
+ {
+ return null;
+ }
+
+ SimpleDateFormat f = new SimpleDateFormat("---dd zz");
+
+ return f.format(date);
+ }
+
+ public synchronized String toMonth(Date date)
+ {
+ if (date == null)
+ {
+ return null;
+ }
+
+ SimpleDateFormat f = new SimpleDateFormat("--MM zz");
+
+ return f.format(date);
+ }
+
+ public synchronized String toMonthDay(Date date)
+ {
+ if (date == null)
+ {
+ return null;
+ }
+
+ SimpleDateFormat f = new SimpleDateFormat("--MM-dd zz");
+
+ return f.format(date);
+ }
+
+ public synchronized String toYear(Date date)
+ {
+ if (date == null)
+ {
+ return null;
+ }
+
+ SimpleDateFormat f = new SimpleDateFormat("yyyy zz");
+
+ return f.format(date);
+ }
+
+ public synchronized String toYearMonth(Date date)
+ {
+ if (date == null)
+ {
+ return null;
+ }
+
+ SimpleDateFormat f = new SimpleDateFormat("yyyy-MM zz");
+
+ return f.format(date);
+ }
+
+ public synchronized String toYearMonthDay(Date date)
+ {
+ if (date == null)
+ {
+ return null;
+ }
+
+ SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd zz");
+
+ return f.format(date);
+ }
+
+ public synchronized String toDateTime(Calendar calendar)
+ {
+ if (calendar == null)
+ {
+ return null;
+ }
+
+ return toDateTime(calendar.getTime());
+ }
+
+ public synchronized String toDuration(Calendar calendar)
+ {
+ if (calendar == null)
+ {
+ return null;
+ }
+
+ return toDuration(calendar.getTime());
+ }
+
+ public synchronized String toTime(Calendar calendar)
+ {
+ if (calendar == null)
+ {
+ return null;
+ }
+
+ return toTime(calendar.getTime());
+ }
+
+ public synchronized String toDay(Calendar calendar)
+ {
+ if (calendar == null)
+ {
+ return null;
+ }
+
+ return toDay(calendar.getTime());
+ }
+
+ public synchronized String toMonth(Calendar calendar)
+ {
+ if (calendar == null)
+ {
+ return null;
+ }
+
+ return toMonth(calendar.getTime());
+ }
+
+ public synchronized String toMonthDay(Calendar calendar)
+ {
+ if (calendar == null)
+ {
+ return null;
+ }
+
+ return toMonthDay(calendar.getTime());
+ }
+
+ public synchronized String toYear(Calendar calendar)
+ {
+ if (calendar == null)
+ {
+ return null;
+ }
+
+ return toYear(calendar.getTime());
+ }
+
+ public synchronized String toYearMonth(Calendar calendar)
+ {
+ if (calendar == null)
+ {
+ return null;
+ }
+
+ return toYearMonth(calendar.getTime());
+ }
+
+ public synchronized String toYearMonthDay(Calendar calendar)
+ {
+ if (calendar == null)
+ {
+ return null;
+ }
+
+ return toYearMonthDay(calendar.getTime());
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/EqualityHelperImpl.java b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/EqualityHelperImpl.java
new file mode 100644
index 0000000000..e59a7d9f1c
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/EqualityHelperImpl.java
@@ -0,0 +1,70 @@
+/**
+ *
+ * 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.helper;
+
+
+import org.eclipse.emf.ecore.EAttribute;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EStructuralFeature;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.helper.EqualityHelper;
+
+
+/**
+ * A helper for comparing DataObjects.
+ */
+public class EqualityHelperImpl implements EqualityHelper
+{
+ public boolean equalShallow(DataObject dataObject1, DataObject dataObject2)
+ {
+ EcoreUtil.EqualityHelper equalityHelper = new EcoreUtil.EqualityHelper()
+ {
+ protected boolean haveEqualFeature(EObject eObject1, EObject eObject2, EStructuralFeature feature)
+ {
+ if (feature instanceof EAttribute)
+ {
+ boolean eIsSet = eObject1.eIsSet(feature);
+ if (eIsSet != eObject2.eIsSet(feature) || !haveEqualAttribute(eObject1, eObject2, (EAttribute)feature))
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ protected boolean equalFeatureMapValues(Object value1, Object value2, EStructuralFeature feature)
+ {
+ if (feature instanceof EAttribute)
+ {
+ return value1 == null ? value2 == null : value1.equals(value2);
+ }
+ return true;
+ }
+ };
+ return equalityHelper.equals((EObject)dataObject1, (EObject)dataObject2);
+ }
+
+ public boolean equal(DataObject dataObject1, DataObject dataObject2)
+ {
+ return EcoreUtil.equals((EObject)dataObject1, (EObject)dataObject2);
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/HelperProviderImpl.java b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/HelperProviderImpl.java
new file mode 100644
index 0000000000..24734cfaaf
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/HelperProviderImpl.java
@@ -0,0 +1,215 @@
+/**
+ *
+ * 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.helper;
+
+
+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.util.DataObjectUtil;
+import org.apache.tuscany.sdo.util.SDOUtil;
+
+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.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 helper INSTANCEs
+ */
+public class HelperProviderImpl extends HelperProvider
+{
+ protected CopyHelper copyHelper;
+
+ protected DataFactory dataFactory;
+
+ protected DataHelper dataHelper;
+
+ protected EqualityHelper equalityHelper;
+
+ protected TypeHelper typeHelper;
+
+ protected XMLHelper xmlHelper;
+
+ protected XSDHelper xsdHelper;
+
+ public HelperProviderImpl()
+ {
+ typeHelper = SDOUtil.createTypeHelper();
+ dataFactory = SDOUtil.createDataFactory(typeHelper);
+ xmlHelper = SDOUtil.createXMLHelper(typeHelper);
+ xsdHelper = SDOUtil.createXSDHelper(typeHelper);
+ copyHelper = new CopyHelperImpl();
+ equalityHelper = new EqualityHelperImpl();
+ dataHelper = new DataHelperImpl();
+ }
+
+ 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 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(DataObjectUtil.getXPath(dataObject));
+ objectOutput.writeObject(dataGraph);
+ }
+ else if (dataObject.getContainer() != null)
+ {
+ objectOutput.writeByte(0);
+ objectOutput.writeUTF(DataObjectUtil.getXPath(dataObject));
+ objectOutput.writeObject(dataObject.getRootObject());
+ }
+ else
+ {
+ // Root object
+ objectOutput.writeByte(1);
+
+ ByteArrayOutputStream compressedByteArrayOutputStream = new ByteArrayOutputStream();
+ GZIPOutputStream gzipOutputStream = new GZIPOutputStream(compressedByteArrayOutputStream);
+
+ xmlHelper.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];
+ objectInput.read(compressedBytes, 0, length);
+
+ GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(compressedBytes));
+
+ XMLDocument doc = xmlHelper.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-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SDOAnnotations.java b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SDOAnnotations.java
new file mode 100644
index 0000000000..816fb3bf9f
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SDOAnnotations.java
@@ -0,0 +1,51 @@
+/**
+ *
+ * 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.helper;
+
+public class SDOAnnotations
+{
+ public static final String COLON = ":";
+ public static final String COMMONJ_SDO_NS = "commonj.sdo";
+ public static final String COMMONJ_SDO_NS_PREFIX = "sdo";
+ public static final String SDO_JAVA_NS = "commonj.sdo/java";
+ public static final String SDO_JAVA_NS_PREFIX = "sdoJava";
+
+ public static final String JAVA_PACKAGE = SDO_JAVA_NS_PREFIX + COLON + "package";
+ public static final String ALIAS_NAMES = COMMONJ_SDO_NS_PREFIX + COLON + "aliasName";
+ public static final String READ_ONLY = COMMONJ_SDO_NS_PREFIX + COLON + "readOnly";
+ public static final String INSTANCE_CLASS = SDO_JAVA_NS_PREFIX + COLON + "instanceClass";
+ public static final String ABSTRACT_TYPE = "abstract";
+ public static final String PROPERTY_TYPE = COMMONJ_SDO_NS_PREFIX + COLON + "propertyType";
+ public static final String OPPOSITE_PROPERTY = COMMONJ_SDO_NS_PREFIX + COLON + "oppositeProperty";
+
+
+ //used by the annotations map to uniquely identify schema elements that need to be annotated
+ //and to store the corresponding annotations in a map
+ public static final String SCHEMA = "schema";
+ public static final String ELEMENT = "element";
+ public static final String COMPLEX_TYPE = "complexType";
+ public static final String SIMPLE_TYPE = "simpleType";
+ public static final String ATTRIBUTE = "attribute";
+
+ public static String makeAnnotationMapKey(String namespace, String schemaElementType, String nameAttrValue)
+ {
+ return namespace + schemaElementType + nameAttrValue;
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SDOExtendedMetaDataImpl.java b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SDOExtendedMetaDataImpl.java
new file mode 100644
index 0000000000..b8bbfcb2d5
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SDOExtendedMetaDataImpl.java
@@ -0,0 +1,99 @@
+/**
+ *
+ * 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.helper;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import org.apache.tuscany.sdo.SDOExtendedMetaData;
+import org.eclipse.emf.ecore.EAnnotation;
+import org.eclipse.emf.ecore.EModelElement;
+import org.eclipse.emf.ecore.EPackage.Registry;
+import org.eclipse.emf.ecore.util.BasicExtendedMetaData;
+
+public class SDOExtendedMetaDataImpl
+ extends BasicExtendedMetaData
+ implements SDOExtendedMetaData {
+
+
+
+ public SDOExtendedMetaDataImpl() {
+ super();
+ }
+
+ public SDOExtendedMetaDataImpl(Registry registry) {
+ super(registry);
+ }
+
+ public SDOExtendedMetaDataImpl(String annotationURI, Registry registry, Map annotationMap) {
+ super(annotationURI, registry, annotationMap);
+ }
+
+ public SDOExtendedMetaDataImpl(String annotationURI, Registry registry) {
+ super(annotationURI, registry);
+ }
+
+ /**
+ * Returns the listing of alias names as specified by the sdo:aliasNames
+ * property.
+ */
+ public List getAliasNames(EModelElement modelElement) {
+ EAnnotation eAnnotation = getAnnotation(modelElement, false);
+ List list = null;
+ if (eAnnotation != null) {
+ String aliasNames = (String)eAnnotation.getDetails().get("aliasNames");
+ if (aliasNames != null) {
+ list = new ArrayList();
+ StringTokenizer st = new StringTokenizer(aliasNames, " ");
+ while (st.hasMoreTokens()) {
+ String t = st.nextToken();
+ list.add(t);
+ }
+ }
+ }
+ return list;
+ }
+
+
+ public void setAliasNames(EModelElement modelElement, List aliasNames) {
+ if (aliasNames == null || aliasNames.isEmpty()) {
+ setAliasNames(modelElement, (String)null);
+ } else {
+ StringBuffer buf = new StringBuffer();
+ for (int n = 0; n < aliasNames.size(); n++) {
+ String name = (String) aliasNames.get(n);
+ buf.append(name);
+ buf.append(" ");
+ }
+ setAliasNames(modelElement, buf.toString());
+ }
+ }
+
+ /**
+ * Adds an alias name per sdo:aliasName
+ */
+ public void setAliasNames(EModelElement modelElement, String aliasNames) {
+ EAnnotation eAnnotation = getAnnotation(modelElement, true);
+ eAnnotation.getDetails().put("aliasNames", aliasNames);
+ }
+
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SDOXSDEcoreBuilder.java b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SDOXSDEcoreBuilder.java
new file mode 100644
index 0000000000..9ec309b547
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SDOXSDEcoreBuilder.java
@@ -0,0 +1,458 @@
+/**
+ *
+ * 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.helper;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.tuscany.sdo.SDOExtendedMetaData;
+import org.apache.tuscany.sdo.model.ModelPackage;
+import org.apache.tuscany.sdo.util.SDOUtil;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EClassifier;
+import org.eclipse.emf.ecore.EDataType;
+import org.eclipse.emf.ecore.EEnum;
+import org.eclipse.emf.ecore.EStructuralFeature;
+import org.eclipse.emf.ecore.util.ExtendedMetaData;
+import org.eclipse.xsd.XSDComplexTypeDefinition;
+import org.eclipse.xsd.XSDComponent;
+import org.eclipse.xsd.XSDConcreteComponent;
+import org.eclipse.xsd.XSDFeature;
+import org.eclipse.xsd.XSDNamedComponent;
+import org.eclipse.xsd.XSDSchema;
+import org.eclipse.xsd.XSDSimpleTypeDefinition;
+import org.eclipse.xsd.XSDTypeDefinition;
+import org.eclipse.xsd.ecore.XSDEcoreBuilder;
+import org.w3c.dom.Element;
+
+/**
+ * TODO:
+ * - Implement support for the SDO XSD Schema annotations
+ * - Override the default ecore type mappings
+ *
+ * DONE:
+ * - Override the default XSDEcoreBuilder name mangling
+ */
+public class SDOXSDEcoreBuilder extends XSDEcoreBuilder
+{
+ public SDOXSDEcoreBuilder(ExtendedMetaData extendedMetaData)
+ {
+ super(extendedMetaData);
+ populateTypeToTypeObjectMap(ModelPackage.eINSTANCE);
+ }
+
+ /**
+ * Overrides method in EMF. This will cause the SDO Properties to be in the
+ * order in which the Attributes appeared in the XSD.
+ */
+ protected boolean useSortedAttributes()
+ {
+ return false;
+ }
+
+ public EClassifier getEClassifier(XSDTypeDefinition xsdTypeDefinition) {
+ EClassifier eClassifier = null;
+ if (rootSchema.getSchemaForSchemaNamespace().equals(xsdTypeDefinition.getTargetNamespace())) {
+ eClassifier =
+ getBuiltInEClassifier(
+ xsdTypeDefinition.getURI(),
+ xsdTypeDefinition.getName());
+ } else {
+ eClassifier = super.getEClassifier(xsdTypeDefinition);
+ }
+ return eClassifier;
+ }
+
+ public EDataType getEDataType(XSDSimpleTypeDefinition xsdSimpleTypeDefinition) {
+ EDataType eClassifier = null;
+ if (rootSchema.getSchemaForSchemaNamespace().equals(xsdSimpleTypeDefinition.getTargetNamespace())) {
+ eClassifier =
+ (EDataType)getBuiltInEClassifier(
+ xsdSimpleTypeDefinition.getURI(),
+ xsdSimpleTypeDefinition.getName());
+ } else {
+ eClassifier = super.getEDataType(xsdSimpleTypeDefinition);
+ }
+ return (EDataType)eClassifier;
+ }
+
+ protected EClassifier getBuiltInEClassifier(String namespace, String name)
+ {
+ EClassifier eClassifier = (EClassifier)SDOUtil.getXSDSDOType(name);
+ if (eClassifier == null) {
+ eClassifier = super.getBuiltInEClassifier(namespace, name);
+ }
+ return eClassifier;
+ }
+
+ public EClass computeEClass(XSDComplexTypeDefinition xsdComplexTypeDefinition) {
+ EClass eclass = super.computeEClass(xsdComplexTypeDefinition);
+ String aliasNames = getEcoreAttribute(xsdComplexTypeDefinition.getElement(), "aliasName");
+ if (aliasNames != null) {
+ SDOExtendedMetaData.INSTANCE.setAliasNames(eclass, aliasNames);
+ }
+ return eclass;
+ }
+
+ protected EClassifier computeEClassifier(XSDTypeDefinition xsdTypeDefinition) {
+ EClassifier eclassifier = super.computeEClassifier(xsdTypeDefinition);
+ EClassifier etype = (EClassifier) typeToTypeObjectMap.get(eclassifier);
+ String aliasNames = getEcoreAttribute(xsdTypeDefinition.getElement(), "aliasName");
+ if (aliasNames != null) {
+ SDOExtendedMetaData.INSTANCE.setAliasNames(eclassifier, aliasNames);
+ if (etype != null) {
+ SDOExtendedMetaData.INSTANCE.setAliasNames(etype, aliasNames);
+ }
+ }
+ return eclassifier;
+ }
+
+ protected EDataType computeEDataType(XSDSimpleTypeDefinition xsdSimpleTypeDefinition) {
+ EDataType edatatype = super.computeEDataType(xsdSimpleTypeDefinition);
+ String aliasNames = getEcoreAttribute(xsdSimpleTypeDefinition.getElement(), "aliasName");
+ if (aliasNames != null) {
+ SDOExtendedMetaData.INSTANCE.setAliasNames(edatatype, aliasNames);
+ }
+ return edatatype;
+ }
+
+ protected EEnum computeEEnum(XSDSimpleTypeDefinition xsdSimpleTypeDefinition) {
+ return null;
+ }
+
+ protected EStructuralFeature createFeature(EClass eClass, String name, EClassifier type, XSDComponent xsdComponent, int minOccurs, int maxOccurs) {
+ EStructuralFeature feature =
+ super.createFeature(eClass, name, type, xsdComponent, minOccurs, maxOccurs);
+ feature.setName(name); // this is needed because super.createFeature() does EMF name mangling (toLower)
+ if (xsdComponent != null) {
+ String aliasNames = getEcoreAttribute(xsdComponent.getElement(), "aliasName");
+ if (aliasNames != null) {
+ SDOExtendedMetaData.INSTANCE.setAliasNames(feature, aliasNames);
+ }
+ }
+ return feature;
+ }
+
+ protected String getInstanceClassName(XSDTypeDefinition typeDefinition, EDataType baseEDataType) {
+ String name = getEcoreAttribute(typeDefinition, "extendedInstanceClass");
+ return (name != null) ? name : super.getInstanceClassName(typeDefinition, baseEDataType);
+ }
+
+ protected String getEcoreAttribute(Element element, String attribute)
+ {
+ String sdoAttribute = null;
+
+ if ("name".equals(attribute))
+ sdoAttribute = "name";
+ else if ("opposite".equals(attribute))
+ sdoAttribute = "oppositeProperty";
+ else if ("mixed".equals(attribute))
+ sdoAttribute = "sequence";
+ else if ("string".equals(attribute))
+ sdoAttribute = "string";
+ else if ("changeable".equals(attribute))
+ sdoAttribute = "readOnly";
+ else if ("aliasName".equals(attribute))
+ sdoAttribute = "aliasName";
+
+ if (sdoAttribute != null)
+ {
+ String value =
+ element != null && element.hasAttributeNS("commonj.sdo/xml", sdoAttribute) ?
+ element.getAttributeNS("commonj.sdo/xml", sdoAttribute) :
+ null;
+ if ("changeable".equals(attribute)) {
+ if ("true".equals(value)) value = "false";
+ else if ("false".equals(value)) value = "true";
+ }
+ return value;
+ }
+
+ if ("package".equals(attribute))
+ sdoAttribute = "package";
+ else if ("instanceClass".equals(attribute))
+ sdoAttribute = "instanceClass";
+ else if ("extendedInstanceClass".equals(attribute))
+ sdoAttribute = "extendedInstanceClass";
+ else if ("nestedInterfaces".equals(attribute))
+ sdoAttribute = "nestedInterfaces";
+
+ if (sdoAttribute != null)
+ {
+ return
+ element != null && element.hasAttributeNS("commonj.sdo/java", sdoAttribute) ?
+ element.getAttributeNS("commonj.sdo/java", sdoAttribute) :
+ null;
+ }
+
+ return super.getEcoreAttribute(element, attribute);
+ }
+
+ /*
+ protected String getEcoreAttribute(XSDConcreteComponent xsdConcreteComponent, String attribute)
+ {
+ String value = super.getEcoreAttribute(xsdConcreteComponent, attribute);
+ if ("package".equals(attribute) && value == null)
+ {
+ XSDSchema xsdSchema = (XSDSchema)xsdConcreteComponent;
+ value = getDefaultPackageName(xsdSchema.getTargetNamespace());
+ }
+ return value;
+ }
+ */
+
+ protected XSDTypeDefinition getEcoreTypeQNameAttribute(XSDConcreteComponent xsdConcreteComponent, String attribute)
+ {
+ if (xsdConcreteComponent == null) return null;
+ String sdoAttribute = null;
+
+ if ("reference".equals(attribute)) sdoAttribute = "propertyType";
+ if ("dataType".equals(attribute)) sdoAttribute = "dataType";
+
+ if (sdoAttribute != null)
+ {
+ Element element = xsdConcreteComponent.getElement();
+ return element == null ? null : getEcoreTypeQNameAttribute(xsdConcreteComponent, element, "commonj.sdo/xml", sdoAttribute);
+ }
+
+ return super.getEcoreTypeQNameAttribute(xsdConcreteComponent, attribute);
+ }
+
+ /**
+ * Override default EMF behavior so that the name is not mangled.
+ */
+ protected String validName(String name, int casing, String prefix) {
+ return name;
+ }
+
+ /**
+ * Override default EMF name mangling for anonymous types (simple and complex)
+ */
+ protected String validAliasName(XSDTypeDefinition xsdTypeDefinition, boolean isUpperCase) {
+ return getAliasName(xsdTypeDefinition);
+ }
+
+ protected String getAliasName(XSDNamedComponent xsdNamedComponent) {
+ String result = xsdNamedComponent.getName();
+ if (result == null)
+ {
+ XSDConcreteComponent container = xsdNamedComponent.getContainer();
+ if (container instanceof XSDNamedComponent)
+ {
+ result = getAliasName((XSDNamedComponent)container);
+ }
+ }
+ return result;
+ }
+
+ protected XSDTypeDefinition getEffectiveTypeDefinition(XSDComponent xsdComponent, XSDFeature xsdFeature) {
+ XSDTypeDefinition typeDef = getEcoreTypeQNameAttribute(xsdComponent, "dataType");
+
+ String isString = getEcoreAttribute(xsdComponent, xsdFeature, "string");
+ if ("true".equalsIgnoreCase(isString)) {
+ typeDef =
+ xsdFeature.resolveSimpleTypeDefinition(rootSchema.getSchemaForSchemaNamespace(), "string");
+ }
+ if (typeDef == null)
+ typeDef = xsdFeature.getType();
+ return typeDef;
+ }
+
+ /**
+ * Override EMF algorithm.
+ */
+ public String qualifiedPackageName(String namespace)
+ {
+ return getDefaultPackageName(namespace);
+ }
+
+ //Code below here to provide common URI to java packagname
+
+ public static String uncapNameStatic(String name)
+ {
+ if (name.length() == 0)
+ {
+ return name;
+ }
+ else
+ {
+ String lowerName = name.toLowerCase();
+ int i;
+ for (i = 0; i < name.length(); i++)
+ {
+ if (name.charAt(i) == lowerName.charAt(i))
+ {
+ break;
+ }
+ }
+ if (i > 1 && i < name.length() && !Character.isDigit(name.charAt(i)))
+ {
+ --i;
+ }
+ return name.substring(0, i).toLowerCase() + name.substring(i);
+ }
+ }
+
+ protected static String validNameStatic(String name, int casing, String prefix)
+ {
+ List parsedName = parseNameStatic(name, '_');
+ StringBuffer result = new StringBuffer();
+ for (Iterator i = parsedName.iterator(); i.hasNext(); )
+ {
+ String nameComponent = (String)i.next();
+ if (nameComponent.length() > 0)
+ {
+ if (result.length() > 0 || casing == UPPER_CASE)
+ {
+ result.append(Character.toUpperCase(nameComponent.charAt(0)));
+ result.append(nameComponent.substring(1));
+ }
+ else
+ {
+ result.append(nameComponent);
+ }
+ }
+ }
+
+ return
+ result.length() == 0 ?
+ prefix :
+ Character.isJavaIdentifierStart(result.charAt(0)) ?
+ casing == LOWER_CASE ?
+ uncapNameStatic(result.toString()) :
+ result.toString() :
+ prefix + result;
+ }
+
+ protected static List parseNameStatic(String sourceName, char separator)
+ {
+ List result = new ArrayList();
+ if (sourceName != null)
+ {
+ StringBuffer currentWord = new StringBuffer();
+ boolean lastIsLower = false;
+ for (int index = 0, length = sourceName.length(); index < length; ++index)
+ {
+ char curChar = sourceName.charAt(index);
+ if (!Character.isJavaIdentifierPart(curChar))
+ {
+ curChar = separator;
+ }
+ if (Character.isUpperCase(curChar) || (!lastIsLower && Character.isDigit(curChar)) || curChar == separator)
+ {
+ if (lastIsLower && currentWord.length() > 1 || curChar == separator && currentWord.length() > 0)
+ {
+ result.add(currentWord.toString());
+ currentWord = new StringBuffer();
+ }
+ lastIsLower = false;
+ }
+ else
+ {
+ if (!lastIsLower)
+ {
+ int currentWordLength = currentWord.length();
+ if (currentWordLength > 1)
+ {
+ char lastChar = currentWord.charAt(--currentWordLength);
+ currentWord.setLength(currentWordLength);
+ result.add(currentWord.toString());
+ currentWord = new StringBuffer();
+ currentWord.append(lastChar);
+ }
+ }
+ lastIsLower = true;
+ }
+
+ if (curChar != separator)
+ {
+ currentWord.append(curChar);
+ }
+ }
+
+ result.add(currentWord.toString());
+ }
+ return result;
+ }
+
+ public static String getDefaultPackageName(String targetNamespace)
+ {
+ if (targetNamespace == null)
+ return null;
+
+ URI uri = URI.createURI(targetNamespace);
+ List parsedName;
+ if (uri.isHierarchical())
+ {
+ String host = uri.host();
+ if (host != null && host.startsWith("www."))
+ {
+ host = host.substring(4);
+ }
+ parsedName = parseNameStatic(host, '.');
+ Collections.reverse(parsedName);
+ if (!parsedName.isEmpty())
+ {
+ parsedName.set(0, ((String)parsedName.get(0)).toLowerCase());
+ }
+
+ parsedName.addAll(parseNameStatic(uri.trimFileExtension().path(), '/'));
+ }
+ else
+ {
+ String opaquePart = uri.opaquePart();
+ int index = opaquePart.indexOf(":");
+ if (index != -1 && "urn".equalsIgnoreCase(uri.scheme()))
+ {
+ parsedName = parseNameStatic(opaquePart.substring(0, index), '-');
+ if (parsedName.size() > 0 && DOMAINS.contains(parsedName.get(parsedName.size() - 1)))
+ {
+ Collections.reverse(parsedName);
+ parsedName.set(0, ((String)parsedName.get(0)).toLowerCase());
+ }
+ parsedName.addAll(parseNameStatic(opaquePart.substring(index + 1), '/'));
+ }
+ else
+ {
+ parsedName = parseNameStatic(opaquePart, '/');
+ }
+ }
+
+ StringBuffer qualifiedPackageName = new StringBuffer();
+ for (Iterator i = parsedName.iterator(); i.hasNext(); )
+ {
+ String packageName = (String)i.next();
+ if (packageName.length() > 0)
+ {
+ if (qualifiedPackageName.length() > 0)
+ {
+ qualifiedPackageName.append('.');
+ }
+ qualifiedPackageName.append(validNameStatic(packageName, LOWER_CASE,"_"));
+ }
+ }
+
+ return qualifiedPackageName.toString().toLowerCase(); //make sure it's lower case .. we can't work with Axis if not.
+ }
+
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SchemaBuilder.java b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SchemaBuilder.java
new file mode 100644
index 0000000000..1597d0bd9e
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SchemaBuilder.java
@@ -0,0 +1,786 @@
+/**
+ *
+ * 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.helper;
+
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Vector;
+
+import javax.xml.namespace.QName;
+
+import org.apache.tuscany.sdo.util.SDOUtil;
+import org.eclipse.xsd.XSDAttributeDeclaration;
+import org.eclipse.xsd.XSDAttributeUse;
+import org.eclipse.xsd.XSDComplexTypeDefinition;
+import org.eclipse.xsd.XSDCompositor;
+import org.eclipse.xsd.XSDConstraint;
+import org.eclipse.xsd.XSDDerivationMethod;
+import org.eclipse.xsd.XSDElementDeclaration;
+import org.eclipse.xsd.XSDFactory;
+import org.eclipse.xsd.XSDForm;
+import org.eclipse.xsd.XSDImport;
+import org.eclipse.xsd.XSDInclude;
+import org.eclipse.xsd.XSDModelGroup;
+import org.eclipse.xsd.XSDParticle;
+import org.eclipse.xsd.XSDProcessContents;
+import org.eclipse.xsd.XSDSchema;
+import org.eclipse.xsd.XSDSchemaContent;
+import org.eclipse.xsd.XSDSimpleTypeDefinition;
+import org.eclipse.xsd.XSDTypeDefinition;
+import org.eclipse.xsd.XSDWildcard;
+
+import commonj.sdo.Property;
+import commonj.sdo.Type;
+import commonj.sdo.helper.XSDHelper;
+
+public class SchemaBuilder extends SDOAnnotations
+{
+ public static final String DEFAULT_SCHEMA_LOCATION = "";
+ public static final String NAME_SPACE_PREFIX = "stn_";
+ private static int prefixCount = 1;
+
+ //public static final String MIXED = "mixed";
+ //public static final String GROUP = "group";
+ public static final String EFEATURE_MAP_ENTRY = "EFeatureMapEntry";
+
+ private Map schemaMap = null;
+ protected Map targetNamespacePrefixMap = new Hashtable();
+ protected Map schemaLocationMap = null;
+ protected TypeTable typeTable = null;
+ protected XSDFactory xsdFactory = XSDFactory.eINSTANCE;
+
+
+ protected SchemaBuilder(Map schemaMap,
+ Map nsPrefixMap,
+ TypeTable typeTable,
+ Map schemaLocMap )
+ {
+ this.schemaMap = schemaMap;
+ this.targetNamespacePrefixMap = nsPrefixMap;
+ this.typeTable = typeTable;
+ this.schemaLocationMap = schemaLocMap;
+ }
+
+
+
+
+ private QName addAttribute2ComplexType(String targetNamespace,
+ XSDComplexTypeDefinition complexType,
+ Property aProperty)
+ {
+ QName attributeSchemaType = null;
+ String prefix = null;
+
+ try
+ {
+ attributeSchemaType = buildSchema(aProperty.getType());
+ }
+ catch ( IllegalArgumentException e )
+ {
+ //schema cannot be generated for this type as there exists an xsd already
+ //so include that original XSD
+ attributeSchemaType = new QName(aProperty.getType().getURI(),
+ aProperty.getType().getName(),
+ generatePrefix());
+ if ( aProperty.getType().isDataType() )
+ {
+ typeTable.addSimpleSchemaType(aProperty.getType().getName(), attributeSchemaType);
+
+ XSDSimpleTypeDefinition simpleType = xsdFactory.createXSDSimpleTypeDefinition();
+ simpleType.setName(aProperty.getType().getName());
+ simpleType.setTargetNamespace(aProperty.getType().getURI());
+ typeTable.addXSDTypeDef(attributeSchemaType.getNamespaceURI(),
+ attributeSchemaType.getLocalPart(),
+ simpleType);
+ }
+ else
+ {
+ typeTable.addComplexSchemaType(aProperty.getType().getURI(),
+ aProperty.getType().getName(),
+ attributeSchemaType);
+
+ XSDComplexTypeDefinition extComplexType = xsdFactory.createXSDComplexTypeDefinition();
+ extComplexType.setName(aProperty.getType().getName());
+ extComplexType.setTargetNamespace(aProperty.getType().getURI());
+ typeTable.addXSDTypeDef(attributeSchemaType.getNamespaceURI(),
+ attributeSchemaType.getLocalPart(),
+ extComplexType);
+ }
+ includeExtXSD(aProperty.getType());
+ }
+ //ensure than an import is done rightaway so that the right prefixes will be used by the
+ //attribute whose type is set as 'this type'. Otherwise when setting the type for the attribute
+ //there will be a duplicate prefix (like Q1 or Q2... ) that will be created
+ prefix = addImports((XSDSchema)schemaMap.get(targetNamespace), attributeSchemaType);
+
+ XSDAttributeDeclaration attribute = xsdFactory.createXSDAttributeDeclaration();
+ attribute.setName(aProperty.getName());
+ XSDAttributeUse orderDateAttributeUse = xsdFactory.createXSDAttributeUse();
+ orderDateAttributeUse.setContent(attribute);
+ complexType.getAttributeContents().add(orderDateAttributeUse);
+ attribute.updateElement();
+
+ if ( aProperty.getType().isDataType() )
+ {
+ attribute.setTypeDefinition((XSDSimpleTypeDefinition)typeTable.getXSDTypeDef(attributeSchemaType.getNamespaceURI(),
+ attributeSchemaType.getLocalPart()));
+
+ }
+ else
+ {
+ attribute.setTypeDefinition((XSDSimpleTypeDefinition)typeTable.getXSDTypeDef(
+ typeTable.getSimpleSchemaTypeName("URI").getNamespaceURI(),
+ typeTable.getSimpleSchemaTypeName("URI").getLocalPart()));
+
+ }
+
+ if ( aProperty.getDefault() != null )
+ {
+ attribute.setConstraint(XSDConstraint.DEFAULT_LITERAL);
+ attribute.setLexicalValue(aProperty.getDefault().toString());
+ }
+
+ addAnnotations(attribute, aProperty );
+ if ( !aProperty.getType().isDataType() )
+ {
+ String value = prefix + COLON + attributeSchemaType.getLocalPart();
+ attribute.getElement().setAttribute(PROPERTY_TYPE, value);
+ }
+
+ return attributeSchemaType;
+ }
+
+ private QName addElement2ComplexType(String targetNamespace,
+ XSDComplexTypeDefinition complexType,
+ Property aProperty)
+ {
+ String prefix = null;
+ QName elementSchemaType = null;
+ try
+ {
+ elementSchemaType = buildSchema(aProperty.getType());
+ }
+ catch ( IllegalArgumentException e )
+ {
+ //schema cannot be generated for this type as there exists an xsd already
+ //so include that original XSD
+ elementSchemaType = new QName(aProperty.getType().getURI(),
+ aProperty.getType().getName(),
+ generatePrefix());
+ if ( aProperty.getType().isDataType() )
+ {
+ typeTable.addSimpleSchemaType(aProperty.getType().getName(), elementSchemaType);
+
+ XSDSimpleTypeDefinition simpleType = xsdFactory.createXSDSimpleTypeDefinition();
+ simpleType.setName(aProperty.getType().getName());
+ simpleType.setTargetNamespace(aProperty.getType().getURI());
+ typeTable.addXSDTypeDef(elementSchemaType.getNamespaceURI(),
+ elementSchemaType.getLocalPart(),
+ simpleType);
+ }
+ else
+ {
+ typeTable.addComplexSchemaType(aProperty.getType().getURI(),
+ aProperty.getType().getName(),
+ elementSchemaType);
+ XSDComplexTypeDefinition extComplexType = xsdFactory.createXSDComplexTypeDefinition();
+ extComplexType.setName(aProperty.getType().getName());
+ extComplexType.setTargetNamespace(aProperty.getType().getURI());
+ typeTable.addXSDTypeDef(elementSchemaType.getNamespaceURI(),
+ elementSchemaType.getLocalPart(),
+ extComplexType);
+ }
+ includeExtXSD(aProperty.getType());
+ }
+
+ //ensure than an import is done rightaway so that the right prefixes will be used by the
+ //element whose type is set as 'this type'. Otherwise when setting the type for the element
+ //there will be a duplicate prefix (like Q1 or Q2... ) that will be created
+ prefix = addImports((XSDSchema)schemaMap.get(targetNamespace), elementSchemaType);
+
+ //XmlSchemaElement element = new XmlSchemaElement();
+ XSDElementDeclaration element = xsdFactory.createXSDElementDeclaration();
+ element.setName(aProperty.getName());
+
+ XSDParticle aParticle = xsdFactory.createXSDParticle();
+ aParticle.setContent(element);
+
+ ((XSDModelGroup)((XSDParticle)complexType.getContent()).getContent()).
+ getContents().add(aParticle);
+
+ element.updateElement();
+
+ if ( aProperty.isMany() )
+ {
+ aParticle.setMaxOccurs(-1);
+ aParticle.setMinOccurs(0);
+
+ }
+
+ if ( aProperty.isContainment() )
+ {
+ element.setTypeDefinition(typeTable.getXSDTypeDef(elementSchemaType.getNamespaceURI(),
+ elementSchemaType.getLocalPart()));
+ }
+ else
+ {
+ if ( !aProperty.getType().isDataType() )
+ {
+ QName qName = typeTable.getSimpleSchemaTypeName("URI");
+ element.setTypeDefinition(typeTable.getXSDTypeDef(qName.getNamespaceURI(),
+ qName.getLocalPart()));
+ }
+ }
+
+ addAnnotations(element, aProperty);
+ if ( !aProperty.isContainment() && !aProperty.getType().isDataType() )
+ {
+ String value = prefix + COLON + elementSchemaType.getLocalPart();
+ element.getElement().setAttribute(PROPERTY_TYPE, value);
+ }
+ return elementSchemaType;
+
+ }
+
+ private void addAnnotations(XSDSchemaContent xsdContent, Property aProperty)
+ {
+ if ( !aProperty.getAliasNames().isEmpty() )
+ {
+ addAliasNamesAnnotation(xsdContent, aProperty.getAliasNames());
+ }
+
+ if ( aProperty.isReadOnly() )
+ {
+ xsdContent.getElement().setAttribute(READ_ONLY, Boolean.toString(aProperty.isReadOnly()));
+ }
+
+ if ( aProperty.getOpposite() != null )
+ {
+ xsdContent.getElement().setAttribute(OPPOSITE_PROPERTY, aProperty.getOpposite().getName());
+ }
+ }
+
+
+ private QName buildComplexSchemaTypeContents(String targetNamespace,
+ XSDComplexTypeDefinition complexType,
+ Type dataType)
+ {
+ //clipProperties(dataType);
+ List properties = dataType.getDeclaredProperties();
+ Iterator iterator = properties.iterator();
+ Property aProperty;
+ QName propertySchemaTypeName = null;
+
+ while ( iterator.hasNext() )
+ {
+ aProperty = (Property)iterator.next();
+ if ( aProperty.isContainment() || aProperty.isMany() || !aProperty.getType().isDataType() )
+ {
+ propertySchemaTypeName = addElement2ComplexType(targetNamespace, complexType, aProperty);
+ }
+ else
+ {
+ propertySchemaTypeName = addAttribute2ComplexType(targetNamespace, complexType, aProperty);
+ }
+
+ /*if ( !EFEATURE_MAP_ENTRY.equals(aProperty.getType().getName()) )
+ {
+ addContents2ComplexType(targetNamespace, complexType, aProperty);
+ }*/
+ }
+
+ return propertySchemaTypeName;
+
+ }
+
+ public QName buildComplexSchemaType(Type dataType)
+ {
+ //this is called from buildSchema only if isXSD(dataType) is false
+ QName complexSchemaTypeName = null ;
+ if ( !dataType.isDataType() &&
+ (complexSchemaTypeName = typeTable.getComplexSchemaTypeName(dataType.getURI(), dataType.getName())) == null )
+ {
+ XSDSchema xmlSchema = getXmlSchema(dataType);
+ String targetNamespace = dataType.getURI();
+ String targetNamespacePrefix = (String)targetNamespacePrefixMap.get(targetNamespace);
+
+ complexSchemaTypeName = new QName(targetNamespace,
+ dataType.getName(),
+ targetNamespacePrefix);
+
+ XSDComplexTypeDefinition complexType = xsdFactory.createXSDComplexTypeDefinition();
+ complexType.setName(dataType.getName());
+ complexType.setTargetNamespace(targetNamespace);
+ complexType.setAbstract(dataType.isAbstract());
+
+ xmlSchema.getTypeDefinitions().add(complexType);
+ xmlSchema.getContents().add(complexType);
+
+ complexType.updateElement();
+
+ addAnnotations(complexType, dataType);
+
+ handleBaseExtn(xmlSchema, dataType, complexType);
+ handleSDOSequence(dataType, complexType);
+ handleSDOOpenType(dataType, complexType);
+
+ //add before constructing the contents because a content element could
+ //possibly be of type 'complexType'.
+ typeTable.addComplexSchemaType(dataType.getURI(), dataType.getName(), complexSchemaTypeName);
+ typeTable.addXSDTypeDef(dataType.getURI(), dataType.getName(), complexType);
+
+ //now compose the contents for this complex type
+ buildComplexSchemaTypeContents(targetNamespace, complexType, dataType);
+
+ //finally create a global element for this type
+ createGlobalElement(xmlSchema, complexType, complexSchemaTypeName);
+ }
+
+ return complexSchemaTypeName;
+ }
+
+ private void addAnnotations(XSDTypeDefinition xsdType, Type dataType)
+ {
+ if ( dataType.isAbstract() )
+ {
+ if ( xsdType instanceof XSDComplexTypeDefinition )
+ {
+ ((XSDComplexTypeDefinition)xsdType).setAbstract(dataType.isAbstract());
+ }
+ else
+ {
+ xsdType.getElement().setAttribute(ABSTRACT_TYPE,
+ Boolean.toString(dataType.isAbstract()));
+ }
+ }
+
+ //add alias names if it exists
+ addAliasNamesAnnotation(xsdType,
+ dataType.getAliasNames());
+
+ //add instanceClass annotation
+ if ( dataType.getInstanceClass() != null )
+ {
+ xsdType.getElement().setAttribute(INSTANCE_CLASS, dataType.getInstanceClass().getName());
+ }
+ }
+
+
+ private QName buildSimpleSchemaType(Type dataType)
+ {
+ QName simpleSchemaTypeName = null;
+ if ( dataType.isDataType() &&
+ (simpleSchemaTypeName = typeTable.getSimpleSchemaTypeName(dataType.getName()) ) == null )
+ {
+ XSDSchema xmlSchema = getXmlSchema(dataType);
+ XSDSimpleTypeDefinition simpleType = xsdFactory.createXSDSimpleTypeDefinition();
+ //set the name
+ simpleType.setName(dataType.getName());
+ simpleType.setTargetNamespace(dataType.getURI());
+ //set abstract=true if abstract
+ simpleSchemaTypeName = new QName(dataType.getURI(),
+ dataType.getName(),
+ (String)targetNamespacePrefixMap.get(dataType.getURI()));
+ xmlSchema.getContents().add(simpleType);
+ simpleType.updateElement();
+
+ addAnnotations(simpleType, dataType);
+
+ if ( !dataType.getBaseTypes().isEmpty() )
+ {
+ Type baseType = (Type)dataType.getBaseTypes().get(0);
+
+ QName baseSchemaType = null;
+
+ try
+ {
+ baseSchemaType = buildSchema(baseType);
+ }
+ catch ( IllegalArgumentException e )
+ {
+ //this means that the base type comes from a original xsd and hence not generated
+ baseSchemaType = new QName(baseType.getURI(),
+ baseType.getName(),
+ generatePrefix());
+
+ typeTable.addSimpleSchemaType(baseType.getName(), baseSchemaType);
+
+ XSDSimpleTypeDefinition baseTypeDef = xsdFactory.createXSDSimpleTypeDefinition();
+ baseTypeDef.setName(baseType.getName());
+ baseTypeDef.setTargetNamespace(baseType.getURI());
+ typeTable.addXSDTypeDef(baseType.getURI(), baseType.getName(), baseTypeDef);
+
+ //include external XSD locations
+ includeExtXSD(baseType);
+ }
+
+ simpleType.setBaseTypeDefinition((XSDSimpleTypeDefinition)typeTable.
+ getXSDTypeDef(baseSchemaType.getNamespaceURI(),baseSchemaType.getLocalPart()));
+ addImports(xmlSchema, baseSchemaType);
+ }
+
+
+ typeTable.addSimpleSchemaType(dataType.getName(), simpleSchemaTypeName);
+ typeTable.addXSDTypeDef(dataType.getURI(), dataType.getName(), simpleType);
+ }
+ return simpleSchemaTypeName;
+ }
+
+ private void includeExtXSD(Type dataType)
+ {
+ //now we know there is a type for which the xsd must come from outside
+ //create a schema for the namespace of this type and add an include in it for
+ //the xsd that is defined externally
+ XSDSchema xmlSchema = getXmlSchema(dataType);
+
+ //ideally there could be more than one external schema defintions for a namespace
+ //and hence schemalocations will be a list of locations
+ //List schemaLocations = (List)schemaLocationMap.get(dataType.getURI());
+
+ //since as per the specs the input to XSDHelper is a map of <String, String> allowing
+ //only one schemalocation for a namespace. So for now this single location will be
+ //picked up and put into a list
+ List schemaLocations = new Vector();
+ if ( schemaLocationMap.get(dataType.getURI()) != null )
+ {
+ schemaLocations.add(schemaLocationMap.get(dataType.getURI()));
+ }
+
+ if ( schemaLocations.size() <= 0 )
+ {
+ schemaLocations.add(DEFAULT_SCHEMA_LOCATION);
+ }
+
+ Object schemaContent = null;
+ Iterator includesIterator = xmlSchema.getContents().iterator();
+ Iterator schemaLocIterator = schemaLocations.iterator();
+ String aSchemaLocation = null;
+ boolean includeExists = false;
+ //include all external schema locations
+ while ( schemaLocIterator.hasNext() )
+ {
+ aSchemaLocation = (String)schemaLocIterator.next();
+ while ( includesIterator.hasNext() )
+ {
+ schemaContent = includesIterator.next();
+ if ( schemaContent instanceof XSDInclude )
+ {
+ if ( !includeExists && aSchemaLocation.equals(
+ ((XSDInclude)schemaContent).getSchemaLocation()
+ ))
+ {
+ includeExists = true;
+ }
+ }
+ }
+
+ if ( !includeExists )
+ {
+ XSDInclude includeElement = xsdFactory.createXSDInclude();
+ includeElement.setSchemaLocation(aSchemaLocation);
+ xmlSchema.getContents().add(0, includeElement);
+ }
+ }
+ }
+
+ protected QName buildSchema(Type dataType) throws IllegalArgumentException
+ {
+ QName schemaTypeName = null;
+
+ if ( dataType.isDataType() )
+ {
+ schemaTypeName = typeTable.getSimpleSchemaTypeName(dataType.getName());
+ }
+ else
+ {
+ schemaTypeName = typeTable.getComplexSchemaTypeName(dataType.getURI(), dataType.getName());
+ }
+
+ //attempt to generate only if we have not done it already..i.e the type is
+ //not found in the typetable
+ if ( schemaTypeName == null )
+ {
+ XSDHelper xsdHelper = SDOUtil.createXSDHelper(SDOUtil.createTypeHelper());
+
+ if ( !xsdHelper.isXSD( dataType ) )
+ {
+ if ( dataType.isDataType() )
+ {
+ schemaTypeName = buildSimpleSchemaType(dataType);
+ }
+ else
+ {
+ schemaTypeName = buildComplexSchemaType(dataType);
+ }
+ }
+ else
+ {
+ throw new IllegalArgumentException("Cannot generate XSD since SDO Type '" +
+ dataType.getName() + "' was orginally generated from XSD. Use original XSD");
+ }
+ }
+ return schemaTypeName;
+ }
+
+
+ private XSDSchema getXmlSchema(Type dataType)
+ {
+ XSDSchema xmlSchema = null;
+
+ if ((xmlSchema = (XSDSchema) schemaMap.get(dataType.getURI())) == null)
+ {
+ String targetNamespacePrefix = generatePrefix();
+
+ xmlSchema = xsdFactory.createXSDSchema();
+ xmlSchema.setTargetNamespace(dataType.getURI());
+ xmlSchema.setAttributeFormDefault(XSDForm.QUALIFIED_LITERAL);
+ xmlSchema.setElementFormDefault(XSDForm.QUALIFIED_LITERAL);
+
+ targetNamespacePrefixMap.put(dataType.getURI(), targetNamespacePrefix);
+ schemaMap.put(dataType.getURI(), xmlSchema);
+
+ xmlSchema.getQNamePrefixToNamespaceMap().put(TypeTable.XS_URI_PREFIX, TypeTable.XML_SCHEMA_URI);
+ xmlSchema.setSchemaForSchemaQNamePrefix(TypeTable.XS_URI_PREFIX);
+
+ xmlSchema.getQNamePrefixToNamespaceMap().put(targetNamespacePrefix, dataType.getURI());
+ //xmlSchema.setSchemaForSchemaQNamePrefix(targetNamespacePrefix);
+
+ addSDONamespaces(xmlSchema);
+ addPackageAnnotation(xmlSchema, dataType);
+ }
+ return xmlSchema;
+ }
+
+
+ private void addSDONamespaces(XSDSchema xmlSchema)
+ {
+ xmlSchema.getQNamePrefixToNamespaceMap().put(COMMONJ_SDO_NS_PREFIX, COMMONJ_SDO_NS);
+ //xmlSchema.setSchemaForSchemaQNamePrefix(COMMONJ_SDO_NS_PREFIX);
+
+ xmlSchema.getQNamePrefixToNamespaceMap().put(SDO_JAVA_NS_PREFIX, SDO_JAVA_NS);
+ //xmlSchema.setSchemaForSchemaQNamePrefix(SDO_JAVA_NS_PREFIX);
+ }
+
+
+ /**
+ * JAM convert first name of an attribute into UpperCase as an example if
+ * there is a instance variable called foo in a bean , then Jam give that as
+ * Foo so this method is to correct that error
+ *
+ * @param wrongName
+ * @return the right name, using english as the locale for case conversion
+ */
+ public static String getCorrectName(String wrongName)
+ {
+ if (wrongName.length() > 1) {
+ return wrongName.substring(0, 1).toLowerCase(Locale.ENGLISH)
+ + wrongName.substring(1, wrongName.length());
+ } else {
+ return wrongName.substring(0, 1).toLowerCase(Locale.ENGLISH);
+ }
+ }
+
+ private String addImports(XSDSchema xmlSchema, QName schemaTypeName)
+ {
+ String prefix = null;
+ Iterator iterator = xmlSchema.getQNamePrefixToNamespaceMap().keySet().iterator();
+ while ( iterator.hasNext() )
+ {
+ prefix = (String)iterator.next();
+
+ if ( schemaTypeName.getNamespaceURI().equals(
+ xmlSchema.getQNamePrefixToNamespaceMap().get(prefix)) )
+ {
+ return prefix;
+ }
+ }
+
+ //the following lines are executed only if a prefix was not found which implies that the
+ //schemaTypeName was not imported earlier and also it does not belong to the targetnamespace
+ XSDImport importElement = xsdFactory.createXSDImport();
+ importElement.setNamespace(schemaTypeName.getNamespaceURI());
+ xmlSchema.getContents().add(0, importElement);
+ prefix = schemaTypeName.getPrefix();
+ if ( prefix == null || prefix.length() <= 0 )
+ {
+ prefix = generatePrefix();
+ }
+ xmlSchema.getQNamePrefixToNamespaceMap().put(prefix, schemaTypeName.getNamespaceURI());
+
+ return prefix;
+ }
+
+ private void handleSDOSequence(Type datatype, XSDComplexTypeDefinition complexType)
+ {
+ if ( datatype.isSequenced() )
+ {
+ complexType.setMixed(true);
+ XSDModelGroup choice = xsdFactory.createXSDModelGroup();
+ choice.setCompositor(XSDCompositor.CHOICE_LITERAL);
+ XSDParticle aParticle = xsdFactory.createXSDParticle();
+ aParticle.setContent(choice);
+ aParticle.setMaxOccurs(-1);
+ complexType.setContent(aParticle);
+ }
+ else
+ {
+ //hack to handle group property as choice
+ /*if ( getPropertyStartsWithName(datatype.getDeclaredProperties(), GROUP).size() > 0 )
+ {
+ XmlSchemaChoice choice = new XmlSchemaChoice();
+ choice.setMaxOccurs(Long.MAX_VALUE);
+ complexType.setParticle(choice);
+ }
+ else*/
+ {
+ XSDModelGroup sequence = xsdFactory.createXSDModelGroup();
+ sequence.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
+ XSDParticle aParticle = xsdFactory.createXSDParticle();
+ aParticle.setContent(sequence);
+ complexType.setContent(aParticle);
+ }
+ }
+ }
+
+ private void handleSDOOpenType(Type datatype, XSDComplexTypeDefinition complexType)
+ {
+ if ( datatype.isOpen() /*&&
+ getPropertyStartsWithName(datatype.getDeclaredProperties(), GROUP).size() <= 0 */)
+ {
+ XSDWildcard elementWildcard = xsdFactory.createXSDWildcard();
+ elementWildcard.getLexicalNamespaceConstraint().add("##other");
+ elementWildcard.setProcessContents(XSDProcessContents.LAX_LITERAL);
+ // Create a particle to hold the wildcard.
+ XSDParticle wildcardParticle = xsdFactory.createXSDParticle();
+ wildcardParticle.setContent(elementWildcard);
+ wildcardParticle.setMaxOccurs(-1);
+ ((XSDModelGroup)((XSDParticle)complexType.getContent()).getContent()).
+ getContents().add(wildcardParticle);
+
+ XSDWildcard attributeWildcard = xsdFactory.createXSDWildcard();
+ attributeWildcard.getLexicalNamespaceConstraint().add("##other");
+ attributeWildcard.setProcessContents(XSDProcessContents.LAX_LITERAL);
+ complexType.setAttributeWildcard(attributeWildcard);
+ }
+ }
+
+ private void handleBaseExtn(XSDSchema xmlSchema,
+ Type datatype,
+ XSDComplexTypeDefinition complexType)
+ {
+ if ( datatype.getBaseTypes().size() > 0 )
+ {
+ Type baseType = (Type)datatype.getBaseTypes().get(0);
+ QName baseSchemaType = null;
+
+ try
+ {
+ baseSchemaType = buildSchema(baseType);
+ }
+ catch ( IllegalArgumentException e )
+ {
+ //schema cannot be generated for this type as there exists an xsd already
+ //so include that original XSD
+ baseSchemaType = new QName(baseType.getURI(),
+ baseType.getName(),
+ generatePrefix());
+
+ XSDSimpleTypeDefinition baseTypeDef = xsdFactory.createXSDSimpleTypeDefinition();
+ baseTypeDef.setName(baseType.getName());
+ baseTypeDef.setTargetNamespace(baseType.getURI());
+ typeTable.addXSDTypeDef(baseType.getURI(), baseType.getName(), baseTypeDef);
+
+ includeExtXSD(baseType);
+ }
+
+ complexType.setDerivationMethod(XSDDerivationMethod.EXTENSION_LITERAL);
+
+ if ( baseType.isDataType() )
+ {
+ XSDSimpleTypeDefinition anonymousSimpleTypeDefinition
+ = xsdFactory.createXSDSimpleTypeDefinition();
+ anonymousSimpleTypeDefinition.setBaseTypeDefinition((XSDSimpleTypeDefinition)typeTable.
+ getXSDTypeDef(baseSchemaType.getNamespaceURI(),baseSchemaType.getLocalPart()));
+ complexType.setContent(anonymousSimpleTypeDefinition);
+ }
+ else
+ {
+ complexType.setBaseTypeDefinition((XSDSimpleTypeDefinition)typeTable.
+ getXSDTypeDef(baseSchemaType.getNamespaceURI(),baseSchemaType.getLocalPart()));
+
+ }
+
+ addImports(xmlSchema, baseSchemaType);
+ }
+ }
+
+ private String formGlobalElementName(String typeName)
+ {
+ String firstChar = typeName.substring(0,1);
+ return typeName.replaceFirst(firstChar, firstChar.toLowerCase());
+ }
+
+ private void createGlobalElement(XSDSchema xmlSchema,
+ XSDComplexTypeDefinition complexType,
+ QName schemaElementName )
+ {
+ XSDElementDeclaration globalElement = xsdFactory.createXSDElementDeclaration();
+ globalElement.setTargetNamespace(xmlSchema.getTargetNamespace());
+ globalElement.setName(formGlobalElementName(complexType.getName()));
+ globalElement.setTypeDefinition
+ (typeTable.getXSDTypeDef(schemaElementName.getNamespaceURI(),
+ schemaElementName.getLocalPart()));
+ xmlSchema.getContents().add(globalElement);
+ xmlSchema.getElementDeclarations().add(globalElement);
+ }
+
+ private void addAliasNamesAnnotation(XSDSchemaContent typeDef,
+ List aliasNames)
+ {
+ if ( !aliasNames.isEmpty() )
+ {
+ StringBuffer sb = new StringBuffer();
+ Iterator iterator = aliasNames.iterator();
+ while ( iterator.hasNext() )
+ {
+ sb.append(iterator.next());
+ }
+ typeDef.getElement().setAttribute(ALIAS_NAMES, sb.toString());
+ }
+ }
+
+ private void addPackageAnnotation(XSDSchema xmlSchema, Type dataType)
+ {
+ if ( dataType.getInstanceClass() != null )
+ {
+ xmlSchema.updateElement();
+ xmlSchema.getElement().setAttribute(JAVA_PACKAGE,
+ dataType.getInstanceClass().getPackage().getName());
+ }
+ }
+
+ private String generatePrefix()
+ {
+ return NAME_SPACE_PREFIX + prefixCount++;
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/TypeHelperImpl.java b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/TypeHelperImpl.java
new file mode 100644
index 0000000000..eee90b866f
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/TypeHelperImpl.java
@@ -0,0 +1,286 @@
+/**
+ *
+ * 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.helper;
+
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.tuscany.sdo.model.impl.ModelPackageImpl;
+import org.apache.tuscany.sdo.model.java.impl.JavaPackageImpl;
+import org.apache.tuscany.sdo.util.SDOUtil;
+import org.apache.tuscany.sdo.util.metadata.impl.MetadataPackageImpl;
+import org.eclipse.emf.ecore.EClassifier;
+import org.eclipse.emf.ecore.EPackage;
+import org.eclipse.emf.ecore.util.ExtendedMetaData;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.Property;
+import commonj.sdo.Type;
+import commonj.sdo.helper.TypeHelper;
+
+
+/**
+ * Look up a Type given the uri and typeName or interfaceClass.
+ * SDO Types are available through the
+ * getType("commonj.sdo", typeName) method.
+ * Defines Types from DataObjects.
+ */
+public class TypeHelperImpl implements TypeHelper
+{
+ protected ExtendedMetaData extendedMetaData;
+
+ public ExtendedMetaData getExtendedMetaData()
+ {
+ return extendedMetaData;
+ }
+
+ public TypeHelperImpl(ExtendedMetaData extendedMetaData)
+ {
+ this.extendedMetaData = extendedMetaData;
+
+ // Register the standard (predefined) SDO types
+ ModelPackageImpl.init();
+ JavaPackageImpl.init();
+ MetadataPackageImpl.init();
+ }
+
+ public Type getType(String uri, String typeName)
+ {
+ EPackage ePackage = extendedMetaData.getPackage(uri);
+ if (ePackage != null)
+ {
+ EClassifier eClassifier = ePackage.getEClassifier(typeName);
+ if (eClassifier == null)
+ {
+ eClassifier = extendedMetaData.getType(ePackage, typeName);
+ }
+ return (Type)eClassifier;
+ }
+ return null;
+ }
+
+ public Type getType(Class interfaceClass)
+ {
+ Type type = SDOUtil.getJavaSDOType(interfaceClass);
+ if (type != null)
+ {
+ return type;
+ }
+
+ //TODO more efficient implementation ... this is a really bad one!
+ for (Iterator iter = EPackage.Registry.INSTANCE.values().iterator(); iter.hasNext(); )
+ {
+ Object value = iter.next();
+ if (value instanceof EPackage)
+ {
+ EPackage ePackage = (EPackage)value;
+ for (Iterator iter2 = ePackage.getEClassifiers().iterator(); iter2.hasNext(); )
+ {
+ EClassifier eClassifier = (EClassifier)iter2.next();
+ if (eClassifier.getInstanceClass() == interfaceClass)
+ {
+ return (Type)eClassifier;
+ }
+ }
+ }
+ }
+
+ return null;
+ }
+
+ public Type define(DataObject type)
+ {
+ if (!(type instanceof org.apache.tuscany.sdo.model.Type))
+ throw new IllegalArgumentException();
+ org.apache.tuscany.sdo.model.Type modeledType = (org.apache.tuscany.sdo.model.Type)type;
+
+ boolean isDataType = modeledType.isDataType();
+ Type definedType = SDOUtil.createType(this, modeledType.getUri(), modeledType.getName(), isDataType);
+ if (definedType == null)
+ {
+ // If type already defined, return the existing Type.
+ return getType(modeledType.getUri(), modeledType.getName());
+ }
+
+ SDOUtil.setJavaClassName(definedType, modeledType.getInstanceClassName());
+
+ if (!isDataType)
+ {
+ SDOUtil.setSequenced(definedType, modeledType.isSequenced());
+ SDOUtil.setOpen(definedType, modeledType.isOpen());
+ SDOUtil.setAbstract(definedType, modeledType.isAbstract());
+
+ for (Iterator iter = modeledType.getBaseType().iterator(); iter.hasNext(); )
+ {
+ Type baseType = getDefinedType((org.apache.tuscany.sdo.model.Type)iter.next());
+ SDOUtil.addBaseType(definedType, baseType);
+ }
+
+ for (Iterator iter = modeledType.getAliasName().iterator(); iter.hasNext(); )
+ {
+ String aliasName = (String)iter.next();
+ SDOUtil.addAliasName(definedType, aliasName);
+ }
+
+ for (Iterator iter = modeledType.getProperty().iterator(); iter.hasNext(); )
+ {
+ org.apache.tuscany.sdo.model.Property modeledProperty = (org.apache.tuscany.sdo.model.Property)iter.next();
+
+ Type propertyType = getDefinedType(modeledProperty.getType_());
+ Property definedProperty = SDOUtil.createProperty(definedType, modeledProperty.getName(), propertyType);
+
+ SDOUtil.setMany(definedProperty, modeledProperty.isMany());
+ SDOUtil.setDefault(definedProperty, modeledProperty.getDefault_());
+ SDOUtil.setReadOnly(definedProperty, modeledProperty.isReadOnly());
+
+ for (Iterator iter2 = modeledProperty.getAliasName().iterator(); iter2.hasNext(); )
+ {
+ String aliasName = (String)iter2.next();
+ SDOUtil.addAliasName(definedProperty, aliasName);
+ }
+
+ if (!propertyType.isDataType())
+ {
+ SDOUtil.setContainment(definedProperty, modeledProperty.isContainment());
+ if (modeledProperty.getOpposite_() != null)
+ {
+ SDOUtil.setOpposite(definedProperty, getDefinedProperty(modeledProperty.getOpposite_()));
+ }
+ }
+ }
+ } // if (!isDataType)
+
+ return definedType;
+ }
+
+ public List /*Type*/define(List /*DataObject*/types)
+ {
+ int count = types.size();
+ List definedTypes = new ArrayList(count);
+ for (int i = 0; i < count; i++)
+ {
+ definedTypes.add(define((DataObject)types.get(i)));
+ }
+ return definedTypes;
+ }
+
+ protected Type getDefinedType(org.apache.tuscany.sdo.model.Type modeledType)
+ {
+ if (modeledType instanceof Type)
+ {
+ return (Type)modeledType;
+ }
+ else
+ {
+ EClassifier eClassifier = extendedMetaData.getType(modeledType.getUri(), modeledType.getName());
+ if (eClassifier != null)
+ {
+ return (Type)eClassifier;
+ }
+ else
+ {
+ return define((DataObject)modeledType);
+ }
+ }
+ }
+
+ protected Property getDefinedProperty(org.apache.tuscany.sdo.model.Property modeledProperty)
+ {
+ if (modeledProperty instanceof Property)
+ {
+ return (Property)modeledProperty;
+ }
+ else
+ {
+ DataObject modeledContainingType = ((DataObject)modeledProperty).getContainer();
+
+ Type definedContainingType = getDefinedType((org.apache.tuscany.sdo.model.Type)modeledContainingType);
+ String propertyName = modeledProperty.getName();
+
+ return definedContainingType.getProperty(propertyName);
+ }
+ }
+
+ public static final String TUSCANY_NO_URI="http://tuscany-no-uri";
+
+ public Property defineOpenContentProperty(String uri, DataObject property)
+ {
+ // validate property and get type
+ if (!(property instanceof org.apache.tuscany.sdo.model.impl.PropertyImpl))
+ throw new IllegalArgumentException();
+ org.apache.tuscany.sdo.model.Property modeledProperty = (org.apache.tuscany.sdo.model.Property)property;
+ Type propertyType = getDefinedType(modeledProperty.getType_());
+
+ if (uri == null) uri = TUSCANY_NO_URI;
+
+ // get/create document root
+ EPackage ePackage = extendedMetaData.getPackage(uri);
+ Type documentRoot =
+ ePackage != null ? (Type)extendedMetaData.getType(extendedMetaData.getPackage(uri), "") : null;
+ if (documentRoot == null)
+ {
+ documentRoot = SDOUtil.createType(this, uri, null, false);
+ }
+
+ // Determine if property already exists
+ Property newProperty = documentRoot.getProperty(modeledProperty.getName());
+ if (newProperty == null)
+ {
+ //FB TBD ... is this code really supposed to be the same as in define()? If so, factor it out and reuse
+
+ // Create the new property 'under' the document root.....
+ newProperty = SDOUtil.createProperty(documentRoot, modeledProperty.getName(), propertyType);
+
+ // Propagate the modeled property's attributes
+ SDOUtil.setMany(newProperty, modeledProperty.isMany());
+ SDOUtil.setDefault(newProperty, modeledProperty.getDefault_());
+ SDOUtil.setReadOnly(newProperty, modeledProperty.isReadOnly());
+ for (Iterator iter = modeledProperty.getAliasName().iterator(); iter.hasNext();)
+ {
+ String aliasName = (String)iter.next();
+ SDOUtil.addAliasName(newProperty, aliasName);
+ }
+ if (!propertyType.isDataType())
+ {
+ SDOUtil.setContainment(newProperty, modeledProperty.isContainment());
+ if (modeledProperty.getOpposite_() != null)
+ {
+ SDOUtil.setOpposite(newProperty, getDefinedProperty(modeledProperty.getOpposite_()));
+ }
+ }
+ }
+ else
+ {
+ // if property already exists, validate the expected type
+ if (!newProperty.getType().equals(propertyType))
+ throw new IllegalArgumentException();
+ }
+
+ return newProperty;
+ }
+
+ public Property getOpenContentProperty(String uri, String propertyName)
+ {
+ //FB TBD ... in the future we will allow elements or attributes - see SDOUtil.createProperty()
+ return (Property)extendedMetaData.getElement(uri, propertyName);
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/TypeTable.java b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/TypeTable.java
new file mode 100644
index 0000000000..a28492e28e
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/TypeTable.java
@@ -0,0 +1,254 @@
+/**
+ *
+ * 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.helper;
+
+import java.util.ArrayList;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Vector;
+
+import javax.xml.namespace.QName;
+
+//import org.apache.axiom.om.OMElement;
+import org.eclipse.xsd.XSDFactory;
+import org.eclipse.xsd.XSDTypeDefinition;
+import org.w3c.dom.Element;
+
+
+
+public class TypeTable
+{
+ public static final String XML_SCHEMA_URI = "http://www.w3.org/2001/XMLSchema";
+ public static final String XS_URI_PREFIX = "xs";
+ public static final QName XS_QNAME = new QName(XML_SCHEMA_URI, "schema", XS_URI_PREFIX);
+ public static final String DELIMITER = "#";
+
+ private Hashtable simpleXSDTypes;
+ private Hashtable complexXSDTypes;
+ private Hashtable xsdTypeDefs;
+
+ public static String asQualifiedName(String uri, String typeName)
+ {
+ return (uri + DELIMITER + typeName);
+ }
+
+
+ public TypeTable()
+ {
+ simpleXSDTypes = new Hashtable();
+ complexXSDTypes = new Hashtable();
+ xsdTypeDefs = new Hashtable();
+ populateSimpleXSDTypes();
+ populateStdSDOTypes();
+ }
+
+ private void populateStdSDOTypes()
+ {
+ simpleXSDTypes.put("Boolean",
+ new QName(XML_SCHEMA_URI, "boolean", XS_URI_PREFIX));
+ simpleXSDTypes.put("Byte",
+ new QName(XML_SCHEMA_URI, "byte", XS_URI_PREFIX));
+ simpleXSDTypes.put("Bytes",
+ new QName(XML_SCHEMA_URI, "hexBinary", XS_URI_PREFIX));
+ simpleXSDTypes.put("Character",
+ new QName(XML_SCHEMA_URI, "string", XS_URI_PREFIX));
+ simpleXSDTypes.put("DataObject",
+ new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
+ simpleXSDTypes.put("Date",
+ new QName(XML_SCHEMA_URI, "dateTime", XS_URI_PREFIX));
+ simpleXSDTypes.put("Day",
+ new QName(XML_SCHEMA_URI, "gDay", XS_URI_PREFIX));
+ simpleXSDTypes.put("Decimal",
+ new QName(XML_SCHEMA_URI, "decimal", XS_URI_PREFIX));
+ simpleXSDTypes.put("Double",
+ new QName(XML_SCHEMA_URI, "double", XS_URI_PREFIX));
+ simpleXSDTypes.put("Duration",
+ new QName(XML_SCHEMA_URI, "duration", XS_URI_PREFIX));
+ simpleXSDTypes.put("Float",
+ new QName(XML_SCHEMA_URI, "float", XS_URI_PREFIX));
+ simpleXSDTypes.put("Int",
+ new QName(XML_SCHEMA_URI, "int", XS_URI_PREFIX));
+ simpleXSDTypes.put("Integer",
+ new QName(XML_SCHEMA_URI, "integer", XS_URI_PREFIX));
+ simpleXSDTypes.put("Long",
+ new QName(XML_SCHEMA_URI, "long", XS_URI_PREFIX));
+ simpleXSDTypes.put("Month",
+ new QName(XML_SCHEMA_URI, "gMonth", XS_URI_PREFIX));
+ simpleXSDTypes.put("monthDay",
+ new QName(XML_SCHEMA_URI, "gMonthDay", XS_URI_PREFIX));
+ simpleXSDTypes.put("Object",
+ new QName(XML_SCHEMA_URI, "anySimpleType", XS_URI_PREFIX));
+ simpleXSDTypes.put("Short",
+ new QName(XML_SCHEMA_URI, "short", XS_URI_PREFIX));
+ simpleXSDTypes.put("String",
+ new QName(XML_SCHEMA_URI, "string", XS_URI_PREFIX));
+ simpleXSDTypes.put("Strings",
+ new QName(XML_SCHEMA_URI, "string", XS_URI_PREFIX));
+ simpleXSDTypes.put("Time",
+ new QName(XML_SCHEMA_URI, "time", XS_URI_PREFIX));
+ simpleXSDTypes.put("Year",
+ new QName(XML_SCHEMA_URI, "gYear", XS_URI_PREFIX));
+ simpleXSDTypes.put("YearMonth",
+ new QName(XML_SCHEMA_URI, "gYearMonth", XS_URI_PREFIX));
+ simpleXSDTypes.put("YearMonthDay",
+ new QName(XML_SCHEMA_URI, "date", XS_URI_PREFIX));
+ simpleXSDTypes.put("URI",
+ new QName(XML_SCHEMA_URI, "anyURI", XS_URI_PREFIX));
+ }
+
+ private void populateSimpleXSDTypes() {
+ //todo pls use the types from org.apache.ws.commons.schema.constants.Constants
+ simpleXSDTypes.put("int",
+ new QName(XML_SCHEMA_URI, "int", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.lang.String",
+ new QName(XML_SCHEMA_URI, "string", XS_URI_PREFIX));
+ simpleXSDTypes.put("boolean",
+ new QName(XML_SCHEMA_URI, "boolean", XS_URI_PREFIX));
+ simpleXSDTypes.put("float",
+ new QName(XML_SCHEMA_URI, "float", XS_URI_PREFIX));
+ simpleXSDTypes.put("double",
+ new QName(XML_SCHEMA_URI, "double", XS_URI_PREFIX));
+ simpleXSDTypes.put("short",
+ new QName(XML_SCHEMA_URI, "short", XS_URI_PREFIX));
+ simpleXSDTypes.put("long",
+ new QName(XML_SCHEMA_URI, "long", XS_URI_PREFIX));
+ simpleXSDTypes.put("byte",
+ new QName(XML_SCHEMA_URI, "byte", XS_URI_PREFIX));
+ simpleXSDTypes.put("char",
+ new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.lang.Integer",
+ new QName(XML_SCHEMA_URI, "int", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.lang.Double",
+ new QName(XML_SCHEMA_URI, "double", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.lang.Float",
+ new QName(XML_SCHEMA_URI, "float", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.lang.Long",
+ new QName(XML_SCHEMA_URI, "long", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.lang.Character",
+ new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.lang.Boolean",
+ new QName(XML_SCHEMA_URI, "boolean", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.lang.Byte",
+ new QName(XML_SCHEMA_URI, "byte", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.lang.Short",
+ new QName(XML_SCHEMA_URI, "short", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.util.Date",
+ new QName(XML_SCHEMA_URI, "dateTime", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.util.Calendar",
+ new QName(XML_SCHEMA_URI, "dateTime", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.lang.Object",
+ new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
+ simpleXSDTypes.put("java.math.BigDecimal",
+ new QName(XML_SCHEMA_URI, "decimal", XS_URI_PREFIX));
+
+ // Any types
+ simpleXSDTypes.put(Element.class.getName(),
+ new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
+ simpleXSDTypes.put(ArrayList.class.getName(),
+ new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
+ simpleXSDTypes.put(Vector.class.getName(),
+ new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
+ simpleXSDTypes.put(List.class.getName(),
+ new QName(XML_SCHEMA_URI, "anyType", XS_URI_PREFIX));
+ }
+
+ public QName getStdSdoType(String typename)
+ {
+ return (QName) simpleXSDTypes.get(typename);
+ }
+
+ public QName getComplexSchemaTypeName(String sdoURI, String sdoTypeName)
+ {
+ return (QName) complexXSDTypes.get(asQualifiedName(sdoURI, sdoTypeName));
+ }
+
+ public boolean isSimpleType(String typeName)
+ {
+ Iterator keys = simpleXSDTypes.keySet().iterator();
+ while (keys.hasNext()) {
+ String s = (String) keys.next();
+ if (s.equals(typeName)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public QName getSimpleSchemaTypeName(String typename)
+ {
+ return (QName) simpleXSDTypes.get(typename);
+ }
+
+ public void addSimpleSchemaType(String typeName, QName schemaType)
+ {
+ simpleXSDTypes.put(typeName, schemaType);
+ }
+
+
+ public void addComplexSchemaType(String namespaceURI, String name, QName schemaType)
+ {
+ complexXSDTypes.put(asQualifiedName(namespaceURI, name), schemaType);
+ }
+
+
+ public QName getQNamefortheType(String namespaceURI, String typeName)
+ {
+ if ( XML_SCHEMA_URI.equals(namespaceURI))
+ {
+ return getSimpleSchemaTypeName(typeName);
+ }
+ else
+ {
+ return getComplexSchemaTypeName(namespaceURI, typeName);
+ }
+ }
+
+ public void addXSDTypeDef(String namespaceURI, String typeName, XSDTypeDefinition aTypeDef)
+ {
+ if ( namespaceURI != null && typeName != null && aTypeDef != null )
+ {
+ xsdTypeDefs.put(asQualifiedName(namespaceURI, typeName), aTypeDef);
+ }
+ }
+
+ public XSDTypeDefinition getXSDTypeDef(String namespaceURI, String typeName)
+ {
+ XSDTypeDefinition typeDef = null;
+ if ( namespaceURI != null && typeName != null )
+ {
+ if ( XML_SCHEMA_URI.equals(namespaceURI) )
+ {
+ if ( ( typeDef = (XSDTypeDefinition)xsdTypeDefs.get(asQualifiedName(namespaceURI, typeName)) ) == null )
+ {
+ typeDef = XSDFactory.eINSTANCE.createXSDSimpleTypeDefinition();
+ typeDef.setName(typeName);
+ typeDef.setTargetNamespace(namespaceURI);
+ addXSDTypeDef(namespaceURI, typeName, typeDef);
+ }
+ }
+ else
+ {
+ typeDef = (XSDTypeDefinition)xsdTypeDefs.get(asQualifiedName(namespaceURI, typeName));
+ }
+ }
+ return typeDef;
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java
new file mode 100644
index 0000000000..ce0031940f
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java
@@ -0,0 +1,536 @@
+/**
+ *
+ * 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.helper;
+
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.tuscany.sdo.SimpleAnyTypeDataObject;
+import org.apache.tuscany.sdo.util.DataObjectUtil;
+import org.apache.tuscany.sdo.util.SDOUtil;
+import org.apache.tuscany.sdo.util.resource.SDOXMLResourceImpl;
+import org.eclipse.emf.common.util.EMap;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EAttribute;
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EReference;
+import org.eclipse.emf.ecore.EStructuralFeature;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.emf.ecore.util.ExtendedMetaData;
+import org.eclipse.emf.ecore.util.FeatureMap;
+import org.eclipse.emf.ecore.xmi.XMLResource;
+import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
+import org.xml.sax.InputSource;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.Type;
+import commonj.sdo.helper.XMLDocument;
+
+
+/**
+ * Represents an XML Document containing a tree of DataObjects.
+ *
+ * An example XMLDocument fragment is:
+ * <?xml version="1.0"?>
+ * <purchaseOrder orderDate="1999-10-20">
+ *
+ * created from this XML Schema fragment:
+ * <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
+ * <xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
+ * <xsd:complexType name="PurchaseOrderType">
+ *
+ * Upon loading this XMLDocument:
+ * DataObject is an instance of Type PurchaseOrderType.
+ * RootElementURI is null because the XSD has no targetNamespace URI.
+ * RootElementName is purchaseOrder.
+ * Encoding is null because the document did not specify an encoding.
+ * XMLDeclaration is true because the document contained an XML declaration.
+ * XMLVersion is 1.0
+ * SchemaLocation and noNamespaceSchemaLocation are null because they are
+ * not specified in the document.
+ *
+ * When saving the root element, if the type of the root dataObject is not the
+ * type of global element specified by rootElementURI and rootElementName,
+ * or if a global element does not exist for rootElementURI and rootElementName,
+ * then an xsi:type declaration is written to record the root DataObject's Type.
+ *
+ * When loading the root element and an xsi:type declaration is found
+ * it is used as the type of the root DataObject. In this case,
+ * if validation is not being performed, it is not an error if the
+ * rootElementName is not a global element.
+ */
+public class XMLDocumentImpl implements XMLDocument
+{
+ protected ExtendedMetaData extendedMetaData;
+
+ protected EObject rootObject;
+
+ protected XMLResource resource;
+
+ protected EStructuralFeature rootElement;
+
+ protected EObject documentRoot;
+
+ protected final static String WHITESPACE_REGEX = "\\s";
+
+ //TODO clean up the options thing
+ protected XMLDocumentImpl(ExtendedMetaData extendedMetaData, Object options)
+ {
+ this.extendedMetaData = extendedMetaData;
+ ResourceSet resourceSet = DataObjectUtil.createResourceSet();
+
+ if (options instanceof Map)
+ {
+ Class resourceFactoryClass = (Class)((Map)options).get("GENERATED_LOADER");
+ if (resourceFactoryClass != null)
+ {
+ try
+ {
+ Object resourceFactory = resourceFactoryClass.newInstance();
+ resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", resourceFactory);
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ resource = (XMLResource)resourceSet.createResource(URI.createURI("http:///temp.xml"));
+ DataObjectUtil.configureXMLResource(resource, extendedMetaData);
+ }
+
+ protected XMLDocumentImpl(ExtendedMetaData extendedMetaData)
+ {
+ this(extendedMetaData, null);
+ }
+
+ protected XMLDocumentImpl(ExtendedMetaData extendedMetaData, DataObject dataObject, String rootElementURI, String rootElementName)
+ {
+ this(extendedMetaData);
+
+ rootObject = (EObject)dataObject;
+
+ rootElement = extendedMetaData.getElement(rootElementURI, rootElementName);
+ if (rootElement == null)
+ {
+ rootElement = ExtendedMetaData.INSTANCE.demandFeature(rootElementURI, rootElementName, true);
+ }
+
+ EClass documentRootClass = rootElement.getEContainingClass();
+ documentRoot = EcoreUtil.create(documentRootClass);
+ resource.getContents().add(documentRoot);
+ }
+
+ protected void save(OutputStream outputStream, Object options) throws IOException
+ {
+ EObject oldContainer = null;
+ EReference oldContainmentReference = null;
+ int oldContainmentIndex = -1;
+
+ if (documentRoot != null)
+ {
+ //TODO also check if rootObject is directly contained in a resource
+ oldContainer = rootObject.eContainer();
+ if (oldContainer != null)
+ {
+ oldContainmentReference = rootObject.eContainmentFeature();
+ }
+ if (oldContainer != documentRoot || oldContainmentReference != rootElement)
+ {
+ if (oldContainmentReference != null && oldContainmentReference.isMany())
+ {
+ oldContainmentIndex = ((List)oldContainer.eGet(oldContainmentReference)).indexOf(rootObject);
+ }
+
+ Object rootValue =
+ rootElement instanceof EAttribute && rootObject instanceof SimpleAnyTypeDataObject ?
+ ((SimpleAnyTypeDataObject)rootObject).getValue() : rootObject;
+
+ documentRoot.eSet(rootElement, rootValue);
+ }
+ }
+
+ resource.save(outputStream, (Map)options);
+
+ if (oldContainer != null)
+ {
+ if (oldContainer != documentRoot || oldContainmentReference != rootElement)
+ {
+ if (oldContainmentReference.isMany())
+ {
+ ((List)oldContainer.eGet(oldContainmentReference)).add(oldContainmentIndex, rootObject);
+ }
+ else
+ {
+ oldContainer.eSet(oldContainmentReference, rootObject);
+ }
+ }
+ }
+ else if (documentRoot != null)
+ {
+ documentRoot.eSet(rootElement, null);
+ }
+ }
+
+ protected void save(Writer outputWriter, Object options) throws IOException
+ {
+ // TODO temporary brute-force implementation ... to be replaced
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+ save(outputStream, options);
+ outputWriter.write(new String(outputStream.toByteArray()));
+ }
+
+ protected void load(InputStream inputStream, String locationURI, Object options) throws IOException
+ {
+ InputSource inputSource = new InputSource(inputStream);
+ load(inputSource, locationURI, options);
+ }
+
+ protected void load(Reader inputReader, String locationURI, Object options) throws IOException
+ {
+ InputSource inputSource = new InputSource(inputReader);
+ load(inputSource, locationURI, options);
+ }
+
+ protected void load(XMLStreamReader reader) throws IOException
+ {
+ ((SDOXMLResourceImpl)resource).load(reader, null);
+ initLoadedRoot();
+ }
+
+ protected void load(InputSource inputSource, String locationURI, Object options) throws IOException
+ {
+ if (locationURI != null)
+ {
+ inputSource.setSystemId(locationURI);
+ resource.setURI(URI.createURI(locationURI));
+ }
+ resource.load(inputSource, (Map)options);
+ initLoadedRoot();
+ }
+
+ private void initLoadedRoot()
+ {
+ rootObject = null;
+ rootElement = null;
+ documentRoot = null;
+
+ if (!resource.getContents().isEmpty())
+ {
+ documentRoot = (EObject)resource.getContents().get(0);
+ EClass documentRootClass = documentRoot.eClass();
+ if ("".equals(extendedMetaData.getName(documentRootClass))) //TODO efficient way to check this? Maybe DataObject.getContainer should also check this?
+ {
+ FeatureMap featureMap = (FeatureMap)documentRoot.eGet(documentRootClass.getEStructuralFeature(0)); // get mixed feature
+ int size = featureMap.size();
+ for (int index = 0; index < size; index++)
+ {
+ EStructuralFeature feature = featureMap.getEStructuralFeature(index);
+ boolean isText =
+ feature == XMLTypePackage.Literals.XML_TYPE_DOCUMENT_ROOT__TEXT ||
+ feature == XMLTypePackage.Literals.XML_TYPE_DOCUMENT_ROOT__CDATA ||
+ feature == XMLTypePackage.Literals.XML_TYPE_DOCUMENT_ROOT__COMMENT;
+ if (!isText)
+ {
+ if (feature instanceof EReference)
+ {
+ rootObject = (EObject)featureMap.getValue(index);
+ documentRoot.eUnset(feature);
+ }
+ else //EAttribute
+ {
+ rootObject = (EObject)SDOUtil.createDataTypeWrapper((Type)feature.getEType(), featureMap.getValue(index));
+ }
+ rootElement = feature;
+ break;
+ }
+ } //for
+ }
+ else
+ {
+ rootObject = documentRoot;
+ documentRoot = null;
+ }
+ }
+ }
+
+ public DataObject getRootObject()
+ {
+ return (DataObject)rootObject;
+ }
+
+ public String getRootElementURI()
+ {
+ if (rootElement != null)
+ {
+ return extendedMetaData.getNamespace(rootElement);
+ }
+ else if (rootObject != null)
+ {
+ return extendedMetaData.getNamespace(rootObject.eClass());
+ }
+ return null;
+ }
+
+ public String getRootElementName()
+ {
+ if (rootElement != null)
+ {
+ return extendedMetaData.getName(rootElement);
+ }
+ else if (rootObject != null)
+ {
+ return extendedMetaData.getName(rootObject.eClass());
+ }
+ return null;
+ }
+
+ public String getEncoding()
+ {
+ return resource.getEncoding();
+ }
+
+ public void setEncoding(String encoding)
+ {
+ resource.setEncoding(encoding);
+ }
+
+ public boolean isXMLDeclaration()
+ {
+ return Boolean.FALSE.equals(resource.getDefaultSaveOptions().get(XMLResource.OPTION_DECLARE_XML));
+ }
+
+ public void setXMLDeclaration(boolean xmlDeclaration)
+ {
+ resource.getDefaultSaveOptions().put(XMLResource.OPTION_DECLARE_XML, xmlDeclaration ? Boolean.TRUE : Boolean.FALSE);
+ }
+
+ public String getXMLVersion()
+ {
+ return resource.getXMLVersion();
+ }
+
+ public void setXMLVersion(String xmlVersion)
+ {
+ resource.setXMLVersion(xmlVersion);
+ }
+
+ /**
+ * @return an EMap containing the schema locations or null when no map
+ */
+ protected EMap getSchemaLocationMap()
+ {
+ EMap result = null;
+ if ((documentRoot != null) && (extendedMetaData != null))
+ {
+ EReference xsiSchemaLocationMapFeature = extendedMetaData
+ .getXSISchemaLocationMapFeature(documentRoot.eClass());
+ if (xsiSchemaLocationMapFeature != null)
+ {
+ result = (EMap) documentRoot.eGet(xsiSchemaLocationMapFeature);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * @param value
+ * from schema location map.
+ * @return string form of URI from provided value, deresolved if appropriate.
+ */
+ protected String deresolve(String value)
+ {
+ URI location = URI.createURI(value);
+ URI resourceURI = resource.getURI();
+ boolean shouldDeresolve = resourceURI != null && !resourceURI.isRelative()
+ && resourceURI.isHierarchical();
+ if (shouldDeresolve && !location.isRelative())
+ {
+ URI deresolvedURI = location.deresolve(resourceURI, true, true, false);
+ if (deresolvedURI.hasRelativePath())
+ {
+ location = deresolvedURI;
+ }
+ }
+ return location.toString();
+ }
+
+ /**
+ * @param value
+ * for schema location from input parameter.
+ * @return string form of URI from provided value, resolved if appropriate.
+ */
+ protected String resolve(String value)
+ {
+ URI location = URI.createURI(value);
+ URI resourceURI = resource.getURI();
+ boolean shouldResolve = resourceURI != null && resourceURI.isHierarchical()
+ && !resourceURI.isRelative();
+ if (shouldResolve && location.isRelative() && location.hasRelativePath())
+ {
+ location = location.resolve(resourceURI, false);
+ }
+ return location.toString();
+ }
+
+ public String getSchemaLocation()
+ {
+ EMap xsiSchemaLocationMap = getSchemaLocationMap();
+ if (xsiSchemaLocationMap != null)
+ {
+ if (!xsiSchemaLocationMap.isEmpty())
+ {
+ StringBuffer xsiSchemaLocation = new StringBuffer();
+ for (Iterator i = xsiSchemaLocationMap.entrySet().iterator(); i
+ .hasNext();)
+ {
+ Map.Entry entry = (Map.Entry) i.next();
+ String namespace = (String) entry.getKey();
+ if (namespace != null)
+ {
+ if (xsiSchemaLocation.length() > 0)
+ {
+ xsiSchemaLocation.append(' ');
+ }
+ xsiSchemaLocation.append(namespace);
+ xsiSchemaLocation.append(' ');
+ String value = entry.getValue().toString();
+ xsiSchemaLocation.append(deresolve(value));
+ }
+ }
+ return xsiSchemaLocation.toString().equals("") ? null
+ : xsiSchemaLocation.toString();
+ }
+ }
+ return null;
+ }
+
+ public void setSchemaLocation(String schemaLocation)
+ {
+ EMap xsiSchemaLocationMap = getSchemaLocationMap();
+ if (xsiSchemaLocationMap != null)
+ {
+ // only remove the entries from xsiSchemaLocationMap that contain a
+ // non-null key
+ for (Iterator i = xsiSchemaLocationMap.entrySet().iterator(); i.hasNext();)
+ {
+ Map.Entry entry = (Map.Entry) i.next();
+ if (entry.getKey() != null)
+ {
+ i.remove();
+ }
+ }
+ if (xsiSchemaLocationMap.size() == 0)
+ {
+ resource.getDefaultSaveOptions().put(
+ XMLResource.OPTION_SCHEMA_LOCATION, Boolean.FALSE);
+ }
+ if (schemaLocation != null)
+ {
+ String[] values = schemaLocation.split(WHITESPACE_REGEX);
+ for (int i = 0; i < values.length; i++) // note: also incremented in
+ // loop
+ {
+ String key = values[i++];
+ if (i < values.length)
+ {
+ xsiSchemaLocationMap.put(key, resolve(values[i]));
+ }
+ }
+ if (xsiSchemaLocationMap.size() != 0)
+ {
+ resource.getDefaultSaveOptions().put(
+ XMLResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
+ }
+ }
+ }
+ }
+
+ public String getNoNamespaceSchemaLocation()
+ {
+ EMap xsiSchemaLocationMap = getSchemaLocationMap();
+ if (xsiSchemaLocationMap != null)
+ {
+ StringBuffer xsiSchemaLocation = new StringBuffer();
+ if (!xsiSchemaLocationMap.isEmpty())
+ {
+ Object valueObject = xsiSchemaLocationMap.get(null);
+ if (valueObject != null)
+ {
+ String valueString = (String) valueObject;
+ String[] values = valueString.split(WHITESPACE_REGEX);
+ for (int i = 0; i < values.length; i++)
+ {
+ if (xsiSchemaLocation.length() > 0)
+ {
+ xsiSchemaLocation.append(' ');
+ }
+ xsiSchemaLocation.append(deresolve(values[i]));
+ }
+ }
+ String result = xsiSchemaLocation.toString();
+ return result.equals("") ? null : result;
+ }
+ }
+ return null;
+ }
+
+ public void setNoNamespaceSchemaLocation(String schemaLocation)
+ {
+ EMap xsiSchemaLocationMap = getSchemaLocationMap();
+ if (xsiSchemaLocationMap != null)
+ {
+ // only remove the entries from xsiSchemaLocationMap that contain a null
+ // key
+ xsiSchemaLocationMap.removeKey(null);
+ if (xsiSchemaLocationMap.size() == 0)
+ {
+ resource.getDefaultSaveOptions().put(
+ XMLResource.OPTION_SCHEMA_LOCATION, Boolean.FALSE);
+ }
+ if (schemaLocation != null)
+ {
+ String[] values = schemaLocation.split(WHITESPACE_REGEX);
+ for (int i = 0; i < values.length; i++)
+ {
+ xsiSchemaLocationMap.put(null, resolve(values[i]));
+ }
+ if (xsiSchemaLocationMap.size() != 0)
+ {
+ resource.getDefaultSaveOptions().put(
+ XMLResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
+ }
+ }
+ }
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLHelperImpl.java b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLHelperImpl.java
new file mode 100644
index 0000000000..8a94167108
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLHelperImpl.java
@@ -0,0 +1,119 @@
+/**
+ *
+ * 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.helper;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.Writer;
+
+import org.eclipse.emf.ecore.util.ExtendedMetaData;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.helper.TypeHelper;
+import commonj.sdo.helper.XMLDocument;
+import commonj.sdo.helper.XMLHelper;
+
+/**
+ * A helper to convert XML documents into DataObects and
+ * DataObjects into XML documnets.
+ */
+public class XMLHelperImpl implements XMLHelper
+{
+ ExtendedMetaData extendedMetaData;
+
+ public XMLHelperImpl(ExtendedMetaData extendedMetaData)
+ {
+ this.extendedMetaData = extendedMetaData;
+ }
+
+ public XMLHelperImpl(TypeHelper typeHelper)
+ {
+ this.extendedMetaData = ((TypeHelperImpl)typeHelper).extendedMetaData;
+ }
+
+ public XMLDocument load(String inputString)
+ {
+ try
+ {
+ return load(new StringReader(inputString), null, null);
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e); // should never happen
+ }
+ }
+
+ public XMLDocument load(InputStream inputStream) throws IOException
+ {
+ return load(inputStream, null, null);
+ }
+
+ public XMLDocument load(InputStream inputStream, String locationURI, Object options) throws IOException
+ {
+ XMLDocumentImpl document = new XMLDocumentImpl(extendedMetaData, options);
+ document.load(inputStream, locationURI, options);
+ return document;
+ }
+
+ public XMLDocument load(Reader inputReader, String locationURI, Object options) throws IOException
+ {
+ XMLDocumentImpl document = new XMLDocumentImpl(extendedMetaData, options);
+ document.load(inputReader, locationURI, options);
+ return document;
+ }
+
+ public String save(DataObject dataObject, String rootElementURI, String rootElementName)
+ {
+ StringWriter stringWriter = new StringWriter();
+ try
+ {
+ save(createDocument(dataObject, rootElementURI, rootElementName), stringWriter, null);
+ return stringWriter.toString();
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e); // should never happen
+ }
+ }
+
+ public void save(DataObject dataObject, String rootElementURI, String rootElementName, OutputStream outputStream) throws IOException
+ {
+ save(createDocument(dataObject, rootElementURI, rootElementName), outputStream, null);
+ }
+
+ public void save(XMLDocument xmlDocument, OutputStream outputStream, Object options) throws IOException
+ {
+ ((XMLDocumentImpl)xmlDocument).save(outputStream, options);
+ }
+
+ public void save(XMLDocument xmlDocument, Writer outputWriter, Object options) throws IOException
+ {
+ ((XMLDocumentImpl)xmlDocument).save(outputWriter, options);
+ }
+
+ public XMLDocument createDocument(DataObject dataObject, String rootElementURI, String rootElementName)
+ {
+ return new XMLDocumentImpl(extendedMetaData, dataObject, rootElementURI, rootElementName);
+ }
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLStreamHelper.java b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLStreamHelper.java
new file mode 100644
index 0000000000..8b01398adf
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLStreamHelper.java
@@ -0,0 +1,92 @@
+/**
+ *
+ * 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.helper;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.helper.XMLDocument;
+
+/**
+ * Helper interface for reading and writing SDO DataObjects from XML streams (StAX).
+ *
+ * @version $Rev$ $Date$
+ */
+public interface XMLStreamHelper {
+ /**
+ * Creates and returns an XMLDocument from an XML input stream.
+ * The reader must be positioned on a START_DOCUMENT event.
+ *
+ * @param reader the stream to read
+ * @return an XMLDocument created from the stream
+ * @throws XMLStreamException if there was a problem reading the stream
+ * @throws IllegalStateException if the reader is not positioned on a START_DOCUMENT event
+ */
+ XMLDocument load(XMLStreamReader reader) throws XMLStreamException, IllegalStateException;
+
+ /**
+ * Save a XMLDocument to an XML stream.
+ *
+ * @param document the document to be written
+ * @param writer the stream to write to
+ * @throws XMLStreamException if there was a problem writing to the stream
+ */
+ void save(XMLDocument document, XMLStreamWriter writer) throws XMLStreamException;
+
+ /**
+ * Creates and returns a XMLStreamReader that can be used to read an XMLDocument as a XML event stream.
+ * The reader will be positioned on a START_DOCUMENT event.
+ *
+ * @param document the XMLDocument to be read
+ * @return an XMLStreamReader that can be used to read the document
+ */
+ XMLStreamReader createXMLStreamReader(XMLDocument document) throws XMLStreamException;
+
+ /**
+ * Create a DataObject from an element in a XML stream.
+ * The reader must be positioned on a START_ELEMENT event.
+ *
+ * @param reader the stream to read
+ * @return a DataObject created from the element in the stream
+ * @throws XMLStreamException if there was a problem reading the stream
+ * @throws IllegalStateException if the reader is not positioned on a START_ELEMENT event
+ */
+ DataObject loadObject(XMLStreamReader reader) throws XMLStreamException, IllegalStateException;
+
+ /**
+ * Save a DataObject to an XML stream.
+ *
+ * @param sdo the DataObject to be written
+ * @param writer the stream to write to
+ * @throws XMLStreamException if there was a problem writing to the stream
+ */
+ void saveObject(DataObject sdo, XMLStreamWriter writer) throws XMLStreamException;
+
+ /**
+ * Creates and returns a XMLStreamReader that can be used to read a DataObject as a XML event stream.
+ * The reader will be positioned on a START_ELEMENT event.
+ *
+ * @param sdo the DataObject to be read
+ * @return an XMLStreamReader that can be used to read the DataObject
+ */
+ XMLStreamReader createXMLStreamReader(DataObject sdo);
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLStreamHelperImpl.java b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLStreamHelperImpl.java
new file mode 100644
index 0000000000..306c60ca2b
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLStreamHelperImpl.java
@@ -0,0 +1,129 @@
+/**
+ *
+ * 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.helper;
+
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.tuscany.sdo.util.resource.DataObjectXMLStreamReader;
+import org.apache.tuscany.sdo.util.resource.XMLDocumentStreamReader;
+import org.apache.tuscany.sdo.util.resource.XMLStreamSerializer;
+import org.eclipse.emf.ecore.resource.Resource;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.Property;
+import commonj.sdo.helper.TypeHelper;
+import commonj.sdo.helper.XMLDocument;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class XMLStreamHelperImpl implements XMLStreamHelper
+{
+ TypeHelperImpl typeHelper;
+
+ public XMLStreamHelperImpl(TypeHelper typeHelper)
+ {
+ this.typeHelper = (TypeHelperImpl)typeHelper;
+ }
+
+ public XMLDocument load(XMLStreamReader reader) throws XMLStreamException, IllegalStateException
+ {
+ if (reader.getEventType() != XMLStreamConstants.START_DOCUMENT)
+ throw new IllegalStateException();
+
+ return loadDocument(reader);
+ }
+
+ public void save(XMLDocument document, XMLStreamWriter writer) throws XMLStreamException
+ {
+ XMLStreamReader reader = createXMLStreamReader(document);
+ new XMLStreamSerializer().serialize(reader, writer);
+ }
+
+ public XMLStreamReader createXMLStreamReader(XMLDocument document) throws XMLStreamException
+ {
+ XMLStreamReader reader = new DataObjectXMLStreamReader(document.getRootObject(), document.getRootElementURI(), document.getRootElementName(), typeHelper);
+ // Wrap the reader so that its position will be START_ELEMENT
+ return new XMLDocumentStreamReader(reader);
+
+ }
+
+ public DataObject loadObject(XMLStreamReader reader) throws XMLStreamException, IllegalStateException
+ {
+ if (reader.getEventType() != XMLStreamConstants.START_ELEMENT)
+ throw new IllegalStateException();
+
+ // StAX2SAXAdapter won't produce START_DOCUMENT if the reader is posisitioned at START_ELEMENT and the EMF loader will fail
+ // Wrap the reader so it represents a document
+ reader = new XMLDocumentStreamReader(reader);
+
+ return loadDocument(reader).getRootObject();
+ }
+
+ public void saveObject(DataObject sdo, XMLStreamWriter writer) throws XMLStreamException
+ {
+ XMLStreamReader reader = createXMLStreamReader(sdo);
+ new XMLStreamSerializer().serialize(new XMLDocumentStreamReader(reader), writer);
+ }
+
+ public XMLStreamReader createXMLStreamReader(DataObject dataObject)
+ {
+ String rootElementURI;
+ String rootElementName;
+
+ Property property = dataObject.getContainmentProperty();
+ if (property != null)
+ {
+ rootElementName = property.getName();
+ rootElementURI = property.getType().getURI();
+ }
+ else
+ {
+ rootElementName = dataObject.getType().getName();
+ rootElementURI = dataObject.getType().getURI();
+ }
+
+ return new DataObjectXMLStreamReader(dataObject, rootElementURI, rootElementName, typeHelper);
+ }
+
+ protected XMLDocument loadDocument(XMLStreamReader reader) throws XMLStreamException
+ {
+ try {
+ XMLDocumentImpl document = new XMLDocumentImpl(typeHelper.extendedMetaData, null);
+ document.load(reader);
+ return document;
+ }
+ catch (Exception e) {
+ if (e instanceof Resource.IOWrappedException)
+ {
+ Resource.IOWrappedException ioe = (Resource.IOWrappedException)e;
+ if (ioe.getWrappedException() instanceof XMLStreamException)
+ {
+ throw (XMLStreamException)ioe.getWrappedException();
+ }
+ }
+ throw new RuntimeException(e); // ????
+ }
+ }
+
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XSDHelperImpl.java b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XSDHelperImpl.java
new file mode 100644
index 0000000000..f67d35d74d
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XSDHelperImpl.java
@@ -0,0 +1,285 @@
+/**
+ *
+ * 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.helper;
+
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.apache.tuscany.sdo.impl.DynamicDataObjectImpl;
+import org.apache.tuscany.sdo.util.DataObjectUtil;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EClassifier;
+import org.eclipse.emf.ecore.EModelElement;
+import org.eclipse.emf.ecore.EPackage;
+import org.eclipse.emf.ecore.EStructuralFeature;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.emf.ecore.util.ExtendedMetaData;
+import org.eclipse.xsd.XSDSchema;
+import org.eclipse.xsd.ecore.XSDEcoreBuilder;
+import org.eclipse.xsd.util.XSDResourceImpl;
+import org.xml.sax.InputSource;
+
+import commonj.sdo.Property;
+import commonj.sdo.Type;
+import commonj.sdo.helper.TypeHelper;
+import commonj.sdo.helper.XSDHelper;
+
+
+/**
+ * Provides access to additional information when the
+ * Type or Property is defined by an XML Schema (XSD).
+ * Methods return null/false otherwise or if the information is unavailable.
+ * Defines Types from an XSD.
+ */
+public class XSDHelperImpl implements XSDHelper
+{
+ private XSDEcoreBuilder ecoreBuilder;
+
+ private ExtendedMetaData extendedMetaData;
+
+ public XSDHelperImpl(ExtendedMetaData extendedMetaData)
+ {
+ this.extendedMetaData = extendedMetaData;
+ ecoreBuilder = new SDOXSDEcoreBuilder(extendedMetaData);
+ }
+
+ public XSDHelperImpl(TypeHelper typeHelper)
+ {
+ this(((TypeHelperImpl)typeHelper).extendedMetaData);
+ }
+
+ public String getLocalName(Type type)
+ {
+ return extendedMetaData.getName((EClassifier)type);
+ }
+
+ public String getLocalName(Property property)
+ {
+ return extendedMetaData.getName((EStructuralFeature)property);
+ }
+
+ public String getNamespaceURI(Property property)
+ {
+ return extendedMetaData.getNamespace((EStructuralFeature)property);
+ }
+
+ public boolean isAttribute(Property property)
+ {
+ return extendedMetaData.getFeatureKind((EStructuralFeature)property) == ExtendedMetaData.ATTRIBUTE_FEATURE;
+ }
+
+ public boolean isElement(Property property)
+ {
+ return extendedMetaData.getFeatureKind((EStructuralFeature)property) == ExtendedMetaData.ELEMENT_FEATURE;
+ }
+
+ public boolean isMixed(Type type)
+ {
+ if (type instanceof EClass)
+ {
+ return extendedMetaData.getContentKind((EClass)type) == ExtendedMetaData.MIXED_CONTENT;
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+ public boolean isXSD(Type type)
+ {
+ return ((EModelElement)type).getEAnnotation(ExtendedMetaData.ANNOTATION_URI) != null;
+ }
+
+ public Property getGlobalProperty(String uri, String propertyName, boolean isElement)
+ {
+ if (isElement)
+ {
+ return (Property)extendedMetaData.getElement(uri, propertyName);
+ }
+ else
+ {
+ return (Property)extendedMetaData.getAttribute(uri, propertyName);
+ }
+ }
+
+ public String getAppinfo(Type type, String source)
+ {
+ return getAppinfo((EModelElement)type, source);
+ }
+
+ public String getAppinfo(Property property, String source)
+ {
+ return getAppinfo((EModelElement)property, source);
+ }
+
+ protected String getAppinfo(EModelElement eModelElement, String source)
+ {
+ return (String)eModelElement.getEAnnotation(source).getDetails().get("appinfo");
+ }
+
+ public List /*Type*/define(String xsd)
+ {
+ InputStream inputStream = new ByteArrayInputStream(xsd.getBytes());
+ return define(inputStream, "*.xsd");
+ }
+
+ public List /*Type*/define(Reader xsdReader, String schemaLocation)
+ {
+ InputSource inputSource = new InputSource(xsdReader);
+ return define(inputSource, schemaLocation);
+
+ }
+
+ public List /*Type*/define(InputStream xsdInputStream, String schemaLocation)
+ {
+ InputSource inputSource = new InputSource(xsdInputStream);
+ return define(inputSource, schemaLocation);
+ }
+
+ protected List /*Type*/define(InputSource inputSource, String schemaLocation)
+ {
+ try
+ {
+ ResourceSet resourceSet = DataObjectUtil.createResourceSet();
+ Resource model = resourceSet.createResource(URI.createURI(schemaLocation != null ? schemaLocation : "null.xsd"));
+ ((XSDResourceImpl)model).load(inputSource, null);
+
+ List newTypes = new ArrayList();
+ for (Iterator schemaIter = model.getContents().iterator(); schemaIter.hasNext(); )
+ {
+ XSDSchema schema = (XSDSchema)schemaIter.next();
+
+ EPackage ePackage = extendedMetaData.getPackage(schema.getTargetNamespace());
+ if (ePackage == null)
+ {
+ Collection originalEPackages = new HashSet(ecoreBuilder.getTargetNamespaceToEPackageMap().values());
+ ecoreBuilder.generate(schema);
+ Collection newEPackages = ecoreBuilder.getTargetNamespaceToEPackageMap().values();
+
+ for (Iterator iter = newEPackages.iterator(); iter.hasNext();)
+ {
+ EPackage currentPackage = (EPackage)iter.next();
+ if (!originalEPackages.contains(currentPackage))
+ {
+ currentPackage.setEFactoryInstance(new DynamicDataObjectImpl.FactoryImpl());
+ EcoreUtil.freeze(currentPackage);
+ newTypes.addAll(currentPackage.getEClassifiers());
+ }
+ }
+ }
+ }
+
+ return newTypes;
+ }
+ catch (Exception e)
+ {
+ //e.printStackTrace();
+ throw new IllegalArgumentException(e.getMessage());
+ }
+ }
+
+ public String generate(List /*Type*/types) throws IllegalArgumentException
+ {
+ return generate(types, new Hashtable());
+ }
+
+ public String generate(List /*Type*/types, Map /*String, String*/namespaceToSchemaLocation) throws IllegalArgumentException
+ {
+ if ( types != null && !types.isEmpty() )
+ {
+ Hashtable schemaMap = new Hashtable();
+ Hashtable nsPrefixMap = new Hashtable();
+ TypeTable typeTable = new TypeTable();
+
+ SchemaBuilder schemaBuilder = new SchemaBuilder( schemaMap,
+ nsPrefixMap,
+ typeTable,
+ namespaceToSchemaLocation);
+
+ Iterator iterator = types.iterator();
+ Type dataType = null;
+
+ try
+ {
+ while ( iterator.hasNext() )
+ {
+ dataType = (Type)iterator.next();
+ schemaBuilder.buildSchema(dataType);
+ }
+
+ XSDSchema xmlSchema = null;
+ iterator = schemaMap.values().iterator();
+ StringWriter writer = new StringWriter();
+
+ TransformerFactory transformerFactory = TransformerFactory.newInstance();
+ Transformer transformer = transformerFactory.newTransformer();
+ transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+ transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+ transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+
+ while ( iterator.hasNext() )
+ {
+ xmlSchema = (XSDSchema)iterator.next();
+
+ if(xmlSchema.getElement() == null)
+ {
+ xmlSchema.updateElement();
+ }
+
+ transformer.transform(new DOMSource(xmlSchema.getElement().getOwnerDocument()),
+ new StreamResult(writer));
+ }
+ writer.close();
+ return writer.getBuffer().toString();
+ }
+ catch ( Exception e )
+ {
+ //System.out.println("Unable to generate schema due to ..." + e);
+ //e.printStackTrace();
+ throw new IllegalArgumentException(e.getMessage());
+ }
+ }
+ else
+ {
+ //System.out.println("No SDO Types to generate schema ...");
+ return "";
+ }
+ }
+
+}