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

svn commit: r535343 - in /incubator/tuscany/java/sdo/impl/src: main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java main/java/org/apache/tuscany/sdo/helper/XMLHelperImpl.java test/java/org/apache/tuscany/sdo/test/XMLHelperTestCase.java

Author: frankb
Date: Fri May  4 11:26:32 2007
New Revision: 535343

URL: http://svn.apache.org/viewvc?view=rev&rev=535343
Log:
Fix for TUSCANY-1214

Modified:
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLHelperImpl.java
    incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLHelperTestCase.java

Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java?view=diff&rev=535343&r1=535342&r2=535343
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java (original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java Fri May  4 11:26:32 2007
@@ -50,6 +50,8 @@
 import org.eclipse.emf.ecore.util.FeatureMapUtil;
 import org.eclipse.emf.ecore.xmi.XMLResource;
 import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
 import org.xml.sax.InputSource;
 
 import commonj.sdo.DataObject;
@@ -154,6 +156,24 @@
 
   protected void save(OutputStream outputStream, Object options) throws IOException
   {
+    save(outputStream, null, options);
+  }
+    
+  protected void save(Writer outputWriter, Object options) throws IOException
+  {
+    // TODO temporary brute-force implementation ... to be replaced
+    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+    save(outputStream, options);
+    outputWriter.write(new String(outputStream.toByteArray()));
+  }
+
+  protected void save(Node node, Object options) throws IOException
+  {
+    save(null, (Document)node, options);
+  }
+  
+  protected void save(OutputStream outputStream, Document document, Object options) throws IOException
+  {
     EObject oldContainer = null;
     EReference oldContainmentReference = null;
     int oldContainmentIndex = -1;
@@ -181,7 +201,10 @@
       }
     }
 
-    resource.save(outputStream, (Map)options);
+    if (outputStream != null)
+      resource.save(outputStream, (Map)options);
+    else // if (document != null)
+      resource.save(document, (Map)options, null);
 
     if (oldContainer != null)
     {
@@ -203,14 +226,6 @@
     }
   }
 
-  protected void save(Writer outputWriter, Object options) throws IOException
-  {
-    // TODO temporary brute-force implementation ... to be replaced
-    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
-    save(outputStream, options);
-    outputWriter.write(new String(outputStream.toByteArray()));
-  }
-
   protected void load(InputStream inputStream, String locationURI, Object options) throws IOException
   {
     InputSource inputSource = new InputSource(inputStream);
@@ -221,6 +236,11 @@
   {
     InputSource inputSource = new InputSource(inputReader);
     load(inputSource, locationURI, options);
+  }
+  
+  protected final void load(Node node, Object options) throws IOException {
+      resource.load(node, (Map)options);
+      initLoadedRoot();
   }
 
   protected final void load(XMLStreamReader reader, Map options) throws IOException

Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLHelperImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLHelperImpl.java?view=diff&rev=535343&r1=535342&r2=535343
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLHelperImpl.java (original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLHelperImpl.java Fri May  4 11:26:32 2007
@@ -29,8 +29,15 @@
 
 import javax.xml.transform.Result;
 import javax.xml.transform.Source;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXResult;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
 
 import org.eclipse.emf.ecore.util.ExtendedMetaData;
+import org.xml.sax.InputSource;
 
 import commonj.sdo.DataObject;
 import commonj.sdo.helper.TypeHelper;
@@ -86,9 +93,26 @@
     return document;
   }
   
-  public XMLDocument load(Source inputSource, String locationURI, Object options) throws IOException
+  public XMLDocument load(Source source, String locationURI, Object options) throws IOException
   {
-    throw new UnsupportedOperationException();
+      if (source instanceof DOMSource) {
+          DOMSource domSource = (DOMSource)source;
+          XMLDocumentImpl document = new XMLDocumentImpl(extendedMetaData, options);
+          document.load(domSource.getNode(), options);
+          return document;
+      }
+      else if (source instanceof SAXSource) {
+          XMLDocumentImpl document = new XMLDocumentImpl(extendedMetaData, options);
+          InputSource inputSource = SAXSource.sourceToInputSource(source);
+          document.load(inputSource, locationURI, options);
+          return document;
+      }
+      else if (source instanceof StreamSource) {
+          return load(((StreamSource)source).getInputStream(), locationURI, options);
+      }
+      else {
+          throw new UnsupportedOperationException();
+      }
   }
   
   public String save(DataObject dataObject, String rootElementURI, String rootElementName)
@@ -122,7 +146,18 @@
   
   public void save(XMLDocument xmlDocument, Result outputResult, Object options) throws IOException
   {
-    throw new UnsupportedOperationException();
+      if (outputResult instanceof DOMResult) {
+          ((XMLDocumentImpl)xmlDocument).save(((DOMResult)outputResult).getNode(), options);
+      }
+      else if (outputResult instanceof SAXResult) {
+          throw new UnsupportedOperationException();
+      }
+      else if (outputResult instanceof StreamResult) {
+          save(xmlDocument, ((StreamResult)outputResult).getOutputStream(), options);
+      }
+      else {
+          throw new UnsupportedOperationException();
+      }
   }
 
   public XMLDocument createDocument(DataObject dataObject, String rootElementURI, String rootElementName)

Modified: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLHelperTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLHelperTestCase.java?view=diff&rev=535343&r1=535342&r2=535343
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLHelperTestCase.java (original)
+++ incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLHelperTestCase.java Fri May  4 11:26:32 2007
@@ -19,15 +19,23 @@
  */
 package org.apache.tuscany.sdo.test;
 
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.net.URL;
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.dom.DOMSource;
+
 import junit.framework.TestCase;
 
 import org.apache.tuscany.sdo.util.SDOUtil;
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
 
 import commonj.sdo.helper.HelperContext;
 import commonj.sdo.helper.XMLDocument;
@@ -57,6 +65,8 @@
     define("/simpleWithChangeSummary.xsd");
 
     define("/SequenceChangeSummary.xsd");
+    
+    define("/simple.xsd");
   }
 
   protected void tearDown() throws Exception {
@@ -157,5 +167,48 @@
 MARGIN+INDENT+INDENT+  "</cs:openQuote>"  +LINE_BREAK+
 MARGIN+INDENT+  "</changes>"  +LINE_BREAK+
 MARGIN+  "</cs:openQuote>");
+  }
+  
+  private String quoteXML =
+      "<?xml version=\"1.0\" encoding=\"ASCII\"?>" +
+      "<simple:stockQuote xmlns:simple=\"http://www.example.com/simple\">" +
+          "<symbol>fbnt</symbol>" +
+          "<companyName>FlyByNightTechnology</companyName>" +
+          "<price>1000.0</price>" +
+          "<open1>1000.0</open1>" +
+          "<high>1000.0</high>" +
+          "<low>1000.0</low>" +
+          "<volume>1000.0</volume>" +
+          "<change1>1000.0</change1>" +
+          "<quotes>" +
+              "<price>2000.0</price>" +
+          "</quotes>" +
+      "</simple:stockQuote>";
+
+  public void dontTestLoadDOMSource() throws IOException, ParserConfigurationException, SAXException
+  {
+      //TODO Enable this test when we move to EMF 2.2.3 
+
+      DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
+      dFactory.setNamespaceAware(true);
+
+      DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
+      Document document = dBuilder.parse(new ByteArrayInputStream(quoteXML.getBytes()));
+      
+      DOMSource domSource = new DOMSource(document);
+      
+      XMLDocument xmlDocument = hc.getXMLHelper().load(domSource, null, null);
+      
+      ByteArrayOutputStream baos = new ByteArrayOutputStream();
+      
+      Map options = new HashMap();
+      options.put(SDOUtil.XML_SAVE_INDENT, "");
+      options.put(SDOUtil.XML_SAVE_MARGIN, "");
+      options.put(SDOUtil.XML_SAVE_LineBreak, "");
+      
+      hc.getXMLHelper().save(xmlDocument, baos, options);
+      
+      boolean isEqual = TestUtil.equalXmlFiles(new ByteArrayInputStream(quoteXML.getBytes()), new ByteArrayInputStream(baos.toByteArray()));
+      assertTrue(isEqual);
   }
 }



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