You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by fr...@apache.org on 2006/05/01 23:04:53 UTC

svn commit: r398690 - in /incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo: helper/XMLDocumentImpl.java util/SDOUtil.java

Author: frankb
Date: Mon May  1 14:04:51 2006
New Revision: 398690

URL: http://svn.apache.org/viewcvs?rev=398690&view=rev
Log:
Fix for TUSCANY-241

Modified:
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java

Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java?rev=398690&r1=398689&r2=398690&view=diff
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java (original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java Mon May  1 14:04:51 2006
@@ -27,25 +27,33 @@
 import java.util.List;
 import java.util.Map;
 
+import org.apache.tuscany.sdo.SDOFactory;
 import org.apache.tuscany.sdo.SDOPackage;
+import org.apache.tuscany.sdo.SimpleAnyTypeDataObject;
 import org.apache.tuscany.sdo.util.DataObjectUtil;
+import org.apache.tuscany.sdo.util.SDOUtil;
 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.EDataType;
 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.XMLOptions;
 import org.eclipse.emf.ecore.xmi.XMLParserPool;
 import org.eclipse.emf.ecore.xmi.XMLResource;
 import org.eclipse.emf.ecore.xmi.impl.XMLOptionsImpl;
 import org.eclipse.emf.ecore.xmi.impl.XMLParserPoolImpl;
+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;
 
 
@@ -189,7 +197,12 @@
         {
           oldContainmentIndex = ((List)oldContainer.eGet(oldContainmentReference)).indexOf(rootObject);
         }
-        documentRoot.eSet(rootElement, rootObject);
+        
+        Object rootValue =
+          rootElement instanceof EAttribute && rootObject instanceof SimpleAnyTypeDataObject ?
+            ((SimpleAnyTypeDataObject)rootObject).getValue() : rootObject;
+            
+        documentRoot.eSet(rootElement, rootValue);
       }
     }
 
@@ -255,12 +268,30 @@
       EClass documentRootClass = documentRoot.eClass();
       if ("".equals(extendedMetaData.getName(documentRootClass))) //TODO efficient way to check this? Maybe DataObject.getContainer should also check this?
       {
-        if (!documentRoot.eContents().isEmpty())
+        FeatureMap featureMap = (FeatureMap)documentRoot.eGet(documentRootClass.getEStructuralFeature(0)); // get mixed feature
+        int size = featureMap.size();
+        for (int index = 0; index < size; index++)
         {
-          rootObject = (EObject)documentRoot.eContents().get(0);
-          rootElement = rootObject.eContainmentFeature();
-          documentRoot.eUnset(rootElement);
-        }
+          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
       {

Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java?rev=398690&r1=398689&r2=398690&view=diff
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java (original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java Mon May  1 14:04:51 2006
@@ -27,6 +27,7 @@
 import org.apache.tuscany.sdo.SDOExtendedMetaData;
 import org.apache.tuscany.sdo.SDOFactory;
 import org.apache.tuscany.sdo.SDOPackage;
+import org.apache.tuscany.sdo.SimpleAnyTypeDataObject;
 import org.apache.tuscany.sdo.helper.DataFactoryImpl;
 import org.apache.tuscany.sdo.helper.SDOExtendedMetaDataImpl;
 import org.apache.tuscany.sdo.helper.TypeHelperImpl;
@@ -54,6 +55,7 @@
 import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
 
 import commonj.sdo.DataGraph;
+import commonj.sdo.DataObject;
 import commonj.sdo.Property;
 import commonj.sdo.Type;
 import commonj.sdo.helper.DataFactory;
@@ -68,55 +70,20 @@
  */
 public final class SDOUtil
 {
-  //XSD to SDO Mappings mappings (p.95 of the SDO spec)
-  private static Map xsdToSdoMappings = new HashMap();
-  static {
-    xsdToSdoMappings.put("anySimpleType", "Object");
-    xsdToSdoMappings.put("anyType", "DataObject");
-    xsdToSdoMappings.put("anyURI", "URI");
-    xsdToSdoMappings.put("base64Binary", "Bytes");
-    xsdToSdoMappings.put("boolean", "Boolean");
-    xsdToSdoMappings.put("byte", "Byte");
-    xsdToSdoMappings.put("date", "YearMonthDay");
-    xsdToSdoMappings.put("dateTime", "DateTime");
-    xsdToSdoMappings.put("decimal", "Decimal");
-    xsdToSdoMappings.put("double", "Double");
-    xsdToSdoMappings.put("duration", "Duration");
-    xsdToSdoMappings.put("ENTITIES", "Strings");
-    xsdToSdoMappings.put("ENTITY", "String");
-    xsdToSdoMappings.put("float", "Float");
-    xsdToSdoMappings.put("gDay", "Day");
-    xsdToSdoMappings.put("gMonth", "Month");
-    xsdToSdoMappings.put("gMonthDay", "MonthDay");
-    xsdToSdoMappings.put("gYear", "Year");
-    xsdToSdoMappings.put("gYearMonth", "YearMonth");
-    xsdToSdoMappings.put("hexBinary", "Bytes");
-    xsdToSdoMappings.put("ID","String");
-    xsdToSdoMappings.put("IDREF","String");
-    xsdToSdoMappings.put("IDREFS","Strings");
-    xsdToSdoMappings.put("int","Int");
-    xsdToSdoMappings.put("integer","Integer");
-    xsdToSdoMappings.put("language","String");
-    xsdToSdoMappings.put("long","Long");
-    xsdToSdoMappings.put("Name","String");
-    xsdToSdoMappings.put("NCName","String");
-    xsdToSdoMappings.put("negativeInteger","Integer");
-    xsdToSdoMappings.put("NMTOKEN","String");
-    xsdToSdoMappings.put("NMTOKENS","Strings");
-    xsdToSdoMappings.put("nonNegativeInteger","Integer");
-    xsdToSdoMappings.put("nonPositiveInteger","Integer");
-    xsdToSdoMappings.put("normalizedString","String");
-    xsdToSdoMappings.put("NOTATION","String");
-    xsdToSdoMappings.put("positiveInteger","Integer");
-    xsdToSdoMappings.put("QName","URI");
-    xsdToSdoMappings.put("short","Short");
-    xsdToSdoMappings.put("string","String");
-    xsdToSdoMappings.put("time","Time");
-    xsdToSdoMappings.put("token","String");
-    xsdToSdoMappings.put("unsignedByte","Short");
-    xsdToSdoMappings.put("unsignedInt","Long");
-    xsdToSdoMappings.put("unsignedLong","Integer");
-    xsdToSdoMappings.put("unsignedShort","Int");
+  /**
+   * Create a DataObject wrapper for an instance of the specified dataType.
+   * This method is typically used to create a root object that can be passed to the XMLHelper.save() 
+   * method when the root element to be serialized is an XMLSchema simpleType.
+   * @param dataType a Type for which isDataType() returns true.
+   * @param value the instance value.
+   * @return a DataObject wrapper for the specified value.
+   */
+  public static DataObject createDataTypeWrapper(Type dataType, Object value)
+  {
+    SimpleAnyTypeDataObject simpleAnyType = SDOFactory.eINSTANCE.createSimpleAnyTypeDataObject();
+    simpleAnyType.setInstanceType((EDataType)dataType);
+    simpleAnyType.setValue(value);
+    return simpleAnyType;
   }
   
   /**
@@ -458,6 +425,57 @@
     }
   }
 
+  //XSD to SDO Mappings mappings (p.95 of the SDO spec)
+  private static Map xsdToSdoMappings = new HashMap();
+  static {
+    xsdToSdoMappings.put("anySimpleType", "Object");
+    xsdToSdoMappings.put("anyType", "DataObject");
+    xsdToSdoMappings.put("anyURI", "URI");
+    xsdToSdoMappings.put("base64Binary", "Bytes");
+    xsdToSdoMappings.put("boolean", "Boolean");
+    xsdToSdoMappings.put("byte", "Byte");
+    xsdToSdoMappings.put("date", "YearMonthDay");
+    xsdToSdoMappings.put("dateTime", "DateTime");
+    xsdToSdoMappings.put("decimal", "Decimal");
+    xsdToSdoMappings.put("double", "Double");
+    xsdToSdoMappings.put("duration", "Duration");
+    xsdToSdoMappings.put("ENTITIES", "Strings");
+    xsdToSdoMappings.put("ENTITY", "String");
+    xsdToSdoMappings.put("float", "Float");
+    xsdToSdoMappings.put("gDay", "Day");
+    xsdToSdoMappings.put("gMonth", "Month");
+    xsdToSdoMappings.put("gMonthDay", "MonthDay");
+    xsdToSdoMappings.put("gYear", "Year");
+    xsdToSdoMappings.put("gYearMonth", "YearMonth");
+    xsdToSdoMappings.put("hexBinary", "Bytes");
+    xsdToSdoMappings.put("ID","String");
+    xsdToSdoMappings.put("IDREF","String");
+    xsdToSdoMappings.put("IDREFS","Strings");
+    xsdToSdoMappings.put("int","Int");
+    xsdToSdoMappings.put("integer","Integer");
+    xsdToSdoMappings.put("language","String");
+    xsdToSdoMappings.put("long","Long");
+    xsdToSdoMappings.put("Name","String");
+    xsdToSdoMappings.put("NCName","String");
+    xsdToSdoMappings.put("negativeInteger","Integer");
+    xsdToSdoMappings.put("NMTOKEN","String");
+    xsdToSdoMappings.put("NMTOKENS","Strings");
+    xsdToSdoMappings.put("nonNegativeInteger","Integer");
+    xsdToSdoMappings.put("nonPositiveInteger","Integer");
+    xsdToSdoMappings.put("normalizedString","String");
+    xsdToSdoMappings.put("NOTATION","String");
+    xsdToSdoMappings.put("positiveInteger","Integer");
+    xsdToSdoMappings.put("QName","URI");
+    xsdToSdoMappings.put("short","Short");
+    xsdToSdoMappings.put("string","String");
+    xsdToSdoMappings.put("time","Time");
+    xsdToSdoMappings.put("token","String");
+    xsdToSdoMappings.put("unsignedByte","Short");
+    xsdToSdoMappings.put("unsignedInt","Long");
+    xsdToSdoMappings.put("unsignedLong","Integer");
+    xsdToSdoMappings.put("unsignedShort","Int");
+  }
+  
   /**
    * Initialize SDO runtime.
    */