tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fra...@apache.org
Subject svn commit: r503913 [2/2] - in /incubator/tuscany/java/sdo/impl/src: main/java/org/apache/tuscany/sdo/helper/ main/java/org/apache/tuscany/sdo/impl/ main/java/org/apache/tuscany/sdo/util/ main/java/org/apache/tuscany/sdo/util/resource/ test/java/org/ap...
Date Mon, 05 Feb 2007 22:53:35 GMT
Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/SDOXMLResourceImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/SDOXMLResourceImpl.java?view=diff&rev=503913&r1=503912&r2=503913
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/SDOXMLResourceImpl.java
(original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/SDOXMLResourceImpl.java
Mon Feb  5 14:53:34 2007
@@ -24,24 +24,20 @@
 import java.security.AccessController;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
+import java.util.*;
 
+import javax.xml.XMLConstants;
 import javax.xml.namespace.NamespaceContext;
 import javax.xml.namespace.QName;
-import javax.xml.stream.XMLOutputFactory;
-import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamReader;
-import javax.xml.stream.XMLStreamWriter;
+import javax.xml.stream.*;
 
+import org.apache.tuscany.sdo.helper.HelperContextImpl;
+import org.apache.tuscany.sdo.helper.XMLStreamHelper;
 import org.apache.tuscany.sdo.model.ModelFactory;
 import org.apache.tuscany.sdo.model.impl.ModelFactoryImpl;
-import org.apache.tuscany.sdo.util.StAX2SAXAdapter;
+import org.apache.tuscany.sdo.util.*;
 import org.eclipse.emf.common.util.URI;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.EStructuralFeature;
+import org.eclipse.emf.ecore.*;
 import org.eclipse.emf.ecore.resource.Resource;
 import org.eclipse.emf.ecore.util.ExtendedMetaData;
 import org.eclipse.emf.ecore.xmi.XMIException;
@@ -49,14 +45,11 @@
 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.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.xml.sax.ContentHandler;
-import org.xml.sax.SAXException;
+import org.eclipse.emf.ecore.xmi.impl.*;
+import org.xml.sax.*;
+import org.xml.sax.helpers.DefaultHandler;
 
-import commonj.sdo.ChangeSummary;
+import commonj.sdo.*;
 
 public class SDOXMLResourceImpl extends XMLResourceImpl {
     private XMLStreamReader reader;
@@ -118,11 +111,33 @@
             this.namespaceSupport = new StreamNamespaceSupport(reader);
         }
         
-        Collection getPrefixes(Object uri) {
-            return (Collection) urisToPrefixes.get(uri);
+        private class NameSpaceContext implements NamespaceContext { // TODO Helper# pushContext()
& popContext
+            public String getNamespaceURI(String prefix) {
+                return SDOXMLHelperImpl.this.getNamespaceURI(prefix);
+            }
+
+            public String getPrefix(String namespaceURI) {
+                return SDOXMLHelperImpl.this.getPrefix(namespaceURI);
+            }
+
+            public Iterator getPrefixes(String namespaceURI) {
+                return ((Collection) urisToPrefixes.get(namespaceURI)).iterator();
+            }
+        }
+
+        NameSpaceContext nameSpaceContext/* = null */;
+
+        protected final NameSpaceContext nameSpaceContext() {
+            if (nameSpaceContext == null)
+                nameSpaceContext = new NameSpaceContext();
+            return nameSpaceContext;
         }
     }
 
+    static final Object ChangeSummary_TYPE = ((ModelFactoryImpl) ModelFactory.INSTANCE).getChangeSummaryType();
+
+    public EObject root;
+
     /**
      * An EMF XMLLoad that loads a model from a StAX stream
      */
@@ -131,6 +146,159 @@
             super(helper);
         }
 
+        final class XmlHandler extends SAXXMLHandler {
+            XmlHandler() {
+                super(resource, SDOXMLLoadImpl.this.helper, options);
+            }
+
+            protected void handleTopLocations(String prefix, String name) {
+                processSchemaLocations(prefix, name);
+                if (!processAnyXML)
+                    return;
+                String nameSpace = helper.getURI(prefix);
+                if (extendedMetaData.getPackage(nameSpace) == null)
+                    if (options.get(XMLStreamHelper.OPTION_DEFAULT_ROOT_TYPE) == null)
+                        extendedMetaData.demandFeature(nameSpace, name, true);
+                    else
+                        extendedMetaData.demandPackage(nameSpace);
+            }
+
+            EClassifier defaultRootType(String prefix, String name, boolean isElement, EObject
peekObject, String value) {
+                Object type = options.get(XMLStreamHelper.OPTION_DEFAULT_ROOT_TYPE);
+                if (type != null)
+                    return (EClassifier) type;
+                super.handleUnknownFeature(prefix, name, isElement, peekObject, value);
+                return null;
+            }
+
+            protected void handleUnknownFeature(String prefix, String name, boolean isElement,
EObject peekObject, String value) {
+                if (objects.size() == 1) {
+                    EFactory eFactory;
+                    EClassifier type;
+                    String typeQName = getXSIType();
+                    if (typeQName == null) {
+                        type = defaultRootType(prefix, name, isElement, peekObject, value);
+                        if (type == null)
+                            return;
+                        eFactory = type.getEPackage().getEFactoryInstance();
+                    } else {// createObjectFromTypeName
+                        String typeName = null;
+                        String xsiPrefix = XMLConstants.DEFAULT_NS_PREFIX;
+                        int index = typeQName.indexOf(":");
+                        if (index > 0) {
+                            xsiPrefix = typeQName.substring(0, index);
+                            typeName = typeQName.substring(index + 1);
+                        } else
+                            typeName = typeQName;
+                        eFactory = getFactoryForPrefix(xsiPrefix);
+                        if (eFactory != null)
+                            type = helper.getType(eFactory, typeName);
+                        else if (XMLConstants.DEFAULT_NS_PREFIX.equals(xsiPrefix) &&
helper.getURI(xsiPrefix) == null) {
+                            EPackage ePackage = handleMissingPackage(null);
+                            if (ePackage == null) {
+                                type = defaultRootType(prefix, name, isElement, peekObject,
value);
+                                if (type == null)
+                                    return;
+                                eFactory = type.getEPackage().getEFactoryInstance();
+                            } else
+                                type = helper.getType(eFactory = ePackage.getEFactoryInstance(),
typeName);
+                        } else {
+                            type = defaultRootType(prefix, name, isElement, peekObject, value);
+                            if (type == null)
+                                return;
+                            eFactory = type.getEPackage().getEFactoryInstance();
+                        }
+                    }
+                    root = helper.createObject(eFactory, type);
+                    if (root != null) {
+                        if (disableNotify)
+                            root.eSetDeliver(false);
+                        handleObjectAttribs(root);
+                        processObject(root);
+                        return;
+                    }
+                }
+                super.handleUnknownFeature(prefix, name, isElement, peekObject, value);
+            }
+
+            protected RecordedEventXMLStreamReader.Tag tag/* =null */;
+
+            protected List nameSpaces/* = null */;
+
+            public void startPrefixMapping(String prefix, String uri) {
+                if (nameSpaces == null)
+                    nameSpaces = new ArrayList();
+                RecordedEventXMLStreamReader.Tag.bind(prefix, uri, nameSpaces);
+                if (tag == null)
+                    super.startPrefixMapping(prefix, uri);
+            }
+
+            public void startElement(String uri, String localName, String qName, Attributes
attributes) throws SAXException {
+                if (tag != null) {
+                    tag.start(uri, localName, qName, attributes, locator, nameSpaces);
+                    nameSpaces = null;
+                    return;
+                }
+                EObject peekObject = objects.peekEObject();
+                if (peekObject != null) {
+                    String prefix = helper.getPrefix(uri.length() == 0 ? null : uri);
+                    EStructuralFeature feature = getFeature(peekObject, prefix == null ?
XMLConstants.DEFAULT_NS_PREFIX : prefix, localName, true);
+                    if (feature != null && feature.getEType() == ChangeSummary_TYPE)
{
+                        tag = new RecordedEventXMLStreamReader.Tag(uri, localName, prefix,
attributes, locator, ((SDOXMLHelperImpl) helper).nameSpaceContext(),
+                                nameSpaces);
+                        nameSpaces = null;
+                        return;
+                    }
+                }
+                if (nameSpaces != null)
+                    nameSpaces.clear();
+                super.startElement(uri, localName, qName, attributes);
+            }
+
+            public void characters(char[] ch, int start, int length) {
+                if (tag == null)
+                    super.characters(ch, start, length);
+                else
+                    tag.text(XMLStreamConstants.CHARACTERS, new String(ch, start, length),
locator);
+            }
+
+            protected Collection changeSummaryDeserializers/* = null */;
+
+            public void endElement(String uri, String localName, String qName) {
+                if (tag == null)
+                    super.endElement(uri, localName, qName);
+                else if (tag.end(uri, localName, qName, locator)) {
+                    if (changeSummaryDeserializers == null)
+                        changeSummaryDeserializers = new ArrayList();
+                    ChangeSummaryStreamDeserializer changeSummaryDeserializer = new ChangeSummaryStreamDeserializer();
+                    try {
+                        changeSummaryDeserializer.begin((DataObject) objects.peekEObject(),
new HelperContextImpl(extendedMetaData), tag
+                                .play(xmlResource));
+                        changeSummaryDeserializers.add(changeSummaryDeserializer);
+                    } catch (XMLStreamException e) {
+                        xmlResource.getErrors().add(new XMIException(e));
+                    }
+                    tag = null;
+                }
+            }
+
+            public void endDocument() {
+                super.endDocument();
+                if (changeSummaryDeserializers != null)
+                    for (Iterator iterator = changeSummaryDeserializers.iterator(); iterator.hasNext();)
+                        try {
+                            ((ChangeSummaryStreamDeserializer) iterator.next()).end();
+                            // iterator.remove();
+                        } catch (XMLStreamException e) {
+                            xmlResource.getErrors().add(new XMIException(e));
+                        }
+            }
+        }
+
+        protected DefaultHandler makeDefaultHandler() {
+            return new XmlHandler();
+        }
+
         /**
          * Start parsing an XMLReader with the default handler.
          */
@@ -202,6 +370,8 @@
         xmlLoad.load(this, reader, mergedOptions);
     }
 
+    ChangeSummaryStreamSerializer changeSummarySerializer/* = null*/;
+
     static private final class LocalName extends QName {
         private LocalName(String name) {
             super(name);
@@ -212,7 +382,7 @@
         }
     }
 
-    static final class SDOXMLSaveImpl extends XMLSaveImpl {
+    final class SDOXMLSaveImpl extends XMLSaveImpl {
         SDOXMLSaveImpl(XMLHelper helper) {
             super(helper);
         }
@@ -239,10 +409,8 @@
 
         XMLStreamWriter xmlStreamWriter/* = null*/;
 
-        ChangeSummaryStreamSerializer changeSummarySerializer;
-
         protected void saveDataTypeElementSingle(EObject o, EStructuralFeature f) {
-            if (f.getEType() == ((ModelFactoryImpl) ModelFactory.INSTANCE).getChangeSummaryType())
{
+            if (f.getEType() == ChangeSummary_TYPE) {
                 Object changeSummary = helper.getValue(o, f);
                 StringBuffer margin = new StringBuffer("  ");
                 for (EObject container = o.eContainer(), grandContainer; (grandContainer
= container.eContainer()) != null; container = grandContainer)
@@ -265,19 +433,19 @@
                                 write(new String(cbuf, off, len));
                             }
                         });
-                        xmlStreamWriter.setNamespaceContext(new NamespaceContext() {
+                        xmlStreamWriter.setNamespaceContext(((SDOXMLHelperImpl) helper).new
NameSpaceContext() {
                             public String getNamespaceURI(String prefix) {
-                                return declareXSI && ExtendedMetaData.XSI_PREFIX.equals(prefix)
? ExtendedMetaData.XSI_URI : helper
+                                return declareXSI && ExtendedMetaData.XSI_PREFIX.equals(prefix)
? ExtendedMetaData.XSI_URI : super
                                         .getNamespaceURI(prefix);
                             }
 
                             public String getPrefix(String namespaceURI) {
-                                return declareXSI && ExtendedMetaData.XSI_URI.equals(namespaceURI)
? ExtendedMetaData.XSI_PREFIX : helper
+                                return declareXSI && ExtendedMetaData.XSI_URI.equals(namespaceURI)
? ExtendedMetaData.XSI_PREFIX : super
                                         .getPrefix(namespaceURI);
                             }
 
                             public Iterator getPrefixes(String namespaceURI) {
-                                final Iterator iterator = ((SDOXMLHelperImpl) helper).getPrefixes(namespaceURI).iterator();
+                                final Iterator iterator = super.getPrefixes(namespaceURI);
                                 return ExtendedMetaData.XSI_URI.equals(namespaceURI) ? new
Iterator() {
                                     boolean first = true;
 
@@ -314,7 +482,8 @@
                         }
                         if (declareXSI)
                             xmlStreamWriter.setPrefix(ExtendedMetaData.XSI_PREFIX, ExtendedMetaData.XSI_URI);
-                        changeSummarySerializer = new ChangeSummaryStreamSerializer();
+                        if (changeSummarySerializer == null)
+                            changeSummarySerializer = new ChangeSummaryStreamSerializer();
                     }
                     changeSummarySerializer.saveChangeSummary((ChangeSummary) changeSummary,
qName(f), xmlStreamWriter,
                             changeSummaryOptions);

Modified: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryPropertyTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryPropertyTestCase.java?view=diff&rev=503913&r1=503912&r2=503913
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryPropertyTestCase.java
(original)
+++ incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryPropertyTestCase.java
Mon Feb  5 14:53:34 2007
@@ -94,11 +94,18 @@
 
         assertTrue(TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(TEST_DATA_BEFORE_UNDO)));
         
+        DataObject loaded = hc.getXMLHelper().load(new ByteArrayInputStream(baos.toByteArray())).getRootObject();
+        baos.reset();
+        hc.getXMLHelper().save(loaded, TEST_NAMESPACE, "stockQuote", baos);
+        //hc.getXMLHelper().save(loaded, TEST_NAMESPACE, "stockQuote", System.out);
+
+        assertTrue(TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(TEST_DATA_BEFORE_UNDO)));
+
         // Undo all changes and then serialize the resulting data graph again
         //
         cs.undoChanges();
         
-        baos = new ByteArrayOutputStream();
+        baos.reset();
         hc.getXMLHelper().save(quote, TEST_NAMESPACE, "stockQuote", baos);
         //hc.getXMLHelper().save(quote, TEST_NAMESPACE, "stockQuote", System.out);
         

Modified: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLStreamHelperTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLStreamHelperTestCase.java?view=diff&rev=503913&r1=503912&r2=503913
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLStreamHelperTestCase.java
(original)
+++ incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLStreamHelperTestCase.java
Mon Feb  5 14:53:34 2007
@@ -19,12 +19,11 @@
  */
 package org.apache.tuscany.sdo.test;
 
-import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.StringReader;
 import java.io.StringWriter;
 import java.net.URL;
-import java.util.List;
+import java.util.*;
 
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLInputFactory;
@@ -96,6 +95,19 @@
             event = reader.next();
         }
         DataObject dataObject = streamHelper.loadObject(reader);
+        Assert.assertNotNull(dataObject);
+        Assert.assertTrue(dataObject.getString("myAttr").equals("helloworld.HelloWorldImpl"));
+    }
+
+    public void testLoadUnqualifiedObject() throws Exception {
+        XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(xml.replaceAll("bar:",
"")));
+        int event = reader.getEventType();
+        while (!(event == XMLStreamConstants.START_ELEMENT && reader.getName().getLocalPart().equals(name.getLocalPart()))
&& reader.hasNext()) {
+            event = reader.next();
+        }
+        Map options = new HashMap();
+        options.put(XMLStreamHelper.OPTION_DEFAULT_ROOT_TYPE, typeHelper.getType(name.getNamespaceURI(),
"MockImplementation"));
+        DataObject dataObject = streamHelper.loadObject(reader, options);
         Assert.assertNotNull(dataObject);
         Assert.assertTrue(dataObject.getString("myAttr").equals("helloworld.HelloWorldImpl"));
     }



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org


Mime
View raw message