You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@abdera.apache.org by jm...@apache.org on 2006/07/13 21:51:34 UTC

svn commit: r421693 - in /incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax: FOMDocument.java FOMElement.java

Author: jmsnell
Date: Thu Jul 13 12:51:34 2006
New Revision: 421693

URL: http://svn.apache.org/viewvc?rev=421693&view=rev
Log:
Clone and Writeto improvements.  

Document.clone wasn't working completely correctly
The writeTo methods were suboptimal

Modified:
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java?rev=421693&r1=421692&r2=421693&view=diff
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java (original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java Thu Jul 13 12:51:34 2006
@@ -19,24 +19,30 @@
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.Date;
+import java.util.Iterator;
 
 import javax.activation.MimeType;
-import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamWriter;
 
 import org.apache.abdera.factory.Factory;
 import org.apache.abdera.model.Document;
 import org.apache.abdera.model.Element;
+import org.apache.axiom.om.OMComment;
+import org.apache.axiom.om.OMDocType;
+import org.apache.axiom.om.OMDocument;
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.OMOutputFormat;
+import org.apache.axiom.om.OMProcessingInstruction;
 import org.apache.axiom.om.OMXMLParserWrapper;
 import org.apache.axiom.om.impl.MTOMXMLStreamWriter;
 import org.apache.axiom.om.impl.llom.OMDocumentImpl;
+import org.apache.axiom.om.util.StAXUtils;
 
 
 public class FOMDocument<T extends Element> 
@@ -103,29 +109,20 @@
   }
 
   public void writeTo(OutputStream out) throws IOException {
-    try {    
-      OMOutputFormat outputFormat = new OMOutputFormat();
-      outputFormat.setCharSetEncoding(this.getCharsetEncoding());
-      MTOMXMLStreamWriter omwriter = new MTOMXMLStreamWriter(out, outputFormat);
-      internalSerialize(omwriter, true);
-      omwriter.flush();
-    } catch (XMLStreamException e) {
-      throw new FOMException(e);
-    }
+    writeTo(new OutputStreamWriter(out));
   }
 
   public void writeTo(java.io.Writer writer) throws IOException {
-    try {
-      setComplete(true);      
+    try {      
       OMOutputFormat outputFormat = new OMOutputFormat();
-      outputFormat.setCharSetEncoding(this.getCharsetEncoding());
-      XMLStreamWriter streamwriter = 
-        XMLOutputFactory.newInstance().createXMLStreamWriter(
-          writer);
-      MTOMXMLStreamWriter omwriter = new MTOMXMLStreamWriter(streamwriter);
+      if (this.getCharsetEncoding() != null)
+        outputFormat.setCharSetEncoding(this.getCharsetEncoding());
+      MTOMXMLStreamWriter omwriter = 
+        new MTOMXMLStreamWriter(
+          StAXUtils.createXMLStreamWriter(writer));
       omwriter.setOutputFormat(outputFormat);
-      this.internalSerializeAndConsume(omwriter);
-      omwriter.flush();
+      this.internalSerialize(omwriter);
+      omwriter.flush();    
     } catch (XMLStreamException e) {
       throw new FOMException(e);
     }
@@ -149,9 +146,29 @@
   
   @SuppressWarnings("unchecked")
   public Object clone() {
-    T rootClone = (T)getRoot().clone();
     Document<T> doc = ((FOMFactory)factory).newDocument();
-    doc.setRoot(rootClone);
+    OMDocument omdoc = (OMDocument) doc;
+    for (Iterator i = getChildren(); i.hasNext();) {
+      OMNode node = (OMNode) i.next();
+      switch(node.getType()) {
+        case OMNode.COMMENT_NODE:
+          OMComment comment = (OMComment) node;
+          factory.createOMComment(omdoc, comment.getValue());
+          break;
+        case OMNode.DTD_NODE:
+          OMDocType doctype = (OMDocType) node;
+          factory.createOMDocType(omdoc, doctype.getValue());
+          break;
+        case OMNode.ELEMENT_NODE:
+          Element el = (Element) node;
+          omdoc.addChild((OMNode) el.clone());
+          break;
+        case OMNode.PI_NODE:
+          OMProcessingInstruction pi = (OMProcessingInstruction) node;
+          factory.createOMProcessingInstruction(omdoc, pi.getTarget(), pi.getValue());
+          break;
+      }
+    }
     return doc;
   }
 

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java?rev=421693&r1=421692&r2=421693&view=diff
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java (original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java Thu Jul 13 12:51:34 2006
@@ -20,6 +20,7 @@
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
@@ -27,9 +28,7 @@
 import java.util.List;
 
 import javax.xml.namespace.QName;
-import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamWriter;
 
 import org.apache.abdera.factory.Factory;
 import org.apache.abdera.model.Base;
@@ -53,10 +52,8 @@
 import org.apache.axiom.om.OMProcessingInstruction;
 import org.apache.axiom.om.OMText;
 import org.apache.axiom.om.OMXMLParserWrapper;
-import org.apache.axiom.om.impl.MTOMXMLStreamWriter;
 import org.apache.axiom.om.impl.llom.OMElementImpl;
 
-
 public class FOMElement 
   extends OMElementImpl 
   implements Element, 
@@ -295,30 +292,15 @@
   }
 
   public void writeTo(OutputStream out) throws IOException {
-    try {
-      OMOutputFormat outputFormat = new OMOutputFormat();
-      if (getDocument() != null)
-        outputFormat.setCharSetEncoding(getDocument().getCharset());
-      MTOMXMLStreamWriter omwriter = 
-        new MTOMXMLStreamWriter(out, outputFormat);
-      internalSerialize(omwriter, true);
-      omwriter.flush();
-    } catch (XMLStreamException e) {
-      throw new FOMException(e);
-    }
+    writeTo(new OutputStreamWriter(out));
   }
 
   public void writeTo(java.io.Writer writer) throws IOException {
     try { 
       OMOutputFormat outputFormat = new OMOutputFormat();
-      outputFormat.setCharSetEncoding(getDocument().getCharset());
-      XMLStreamWriter streamwriter = 
-        XMLOutputFactory.newInstance().createXMLStreamWriter(
-          writer);
-      MTOMXMLStreamWriter omwriter = new MTOMXMLStreamWriter(streamwriter);
-      omwriter.setOutputFormat(outputFormat);
-      this.internalSerialize(omwriter, true);
-      omwriter.flush();
+      if (getDocument() != null && getDocument().getCharset() != null)
+        outputFormat.setCharSetEncoding(getDocument().getCharset());
+      serializeAndConsume(writer, outputFormat);
     } catch (XMLStreamException e) {
       throw new FOMException(e);
     }