abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r640073 - /incubator/abdera/java/branches/abdera-0.4.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/StaxStreamWriter.java
Date Sat, 22 Mar 2008 20:23:53 GMT
Author: jmsnell
Date: Sat Mar 22 13:23:52 2008
New Revision: 640073

URL: http://svn.apache.org/viewvc?rev=640073&view=rev
Log:
Fix a pretty serious bug in the StreamWriter implementation that messed up the serialization
of default namespaces and would produce invalid Atom documents

Modified:
    incubator/abdera/java/branches/abdera-0.4.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/StaxStreamWriter.java

Modified: incubator/abdera/java/branches/abdera-0.4.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/StaxStreamWriter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/abdera-0.4.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/StaxStreamWriter.java?rev=640073&r1=640072&r2=640073&view=diff
==============================================================================
--- incubator/abdera/java/branches/abdera-0.4.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/StaxStreamWriter.java
(original)
+++ incubator/abdera/java/branches/abdera-0.4.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/StaxStreamWriter.java
Sat Mar 22 13:23:52 2008
@@ -25,6 +25,7 @@
 import java.util.Map;
 import java.util.Stack;
 
+import javax.xml.namespace.NamespaceContext;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 
@@ -150,6 +151,12 @@
     }
   }
   
+  private boolean needToWriteNamespace(String prefix, String namespace) {
+    NamespaceContext nc = writer.getNamespaceContext();
+      String uri = nc.getNamespaceURI(prefix);
+      return uri != null ? !uri.equals(namespace) : true;
+  }
+  
   public StreamWriter startElement(
     String name, 
     String namespace, 
@@ -157,21 +164,24 @@
     try {
       if (autoindent && textwritten == 0) indent();
       push();
-      if (prefix != null) {
+      if (prefix != null && !prefix.equals("")) {
         writer.writeStartElement(
           prefix,
           name,
           namespace);
+        if (needToWriteNamespace(prefix,namespace))
+          writeNamespace(prefix,namespace,false);
       } else if (namespace != null) {
         writer.writeStartElement(
           "",
           name, 
           namespace);
+        if (needToWriteNamespace(prefix,namespace))
+          writer.writeDefaultNamespace(namespace);
       } else {
         writer.writeStartElement("",name,"");
         writer.writeDefaultNamespace("");
       }
-      writeNamespace(prefix,namespace,false);
       if (autoflush) writer.flush();
     } catch(XMLStreamException e) {
       throw new RuntimeException(e);
@@ -227,6 +237,26 @@
     return writeId(FOMHelper.generateUuid());
   }
 
+  public StreamWriter writeDefaultNamespace(String uri) {
+    try {
+      writer.writeDefaultNamespace(uri);
+    } catch (XMLStreamException e) {
+      throw new RuntimeException(e);
+    }
+    return this;
+  }
+  
+  public StreamWriter writeNamespace(
+    String prefix,
+    String uri) {
+      try {
+        writer.writeNamespace(prefix, uri);
+      } catch (XMLStreamException e) {
+        throw new RuntimeException(e);
+      }
+      return this;
+  }
+  
   public StreamWriter writeAttribute(
     String name, 
     String namespace,



Mime
View raw message