You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by ke...@apache.org on 2007/02/14 14:35:08 UTC

svn commit: r507527 - in /incubator/tuscany/java/sdo/impl/src: main/java/org/apache/tuscany/sdo/util/ main/java/org/apache/tuscany/sdo/util/resource/ test/java/org/apache/tuscany/sdo/test/

Author: kelvingoodson
Date: Wed Feb 14 05:35:07 2007
New Revision: 507527

URL: http://svn.apache.org/viewvc?view=rev&rev=507527
Log:
TUSCANY-928 

Added:
    incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLHelperTestCase.java   (with props)
Modified:
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/ChangeSummaryStreamSerializer.java
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/SDOXMLResourceImpl.java

Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java?view=diff&rev=507527&r1=507526&r2=507527
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java (original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java Wed Feb 14 05:35:07 2007
@@ -77,6 +77,7 @@
 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.XMLResource;
 import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
 
 import commonj.sdo.DataGraph;
@@ -98,6 +99,31 @@
  */
 public final class SDOUtil
 {
+  /**
+   * Line Break String such as "\n", "\r\n", "\r" and "", absence/null is the default (line.separator System Property)
+   */
+  static public final String XML_SAVE_LineBreak = "LineBreak",
+  /**
+   * Indent String such as "\t", "", etc. absence/null is the default ("  ")
+   */
+  XML_SAVE_INDENT = "indent",
+  /**
+   * Margin String such as " ", "\t\t", etc. Absence/null/"" is the default (no margin)
+   */
+  XML_SAVE_MARGIN = "margin",
+  /**
+   * Attribute formatting that exceeds the specified width as Integer will cause a line break so that formatting will continue indented on the next line
+   */
+  XML_SAVE_LineWidth = XMLResource.OPTION_LINE_WIDTH,
+  /**
+   * Boolean to save a doctype declaration 
+   */
+  XML_SAVE_DocType = XMLResource.OPTION_SAVE_DOCTYPE,
+  /**
+   * Boolean to process the schemaLocation/noNamespaceSchemaLocation attributes occurring in the instance document to {@link XSDHelper#define convert XSD(s) to Types} 
+   */
+  XML_LOAD_SCHEMA = "ProcessSchemaLocations";
+
   /**
    * Create a DataObject wrapper for an instance of the specified dataType.
    * This method is typically used to create a root object that can be passed to the XMLHelper.save() 

Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/ChangeSummaryStreamSerializer.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/ChangeSummaryStreamSerializer.java?view=diff&rev=507527&r1=507526&r2=507527
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/ChangeSummaryStreamSerializer.java (original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/ChangeSummaryStreamSerializer.java Wed Feb 14 05:35:07 2007
@@ -29,6 +29,7 @@
 import org.apache.tuscany.sdo.SDOPackage;
 import org.apache.tuscany.sdo.helper.*;
 import org.apache.tuscany.sdo.impl.ChangeSummaryImpl;
+import org.apache.tuscany.sdo.util.SDOUtil;
 import org.eclipse.emf.ecore.*;
 import org.eclipse.emf.ecore.change.*;
 import org.eclipse.emf.ecore.util.*;
@@ -115,13 +116,13 @@
         if (options == null)
             return;
         if (lineBreak == STRING_OPTION)
-            lineBreak = (String) options.get(OPTION_LINE_BREAK);
+            lineBreak = (String) options.get(SDOUtil.XML_SAVE_LineBreak);
         if (lineBreak == null)
             return;
         if (margin == STRING_OPTION)
-            margin = (String) options.get(OPTION_MARGIN);
+            margin = (String) options.get(SDOUtil.XML_SAVE_MARGIN);
         if (indent == STRING_OPTION)
-            indent = (String) options.get(OPTION_INDENT);
+            indent = (String) options.get(SDOUtil.XML_SAVE_INDENT);
         breakLine();
     }
 
@@ -414,21 +415,9 @@
     static protected final Object CHANGE_SUMMARY = SDOPackage.eINSTANCE.getChangeSummary();
 
     /**
-     * Line Break String such as "\n", "\r\n" and "\r", absent/null is the default (no Line Breaking)
-     */
-    static public final String OPTION_LINE_BREAK = "LineBreak",
-    /**
-     * Indent String such as " ", "\t", etc. Absent/null is the default (no indentation)
-     */
-    OPTION_INDENT = "indent",
-    /**
-     * Margin String such as " ", "\t\t", etc. Absent/null is the default (no margin)
-     */
-    OPTION_MARGIN = "margin",
-    /**
      * Root Object path String such as "#", etc. Absent/null is the default (automatic computation)
      */
-    OPTION_RootObject_PATH = "RootObject path",
+    static public final String OPTION_RootObject_PATH = "RootObject path",
     /**
      * Boolean to optimize sequence/list/array. Absent/null/Boolean.FALSE is the default (no optimization)
      */
@@ -449,7 +438,7 @@
      * @param writer
      *            Never null
      * @param options
-     *            {@link #OPTION_LINE_BREAK}, {@link #OPTION_INDENT}, {@link #OPTION_MARGIN}, {@link #OPTION_RootObject_PATH}, {@link #OPTION_OPTIMIZE_LIST} and XMLResource.OPTION_EXTENDED_META_DATA; can be null or empty
+     *            {@link SDOUtil#XML_SAVE_LineBreak} (absence/null is the default i.e. no Line Breaking), {@link SDOUtil#XML_SAVE_INDENT} (absence/null is the default i.e. no indentation), {@link SDOUtil#XML_SAVE_MARGIN}, {@link #OPTION_RootObject_PATH}, {@link #OPTION_OPTIMIZE_LIST} and XMLResource.OPTION_EXTENDED_META_DATA; can be null or empty
      */
     public final void saveChangeSummary(ChangeSummary changeSummary, QName changeSummaryElement, XMLStreamWriter writer, Map options)
             throws XMLStreamException {

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=507527&r1=507526&r2=507527
==============================================================================
--- 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 Wed Feb 14 05:35:07 2007
@@ -383,22 +383,118 @@
         }
     }
 
+    static final String INDENT = "  ", LINE_SEPARATOR = System.getProperty("line.separator");
+
+    static final class XmlString extends XMLString {
+        XmlString(int lineWidth, String temporaryFileName) {
+            super(lineWidth, temporaryFileName); // setLineWidth & setTemporaryFileName
+        }
+
+        XmlString(int lineWidth, String publicId, String systemId, String temporaryFileName) {
+            super(lineWidth, publicId, systemId, temporaryFileName);
+        }
+
+        void setLineBreak(String lineBreak) {
+            lineSeparator = lineBreak;
+        }
+
+        void margin(String margin) {
+            indents.set(0, margin);
+        }
+
+        String indent = INDENT;
+
+        protected String getElementIndent(int extra) {
+            int nesting = depth + extra - 1;
+            for (int i = indents.size() - 1; i < nesting; ++i) {
+                indents.add(indents.get(i) + indent);
+            }
+            return (String) indents.get(nesting);
+        }
+
+        protected String getAttributeIndent() {
+            return getElementIndent();
+        }
+
+        public void reset(String publicId, String systemId, int lineWidth, String temporaryFileName) {
+            super.reset(publicId, systemId, lineWidth, temporaryFileName);
+            setLineBreak(LINE_SEPARATOR);
+            indent = INDENT;
+        }
+    }
+
+    static final char MARK = '\n';
+
+    static final String LINE_BREAK = new String(new char[] { MARK });
+
     final class SDOXMLSaveImpl extends XMLSaveImpl {
         SDOXMLSaveImpl(XMLHelper helper) {
             super(helper);
         }
 
+        XmlString doc(XMLResource resource, Map options) {
+            if (doc instanceof XmlString)
+                return (XmlString) doc;
+            Object lineWidth = options.get(OPTION_LINE_WIDTH);
+            int width = lineWidth == null ? Integer.MAX_VALUE : ((Number) lineWidth).intValue();
+            XmlString d = resource != null && Boolean.TRUE.equals(options.get(OPTION_SAVE_DOCTYPE)) ? new XmlString(width, resource.getPublicId(),
+                    resource.getSystemId(), doc.getTemporaryFileName()) : new XmlString(width, doc.getTemporaryFileName());
+            doc = d;
+            return d;
+        }
+
         Map changeSummaryOptions = new HashMap();
+        String indent = INDENT, margin;
 
         protected void init(XMLResource resource, Map options) {
             super.init(resource, options);
-            //changeSummaryOptions.put(ChangeSummaryStreamSerializer.OPTION_RootObject_PATH, "#");
-            //changeSummaryOptions.put(ChangeSummaryStreamSerializer.OPTION_OPTIMIZE_LIST, Boolean.TRUE);
+            int unformat = 0;
+            String lineBreak = (String) options.get(SDOUtil.XML_SAVE_LineBreak);
+            if (lineBreak == null)
+                changeSummaryOptions.put(SDOUtil.XML_SAVE_LineBreak, LINE_BREAK);
+            else if (lineBreak.length() == 0)
+                ++unformat;
+            else {
+                changeSummaryOptions.put(SDOUtil.XML_SAVE_LineBreak, LINE_BREAK);
+                if (lineBreak.equals(LINE_SEPARATOR))
+                    lineBreak = null;
+            }
+            String indent = (String) options.get(SDOUtil.XML_SAVE_INDENT);
+            if (indent == null)
+                changeSummaryOptions.put(SDOUtil.XML_SAVE_INDENT, INDENT);
+            else if (indent.length() == 0)
+                ++unformat;
+            else {
+                changeSummaryOptions.put(SDOUtil.XML_SAVE_INDENT, this.indent = indent);
+                if (indent.equals(INDENT))
+                    indent = null;
+            }
+            String margin = (String) options.get(SDOUtil.XML_SAVE_MARGIN);
+            if (margin == null || margin.length() == 0) {
+                if (unformat == 2)
+                    doc.setUnformatted(true);
+                else if (lineBreak != null) {
+                    XmlString d = doc(resource, options);
+                    d.setLineBreak(lineBreak);
+                    if (indent != null)
+                        d.indent = indent;
+                } else if (indent != null)
+                    doc(resource, options).indent = indent;
+                this.margin = this.indent;
+            } else {
+                XmlString d = doc(resource, options);
+                d.margin(margin);
+                if (lineBreak != null)
+                    d.setLineBreak(lineBreak);
+                if (indent != null)
+                    d.indent = indent;
+                this.margin = margin + this.indent;
+                if (!toDOM && declareXML)
+                    d.add(margin);
+            }
+            // changeSummaryOptions.put(ChangeSummaryStreamSerializer.OPTION_RootObject_PATH, "#");
+            // changeSummaryOptions.put(ChangeSummaryStreamSerializer.OPTION_OPTIMIZE_LIST, Boolean.TRUE);
             changeSummaryOptions.put(OPTION_EXTENDED_META_DATA, extendedMetaData);
-            if (Boolean.FALSE.equals(options.get(OPTION_FORMATTED)))
-                return;
-            changeSummaryOptions.put(ChangeSummaryStreamSerializer.OPTION_LINE_BREAK, "\n");
-            changeSummaryOptions.put(ChangeSummaryStreamSerializer.OPTION_INDENT, "  ");
         }
 
         QName qName(EStructuralFeature f) {
@@ -413,10 +509,10 @@
         protected void saveDataTypeElementSingle(EObject o, EStructuralFeature f) {
             if (f.getEType() == ChangeSummary_TYPE) {
                 Object changeSummary = helper.getValue(o, f);
-                StringBuffer margin = new StringBuffer("  ");
+                StringBuffer margin = new StringBuffer(this.margin);
                 for (EObject container = o.eContainer(), grandContainer; (grandContainer = container.eContainer()) != null; container = grandContainer)
-                    margin.append("  ");
-                changeSummaryOptions.put(ChangeSummaryStreamSerializer.OPTION_MARGIN, margin.toString());
+                    margin.append(indent);
+                changeSummaryOptions.put(SDOUtil.XML_SAVE_MARGIN, margin.toString());
                 try {
                     if (xmlStreamWriter == null) {
                         xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(new Writer() {
@@ -426,12 +522,35 @@
                             public void flush() {
                             }
 
-                            public final void write(String str) {
-                                doc.add(str);
+                            protected final void add(char[] cbuf, int index, int off) {
+                                doc.add(new String(cbuf, index, off - index));
                             }
 
                             public void write(char[] cbuf, int off, int len) {
-                                write(new String(cbuf, off, len));
+                                if (len != 0)
+                                    for (;;) {
+                                        while (cbuf[off] == MARK) {
+                                            doc.addLine();
+                                            if (--len == 0)
+                                                return;
+                                            ++off;
+                                        }
+                                        for (int index = off;/* true */;) {
+                                            ++off;
+                                            if (--len == 0)
+                                                add(cbuf, index, off);
+                                            else {
+                                                if (cbuf[off] != MARK)
+                                                    continue;
+                                                add(cbuf, index, off);
+                                                doc.addLine();
+                                                if (--len != 0)
+                                                    break;
+                                            }
+                                            return;
+                                        }
+                                        ++off;
+                                    }
                             }
                         });
                         xmlStreamWriter.setNamespaceContext(((SDOXMLHelperImpl) helper).new NameSpaceContext() {

Added: 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=auto&rev=507527
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLHelperTestCase.java (added)
+++ incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLHelperTestCase.java Wed Feb 14 05:35:07 2007
@@ -0,0 +1,113 @@
+/**
+ *
+ *  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.test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.tuscany.sdo.util.SDOUtil;
+
+import commonj.sdo.helper.*;
+
+import junit.framework.TestCase;
+
+public class XMLHelperTestCase extends TestCase {
+
+  HelperContext hc;
+  private XSDHelper xsdHelper;
+  XMLHelper xmlh;
+  static final String INDENT = "    ", MARGIN = "  ", LINE_BREAK = "\n\n";
+
+  void define(String model) throws Exception {
+    // Populate the meta data for the test model
+    URL url = getClass().getResource(model);
+    xsdHelper.define(url.openStream(), url.toString());
+  }
+  
+  protected void setUp() throws Exception {
+    super.setUp();
+    hc = SDOUtil.createHelperContext();
+    xsdHelper = hc.getXSDHelper();
+    xmlh = hc.getXMLHelper();
+
+    // Populate the meta data for the test (Stock Quote) model
+    define("/simpleWithChangeSummary.xsd");
+  }
+
+  protected void tearDown() throws Exception {
+    super.tearDown();
+  }
+
+  public void testLoadInputStreamStringObject() throws IOException {
+ 
+  }
+
+  protected final void format(String xml,String formetted) throws IOException {
+    XMLDocument doc = xmlh.load(getClass().getResource(xml).openStream());
+    Map options = new HashMap();
+
+    options.put(SDOUtil.XML_SAVE_INDENT, INDENT);
+    options.put(SDOUtil.XML_SAVE_MARGIN, MARGIN);
+    options.put(SDOUtil.XML_SAVE_LineBreak, LINE_BREAK);
+
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    
+    // doc changes encoding (https://bugs.eclipse.org/bugs/show_bug.cgi?id=173681)
+    // doc declares NameSpaces at root
+    xmlh.save(xmlh.createDocument(doc.getRootObject(), doc.getRootElementURI(), doc.getRootElementName()), baos, options);
+    
+    assertEquals(baos.toString(), formetted);
+  }
+
+  public void testSaveXMLDocumentOutputStreamObject() throws IOException {
+    format("/simpleWithChangeSummary.xml",
+MARGIN+  "<?xml version=\"1.0\" encoding=\"ASCII\"?>"  +LINE_BREAK+
+MARGIN+  "<cs:stockQuote xmlns:cs=\"http://www.example.com/simpleCS\">"  +LINE_BREAK+
+MARGIN+INDENT+  "<symbol>FBNT</symbol>"  +LINE_BREAK+
+MARGIN+INDENT+  "<companyName>FlyByNightTechnology</companyName>"  +LINE_BREAK+
+MARGIN+INDENT+  "<price>999.0</price>"  +LINE_BREAK+
+MARGIN+INDENT+  "<volume>1000.0</volume>"  +LINE_BREAK+
+MARGIN+INDENT+  "<quotes>"  +LINE_BREAK+
+MARGIN+INDENT+INDENT+  "<price>1500.0</price>"  +LINE_BREAK+
+MARGIN+INDENT+  "</quotes>"  +LINE_BREAK+
+MARGIN+INDENT+  "<quotes>"  +LINE_BREAK+
+MARGIN+INDENT+INDENT+  "<price>2500.0</price>"  +LINE_BREAK+
+MARGIN+INDENT+  "</quotes>"  +LINE_BREAK+
+MARGIN+INDENT+  "<quotes>"  +LINE_BREAK+
+MARGIN+INDENT+INDENT+  "<price>3000.0</price>"  +LINE_BREAK+
+MARGIN+INDENT+  "</quotes>"  +LINE_BREAK+
+MARGIN+INDENT+  "<quotes>"  +LINE_BREAK+
+MARGIN+INDENT+INDENT+  "<price>4000.0</price>"  +LINE_BREAK+
+MARGIN+INDENT+  "</quotes>"  +LINE_BREAK+
+MARGIN+INDENT+  "<changes create=\"#//quotes[3] #//quotes[4]\" delete=\"#//changes/stockQuote[1]/quotes[2]\" logging=\"false\" xmlns:sdo=\"commonj.sdo\">"  +LINE_BREAK+
+MARGIN+INDENT+INDENT+  "<cs:stockQuote sdo:ref=\"#/stockQuote\" sdo:unset=\"volume\">"  +LINE_BREAK+
+MARGIN+INDENT+INDENT+INDENT+  "<symbol>fbnt</symbol>"  +LINE_BREAK+
+MARGIN+INDENT+INDENT+INDENT+  "<price>1000.0</price>"  +LINE_BREAK+
+MARGIN+INDENT+INDENT+INDENT+  "<quotes sdo:ref=\"#//quotes[1]\" />"  +LINE_BREAK+
+MARGIN+INDENT+INDENT+INDENT+  "<quotes><price>2000.0</price><quotes><price>2000.99</price></quotes></quotes>"  +LINE_BREAK+
+MARGIN+INDENT+INDENT+INDENT+  "<quotes sdo:ref=\"#//quotes[2]\" />"  +LINE_BREAK+
+MARGIN+INDENT+INDENT+  "</cs:stockQuote>"  +LINE_BREAK+
+MARGIN+INDENT+  "</changes>"  +LINE_BREAK+
+MARGIN+  "</cs:stockQuote>");
+  }
+}

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLHelperTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLHelperTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



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