You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2011/10/20 22:38:59 UTC

svn commit: r1187040 - in /cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils: StaxSource.java StaxUtils.java StreamWriterContentHandler.java

Author: dkulp
Date: Thu Oct 20 20:38:58 2011
New Revision: 1187040

URL: http://svn.apache.org/viewvc?rev=1187040&view=rev
Log:
Add lexical handler to SAXSource's to get comments

Modified:
    cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxSource.java
    cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
    cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StreamWriterContentHandler.java

Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxSource.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxSource.java?rev=1187040&r1=1187039&r2=1187040&view=diff
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxSource.java (original)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxSource.java Thu Oct 20 20:38:58 2011
@@ -103,7 +103,7 @@ public class StaxSource extends SAXSourc
                         int start = streamReader.getTextStart();
                         char[] chars = streamReader.getTextCharacters();
                         lexicalHandler.comment(chars, start, length);
-                    }
+                    } 
                     break;
                 case XMLStreamConstants.DTD:
                     break;
@@ -253,6 +253,10 @@ public class StaxSource extends SAXSourc
 
     public void setContentHandler(ContentHandler handler) {
         this.contentHandler = handler;
+        if (handler instanceof LexicalHandler
+            && lexicalHandler == null) {
+            lexicalHandler = (LexicalHandler)handler;
+        }
     }
 
     public ContentHandler getContentHandler() {

Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java?rev=1187040&r1=1187039&r2=1187040&view=diff
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java (original)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java Thu Oct 20 20:38:58 2011
@@ -430,6 +430,11 @@ public final class StaxUtils {
                         } catch (Throwable t) {
                             //ignore
                         }
+                        try {
+                            reader.setProperty("http://xml.org/sax/properties/lexical-handler", ch);
+                        } catch (Throwable t) {
+                            //ignore
+                        }
                         reader.parse(((SAXSource)source).getInputSource());
                         return;
                     } catch (Exception e) {

Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StreamWriterContentHandler.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StreamWriterContentHandler.java?rev=1187040&r1=1187039&r2=1187040&view=diff
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StreamWriterContentHandler.java (original)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StreamWriterContentHandler.java Thu Oct 20 20:38:58 2011
@@ -29,16 +29,18 @@ import org.xml.sax.Attributes;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
+import org.xml.sax.ext.LexicalHandler;
 
 import org.apache.cxf.common.util.StringUtils;
 
 /**
  * 
  */
-public class StreamWriterContentHandler implements ContentHandler {
+public class StreamWriterContentHandler implements ContentHandler, LexicalHandler {
     
     XMLStreamWriter writer;
     Map<String, String> mapping = new LinkedHashMap<String, String>();
+    boolean inCDATA;
     
     public StreamWriterContentHandler(XMLStreamWriter w) {
         writer = w;
@@ -72,7 +74,11 @@ public class StreamWriterContentHandler 
      */
     public void characters(char ch[], int start, int length) throws SAXException {
         try {
-            writer.writeCharacters(ch, start, length);
+            if (inCDATA) {
+                writer.writeCData(new String(ch, start, length));
+            } else {
+                writer.writeCharacters(ch, start, length);
+            }
         } catch (XMLStreamException e) {
             throw new SAXException(e);
         }
@@ -87,6 +93,11 @@ public class StreamWriterContentHandler 
      * @throws SAXException
      */
     public void ignorableWhitespace(char ch[], int start, int length) throws SAXException {
+        try {
+            writer.writeCharacters(ch, start, length);
+        } catch (XMLStreamException e) {
+            throw new SAXException(e);
+        }
     }
 
     /**
@@ -242,7 +253,27 @@ public class StreamWriterContentHandler 
             throw new SAXException(e);
         }
     }
-    
-    
 
+    public void startDTD(String name, String publicId, String systemId) throws SAXException {
+    }
+    public void endDTD() throws SAXException {
+    }
+    public void startEntity(String name) throws SAXException {
+    }
+    public void endEntity(String name) throws SAXException {
+    }
+    public void startCDATA() throws SAXException {
+        inCDATA = true;
+    }
+    public void endCDATA() throws SAXException {
+        inCDATA = false;
+    }
+
+    public void comment(char[] ch, int start, int length) throws SAXException {
+        try {
+            writer.writeComment(new String(ch, start, length));
+        } catch (XMLStreamException e) {
+            throw new SAXException(e);
+        }
+    }
 }