abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r771446 - in /abdera/java/trunk: core/src/main/java/org/apache/abdera/factory/StreamBuilder.java core/src/main/java/org/apache/abdera/writer/StreamWriter.java parser/src/main/java/org/apache/abdera/parser/stax/StaxStreamWriter.java
Date Mon, 04 May 2009 21:12:35 GMT
Author: jmsnell
Date: Mon May  4 21:12:34 2009
New Revision: 771446

URL: http://svn.apache.org/viewvc?rev=771446&view=rev
Log:
One fix for two issues:

https://issues.apache.org/jira/browse/ABDERA-213
https://issues.apache.org/jira/browse/ABDERA-211

No way to explicitly declare namespaces using StreamWriter interface
StreamWriter interface not outputting correct namespace for atom:link element in service docs

Modified:
    abdera/java/trunk/core/src/main/java/org/apache/abdera/factory/StreamBuilder.java
    abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/StreamWriter.java
    abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/StaxStreamWriter.java

Modified: abdera/java/trunk/core/src/main/java/org/apache/abdera/factory/StreamBuilder.java
URL: http://svn.apache.org/viewvc/abdera/java/trunk/core/src/main/java/org/apache/abdera/factory/StreamBuilder.java?rev=771446&r1=771445&r2=771446&view=diff
==============================================================================
--- abdera/java/trunk/core/src/main/java/org/apache/abdera/factory/StreamBuilder.java (original)
+++ abdera/java/trunk/core/src/main/java/org/apache/abdera/factory/StreamBuilder.java Mon
May  4 21:12:34 2009
@@ -849,5 +849,15 @@
   public StreamBuilder writeUpdated(String date) {
     return (StreamBuilder)super.writeUpdated(date);
   }
+
+  public StreamBuilder setPrefix(String prefix, String uri) {
+    if (!(current instanceof Element)) throw new IllegalStateException("Not currently an
element");
+    ((Element)current).declareNS(uri, prefix);
+    return this;
+  }
+
+  public StreamBuilder writeNamespace(String prefix, String uri) {
+    return setPrefix(prefix,uri);
+  }
   
 }

Modified: abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/StreamWriter.java
URL: http://svn.apache.org/viewvc/abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/StreamWriter.java?rev=771446&r1=771445&r2=771446&view=diff
==============================================================================
--- abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/StreamWriter.java (original)
+++ abdera/java/trunk/core/src/main/java/org/apache/abdera/writer/StreamWriter.java Mon May
 4 21:12:34 2009
@@ -1101,5 +1101,15 @@
    * Write the xml:lang attribute
    */
   StreamWriter writeLanguage(Locale locale);
+  
+  /**
+   * Specify the namespace prefix
+   */
+  StreamWriter setPrefix(String prefix, String uri);
+  
+  /**
+   * Write a namespace declaration 
+   */
+  StreamWriter writeNamespace(String prefix, String uri);
 }
 

Modified: abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/StaxStreamWriter.java
URL: http://svn.apache.org/viewvc/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/StaxStreamWriter.java?rev=771446&r1=771445&r2=771446&view=diff
==============================================================================
--- abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/StaxStreamWriter.java
(original)
+++ abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/StaxStreamWriter.java
Mon May  4 21:12:34 2009
@@ -26,6 +26,7 @@
 import java.util.Stack;
 
 import javax.xml.namespace.NamespaceContext;
+import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 
@@ -69,27 +70,79 @@
 
   public StreamWriter setWriter(java.io.Writer writer) {
     try {
-      this.writer = StAXUtils.createXMLStreamWriter(writer);
+      this.writer = createXMLStreamWriter(writer);
     } catch (Exception e) {
       throw new RuntimeException(e);
     }
     return this;
   }
-  public StreamWriter setOutputStream(java.io.OutputStream out) {
-    try {
-      this.writer = StAXUtils.createXMLStreamWriter(out,"UTF-8");
-    } catch (Exception e) {
-      throw new RuntimeException(e);
-    }
-    return this;
+  
+  private static XMLStreamWriter createXMLStreamWriter(
+    Writer out)
+      throws XMLStreamException {
+        XMLOutputFactory outputFactory = 
+          StAXUtils.getXMLOutputFactory();
+        Object curval = 
+          outputFactory.getProperty(
+            XMLOutputFactory.IS_REPAIRING_NAMESPACES);
+        try {
+          outputFactory.setProperty(
+            XMLOutputFactory.IS_REPAIRING_NAMESPACES, 
+            true);
+          XMLStreamWriter writer = 
+            outputFactory.createXMLStreamWriter(out);          
+          return writer;
+        } finally {
+          outputFactory.setProperty(
+            XMLOutputFactory.IS_REPAIRING_NAMESPACES, 
+            curval);
+          StAXUtils.releaseXMLOutputFactory(outputFactory);
+        }
   }
-  public StreamWriter setOutputStream(java.io.OutputStream out, String charset) {
-    try {
-      this.writer = StAXUtils.createXMLStreamWriter(out,charset);
-    } catch (Exception e) {
-      throw new RuntimeException(e);
-    }
-    return this;
+  
+  public StreamWriter setOutputStream(
+    java.io.OutputStream out) {
+      try {
+        this.writer = createXMLStreamWriter(out,"UTF-8");
+      } catch (Exception e) {
+        throw new RuntimeException(e);
+      }
+      return this;
+  }
+  
+  private static XMLStreamWriter createXMLStreamWriter(
+    OutputStream out, 
+    String encoding)
+      throws XMLStreamException {
+        XMLOutputFactory outputFactory = 
+          StAXUtils.getXMLOutputFactory();
+        Object curval = 
+          outputFactory.getProperty(
+            XMLOutputFactory.IS_REPAIRING_NAMESPACES);
+        try {
+          outputFactory.setProperty(
+              XMLOutputFactory.IS_REPAIRING_NAMESPACES, 
+              true);
+          XMLStreamWriter writer = 
+            outputFactory.createXMLStreamWriter(out, encoding);
+          return writer;
+        } finally {
+          outputFactory.setProperty(
+              XMLOutputFactory.IS_REPAIRING_NAMESPACES, 
+              curval);
+          StAXUtils.releaseXMLOutputFactory(outputFactory);
+        }
+  }
+  
+  public StreamWriter setOutputStream(
+    java.io.OutputStream out, 
+    String charset) {
+      try {
+        this.writer = createXMLStreamWriter(out,charset);
+      } catch (Exception e) {
+        throw new RuntimeException(e);
+      }
+      return this;
   }
   
   public StreamWriter startDocument(String xmlversion, String charset) {
@@ -154,8 +207,8 @@
   
   private boolean needToWriteNamespace(String prefix, String namespace) {
     NamespaceContext nc = writer.getNamespaceContext();
-      String uri = nc.getNamespaceURI(prefix);
-      return uri != null ? !uri.equals(namespace) : true;
+    String uri = nc.getNamespaceURI(prefix);
+    return uri != null ? !uri.equals(namespace) : true;
   }
   
   public StreamWriter startElement(
@@ -163,6 +216,8 @@
     String namespace, 
     String prefix) {
     try {
+      if (prefix == null || prefix.equals(""))
+        prefix = writer.getPrefix(namespace);
       if (autoindent && textwritten == 0) indent();
       push();
       if (prefix != null && !prefix.equals("")) {
@@ -178,7 +233,7 @@
           name, 
           namespace);
         if (needToWriteNamespace(prefix,namespace))
-          writer.writeDefaultNamespace(namespace);
+          writeNamespace(prefix,namespace,false);
       } else {
         writer.writeStartElement("",name,"");
         writer.writeDefaultNamespace("");
@@ -343,4 +398,16 @@
       throw new RuntimeException(e);
     }
   }
+
+  public StreamWriter setPrefix(
+    String prefix, 
+    String uri) {   
+      try {
+        writer.setPrefix(prefix, uri);
+      } catch (XMLStreamException e) {
+        throw new RuntimeException(e);
+      }
+      return this;
+  }
+
 }



Mime
View raw message