From 195774c489a1a671aca514b0afa88332bf9c6ee3 Mon Sep 17 00:00:00 2001 From: lresende Date: Tue, 10 Nov 2009 19:20:12 +0000 Subject: Moving SDO tags git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@834617 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tuscany/sdo/util/BasicSequence.java | 176 ++ .../sdo/util/DataGraphResourceFactoryImpl.java | 711 +++++ .../apache/tuscany/sdo/util/DataObjectUtil.java | 3035 ++++++++++++++++++++ .../java/org/apache/tuscany/sdo/util/SDOUtil.java | 593 ++++ .../apache/tuscany/sdo/util/StAX2SAXAdapter.java | 250 ++ .../tuscany/sdo/util/StreamDeserializer.java | 92 + .../apache/tuscany/sdo/util/VirtualSequence.java | 780 +++++ .../tuscany/sdo/util/metadata/JavaMetaData.java | 90 + .../tuscany/sdo/util/metadata/MetadataFactory.java | 77 + .../sdo/util/metadata/SDOMetaDataGroup.java | 87 + .../tuscany/sdo/util/metadata/TypeMetaData.java | 65 + .../tuscany/sdo/util/metadata/XSDMetaData.java | 66 + .../sdo/util/metadata/impl/JavaMetaDataImpl.java | 267 ++ .../util/metadata/impl/MetadataFactoryImpl.java | 367 +++ .../util/metadata/impl/SDOMetaDataGroupImpl.java | 285 ++ .../sdo/util/metadata/impl/TypeMetaDataImpl.java | 203 ++ .../sdo/util/metadata/impl/XSDMetaDataImpl.java | 203 ++ .../resource/ChangeSummaryStreamDeserializer.java | 663 +++++ .../resource/ChangeSummaryStreamSerializer.java | 690 +++++ .../util/resource/DataObjectXMLStreamReader.java | 1579 ++++++++++ .../resource/RecordedEventXMLStreamReader.java | 868 ++++++ .../tuscany/sdo/util/resource/SDODeserializer.java | 340 +++ .../sdo/util/resource/SDOURIConverterImpl.java | 60 + .../util/resource/SDOXMLResourceFactoryImpl.java | 47 + .../sdo/util/resource/SDOXMLResourceImpl.java | 1009 +++++++ .../sdo/util/resource/XMLDocumentStreamReader.java | 438 +++ .../sdo/util/resource/XMLFragmentStreamReader.java | 44 + .../sdo/util/resource/XMLStreamSerializer.java | 414 +++ 28 files changed, 13499 insertions(+) create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/BasicSequence.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/DataGraphResourceFactoryImpl.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/DataObjectUtil.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/StAX2SAXAdapter.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/StreamDeserializer.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/VirtualSequence.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/metadata/JavaMetaData.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/metadata/MetadataFactory.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/metadata/SDOMetaDataGroup.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/metadata/TypeMetaData.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/metadata/XSDMetaData.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/metadata/impl/JavaMetaDataImpl.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/metadata/impl/MetadataFactoryImpl.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/metadata/impl/SDOMetaDataGroupImpl.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/metadata/impl/TypeMetaDataImpl.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/metadata/impl/XSDMetaDataImpl.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/resource/ChangeSummaryStreamDeserializer.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/resource/ChangeSummaryStreamSerializer.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/resource/DataObjectXMLStreamReader.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/resource/RecordedEventXMLStreamReader.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/resource/SDODeserializer.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/resource/SDOURIConverterImpl.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/resource/SDOXMLResourceFactoryImpl.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/resource/SDOXMLResourceImpl.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/resource/XMLDocumentStreamReader.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/resource/XMLFragmentStreamReader.java create mode 100644 sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/resource/XMLStreamSerializer.java (limited to 'sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util') diff --git a/sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/BasicSequence.java b/sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/BasicSequence.java new file mode 100644 index 0000000000..e25d30e61c --- /dev/null +++ b/sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/BasicSequence.java @@ -0,0 +1,176 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sdo.util; + +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.ecore.util.FeatureMap; +import org.eclipse.emf.ecore.util.FeatureMapUtil; +import org.eclipse.emf.ecore.xml.type.XMLTypePackage; + +import commonj.sdo.DataObject; +import commonj.sdo.Property; +import commonj.sdo.Sequence; +import commonj.sdo.Type; + +/** + * SDO Sequance implementation which delegates to a feature map. + */ +public class BasicSequence implements Sequence, FeatureMap.Internal.Wrapper +{ + protected FeatureMap.Internal featureMap; + + public BasicSequence(FeatureMap.Internal featureMap) + { + this.featureMap = featureMap; + featureMap.setWrapper(this); + } + + public FeatureMap featureMap() + { + return featureMap; + } + + public int size() + { + return featureMap.size(); + } + + public Property getProperty(int index) + { + EStructuralFeature feature = featureMap.getEStructuralFeature(index); + return getFeatureProperty(feature); + } + + public static Property getFeatureProperty(EStructuralFeature feature) + { + 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; + return isText ? null : (Property)feature; + } + + public Object getValue(int index) + { + return featureMap.getValue(index); + } + + public Object setValue(int index, Object value) + { + return featureMap.setValue(index, value); + } + + /* + protected EStructuralFeature getEStructuralFeature(String propertyName) + { + return featureMap.getEObject().eClass().getEStructuralFeature(propertyName); + } + */ + + protected EStructuralFeature getEStructuralFeature(String propertyName, Object value) + { + EStructuralFeature result = featureMap.getEObject().eClass().getEStructuralFeature(propertyName); + if (result == null) + { + Type type = (Type)featureMap.getEObject().eClass(); + if (type.isOpen()) + { + result = (EStructuralFeature)DataObjectUtil.demandOpenProperty(type, propertyName, value, true); + } + } + return result; + } + + protected EStructuralFeature getEStructuralFeature(int propertyIndex) + { + return (EStructuralFeature)DataObjectUtil.getProperty((DataObject)featureMap.getEObject(), propertyIndex); + } + + public boolean add(String propertyName, Object value) + { + return featureMap.add(getEStructuralFeature(propertyName, value), value); + } + + public boolean add(int propertyIndex, Object value) + { + return featureMap.add(getEStructuralFeature(propertyIndex), value); + } + + public boolean add(Property property, Object value) + { + return featureMap.add((EStructuralFeature)property, value); + } + + public void add(int index, String propertyName, Object value) + { + featureMap.add(index, getEStructuralFeature(propertyName, value), value); + } + + public void add(int index, int propertyIndex, Object value) + { + featureMap.add(index, getEStructuralFeature(propertyIndex), value); + } + + public void add(int index, Property property, Object value) + { + featureMap.add(index, (EStructuralFeature)property, value); + } + + /** + * @deprecated + */ + public void add(String text) + { + FeatureMapUtil.addText(featureMap, text); + } + + /** + * @deprecated + */ + public void add(int index, String text) + { + FeatureMapUtil.addText(featureMap, index, text); + } + + public void addText(String text) + { + FeatureMapUtil.addText(featureMap, text); + } + + public void addText(int index, String text) + { + FeatureMapUtil.addText(featureMap, index, text); + } + + public void remove(int index) + { + featureMap.remove(index); + } + + public void move(int toIndex, int fromIndex) + { + featureMap.move(toIndex, fromIndex); + } + + public String toString() + { + return featureMap.toString(); + } +} diff --git a/sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/DataGraphResourceFactoryImpl.java b/sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/DataGraphResourceFactoryImpl.java new file mode 100644 index 0000000000..17a4ec709c --- /dev/null +++ b/sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/DataGraphResourceFactoryImpl.java @@ -0,0 +1,711 @@ +/** + * + * 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.util; + + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +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.helper.TypeHelperImpl; +import org.apache.tuscany.sdo.impl.ChangeSummaryImpl; +import org.apache.tuscany.sdo.impl.DataGraphImpl; +import org.apache.tuscany.sdo.impl.DynamicDataObjectImpl; +import org.eclipse.emf.common.util.EMap; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EFactory; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EPackage; +import org.eclipse.emf.ecore.EReference; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.ecore.EcorePackage; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.emf.ecore.util.ExtendedMetaData; +import org.eclipse.emf.ecore.xmi.EcoreBuilder; +import org.eclipse.emf.ecore.xmi.NameInfo; +import org.eclipse.emf.ecore.xmi.XMLHelper; +import org.eclipse.emf.ecore.xmi.XMLLoad; +import org.eclipse.emf.ecore.xmi.XMLResource; +import org.eclipse.emf.ecore.xmi.XMLSave; +import org.eclipse.emf.ecore.xmi.impl.SAXXMLHandler; +import org.eclipse.emf.ecore.xmi.impl.XMLHelperImpl; +import org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl; +import org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl; +import org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl; +import org.eclipse.emf.ecore.xmi.util.DefaultEcoreBuilder; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; +import org.xml.sax.helpers.DefaultHandler; + +import commonj.sdo.ChangeSummary; +import commonj.sdo.helper.TypeHelper; + + +public class DataGraphResourceFactoryImpl extends ResourceFactoryImpl +{ + /** + * Constructor for DataGraphResourceFactoryImpl. + */ + public DataGraphResourceFactoryImpl() + { + super(); + } + + public Resource createResource(URI uri) + { + XMLResourceImpl result = new DataGraphResourceImpl(uri); + + ExtendedMetaData extendedMetaData = ((TypeHelperImpl)TypeHelper.INSTANCE).getExtendedMetaData(); + DataObjectUtil.configureXMLResource(result, extendedMetaData); + + result.getDefaultSaveOptions().put(XMLResource.OPTION_USE_ENCODED_ATTRIBUTE_STYLE, Boolean.TRUE); + result.getDefaultLoadOptions().put(XMLResource.OPTION_USE_DEPRECATED_METHODS, Boolean.TRUE); + //result.setEncoding("UTF-8"); + //result.getDefaultLoadOptions().put(XMLResource.OPTION_USE_LEXICAL_HANDLER, Boolean.TRUE); + //result.getDefaultSaveOptions().put(XMLResource.OPTION_LINE_WIDTH, new Integer(80)); + + return result; + } + + public static class DataGraphResourceImpl extends XMLResourceImpl + { + public DataGraphResourceImpl(URI uri) + { + super(uri); + } + + public static class HelperImpl extends XMLHelperImpl + { + protected DataGraphImpl eDataGraph; + + protected List resources; + protected List uris; + + public HelperImpl(XMLResource xmlResource) + { + super(xmlResource); + } + + public void setResource(XMLResource resource) + { + super.setResource(resource); + if (!resource.getContents().isEmpty()) + { + eDataGraph = (DataGraphImpl)resource.getContents().get(0); + + resources = new ArrayList(); + uris = new ArrayList(); + + resources.add(eDataGraph.getRootResource()); + uris.add("#" + resource.getURIFragment(eDataGraph) + "/@eRootObject"); + + if (eDataGraph.getEChangeSummary() != null) + { + // Ensure that resource exists. + // + resources.add(((EObject)eDataGraph.getChangeSummary()).eResource()); + uris.add("#" + resource.getURIFragment(eDataGraph) + "/@eChangeSummary"); + } + + if (eDataGraph.eResource() != null && eDataGraph.eResource().getResourceSet() != null) + { + int count = 0; + for (Iterator i = eDataGraph.eResource().getResourceSet().getResources().iterator(); i.hasNext();) + { + Resource ePackageResource = (Resource)i.next(); + List resourceContents = ePackageResource.getContents(); + if (resourceContents.size() == 1 && resourceContents.get(0) instanceof EPackage) + { + resources.add(ePackageResource); + uris.add("#" + resource.getURIFragment(eDataGraph) + "/@models." + count++); + } + } + } + } + } + + public String getID(EObject eObject) + { + return super.getID(eObject); + } + + public String getIDREF(EObject eObject) + { + String fragment = super.getIDREF(eObject); + if (fragment.startsWith("/")) + { + int index = resources.indexOf(eObject.eResource()); + if (index != -1) + { + fragment = ((String)uris.get(index)).substring(1) + fragment.substring(1); + } + } + return fragment; + } + + public String getHREF(EObject eObject) + { + return super.getHREF(eObject); + } + + protected URI getHREF(Resource otherResource, EObject obj) + { + int index = resources.indexOf(otherResource); + if (index == -1) + { + return super.getHREF(otherResource, obj); + } + else + { + return createHREF((String)uris.get(index), otherResource.getURIFragment(obj)); + } + } + + protected URI createHREF(String baseURI, String fragment) + { + if (fragment.startsWith("/")) + { + return URI.createURI(baseURI + fragment.substring(1)); + } + else + { + return URI.createURI("#" + fragment); + } + } + + public void populateNameInfo(NameInfo nameInfo, EClass c) + { + if (c == SDOPackage.eINSTANCE.getDataGraph()) + { + if (extendedMetaData != null) + { + extendedMetaData.demandPackage("commonj.sdo").setNsPrefix("sdo"); + } + nameInfo.setQualifiedName(getQName("commonj.sdo", "datagraph")); + nameInfo.setNamespaceURI("commonj.sdo"); + nameInfo.setLocalPart("datagraph"); + } + else if (c == SDOPackage.eINSTANCE.getChangeSummary()) + { + if (extendedMetaData != null) + { + extendedMetaData.demandPackage("commonj.sdo").setNsPrefix("sdo"); + } + nameInfo.setQualifiedName("changeSummary"); + nameInfo.setNamespaceURI(null); + nameInfo.setLocalPart("changeSummary"); + } + else + { + super.populateNameInfo(nameInfo, c); + } + } + + public String getQName(EClass c) + { + if (c == SDOPackage.eINSTANCE.getDataGraph()) + { + if (extendedMetaData != null) + { + extendedMetaData.demandPackage("commonj.sdo").setNsPrefix("sdo"); + } + return getQName("commonj.sdo", "datagraph"); + } + else if (c == SDOPackage.eINSTANCE.getChangeSummary()) + { + if (extendedMetaData != null) + { + extendedMetaData.demandPackage("commonj.sdo").setNsPrefix("sdo"); + } + return getQName((String)null, "changeSummary"); + } + else + { + return super.getQName(c); + } + } + } + + protected XMLHelper createXMLHelper() + { + return new HelperImpl(this); + } + + protected EObject getEObjectByID(String id) + { + List contents = getContents(); + if (contents.size() >= 1) + { + Object rootObject = contents.get(0); + if (rootObject instanceof DataGraphImpl) + { + DataGraphImpl eDataGraph = (DataGraphImpl)rootObject; + EObject result = eDataGraph.getRootResource().getEObject(id); + if (result != null) + { + return result; + } + else + { + ChangeSummary eChangeSummary = eDataGraph.getEChangeSummary(); + if (eChangeSummary != null) + { + result = ((EObject)eDataGraph.getChangeSummary()).eResource().getEObject(id); + if (result != null) + { + return result; + } + } + } + } + } + return super.getEObjectByID(id); + } + + public static class SaveImpl extends XMLSaveImpl + { + protected DataGraphImpl eDataGraph; + + public SaveImpl(XMLHelper xmlHelper) + { + super(xmlHelper); + } + + public void traverse(List contents) + { + if (contents.size() >= 1 && contents.get(0) instanceof DataGraphImpl) + { + eDataGraph = (DataGraphImpl)contents.get(0); + + Object datagraphMark = null; + if (!toDOM) + { + if (declareXML) + { + doc.add(""); + doc.addLine(); + } + String elementName = helper.getQName(eDataGraph.eClass()); + doc.startElement(elementName); + datagraphMark = doc.mark(); + } + else + { + helper.populateNameInfo(nameInfo, eDataGraph.eClass()); + currentNode = document.createElementNS(nameInfo.getNamespaceURI(), nameInfo.getQualifiedName()); + document.appendChild(currentNode); + // not calling handler since there is no corresponding EObject + } + + if (eDataGraph.eResource() != null && eDataGraph.eResource().getResourceSet() != null) + { + List ePackages = new ArrayList(); + for (Iterator i = eDataGraph.eResource().getResourceSet().getResources().iterator(); i.hasNext();) + { + List resourceContents = ((Resource)i.next()).getContents(); + if (resourceContents.size() == 1 && resourceContents.get(0) instanceof EPackage) + { + ePackages.add(resourceContents.get(0)); + } + } + if (!ePackages.isEmpty()) + { + if (!toDOM) + { + doc.startElement("models"); + doc.addAttribute("xmlns", ""); + } + else + { + currentNode = currentNode.appendChild(document.createElementNS(null, "models")); + ((Element)currentNode).setAttributeNS(ExtendedMetaData.XMLNS_URI, ExtendedMetaData.XMLNS_PREFIX, ""); + // not calling handler since there is no corresponding EObject + } + for (Iterator i = ePackages.iterator(); i.hasNext();) + { + writeTopObject((EPackage)i.next()); + } + if (!toDOM) + { + doc.endElement(); + } + else + { + currentNode = currentNode.getParentNode(); + } + } + } + + // use namespace declarations defined in the document (if any) + EObject eRootObject = eDataGraph.getERootObject(); + EReference xmlnsPrefixMapFeature = extendedMetaData.getXMLNSPrefixMapFeature(eRootObject.eClass()); + if (xmlnsPrefixMapFeature != null) + { + EMap xmlnsPrefixMap = (EMap)eRootObject.eGet(xmlnsPrefixMapFeature); + helper.setPrefixToNamespaceMap(xmlnsPrefixMap); + } + ChangeSummary changeSummary = eDataGraph.getEChangeSummary(); + + if (changeSummary != null) + { + helper.setMustHavePrefix(true); + if (changeSummary.isLogging()) + { + ((ChangeSummaryImpl)changeSummary).summarize(); + writeTopObject((EObject)changeSummary); + } + else + { + writeTopObject((EObject)changeSummary); + } + helper.setMustHavePrefix(false); + } + + if (eRootObject != null && writeTopObject(eRootObject) == null && !toDOM) + { + doc.addLine(); + doc.setMixed(false); + } + if (!toDOM) + { + doc.endElement(); + // reset to add namespace declarations + // + doc.resetToMark(datagraphMark); + } + else + { + currentNode = document.getFirstChild(); + } + addNamespaceDeclarations(); + } + else + { + super.traverse(contents); + } + } + + protected void writeTopAttributes(EObject top) + { + if (top == eDataGraph.getEChangeSummary()) + { + if (!toDOM) + { + doc.addAttribute("xmlns", ""); + doc.addAttribute("logging", String.valueOf(eDataGraph.getEChangeSummary().isLogging())); + } + else + { + ((Element)currentNode).setAttributeNS(ExtendedMetaData.XMLNS_URI, ExtendedMetaData.XMLNS_PREFIX, ""); + ((Element)currentNode).setAttributeNS("", "logging", String.valueOf(eDataGraph.getEChangeSummary().isLogging())); + } + } + } + + protected EObject getSchemaLocationRoot(EObject eObject) + { + return eDataGraph.getERootObject(); + } + } + + protected XMLSave createXMLSave() + { + return new SaveImpl(createXMLHelper()); + } + + public static class LoadImpl extends XMLLoadImpl + { + protected boolean resumeLogging = false; + + public void load(XMLResource resource, InputSource inputSource, Map options) throws IOException + { + super.load(resource, inputSource, options); + if (resumeLogging) ((ChangeSummaryImpl)((DataGraphImpl)resource.getContents().get(0)).getChangeSummary()).resumeLogging(); + } + + public void load(XMLResource resource, InputStream inputStream, Map options) throws IOException + { + super.load(resource, inputStream, options); + if (resumeLogging) ((ChangeSummaryImpl)((DataGraphImpl)resource.getContents().get(0)).getChangeSummary()).resumeLogging(); + } + + public void load(XMLResource resource, Node node, Map options) throws IOException + { + super.load(resource, node, options); + if (resumeLogging) ((ChangeSummaryImpl)((DataGraphImpl)resource.getContents().get(0)).getChangeSummary()).resumeLogging(); + } + + public LoadImpl(XMLHelper xmlHelper) + { + super(xmlHelper); + } + + protected DefaultHandler makeDefaultHandler() + { + return new SAXXMLHandler(resource, helper, options) + { + protected DataGraphImpl eDataGraph; + + protected boolean isInModels; + + protected List ePackages = new ArrayList(); + + protected EObject createDocumentRoot(String prefix, String uri, String name, EFactory eFactory, boolean top) + { + return null; + } + + protected void setAttribValue(EObject object, String name, String value) + { + if ("logging".equals(name) && object instanceof ChangeSummaryImpl) + { + resumeLogging = Boolean.valueOf(value).booleanValue(); + } + else + { + super.setAttribValue(object, name, value); + } + } + + protected EMap recordNamespacesSchemaLocations(EObject root) + { + EObject dgroot = eDataGraph.getERootObject(); + if (dgroot == null) + { + return null; + } + EMap prefixToNamespaceMap = super.recordNamespacesSchemaLocations(dgroot); + if (prefixToNamespaceMap != null) + { + for (Iterator i = prefixToNamespaceMap.iterator(); i.hasNext();) + { + Map.Entry entry = (Map.Entry)i.next(); + String prefix = (String)entry.getKey(); + String namespace = (String)entry.getValue(); + if (namespace.equals("commonj.sdo")) + { + prefixToNamespaceMap.removeKey(prefix); + break; + } + } + } + return prefixToNamespaceMap; + } + + protected void handleFeature(String prefix, String name) + { + if (isInModels && objects.size() == 2) + { + EObject modelObject = createObjectByType(prefix, name, false); + processObject(modelObject); + ePackages.add(modelObject); + } + else if (objects.size() == 1) + { + eDataGraph = (DataGraphImpl)objects.peek(); + eDataGraph.getResourceSet(); + if ("".equals(prefix) && "changeSummary".equals(name)) + { + ChangeSummary eChangeSummary = (ChangeSummary)createObjectFromFactory(SDOFactory.eINSTANCE, "ChangeSummary"); + eDataGraph.setEChangeSummary(eChangeSummary); + processObject((EObject)eChangeSummary); + } + else if ("".equals(prefix) && "models".equals(name)) + { + isInModels = true; + types.push(OBJECT_TYPE); + objects.push(eDataGraph); + mixedTargets.push(null); + } + else if (eDataGraph.getERootObject() == null) + { + if (useNewMethods) + { + handleSchemaLocation(); + } + processSchemaLocations(prefix, name); + if (processAnyXML) + { + // Ensure that anything can be handled, even if it's not recognized. + // + String namespaceURI = helper.getURI(prefix); + if (extendedMetaData.getPackage(namespaceURI) == null) + { + EStructuralFeature rootFeature = extendedMetaData.demandFeature(namespaceURI, name, true); + rootFeature.getEContainingClass().getEPackage().setEFactoryInstance(new DynamicDataObjectImpl.FactoryImpl()); + } + } + + //FB TEMPORARY allow loading proper serialization (global element instead of type name) + //FB Proper fix is to reimplement DataGraph as proper DataObject, and remove this entire class + EStructuralFeature rootFeature = extendedMetaData.getElement(helper.getURI(prefix), name); + if (rootFeature != null) name = rootFeature.getEType().getName(); + + EObject rootObject = createObjectByType(prefix, name, false); + + eDataGraph.setERootObject(rootObject); + processObject(rootObject); + if (rootObject != null + && rootObject.eClass() == ExtendedMetaData.INSTANCE.getDocumentRoot(rootObject.eClass().getEPackage())) + { + super.handleFeature(prefix, name); + + // Remove the document root from the stack. + // + Object mixedTarget = mixedTargets.pop(); + Object object = objects.pop(); + mixedTargets.pop(); + objects.pop(); + mixedTargets.push(mixedTarget); + objects.push(object); + } + } + } + else + { + super.handleFeature(prefix, name); + } + } + + public void endElement(String uri, String localName, String name) + { + if (isInModels && objects.size() == 2) + { + if (!ePackages.isEmpty()) + { + for (Iterator i = ePackages.iterator(); i.hasNext();) + { + EPackage ePackage = (EPackage)i.next(); + ePackage.setEFactoryInstance(new DynamicDataObjectImpl.FactoryImpl()); + Resource resource = resourceSet.createResource(URI.createURI("*.ecore")); + resource.getContents().add(ePackage); + if (ePackage.getNsURI() != null) + { + resource.setURI(URI.createURI(ePackage.getNsURI())); + } + + if (extendedMetaData != null) + { + extendedMetaData.putPackage(extendedMetaData.getNamespace(ePackage), ePackage); + } + else + { + packageRegistry.put(ePackage.getNsURI(), ePackage); + } + } + handleForwardReferences(); + } + isInModels = false; + } + // TODO The following 3 lines of code are a temporary work-around for JIRA issue TUSCANY-1862. + // These lines of code should be removed when TUSCANY-1862 is resolved. + if ("".equals(uri) && "objectsToAttach".equals(name) && text != null && text.length() == 0) + { + text = null; + } + super.endElement(uri, localName, name); + } + + protected EPackage getPackageForURI(String uriString) + { + if ("commonj.sdo".equals(uriString)) + { + return SDOPackage.eINSTANCE; + } + else + { + return super.getPackageForURI(uriString); + } + } + + protected EObject createObjectFromFactory(EFactory factory, String typeName) + { + if (factory == SDOFactory.eINSTANCE) + { + if ("datagraph".equals(typeName)) + { + return super.createObjectFromFactory(factory, "DataGraph"); + } + } + return super.createObjectFromFactory(factory, typeName); + } + + protected EcoreBuilder createEcoreBuilder(Map options, ExtendedMetaData extendedMetaData) + { + return new DefaultEcoreBuilder(extendedMetaData) + { + public Collection generate(Map urisToLocations) throws Exception + { + Collection result = super.generate(urisToLocations); + return updateDynamicFactory(result); + } + + public Collection generate(Collection urisToLocations) throws Exception + { + Collection result = super.generate(urisToLocations); + return updateDynamicFactory(result); + } + + protected Collection updateDynamicFactory(Collection result) + { + for (Iterator i = result.iterator(); i.hasNext();) + { + Resource resource = (Resource)i.next(); + for (Iterator j = EcoreUtil.getObjectsByType(resource.getContents(), EcorePackage.eINSTANCE.getEPackage()).iterator(); j.hasNext();) + { + EPackage ePackage = (EPackage)j.next(); + ePackage.setEFactoryInstance(new DynamicDataObjectImpl.FactoryImpl()); + } + } + return result; + } + + }; + } + + protected EPackage handleMissingPackage(String uriString) + { + EPackage result = super.handleMissingPackage(uriString); + if (processAnyXML && objects.size() == 1) + { + result = extendedMetaData.demandPackage(uriString); + } + return result; + } + }; + } + } + + protected XMLLoad createXMLLoad() + { + return new LoadImpl(createXMLHelper()); + } + } +} diff --git a/sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/DataObjectUtil.java b/sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/DataObjectUtil.java new file mode 100644 index 0000000000..8481fdfd06 --- /dev/null +++ b/sdo-java/tags/1.1.1-RC2a/impl/src/main/java/org/apache/tuscany/sdo/util/DataObjectUtil.java @@ -0,0 +1,3035 @@ +/** + * + * 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.util; + + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.security.AccessController; +import java.security.PrivilegedExceptionAction; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TimeZone; + +import org.apache.tuscany.sdo.SDOExtendedMetaData; +import org.apache.tuscany.sdo.SDOFactory; +import org.apache.tuscany.sdo.SDOPackage; +import org.apache.tuscany.sdo.api.SDOUtil; +import org.apache.tuscany.sdo.helper.HelperContextImpl; +import org.apache.tuscany.sdo.impl.ClassImpl; +import org.apache.tuscany.sdo.impl.DataGraphImpl; +import org.apache.tuscany.sdo.model.ModelFactory; +import org.apache.tuscany.sdo.model.impl.ModelFactoryImpl; +import org.apache.tuscany.sdo.util.resource.SDOURIConverterImpl; +import org.apache.tuscany.sdo.util.resource.SDOXMLResourceFactoryImpl; +import org.eclipse.emf.common.util.BasicEList; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.common.util.UniqueEList; +import org.eclipse.emf.ecore.EAnnotation; +import org.eclipse.emf.ecore.EAttribute; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EClassifier; +import org.eclipse.emf.ecore.EDataType; +import org.eclipse.emf.ecore.EModelElement; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EReference; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.ecore.EcorePackage; +import org.eclipse.emf.ecore.impl.EDataTypeImpl; +import org.eclipse.emf.ecore.impl.EPackageRegistryImpl; +import org.eclipse.emf.ecore.impl.EStringToStringMapEntryImpl; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; +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.util.FeatureMapUtil; +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.EMOFResourceFactoryImpl; +import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl; +import org.eclipse.emf.ecore.xmi.impl.XMLOptionsImpl; +import org.eclipse.emf.ecore.xmi.impl.XMLParserPoolImpl; +import org.eclipse.emf.ecore.xml.type.XMLTypeFactory; +import org.eclipse.emf.ecore.xml.type.XMLTypePackage; +import org.eclipse.xsd.util.XSDResourceFactoryImpl; + +import commonj.sdo.ChangeSummary; +import commonj.sdo.DataGraph; +import commonj.sdo.DataObject; +import commonj.sdo.Property; +import commonj.sdo.Sequence; +import commonj.sdo.Type; +import commonj.sdo.helper.DataHelper; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.TypeHelper; +import commonj.sdo.impl.HelperProvider; + + +public final class DataObjectUtil +{ + public static void setString(DataObject dataObject, Property property, String value) { + dataObject.set(property, getSetValue(property, value)); + } + + public static void setShort(DataObject dataObject, Property property, short value) { + dataObject.set(property, getSetValue(property, value)); + } + + public static void setLong(DataObject dataObject, Property property, long value) { + dataObject.set(property, getSetValue(property, value)); + } + + public static void setList(DataObject dataObject, Property property, List value) { + dataObject.set(property, value); + } + + public static void setInt(DataObject dataObject, Property property, int value) { + dataObject.set(property, getSetValue(property, value)); + } + + public static void setFloat(DataObject dataObject, Property property, float value) { + dataObject.set(property, getSetValue(property, value)); + } + + public static void setDouble(DataObject dataObject, Property property, double value) { + dataObject.set(property, getSetValue(property, value)); + } + + public static void setDate(DataObject dataObject, Property property, Date value) { + dataObject.set(property, getSetValue(property, value)); + } + + public static void setDataObject(DataObject dataObject, Property property, DataObject value) { + dataObject.set(property, value); + } + + public static void setChar(DataObject dataObject, Property property, char value) { + dataObject.set(property, getSetValue(property, value)); + } + + public static void setBytes(DataObject dataObject, Property property, byte[] value) { + dataObject.set(property, getSetValue(property, value)); + } + + public static void setByte(DataObject dataObject, Property property, byte value) { + dataObject.set(property, getSetValue(property, value)); + } + + public static void setBoolean(DataObject dataObject, Property property, boolean value) { + dataObject.set(property, getSetValue(property, value)); + } + + public static void setBigInteger(DataObject dataObject, Property property, BigInteger value) + { + dataObject.set(property, getSetValue(property, value)); + } + + public static void setBigDecimal(DataObject dataObject, Property property, BigDecimal value) + { + dataObject.set(property, getSetValue(property, value)); + } + + public static String getString(DataObject dataObject, Property property) + { + return getString(dataObject.get(property)); + } + + public static short getShort(DataObject dataObject, Property property) + { + return getShort(dataObject.get(property)); + } + + public static Sequence getSequence(DataObject dataObject, Property property) + { + return (Sequence)dataObject.get(property); + } + + public static long getLong(DataObject dataObject, Property property) + { + return getLong(dataObject.get(property)); + } + + public static List getList(DataObject dataObject, Property property) + { + return (List)dataObject.get(property); + } + + public static int getInt(DataObject dataObject, Property property) + { + return getInt(dataObject.get(property)); + } + + public static float getFloat(DataObject dataObject, Property property) + { + return getFloat(dataObject.get(property)); + } + + public static double getDouble(DataObject dataObject, Property property) + { + return getDouble(dataObject.get(property)); + } + + public static Date getDate(DataObject dataObject, Property property) + { + return getDate(dataObject.get(property)); + } + + public static DataObject getDataObject(DataObject dataObject, Property property) + { + return (DataObject)dataObject.get(property); + } + + public static char getChar(DataObject dataObject, Property property) + { + return getChar(dataObject.get(property)); + } + + public static byte[] getBytes(DataObject dataObject, Property property) + { + return getBytes(dataObject.get(property)); + } + + public static byte getByte(DataObject dataObject, Property property) + { + return getByte(dataObject.get(property)); + } + + public static boolean getBoolean(DataObject dataObject, Property property) + { + return getBoolean(dataObject.get(property)); + } + + public static BigInteger getBigInteger(DataObject dataObject, Property property) + { + return getBigInteger(dataObject.get(property)); + } + + public static BigDecimal getBigDecimal(DataObject dataObject, Property property) + { + return getBigDecimal(dataObject.get(property)); + } + + + public static void setString(DataObject dataObject, int propertyIndex, String value) { + setString(dataObject,getProperty(dataObject, propertyIndex), value); + } + + public static void setShort(DataObject dataObject, int propertyIndex, short value) { + setShort(dataObject,getProperty(dataObject, propertyIndex), value); + } + + public static void setLong(DataObject dataObject, int propertyIndex, long value) { + setLong(dataObject,getProperty(dataObject, propertyIndex), value); + } + + public static void setList(DataObject dataObject, int propertyIndex, List value) { + setList(dataObject,getProperty(dataObject, propertyIndex), value); + } + + public static void setInt(DataObject dataObject, int propertyIndex, int value) { + setInt(dataObject,getProperty(dataObject, propertyIndex), value); + } + + public static void setFloat(DataObject dataObject, int propertyIndex, float value) { + setFloat(dataObject,getProperty(dataObject, propertyIndex), value); + } + + public static void setDouble(DataObject dataObject, int propertyIndex, double value) { + setDouble(dataObject,getProperty(dataObject, propertyIndex), value); + } + + public static void setDate(DataObject dataObject, int propertyIndex, Date value) { + setDate(dataObject,getProperty(dataObject, propertyIndex), value); + } + + public static void setDataObject(DataObject dataObject, int propertyIndex, DataObject value) { + dataObject.set(getProperty(dataObject, propertyIndex), value); + } + + public static void setChar(DataObject dataObject, int propertyIndex, char value) { + setChar(dataObject,getProperty(dataObject, propertyIndex), value); + } + + public static void setBytes(DataObject dataObject, int propertyIndex, byte[] value) { + setBytes(dataObject,getProperty(dataObject, propertyIndex), value); + } + + public static void setByte(DataObject dataObject, int propertyIndex, byte value) { + setByte(dataObject,getProperty(dataObject, propertyIndex), value); + } + + public static void setBoolean(DataObject dataObject, int propertyIndex, boolean value) { + setBoolean(dataObject,getProperty(dataObject, propertyIndex), value); + } + + public static void setBigInteger(DataObject dataObject, int propertyIndex, BigInteger value) + { + setBigInteger(dataObject,getProperty(dataObject, propertyIndex), value); + } + + public static void setBigDecimal(DataObject dataObject, int propertyIndex, BigDecimal value) + { + setBigDecimal(dataObject,getProperty(dataObject, propertyIndex), value); + } + + public static String getString(DataObject dataObject, int propertyIndex) + { + return getString(dataObject.get(getProperty(dataObject, propertyIndex))); + } + + public static short getShort(DataObject dataObject, int propertyIndex) + { + return getShort(dataObject.get(getProperty(dataObject, propertyIndex))); + } + + + public static Sequence getSequence(DataObject dataObject, int propertyIndex) + { + return (Sequence)dataObject.get(propertyIndex); + } + + public static long getLong(DataObject dataObject, int propertyIndex) + { + return getLong(dataObject.get(getProperty(dataObject, propertyIndex))); + } + + public static List getList(DataObject dataObject, int propertyIndex) + { + return (List)dataObject.get(getProperty(dataObject, propertyIndex)); + } + + public static int getInt(DataObject dataObject, int propertyIndex) + { + return getInt(dataObject.get(getProperty(dataObject, propertyIndex))); + } + + public static float getFloat(DataObject dataObject, int propertyIndex) + { + return getFloat(dataObject.get(getProperty(dataObject, propertyIndex))); + } + + public static double getDouble(DataObject dataObject, int propertyIndex) + { + return getDouble(dataObject.get(getProperty(dataObject, propertyIndex))); + } + + public static Date getDate(DataObject dataObject, int propertyIndex) + { + return getDate(dataObject.get(getProperty(dataObject, propertyIndex))); + } + + public static DataObject getDataObject(DataObject dataObject, int propertyIndex) + { + return (DataObject)dataObject.get(getProperty(dataObject, propertyIndex)); + } + + public static char getChar(DataObject dataObject, int propertyIndex) + { + return getChar(dataObject.get(getProperty(dataObject, propertyIndex))); + } + + public static byte[] getBytes(DataObject dataObject, int propertyIndex) + { + return getBytes(dataObject.get(getProperty(dataObject, propertyIndex))); + } + + public static byte getByte(DataObject dataObject, int propertyIndex) + { + return getByte(dataObject.get(getProperty(dataObject, propertyIndex))); + } + + public static boolean getBoolean(DataObject dataObject, int propertyIndex) + { + return getBoolean(dataObject.get(getProperty(dataObject, propertyIndex))); + } + + public static BigInteger getBigInteger(DataObject dataObject, int propertyIndex) + { + return getBigInteger(dataObject.get(getProperty(dataObject, propertyIndex))); + } + + public static BigDecimal getBigDecimal(DataObject dataObject, int propertyIndex) + { + return getBigDecimal(dataObject.get(getProperty(dataObject, propertyIndex))); + } + + public static void detach(DataObject dataObject) { + EcoreUtil.remove((EObject)dataObject); + } + + public static DataObject getRootObject(DataObject dataObject) + { + return (DataObject)EcoreUtil.getRootContainer((EObject)dataObject); + } + + public static boolean isInternalProperty(EStructuralFeature eStructuralFeature) + { + //return FeatureMapUtil.isFeatureMap(eStructuralFeature); + EClassifier eClassifier = eStructuralFeature.getEType(); + return !(eClassifier instanceof Type || eClassifier == XMLTypePackage.Literals.BASE64_BINARY); + } + + public static List getInstanceProperties(DataObject dataObject) + { + Type type = dataObject.getType(); + List result = new UniqueEList(type.getProperties()); + ((ClassImpl)type).addOpenProperties((EObject)dataObject, result); + return result; + } + + public static void delete(DataObject dataObject) + { + EObject eDataObject = (EObject)dataObject; + EcoreUtil.remove(eDataObject); + List contents = new ArrayList((eDataObject).eContents()); + for (int i = 0, size = contents.size(); i < size; ++i) + { + ((DataObject)contents.get(i)).delete(); + } + EClass eClass = eDataObject.eClass(); + for (int i = 0, size = eClass.getFeatureCount(); i < size; ++i) + { + EStructuralFeature eStructuralFeature = eClass.getEStructuralFeature(i); + if (eStructuralFeature.isChangeable() && !eStructuralFeature.isDerived() && !((Property)eStructuralFeature).isReadOnly()) + { + eDataObject.eUnset(eStructuralFeature); + } + } + } + + public static DataObject createDataObject(DataObject dataObject, Property property, Type type) + { + if (!property.isContainment()) + { + throw new IllegalArgumentException("The property '" + property.getName() + "' of '" + property.getContainingType().getName() + + "' isn't a containment"); + } + DataObject result = DataObjectUtil.create(type); + if (FeatureMapUtil.isMany((EObject)dataObject, (EStructuralFeature)property)) + { + ((List)dataObject.get(property)).add(result); + } + else + { + dataObject.set(property, result); + } + return result; + } + + public static DataObject createDataObject(DataObject dataObject, int propertyIndex, String namespaceURI, String typeName) + { + Property property = DataObjectUtil.getProperty(dataObject, propertyIndex); + Type type = DataObjectUtil.getType(dataObject, namespaceURI, typeName); + return createDataObject(dataObject, property, type); + } + + public static DataObject createDataObject(DataObject dataObject, String propertyName, String namespaceURI, String typeName) + { + Property property = getInstanceProperty(dataObject, propertyName); + if (property != null) { + Type type = DataObjectUtil.getType(dataObject, namespaceURI, typeName); + return createDataObject(dataObject, property, type); + } + else { + if (dataObject.getType().isOpen()) { + HelperContext ctx = HelperProvider.getDefaultContext(); + Type propertyType = ctx.getTypeHelper().getType( namespaceURI, typeName ); + if (propertyType == null) { + throw new IllegalStateException( "type does not exist: uri=" + namespaceURI + ", name=" + typeName ); + } + DataObject value = ctx.getDataFactory().create( propertyType ); + List list = new ArrayList(1); + list.add(value); + dataObject.setList( propertyName, list ); + return value; + } + else { + throw new IllegalArgumentException( "property '" + propertyName + "' does not exist" ); + } + } + } + + public static DataObject createDataObject(DataObject dataObject, Property property) + { + Type type = property.getType(); + return createDataObject(dataObject, property, type); + } + + public static DataObject createDataObject(DataObject dataObject, int propertyIndex) + { + Property property = getProperty(dataObject, propertyIndex); + Type type = property.getType(); + return createDataObject(dataObject,property, type); + } + + public static DataObject createDataObject(DataObject dataObject, String propertyName) + { + Property property = (Property)getInstanceProperty(dataObject, propertyName); + if (property != null) { + Type type = property.getType(); + return createDataObject(dataObject,property, type); + } + else { + return createDataObject(dataObject, propertyName, "http://www.apache.org/tuscany/2005/SDO", "AnyTypeDataObject" ); + } + } + + public static void setString(DataObject dataObject, String path, String value) + { + Property property = dataObject.getType().getProperty(path); + if (property != null) + { + dataObject.set(property, DataObjectUtil.getSetValue(property, value)); + } + else + { + DataObjectUtil.Accessor accessor = DataObjectUtil.Accessor.create((EObject)dataObject, path); + accessor.setAndRecyle(DataObjectUtil.getSetValue(accessor.getProperty(), value)); + } + } + + + public static void setShort(DataObject dataObject, String path, short value) + { + Property property = dataObject.getType().getProperty(path); + if (property != null) + { + dataObject.set(property, DataObjectUtil.getSetValue(property, value)); + } + else + { + DataObjectUtil.Accessor accessor = DataObjectUtil.Accessor.create((EObject)dataObject, path); + accessor.setAndRecyle(DataObjectUtil.getSetValue(accessor.getProperty(), value)); + } + } + + public static void setLong(DataObject dataObject, String path, long value) + { + Property property = dataObject.getType().getProperty(path); + if (property != null) + { + dataObject.set(property, DataObjectUtil.getSetValue(property, value)); + } + else + { + DataObjectUtil.Accessor accessor = DataObjectUtil.Accessor.create((EObject)dataObject, path); + accessor.setAndRecyle(DataObjectUtil.getSetValue(accessor.getProperty(), value)); + } + } + + public static void setList(DataObject dataObject, String path, List value) + { + dataObject.set(path, value); + } + + public static void setInt(DataObject dataObject, String path, int value) + { + Property property = dataObject.getType().getProperty(path); + if (property != null) + { + dataObject.set(property, DataObjectUtil.getSetValue(property, value)); + } + else + { + DataObjectUtil.Accessor accessor = DataObjectUtil.Accessor.create((EObject)dataObject, path); + accessor.setAndRecyle(DataObjectUtil.getSetValue(accessor.getProperty(), value)); + } + } + + public static void setFloat(DataObject dataObject, String path, float value) + { + Property property = dataObject.getType().getProperty(path); + if (property != null) + { + dataObject.set(property, DataObjectUtil.getSetValue(property, value)); + } + else + { + DataObjectUtil.Accessor accessor = DataObjectUtil.Accessor.create((EObject)dataObject, path); + accessor.setAndRecyle(DataObjectUtil.getSetValue(accessor.getProperty(), value)); + } + } + + public static void setDouble(DataObject dataObject, String path, double value) + { + Property property = dataObject.getType().getProperty(path); + if (property != null) + { + dataObject.set(property, DataObjectUtil.getSetValue(property, value)); + } + else + { + DataObjectUtil.Accessor accessor = DataObjectUtil.Accessor.create((EObject)dataObject, path); + accessor.setAndRecyle(DataObjectUtil.getSetValue(accessor.getProperty(), value)); + } + } + + public static void setDate(DataObject dataObject, String path, Date value) + { + Property property = dataObject.getType().getProperty(path); + if (property != null) + { + dataObject.set(property, DataObjectUtil.getSetValue(property, value)); + } + else + { + DataObjectUtil.Accessor accessor = DataObjectUtil.Accessor.create((EObject)dataObject, path); + accessor.setAndRecyle(DataObjectUtil.getSetValue(accessor.getProperty(), value)); + } + } + + public static void setDataObject(DataObject dataObject, String path, DataObject value) + { + dataObject.set(path, value); + } + + public static void setChar(DataObject dataObject, String path, char value) + { + Property property = dataObject.getType().getProperty(path); + if (property != null) + { + dataObject.set(property, DataObjectUtil.getSetValue(property, value)); + } + else + { + DataObjectUtil.Accessor accessor = DataObjectUtil.Accessor.create((EObject)dataObject, path); + accessor.setAndRecyle(DataObjectUtil.getSetValue(accessor.getProperty(), value)); + } + } + + public static void setBytes(DataObject dataObject, String path, byte[] value) + { + Property property = dataObject.getType().getProperty(path); + if (property != null) + { + dataObject.set(property, DataObjectUtil.getSetValue(property, value)); + } + else + { + DataObjectUtil.Accessor accessor = DataObjectUtil.Accessor.create((EObject)dataObject, path); + accessor.setAndRecyle(DataObjectUtil.getSetValue(accessor.getProperty(), value)); + } + } + + public static void setByte(DataObject dataObject, String path, byte value) + { + Property property = dataObject.getType().getProperty(path); + if (property != null) + { + dataObject.set(property, DataObjectUtil.getSetValue(property, value)); + } + else + { + DataObjectUtil.Accessor accessor = DataObjectUtil.Accessor.create((EObject)dataObject, path); + accessor.setAndRecyle(DataObjectUtil.getSetValue(accessor.getProperty(), value)); + } + } + + public static void setBoolean(DataObject dataObject, String path, boolean value) + { + Property property = dataObject.getType().getProperty(path); + if (property != null) + { + dataObject.set(property, DataObjectUtil.getSetValue(property, value)); + } + else + { + DataObjectUtil.Accessor accessor = DataObjectUtil.Accessor.create((EObject)dataObject, path); + accessor.setAndRecyle(DataObjectUtil.getSetValue(accessor.getProperty(), value)); + } + } + + public static void setBigInteger(DataObject dataObject, String path, BigInteger value) + { + Property property = dataObject.getType().getProperty(path); + if (property != null) + { + dataObject.set(property, DataObjectUtil.getSetValue(property, value)); + } + else + { + DataObjectUtil.Accessor accessor = DataObjectUtil.Accessor.create((EObject)dataObject, path); + accessor.setAndRecyle(DataObjectUtil.getSetValue(accessor.getProperty(), value)); + } + } + + public static void setBigDecimal(DataObject dataObject, String path, BigDecimal value) + { + Property property = dataObject.getType().getProperty(path); + if (property != null) + { + dataObject.set(property, DataObjectUtil.getSetValue(property, value)); + } + else + { + DataObjectUtil.Accessor accessor = DataObjectUtil.Accessor.create((EObject)dataObject, path); + accessor.setAndRecyle(DataObjectUtil.getSetValue(accessor.getProperty(), value)); + } + } + + public static DataGraph getDataGraph(DataObject dataObject) + { + Resource resource = ((EObject)dataObject).eResource(); + if (resource != null) + { + ResourceSet resourceSet = resource.getResourceSet(); + if (resourceSet != null) + { + return (DataGraphImpl)EcoreUtil.getAdapter(resourceSet.eAdapters(), DataGraph.class); + } + } + return null; + } + + public static ChangeSummary getChangeSummary(DataObject dataObject) + { + DataGraph dataGraph = getDataGraph(dataObject); + if (dataGraph != null) + { + return dataGraph.getChangeSummary(); + } + else + { + for (DataObject csDataObject = dataObject; csDataObject != null; csDataObject = csDataObject.getContainer()) + { + Property csp = ((ClassImpl)csDataObject.getType()).getChangeSummaryProperty(); + if (csp != null) return (ChangeSummary)csDataObject.get(csp); + } + } + return null; + } + + public static void unset(DataObject dataObject, String path) + { + Property property = dataObject.getType().getProperty(path); + if (property != null) + { + dataObject.unset(property); + } + else + { + DataObjectUtil.Accessor.create((EObject)dataObject, path).unsetAndRecyle(); + } + } + + public static boolean isSet(DataObject dataObject, String path) + { + Property property = dataObject.getType().getProperty(path); + if (property != null) + { + return dataObject.isSet(property); + } + else + { + return DataObjectUtil.Accessor.create( + (EObject)dataObject, path).isSetAndRecyle(); + } + } + + public static void set(DataObject dataObject, String path, Object value) + { + Property property = dataObject.getType().getProperty(path); + if (property != null) + { + dataObject.set(property, value); + } + else + { + DataObjectUtil.Accessor.create( + (EObject)dataObject, path, value).setAndRecyle(value); + } + } + + public static Object get(DataObject dataObject, String path) + { + Property property = dataObject.getType().getProperty(path); + if (property != null) { + return dataObject.get(property); + } else { + return Accessor.create((EObject)dataObject, path).getAndRecyle(); + } + } + + public static BigDecimal getBigDecimal(Object value) + { + if (value instanceof BigDecimal) + { + return (BigDecimal)value; + } + + if (value instanceof BigInteger) + { + return new BigDecimal((BigInteger)value); + } + + if (value instanceof Long) + { + return new BigDecimal(((Long)value).longValue()); + } + + if (value instanceof Number) + { + return new BigDecimal(((Number)value).doubleValue()); + } + + if (value instanceof String) + { + return new BigDecimal((String)value); + } + + if (value == null) + { + return null; + } + + throw new ClassCastException("The value of type '" + value.getClass().getName() + "' cannot be converted to BigDecimal"); + } + + public static Object getSetValue(Property property, BigDecimal value) + { + EStructuralFeature eStructuralFeature = (EStructuralFeature)property; + EClassifier eType = eStructuralFeature.getEType(); + if (value == null) + { + return eType.getDefaultValue(); + } + + String name = eType.getInstanceClassName(); + if (name == "java.math.BigDecimal") + { + return value; + } + + if (name == "java.math.BigInteger") + { + return value.toBigInteger(); + } + + if (name == "java.lang.Byte" || name == "byte") + { + return new Byte(value.byteValue()); + } + + if (name == "java.lang.Double" || name == "double") + { + return new Double(value.doubleValue()); + } + + if (name == "java.lang.Float" || name == "float") + { + return new Float(value.floatValue()); + } + + if (name == "java.lang.Integer" || name == "int") + { + return new Integer(value.intValue()); + } + + if (name == "java.lang.Long" || name == "long") + { + return new Long(value.longValue()); + } + + if (name == "java.lang.Short" || name == "short") + { + return new Short(value.shortValue()); + } + + if (name == "java.lang.String") + { + return String.valueOf(value); + } + + //Instead of throwing an ClassCastException we will pass the value to the property + return value; + } + + public static BigInteger getBigInteger(Object value) + { + if (value instanceof BigInteger) + { + return (BigInteger)value; + } + + if (value instanceof BigDecimal) + { + return ((BigDecimal)value).toBigInteger(); + } + + if (value instanceof Number) + { + return BigInteger.valueOf(((Number)value).longValue()); + } + + if (value instanceof String) + { + return new BigInteger((String)value); + } + + if (value instanceof byte[]) + { + return new BigInteger((byte[])value); + } + + if (value == null) + { + return null; + } + + throw new ClassCastException("The value of type '" + value.getClass().getName() + "' cannot be converted to BigInteger"); + } + + public static Object getSetValue(Property property, BigInteger value) + { + EStructuralFeature eStructuralFeature = (EStructuralFeature)property; + EClassifier eType = eStructuralFeature.getEType(); + if (value == null) + { + return eType.getDefaultValue(); + } + + String name = eType.getInstanceClassName(); + if (name == "java.math.BigInteger") + { + return value; + } + + if (name == "java.math.BigDecimal") + { + return new BigDecimal(value); + } + + if (name == "java.lang.Byte" || name == "byte") + { + return new Byte(value.byteValue()); + } + + if (name == "java.lang.Double" || name == "double") + { + return new Double(value.doubleValue()); + } + + if (name == "java.lang.Float" || name == "float") + { + return new Float(value.floatValue()); + } + + if (name == "java.lang.Integer" || name == "int") + { + return new Integer(value.intValue()); + } + + if (name == "java.lang.Long" || name == "long") + { + return new Long(value.longValue()); + } + + if (name == "java.lang.Short" || name == "short") + { + return new Short(value.shortValue()); + } + + if (name == "java.lang.String") + { + return String.valueOf(value); + } + + if (name == "byte[]") + { + return value.toByteArray(); + } + + //Instead of throwing an ClassCastException we will pass the value to the property + return value; + } + + public static boolean getBoolean(Object value) + { + if (value instanceof Boolean) + { + return ((Boolean)value).booleanValue(); + } + + if (value instanceof String) + { + return Boolean.valueOf((String)value).booleanValue(); + } + + if (value == null) + { + return false; + } + + throw new ClassCastException("The value of type '" + value.getClass().getName() + "' cannot be converted to boolean"); + } + + public static Object getSetValue(Property property, boolean value) + { + EStructuralFeature eStructuralFeature = (EStructuralFeature)property; + String name = eStructuralFeature.getEType().getInstanceClassName(); + if (name == "java.lang.Boolean" || name == "boolean") + { + return value ? Boolean.TRUE : Boolean.FALSE; + } + + if (name == "java.lang.String") + { + return String.valueOf(value); + } + + //Instead of throwing an ClassCastException we will pass the value to the property + return value ? Boolean.TRUE : Boolean.FALSE; + } + + public static byte getByte(Object value) + { + if (value instanceof Number) + { + return ((Number)value).byteValue(); + } + + if (value instanceof String) + { + return Byte.parseByte((String)value); + } + + if (value == null) + { + return 0; + } + + throw new ClassCastException("The value of type '" + value.getClass().getName() + "' cannot be converted to byte"); + } + + public static Object getSetValue(Property property, byte value) + { + EStructuralFeature eStructuralFeature = (EStructuralFeature)property; + String name = eStructuralFeature.getEType().getInstanceClassName(); + if (name == "java.lang.Byte" || name == "byte") + { + return new Byte(value); + } + + if (name == "java.lang.Double" || name == "double") + { + return new Double(value); + } + + if (name == "java.lang.Float" || name == "float") + { + return new Float(value); + } + + if (name == "java.lang.Integer" || name == "int") + { + return new Integer(value); + } + + if (name == "java.lang.Long" || name == "long") + { + return new Long(value); + } + + if (name == "java.lang.Short" || name == "short") + { + return new Short(value); + } + + if (name == "java.math.BigDecimal") + { + return getBigDecimal(new Byte(value)); + } + + if (name == "java.math.BigInteger") + { + return getBigInteger(new Byte(value)); + } + + if (name == "java.lang.String") + { + return String.valueOf(value); + } + + //Instead of throwing an ClassCastException we will pass the value to the property + return new Byte(value); + } + + public static byte[] getBytes(Object value) + { + if (value instanceof byte[]) + { + return (byte[])value; + } + + if (value instanceof BigInteger) + { + return ((BigInteger)value).toByteArray(); + } + + if (value instanceof String) + { + return (XMLTypeFactory.eINSTANCE.createHexBinary((String)value)); + } + + if (value == null) + { + return null; + } + + throw new ClassCastException("The value of type '" + value.getClass().getName() + "' cannot be converted to byte array"); + } + + public static Object getSetValue(Property property, byte[] value) + { + EStructuralFeature eStructuralFeature = (EStructuralFeature)property; + EClassifier eType = eStructuralFeature.getEType(); + if (value == null) + { + return eType.getDefaultValue(); + } + + String name = eType.getInstanceClassName(); + if (name == "byte[]") + { + return value; + } + + if (name == "java.math.BigInteger") + { + return new BigInteger(value); + } + + if (name == "java.lang.String" ) + { + return XMLTypeFactory.eINSTANCE.convertHexBinary((byte[])value); + } + + //Instead of throwing an ClassCastException we will pass the value to the property + return value; + } + + public static char getChar(Object value) + { + if (value instanceof Character) + { + return ((Character)value).charValue(); + } + + if (value instanceof String) + { + return ((String)value).charAt(0); + } + + if (value == null) + { + return 0; + } + + throw new ClassCastException("The value of type '" + value.getClass().getName() + "' cannot be converted to char"); + } + + public static Object getSetValue(Property property, char value) + { + EStructuralFeature eStructuralFeature = (EStructuralFeature)property; + String name = eStructuralFeature.getEType().getInstanceClassName(); + if (name == "java.lang.Character" || name == "char") + { + return new Character(value); + } + + if (name == "java.lang.String") + { + return String.valueOf(value); + } + + //Instead of throwing an ClassCastException we will pass the value to the property + return new Character(value); + } + + public static Date getDate(Object value) + { + if (value instanceof String) { + return DataHelper.INSTANCE.toDate((String)value); + } + + //if (value instanceof XMLCalendar) + //{ + //return ((XMLCalendar)value).getDate(); + //} + + if (value instanceof Date) + { + return (Date)value; + } + + if (value instanceof Long) + { + return new Date(((Long)value).longValue()); + } + + if (value == null) + { + return null; + } + + throw new ClassCastException("The value of type '" + value.getClass().getName() + "' cannot be converted to Date"); + } + + public static Object getSetValue(Property property, Date value) + { + EStructuralFeature eStructuralFeature = (EStructuralFeature)property; + EClassifier eType = eStructuralFeature.getEType(); + if (value == null) + { + return eType.getDefaultValue(); + } + + String name = eType.getInstanceClassName(); + + if (name == "java.lang.String") + { + String typeName = getDateTypeName((EDataType)eType); + if ("DateTime".equals(typeName)) { + return DataHelper.INSTANCE.toDateTime(value); + } + else if ("Day".equals(typeName)) { + return DataHelper.INSTANCE.toDay(value); + } + else if ("Duration".equals(typeName)) { + return DataHelper.INSTANCE.toDuration(value); + } + else if ("Month".equals(typeName)) { + return DataHelper.INSTANCE.toMonth(value); + } + else if ("MonthDay".equals(typeName)) { + return DataHelper.INSTANCE.toMonthDay(value); + } + else if ("Time".equals(typeName)) { + return DataHelper.INSTANCE.toTime(value); + } + else if ("Year".equals(typeName)) { + return DataHelper.INSTANCE.toYear(value); + } + else if ("YearMonth".equals(typeName)) { + return DataHelper.INSTANCE.toYearMonth(value); + } + else if ("YearMonthDay".equals(typeName)) { + return DataHelper.INSTANCE.toYearMonthDay(value); + } + else if ("String".equals(typeName)) + { + return DataHelper.INSTANCE.toDateTime(value); + } + + // Instead of throwing an ClassCastException we will pass the value to the property + return value; + } + + //if (name == "java.util.Date") + //{ + //return new XMLCalendar(value, XMLCalendar.DATE); + //} + + if (name == "java.lang.Long" || name == "long") + { + return new Long(value.getTime()); + } + + // Instead of throwing an ClassCastException we will pass the value to the property + return value; + } + + protected static String getDateTypeName(EDataType eDataType) + { + String name = eDataType.getName(); + if (("DateTime".equals(name)) || + ("Day".equals(name)) || + ("Duration".equals(name)) || + ("Month".equals(name)) || + ("MonthDay".equals(name)) || + ("Time".equals(name)) || + ("Year".equals(name)) || + ("YearMonth".equals(name)) || + ("YearMonthDay".equals(name)) || + ("String".equals(name))) + { + return name; + } + + EDataType baseType = ExtendedMetaData.INSTANCE.getBaseType(eDataType); + if (baseType != null) + { + return getDateTypeName(baseType); + } + + List memberTypes = ExtendedMetaData.INSTANCE.getMemberTypes(eDataType); + if (!memberTypes.isEmpty()) + { + for (int i = 0, size = memberTypes.size(); i < size; ++i) + { + EDataType memberType = (EDataType)memberTypes.get(i); + String memberTypeName = getDateTypeName(memberType); + if (("DateTime".equals(memberTypeName)) || + ("Day".equals(memberTypeName)) || + ("Duration".equals(memberTypeName)) || + ("Month".equals(memberTypeName)) || + ("MonthDay".equals(memberTypeName)) || + ("Time".equals(memberTypeName)) || + ("Year".equals(memberTypeName)) || + ("YearMonth".equals(memberTypeName)) || + ("YearMonthDay".equals(memberTypeName)) || + ("String".equals(memberTypeName))) + { + return memberTypeName; + } + } + } + + return ""; + } + + public static double getDouble(Object value) + { + if (value instanceof Number) + { + return ((Number)value).doubleValue(); + } + + if (value instanceof String) + { + return Double.parseDouble((String)value); + } + + if (value == null) + { + return 0; + } + + throw new ClassCastException("The value of type '" + value.getClass().getName() + "' cannot be converted to double"); + } + + public static Object getSetValue(Property property, double value) + { + EStructuralFeature eStructuralFeature = (EStructuralFeature)property; + String name = eStructuralFeature.getEType().getInstanceClassName(); + if (name == "java.lang.Byte" || name == "byte") + { + return new Byte((byte)value); + } + + if (name == "java.lang.Double" || name == "double") + { + return new Double(value); + } + + if (name == "java.lang.Float" || name == "float") + { + return new Float(value); + } + + if (name == "java.lang.Integer" || name == "int") + { + return new Integer((int)value); + } + + if (name == "java.lang.Long" || name == "long") + { + return new Long((long)value); + } + + if (name == "java.lang.Short" || name == "short") + { + return new Short((short)value); + } + + if (name == "java.math.BigDecimal") + { + return getBigDecimal(new Double(value)); + } + + if (name == "java.math.BigInteger") + { + return getBigInteger(new Double(value)); + } + + if (name == "java.lang.String") + { + return String.valueOf(value); + } + + //Instead of throwing an ClassCastException we will pass the value to the property + return new Double(value); + } + + public static float getFloat(Object value) + { + if (value instanceof Number) + { + return ((Number)value).floatValue(); + } + + if (value instanceof String) + { + return Float.parseFloat((String)value); + } + + if (value == null) + { + return 0; + } + + throw new ClassCastException("The value of type '" + value.getClass().getName() + "' cannot be converted to float"); + } + + public static Object getSetValue(Property property, float value) + { + EStructuralFeature eStructuralFeature = (EStructuralFeature)property; + String name = eStructuralFeature.getEType().getInstanceClassName(); + if (name == "java.lang.Byte" || name == "byte") + { + return new Byte((byte)value); + } + + if (name == "java.lang.Double" || name == "double") + { + return new Double(value); + } + + if (name == "java.lang.Float" || name == "float") + { + return new Float(value); + } + + if (name == "java.lang.Integer" || name == "int") + { + return new Integer((int)value); + } + + if (name == "java.lang.Long" || name == "long") + { + return new Long((long)value); + } + + if (name == "java.lang.Short" || name == "short") + { + return new Short((short)value); + } + + if (name == "java.math.BigDecimal") + { + return getBigDecimal(new Float(value)); + } + + if (name == "java.math.BigInteger") + { + return getBigInteger(new Float(value)); + } + + if (name == "java.lang.String") + { + return String.valueOf(value); + } + + // Instead of throwing an ClassCastException we will pass the value to the property + return new Float(value); + } + + public static int getInt(Object value) + { + if (value instanceof Number) + { + return ((Number)value).intValue(); + } + + if (value instanceof String) + { + return Integer.parseInt((String)value); + } + + if (value == null) + { + return 0; + } + + throw new ClassCastException("The value of type '" + value.getClass().getName() + "' cannot be converted to int"); + } + + public static Object getSetValue(Property property, int value) + { + EStructuralFeature eStructuralFeature = (EStructuralFeature)property; + String name = eStructuralFeature.getEType().getInstanceClassName(); + if (name == "java.lang.Byte" || name == "byte") + { + return new Byte((byte)value); + } + + if (name == "java.lang.Double" || name == "double") + { + return new Double(value); + } + + if (name == "java.lang.Float" || name == "float") + { + return new Float(value); + } + + if (name == "java.lang.Integer" || name == "int") + { + return new Integer(value); + } + + if (name == "java.lang.Long" || name == "long") + { + return new Long(value); + } + + if (name == "java.lang.Short" || name == "short") + { + return new Short((short)value); + } + + if (name == "java.math.BigDecimal") + { + return getBigDecimal(new Integer(value)); + } + + if (name == "java.math.BigInteger") + { + return getBigInteger(new Integer(value)); + } + + if (name == "java.lang.String") + { + return String.valueOf(value); + } + + // Instead of throwing an ClassCastException we will pass the value to the property + return new Integer(value); + } + + public static long getLong(Object value) + { + if (value instanceof Number) + { + return ((Number)value).longValue(); + } + + if (value instanceof String) + { + return Long.parseLong((String)value); + } + + if (value instanceof Date) + { + return ((Date)value).getTime(); + } + + if (value == null) + { + return 0; + } + + throw new ClassCastException("The value of type '" + value.getClass().getName() + "' cannot be converted to long"); + } + + public static Object getSetValue(Property property, long value) + { + EStructuralFeature eStructuralFeature = (EStructuralFeature)property; + String name = eStructuralFeature.getEType().getInstanceClassName(); + if (name == "java.lang.Byte" || name == "byte") + { + return new Byte((byte)value); + } + + if (name == "java.lang.Double" || name == "double") + { + return new Double(value); + } + + if (name == "java.lang.Float" || name == "float") + { + return new Float(value); + } + + if (name == "java.lang.Integer" || name == "int") + { + return new Integer((int)value); + } + + if (name == "java.lang.Long" || name == "long") + { + return new Long(value); + } + + if (name == "java.lang.Short" || name == "short") + { + return new Short((short)value); + } + + if (name == "java.math.BigDecimal") + { + return getBigDecimal(new Long(value)); + } + + if (name == "java.math.BigInteger") + { + return getBigInteger(new Long(value)); + } + + if (name == "java.lang.String") + { + return String.valueOf(value); + } + + if (name == "java.util.Date") + { + return new Date(value); + } + + // Instead of throwing an ClassCastException we will pass the value to the property + return new Long(value); + } + + public static short getShort(Object value) + { + if (value instanceof Number) + { + return ((Number)value).shortValue(); + } + + if (value instanceof String) + { + return Short.parseShort((String)value); + } + + if (value == null) + { + return 0; + } + + throw new ClassCastException("The value of type '" + value.getClass().getName() + "' cannot be converted to short"); + } + + public static Object getSetValue(Property property, short value) + { + EStructuralFeature eStructuralFeature = (EStructuralFeature)property; + String name = eStructuralFeature.getEType().getInstanceClassName(); + if (name == "java.lang.Byte" || name == "byte") + { + return new Byte((byte)value); + } + + if (name == "java.lang.Double" || name == "double") + { + return new Double(value); + } + + if (name == "java.lang.Float" || name == "float") + { + return new Float(value); + } + + if (name == "java.lang.Integer" || name == "int") + { + return new Integer(value); + } + + if (name == "java.lang.Long" || name == "long") + { + return new Long(value); + } + + if (name == "java.lang.Short" || name == "short") + { + return new Short(value); + } + + if (name == "java.math.BigDecimal") + { + return getBigDecimal(new Short(value)); + } + + if (name == "java.math.BigInteger") + { + return getBigInteger(new Short(value)); + } + + if (name == "java.lang.String") + { + return String.valueOf(value); + } + + // Instead of throwing an ClassCastException we will pass the value to the property + return new Short(value); + } + + public static String getString(Object value) + { + if (value instanceof String) + { + return (String)value; + } + + if (value instanceof Number || value instanceof Boolean || value instanceof Character) + { + return String.valueOf(value); + } + + if (value instanceof Date) + { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy'-'MM'-'dd'T'H':'mm':'ss.S'Z'"); + sdf.setTimeZone(TimeZone.getTimeZone("GMT")); + return sdf.format((Date) value); + } + + if (value instanceof byte[]) { + return XMLTypeFactory.eINSTANCE.convertHexBinary((byte[])value); + } + + if (value == null) + { + return null; + } + + throw new ClassCastException("The value of type '" + value.getClass().getName() + "' cannot be converted to String"); + } + + public static Object getSetValue(Property property, String value) + { + EStructuralFeature eStructuralFeature = (EStructuralFeature)property; + EClassifier eType = eStructuralFeature.getEType(); + if (value == null) + { + return eType.getDefaultValue(); + } + + String name = eType.getInstanceClassName(); + if (name == "java.lang.String") + { + return value; + } + + if (name == "java.lang.Byte" || name == "byte") + { + return Byte.valueOf(value); + } + + if (name == "java.util.Date") + { + return DataHelper.INSTANCE.toDate(value); + } + + if (name == "java.lang.Double" || name == "double" || name == "java.lang.Number") + { + return Double.valueOf(value); + } + + if (name == "java.lang.Float" || name == "float") + { + return new Float(value); + } + + if (name == "java.lang.Integer" || name == "int") + { + return Integer.valueOf(value); + } + + if (name == "java.lang.Long" || name == "long") + { + return Long.valueOf(value); + } + + if (name == "java.lang.Short" || name == "short") + { + return Short.valueOf(value); + } + + if (name == "java.lang.Character" || name == "char") + { + return new Character(value.charAt(0)); + } + + if (name == "java.math.BigDecimal") + { + return getBigDecimal(value); + } + + if (name == "java.math.BigInteger") + { + return getBigInteger(value); + } + + if (name == "java.lang.Boolean" || name == "boolean") + { + return Boolean.valueOf(value); + } + + if (name == "byte[]") + { + return XMLTypeFactory.eINSTANCE.createHexBinary(value); + } + + // Instead of throwing an ClassCastException we will pass the value to the property + return value; + } + + public static EStructuralFeature getOpenFeature(EObject eObject, int featureID) + { + EClass eClass = eObject.eClass(); + int openFeatureCount = featureID - eClass.getFeatureCount(); + Set openFeatureSet = new HashSet(); + for (int i = 0, count = eClass.getEAllStructuralFeatures().size(); i < count; ++i) + { + EStructuralFeature eStructuralFeature = eClass.getEStructuralFeature(i); + if (!eStructuralFeature.isDerived() && FeatureMapUtil.isFeatureMap(eStructuralFeature)) + { + List features = (List)eObject.eGet(eStructuralFeature); + for (int j = 0, size = features.size(); j < size; ++j) + { + FeatureMap.Entry entry = (FeatureMap.Entry)features.get(j); + EStructuralFeature entryFeature = entry.getEStructuralFeature(); + if (openFeatureSet.add(entryFeature)) + { + if (--openFeatureCount < 0) return entryFeature; + } + } + } + } + throw new IndexOutOfBoundsException(); + } + + public static List getAliasNames(EStructuralFeature eStructuralFeature) + { + List list = SDOExtendedMetaData.INSTANCE.getAliasNames(eStructuralFeature); + if (list == null) { + list = new ArrayList(); + } + return list; + } + + public static List getAliasNames(EClassifier eClassifier) + { + List list = SDOExtendedMetaData.INSTANCE.getAliasNames(eClassifier); + if (list == null) { + list = new ArrayList(); + } + return list; + } + + protected static Property demandOpenProperty(Type type, String name, Object value, boolean isSequence) + { + TypeHelper typeHelper = TypeHelper.INSTANCE; //FB TODO: what TypeHelper to use? + + String uri = type.getURI() + "/" + type.getName(); // unique URI for open content properties on instances of the type + Property property = typeHelper.getOpenContentProperty(uri, name); + if (property != null) + return property; + + boolean isMany = isSequence; + boolean isContainment = false; + Type propertyType; + + if (value instanceof DataObject) + { + DataObject dataObject = (DataObject)value; + propertyType = dataObject.getType(); + isContainment = dataObject.getContainer() == null; + } + else if (value instanceof List && !((List)value).isEmpty()) + { + Object listValue = ((List)value).get(0); //TODO: get common base class if all values are not the same type? + if (listValue instanceof DataObject) + propertyType = ((DataObject)listValue).getType(); + else + propertyType = typeHelper.getType(listValue.getClass()); + isMany = true; + } + else + { + propertyType = typeHelper.getType(value.getClass()); + } + if (propertyType == null) + { + propertyType = ((ModelFactoryImpl)ModelFactory.INSTANCE).getObject(); + } + + Property newProperty = SDOUtil.createOpenContentProperty(HelperProvider.getDefaultContext(), uri, name, propertyType); + if (isMany) + SDOUtil.setMany(newProperty, isMany); + if (isContainment) + SDOUtil.setContainment(newProperty, isContainment); + + return newProperty; + } + + /** + * Process the default EMF path and minimal XPath syntax. + * This design is still under review and construction. + * + * Syntax: + * + *
+   * path = /? (step '/')* step
+   * step = feature
+   *      | feature '.' index_from_0 
+   *      | feature '[' index_from_1 ']'
+   *      | reference '[' attribute '=' value ']'
+   *      | ..
+   *      | '@' step
+   *
+ * + * feature = the name of an attribute or reference + * attribute = the name of an attribute + * reference = the name of a reference + * index = positive integer + * value = the string value of an attribute + * leading / begins at the root + * .. is containing object + * + * features must be multi-valued to use '.' and '[' operations. + * Only the last step may have an attribute as the feature. + */ + public static final class Accessor //TODO rewrite this using SDO APIs + { + /** + * Creates an accessor for the path of the object. + */ + public static Accessor create(EObject eObject, String path) + { + return create(eObject, path, null); + } + + public static Accessor create(EObject eObject, String path, Object value) + { + Accessor result = pool.get(); + result.init(eObject, path, value); + return result; + } + + /** + * Only the get and recycle methods should be call; they are the only synchronized methods. + */ + protected static class Pool extends BasicEList + { + protected Accessor[] accessors; + + public Pool() + { + super(10); + } + + protected Object[] newData(int capacity) + { + return accessors = new Accessor [capacity]; + } + + /** + * Returns a recyled instance or a new instance. + */ + public synchronized Accessor get() + { + if (size > 0) + { + return accessors[--size]; + } + else + { + return new Accessor(); + } + } + + /** Safely gives the accessor back for recycled use. + */ + public synchronized void recycle(Accessor accessor) + { + int minimumCapacity = size + 1; + if (minimumCapacity > data.length) + { + grow(minimumCapacity); + } + accessors[size++] = accessor; + } + } + + /** + * A static thread safe pool of Accessors. + */ + static final Pool pool = new Pool(); + + protected static final int NO_INDEX = -1; + + protected EObject eObject; + protected Object value; + + protected EStructuralFeature feature; + + protected int index; + + RuntimeException runtimeException; + + protected Accessor() + { + } + + //protected Accessor(EObject eObject, String path) + //{ + // init(eObject, path); + //} + + protected void init(EObject eObject, String path, Object value) + { + this.eObject = eObject; + this.value = value; + runtimeException = null; + + // This should only be called with a path right now. + // + //feature = getType(eObject).getProperty(path).getEStructuralFeature(); + //if (feature == null) + { + process(path); + } + //else + { + //index = NO_INDEX; + } + } + + public Object get() + { + if (feature == null) + { + return eObject; + } + else + { + Object value = eObject.eGet(feature, true); + if (index >= 0) + { + value = ((List)value).get(index); + if (value instanceof FeatureMap.Entry) + { + value = ((FeatureMap.Entry)value).getValue(); + } + } + else if (FeatureMapUtil.isFeatureMap(feature)) + { + value = new BasicSequence((FeatureMap.Internal)value); + } + return value; + } + } + + public Object getAndRecyle() + { + Object result = get(); + pool.recycle(this); + return result; + } + + protected final void assertSuccessfulProcess() + { + if (runtimeException != null) + throw runtimeException; + } + + public void set(Object newValue) + { + assertSuccessfulProcess(); + if (index >= 0) + { + List list = (List)eObject.eGet(feature, true); + list.set(index, newValue); + } + else + { + // EATM newValue = string2Enum(feature, newValue); + eObject.eSet(feature, newValue); + } + } + + public void setAndRecyle(Object newValue) + { + set(newValue); + pool.recycle(this); + } + + public boolean isSet() + { + return feature != null && eObject.eIsSet(feature); + } + + public boolean isSetAndRecyle() + { + boolean result = isSet(); + pool.recycle(this); + return result; + } + + public void unset() + { + assertSuccessfulProcess(); + eObject.eUnset(feature); + } + + public void unsetAndRecyle() + { + unset(); + pool.recycle(this); + } + + public void recycle() + { + pool.recycle(this); + } + + public EObject getEObject() + { + return eObject; + } + + protected void setEObject(EObject eObject) + { + this.eObject = eObject; + feature = null; + index = NO_INDEX; + } + + public EStructuralFeature getEStructuralFeature() + { + return feature; + } + + public Property getProperty() + { + assertSuccessfulProcess(); + return (Property)feature; + } + + protected void setFeatureName(String name) + { + if (name != null) + { + feature = (EStructuralFeature)((DataObject)eObject).getInstanceProperty(name); + if (feature == null) + { + int index = name.lastIndexOf('.'); + if (index == -1) + { + Type type = (Type)eObject.eClass(); + if (value != null && type.isOpen()) + { + feature = (EStructuralFeature)demandOpenProperty(type, name, value, false); + this.index = NO_INDEX; + return; + } + runtimeException = new IllegalArgumentException("Class '" + eObject.eClass().getName() + "' does not have a feature named '" + name + '\''); + } + else + { + int propertyNameEnd = index; + try + { + index = Integer.parseInt(name.substring(++index)); + // NumberFormatException may be thrown + String propertyName = name.substring(0, propertyNameEnd); + feature = (EStructuralFeature)((DataObject)eObject).getInstanceProperty(propertyName); + if (feature != null) + { + setIndex(index); + return; + } + runtimeException = new IllegalArgumentException("Class '" + eObject.eClass().getName() + "' does not have a feature named '" + name + "' or '" + propertyName + '\''); + } + catch(NumberFormatException eNumberFormat) + { + runtimeException = eNumberFormat; + } + } + setEObject(null); + } + } + else + { + feature = null; + } + index = NO_INDEX; + } + + protected int getIndex() + { + return index; + } + + /* + * calling constraint, this method is not to be used for semantic of setting index to NO_INDEX + */ + protected void setIndex(int index) + { + if (index < 0) { + // The index value should not be less than 0. + runtimeException = new IndexOutOfBoundsException("Index value is too low"); + setEObject(null); + return; + } + if (!FeatureMapUtil.isMany(eObject, feature)) + { + runtimeException = new IndexOutOfBoundsException("Index applies only to multi-valued features"); + setEObject(null); + return; + } + int size = ((List) eObject.eGet(feature)).size(); + if (index >= size) + { + // The index value should be less than size. + runtimeException = new IndexOutOfBoundsException("Index value should have been less than " + size); + setEObject(null); + } + this.index = index; + } + + protected void process(String pathString) + { + TokenList tokens = new TokenList(pathString.toCharArray()); + String token; + int size = tokens.size(); + int x = 0; + + if ("/".equals(tokens.peek(0))) + { + setEObject(EcoreUtil.getRootContainer(eObject)); + x++; + } + + for (; x < size; x++) + { + token = tokens.peek(x); + char c = token.charAt(0); + if ('/' == c) + { + setEObject((EObject)get()); + if (eObject == null) break; + } + else if ("..".equals(token)) + { + setEObject(eObject.eContainer()); + if (eObject == null) break; + } + else if ('[' == c) + { + x++; // skip [ + token = tokens.peek(x); // name or index + char following = tokens.peek(x + 1).charAt(0); + if ('=' != following) + { + try + { + setIndex(Integer.parseInt(token) - 1); + // runtimeException may be recorded + } + catch(NumberFormatException eNumberFormat) + { + runtimeException = eNumberFormat; + setEObject(null); + break; + } + if (runtimeException != null) + break; + x++; // skip ] + } + else + { + x++; // done name + x++; // skip = + String attributeValue = tokens.peek(x); // value + if ("\"".equals(attributeValue)) + { + x++; // skip " + attributeValue = tokens.peek(++x); + } + x++; // skip ] + int index = matchingIndex((List)get(), token, attributeValue); + if (index < 0) + { + setEObject(null); + break; + } + else + { + setIndex(index); + } + } + } + else if ('@' == c) + { + // skip @ + } + else + { + setFeatureName(token); + if (eObject == null) break; + } + } + } + + protected static int matchingIndex(List dataObjects, String attributeName, String attributeValue) + { + for (int i = 0, size = dataObjects.size(); i < size; i++) + { + DataObject dataObject = (DataObject)dataObjects.get(i); + Property property = getInstanceProperty(dataObject, attributeName); + if (property != null) + { + Object test = dataObject.get(property); + if (test != null) + { + String testString = EcoreUtil.convertToString((EDataType)property.getType(), test); + if (attributeValue.equals(testString)) + { + return i; + } + } + } + } + return -1; + } + + protected static class TokenList extends BasicEList + { + public TokenList(char[] path) + { + super(4); + + int pathLength = path.length; + StringBuffer token = new StringBuffer(); + char cPrev; + char c = 0; + char cNext; + char stringConstant = 0; + for (int pos = 0; pos < pathLength; pos++) + { + cPrev = c; + c = path[pos]; + cNext = pos < pathLength - 1 ? path[pos + 1] : 0; + + if (stringConstant != 0) + { + if (c == stringConstant) + { + endToken(token, true); + stringConstant = 0; + } + else + { + token.append(c); + } + } + else + { + switch (c) + { + case ' ': + case 0xA: + case 0xD: + case 0x9: + if (cPrev != ' ') + { + endToken(token, false); + } + c = ' '; + break; + + case '"': + case '\'': + endToken(token, false); + stringConstant = c; + break; + + // double or single tokens + case '/': + case ':': + if (cPrev != c) + { + endToken(token, false); + } + token.append(c); + if (cNext != c) + { + endToken(token, false); + } + break; + + // double token (..) + case '.': + if (cNext == '.') + endToken(token, false); + token.append(c); + if (cPrev == '.') + endToken(token, false); + break; + + // single tokens + case '*': + case '@': + case '[': + case ']': + case '(': + case ')': + case '|': + endToken(token, false); + add(String.valueOf(c)); + break; + + // TODO: < > <= >= + - != + case '!': + endToken(token, false); + token.append(c); + break; + + case '=': + endToken(token, false); + add(String.valueOf(c)); + break; + + default: + token.append(c); + } + } + } + endToken(token, false); + } + + public String peek() + { + return size > 0 ? (String)data[0] : " "; + } + + public String peek(int index) + { + return index < size ? (String)data[index] : " "; + } + + public TokenList pop() + { + remove(0); + return this; + } + + public TokenList pop(int count) + { + while (count-- > 0) + { + remove(count); + } + return this; + } + + protected void endToken(StringBuffer token, boolean includeEmpty) + { + if (includeEmpty || token.length() > 0) + { + add(token.toString()); + } + token.setLength(0); + } + + protected boolean canContainNull() + { + return false; + } + + protected Object[] newData(int capacity) + { + return new String [capacity]; + } + } + + public String toString() + { + StringBuffer result = new StringBuffer("Accessor (object:"); + result.append(eObject == null ? "null" : eObject.toString()); + result.append(", feature:"); + result.append(feature == null ? "null" : feature.getName()); + result.append(", index:"); + result.append(index); + result.append(")"); + return result.toString(); + } + } + + public static Type getType(DataObject dataObject, String namespaceURI, String typeName) + { + DataGraph dataGraph = dataObject.getDataGraph(); + if (dataGraph != null) + { + return dataGraph.getType(namespaceURI, typeName); + } + else + { + //TODO think about where else to find the type + return TypeHelper.INSTANCE.getType(namespaceURI, typeName); + } + } + + public static Property getInstanceProperty(DataObject dataObject, String propertyName) + { + ClassImpl type = (ClassImpl)dataObject.getType(); + Property property = type.getProperty(propertyName); + if (property == null) + { + property = type.getOpenProperty((EObject)dataObject, propertyName); + //throw new IllegalArgumentException("Type '" + dataObject.getType().getName() + "' does not have a property named '" + propertyName + "'"); + } + + return property; + } + + public static Property getProperty(DataObject dataObject, int propertyIndex) + { + List typeProperties = dataObject.getType().getProperties(); + try { + Property property = propertyIndex < typeProperties.size() ? + (Property)typeProperties.get(propertyIndex) : + (Property)dataObject.getInstanceProperties().get(propertyIndex); + + return property; + } catch (IndexOutOfBoundsException e) { + throw new IllegalArgumentException(); + } + } + + public static Property getContainmentProperty(Property property) + { + if (property.isContainment()) + { + return property; + } + throw new IllegalArgumentException("The property '" + property.getName() + "' of '" + property.getContainingType().getName() + + "' isn't a containment"); + } + + public static DataObject create(Type type) + { + //return (DataObject)EcoreUtil.create((EClass)type); + if ((type instanceof EClass) && !type.isAbstract()) { + EClass eClass = (EClass)type; + try { + return (DataObject)EcoreUtil.create(eClass); + } catch (ClassCastException e) { + throw new IllegalArgumentException(); + } + } + throw new IllegalArgumentException(); + } + + public static ResourceSet createResourceSet() + { + ResourceSet result = new ResourceSetImpl(); + configureResourceSet(result); + return result; + } + + protected static Map registrations; + + protected static Map getRegistrations() + { + if (registrations == null) + { + Map result = new HashMap(); + + Resource.Factory factory = Resource.Factory.Registry.INSTANCE.getFactory(URI.createURI("*.datagraph")); + result.put("datagraph", factory instanceof DataGraphResourceFactoryImpl ? factory : new DataGraphResourceFactoryImpl()); + + factory = Resource.Factory.Registry.INSTANCE.getFactory(URI.createURI("*.ecore")); + result.put("ecore", factory instanceof EcoreResourceFactoryImpl ? factory : new EcoreResourceFactoryImpl()); + + factory = Resource.Factory.Registry.INSTANCE.getFactory(URI.createURI("*.emof")); + result.put("emof", factory instanceof EMOFResourceFactoryImpl ? factory : new EMOFResourceFactoryImpl()); + + factory = Resource.Factory.Registry.INSTANCE.getFactory(URI.createURI("*.xsd")); + result.put("xsd", factory instanceof XSDResourceFactoryImpl ? factory : new XSDResourceFactoryImpl()); + + factory = Resource.Factory.Registry.INSTANCE.getFactory(URI.createURI("*.wsdl")); + result.put("wsdl", factory instanceof XSDResourceFactoryImpl ? factory : new XSDResourceFactoryImpl()); + + factory = Resource.Factory.Registry.INSTANCE.getFactory(URI.createURI("*.*")); + result.put("*", factory instanceof SDOXMLResourceFactoryImpl ? factory : new SDOXMLResourceFactoryImpl()); + + registrations = result; + } + + return registrations; + } + + protected static void configureResourceSet(ResourceSet resourceSet) + { + resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().putAll(getRegistrations()); + resourceSet.setPackageRegistry(new EPackageRegistryImpl(HelperContextImpl.getBuiltInModelRegistry())); + resourceSet.setURIConverter(new SDOURIConverterImpl()); + } + + public static EClass createDocumentRoot() + { + EClass documentRootEClass = (EClass)SDOFactory.eINSTANCE.createClass(); + documentRootEClass.setName("DocumentRoot"); + ExtendedMetaData.INSTANCE.setName(documentRootEClass, ""); + ExtendedMetaData.INSTANCE.setContentKind(documentRootEClass, ExtendedMetaData.MIXED_CONTENT); + + EAttribute mixed = (EAttribute)SDOFactory.eINSTANCE.createAttribute(); + mixed.setName("mixed"); + mixed.setEType(EcorePackage.eINSTANCE.getEFeatureMapEntry()); + mixed.setUpperBound(EStructuralFeature.UNBOUNDED_MULTIPLICITY); + ExtendedMetaData.INSTANCE.setName(mixed, ":mixed"); + ExtendedMetaData.INSTANCE.setFeatureKind(mixed, ExtendedMetaData.ELEMENT_WILDCARD_FEATURE); + documentRootEClass.getEStructuralFeatures().add(mixed); + + EReference xmlnsPrefixMap = (EReference)SDOFactory.eINSTANCE.createReference(); + xmlnsPrefixMap.setName("xMLNSPrefixMap"); + xmlnsPrefixMap.setEType(EcorePackage.eINSTANCE.getEStringToStringMapEntry()); + xmlnsPrefixMap.setUpperBound(EStructuralFeature.UNBOUNDED_MULTIPLICITY); + xmlnsPrefixMap.setContainment(true); + xmlnsPrefixMap.setTransient(true); + ExtendedMetaData.INSTANCE.setName(xmlnsPrefixMap, "xmlns:prefix"); + ExtendedMetaData.INSTANCE.setFeatureKind(xmlnsPrefixMap, ExtendedMetaData.ATTRIBUTE_FEATURE); + documentRootEClass.getEStructuralFeatures().add(xmlnsPrefixMap); + + EReference xsiSchemaLocation = (EReference)SDOFactory.eINSTANCE.createReference(); + xsiSchemaLocation.setName("xSISchemaLocation"); + xsiSchemaLocation.setEType(EcorePackage.eINSTANCE.getEStringToStringMapEntry()); + xsiSchemaLocation.setUpperBound(EStructuralFeature.UNBOUNDED_MULTIPLICITY); + xsiSchemaLocation.setContainment(true); + xsiSchemaLocation.setTransient(true); + ExtendedMetaData.INSTANCE.setName(xsiSchemaLocation, "xsi:schemaLocation"); + ExtendedMetaData.INSTANCE.setFeatureKind(xsiSchemaLocation, ExtendedMetaData.ATTRIBUTE_FEATURE); + documentRootEClass.getEStructuralFeatures().add(xsiSchemaLocation); + + return documentRootEClass; + } + + /** + * Configure EMF to support the SDO runtime by registering a specialized Ecore factory, SDOEcoreFactory. + * This static initializion must run before any SDO metadata is created or loaded. + * As long as SDO helper classes (e.g., TypeHelper, XMLHelper, etc.) are accessed though their + * corresponding INSTANCE fields (e.g., TypeHelper.INSTANCE), or using the SDOUtil methods (e.g., + * SDOUtil.createTypeHelper(), this will always be the case. + */ + /* + static + { + EPackage.Registry.INSTANCE.put(EcorePackage.eNS_URI, new EPackage.Descriptor() + { + public EPackage getEPackage() + { + return EcorePackage.eINSTANCE; + } + + public EFactory getEFactory() + { + return new SDOFactoryImpl.SDOEcoreFactory(); + } + }); + } + */ + + /* + protected static StringBuffer getXPath(DataObject dataObject, StringBuffer path, DataObject root) + { + DataObject container = dataObject.getContainer(); + if (container == null) return path; + + if (container == root) { + throw new IllegalStateException("There is a cycle in the containment hierarchy of " + root); + } + + boolean first = path.length() == 0; + Property property = dataObject.getContainmentProperty(); + if (SDOUtil.isMany(property, dataObject)) + { + List list = container.getList(property); + int pos = list.indexOf(dataObject); + path.insert(0, property.getName() + "." + pos + (first ? "" : "/")); + } + else + { + path.insert(0, property.getName() + (first ? "" : "/")); + } + + return getXPath(container, path, root); + } + */ + + public static String getXPath(DataObject dataObject) + { + return SDOUtil.getXPath(dataObject); + //StringBuffer path = getXPath(dataObject, new StringBuffer(), dataObject); + //return path.toString(); + } + + protected static XMLParserPool globalXMLParserPool = new XMLParserPoolImpl(); + + public static void configureXMLResource(XMLResource resource, ExtendedMetaData extendedMetaData) + { + XMLOptions xmlOptions = new XMLOptionsImpl(); + xmlOptions.setProcessAnyXML(true); + resource.getDefaultLoadOptions().put(XMLResource.OPTION_XML_OPTIONS, xmlOptions); + + resource.getDefaultSaveOptions().put(XMLResource.OPTION_EXTENDED_META_DATA, extendedMetaData); + resource.getDefaultLoadOptions().put(XMLResource.OPTION_EXTENDED_META_DATA, extendedMetaData); + + resource.getDefaultLoadOptions().put(XMLResource.OPTION_USE_PARSER_POOL, globalXMLParserPool); + + resource.getDefaultLoadOptions().put(XMLResource.OPTION_USE_DEPRECATED_METHODS, Boolean.FALSE); + + resource.getDefaultSaveOptions().put(XMLResource.OPTION_CONFIGURATION_CACHE, Boolean.TRUE); + resource.getDefaultLoadOptions().put(XMLResource.OPTION_CONFIGURATION_CACHE, Boolean.TRUE); + + resource.getDefaultLoadOptions().put(XMLResource.OPTION_ANY_TYPE, SDOPackage.eINSTANCE.getAnyTypeDataObject()); + resource.getDefaultSaveOptions().put(XMLResource.OPTION_ANY_TYPE, SDOPackage.eINSTANCE.getAnyTypeDataObject()); + + resource.getDefaultLoadOptions().put(XMLResource.OPTION_ANY_SIMPLE_TYPE, SDOPackage.eINSTANCE.getSimpleAnyTypeDataObject()); + resource.getDefaultSaveOptions().put(XMLResource.OPTION_ANY_SIMPLE_TYPE, SDOPackage.eINSTANCE.getSimpleAnyTypeDataObject()); + + //resource.getDefaultLoadOptions().put(XMLResource.OPTION_USE_XML_NAME_TO_FEATURE_MAP, globalHashMap); + + //resource.getDefaultSaveOptions().put(XMLResource.OPTION_FORMATTED, Boolean.FALSE); + } + + /** + * @deprecated SDO runtime initialization is no longer required + */ + public static void initRuntime() + { + // NOOP since init is done during static initialization of this class. See above. + } + + /* + public static Object get(org.apache.tuscany.sdo.model.Property property, int propertyIndex) { + switch(propertyIndex) + { + case ModelPackageImpl.PROPERTY__ALIAS_NAME: + return property.getAliasName(); + case ModelPackageImpl.PROPERTY__ANY: + return property.getAny(); + case ModelPackageImpl.PROPERTY__ANY_ATTRIBUTE: + return property.getAnyAttribute(); + case ModelPackageImpl.PROPERTY__CONTAINMENT: + return Boolean.valueOf(property.isContainment()); + case ModelPackageImpl.PROPERTY__DEFAULT: + return property.getDefault_(); + case ModelPackageImpl.PROPERTY__MANY: + return Boolean.valueOf(property.isMany()); + case ModelPackageImpl.PROPERTY__NAME: + return property.getName(); + case ModelPackageImpl.PROPERTY__OPPOSITE: + return property.getOpposite_(); + case ModelPackageImpl.PROPERTY__READ_ONLY: + return Boolean.valueOf(property.isReadOnly()); + case ModelPackageImpl.PROPERTY__TYPE: + return property.getType_(); + } + return null; + } + + public static boolean isSet(org.apache.tuscany.sdo.model.Property property, int propertyIndex) { + switch(propertyIndex) + { + case ModelPackageImpl.PROPERTY__ALIAS_NAME: + return !property.getAliasName().isEmpty(); + case ModelPackageImpl.PROPERTY__ANY: + return false; + case ModelPackageImpl.PROPERTY__ANY_ATTRIBUTE: + return false; + case ModelPackageImpl.PROPERTY__CONTAINMENT: + return property.isSetContainment(); + case ModelPackageImpl.PROPERTY__DEFAULT: + return property.getDefault_() != null; + case ModelPackageImpl.PROPERTY__MANY: + return property.isSetMany(); + case ModelPackageImpl.PROPERTY__NAME: + return property.getName() != null; + case ModelPackageImpl.PROPERTY__OPPOSITE: + return property.getOpposite_() != null; + case ModelPackageImpl.PROPERTY__READ_ONLY: + return property.isSetReadOnly(); + case ModelPackageImpl.PROPERTY__TYPE: + return property.getType_() != null; + } + return false; + } + + public static Object get(org.apache.tuscany.sdo.model.Type type, int propertyIndex) { + switch (propertyIndex) + { + case ModelPackageImpl.TYPE__BASE_TYPE: + return type.getBaseType(); + case ModelPackageImpl.TYPE__PROPERTY: + return type.getProperty(); + case ModelPackageImpl.TYPE__ALIAS_NAME: + return type.getAliasName(); + case ModelPackageImpl.TYPE__ANY: + return type.getAny(); + case ModelPackageImpl.TYPE__ABSTRACT: + return Boolean.valueOf(type.isAbstract()); + case ModelPackageImpl.TYPE__DATA_TYPE: + return Boolean.valueOf(type.isDataType()); + case ModelPackageImpl.TYPE__NAME: + return type.getName(); + case ModelPackageImpl.TYPE__OPEN: + return Boolean.valueOf(type.isOpen()); + case ModelPackageImpl.TYPE__SEQUENCED: + return Boolean.valueOf(type.isSequenced()); + case ModelPackageImpl.TYPE__URI: + return type.getUri(); + case ModelPackageImpl.TYPE__ANY_ATTRIBUTE: + return type.getAnyAttribute(); + } + return null; + } + + public static boolean isSet(org.apache.tuscany.sdo.model.Type type, int propertyIndex) { + //FB Note that this implementation has the undesirable effect of invoking lazy creation of feature lists + switch (propertyIndex) + { + case ModelPackageImpl.TYPE__BASE_TYPE: + return !type.getBaseType().isEmpty(); + case ModelPackageImpl.TYPE__PROPERTY: + return !type.getProperty().isEmpty(); + case ModelPackageImpl.TYPE__ALIAS_NAME: + return !type.getAliasName().isEmpty(); + case ModelPackageImpl.TYPE__ANY: + return false; + case ModelPackageImpl.TYPE__ABSTRACT: + return type.isSetAbstract(); + case ModelPackageImpl.TYPE__DATA_TYPE: + return type.isSetDataType(); + case ModelPackageImpl.TYPE__NAME: + return type.getName() != null; + case ModelPackageImpl.TYPE__OPEN: + return type.isSetOpen(); + case ModelPackageImpl.TYPE__SEQUENCED: + return type.isSetSequenced(); + case ModelPackageImpl.TYPE__URI: + return type.getUri() != null; + case ModelPackageImpl.TYPE__ANY_ATTRIBUTE: + return false; + } + return false; + } + */ + + /** + * Returns a unique list of meta object instance properties (stored in EAnnotations) + * + * @param metaObject - A Type or Property instance + * @return A list of commonj.sdo.Property instances + */ + public static List getMetaObjectInstanceProperties(EModelElement metaObject) + { + // Use the default helper context for now + // TypeHelper typeHelper = HelperProvider.getDefaultContext().getTypeHelper(); + HelperContext hc = HelperProvider.getDefaultContext(); + + List result = new UniqueEList(); + List annotations = metaObject.getEAnnotations(); + int size = annotations.size(); + for (int i=0; i + *
  • ignorableWhitespace
  • + *
  • skippedEntity
  • + *