servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r468356 - in /incubator/servicemix/branches/servicemix-3.0/servicemix-core/src: main/java/org/apache/servicemix/jbi/jaxp/StaxSource.java test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java
Date Fri, 27 Oct 2006 12:13:34 GMT
Author: gnodet
Date: Fri Oct 27 05:13:33 2006
New Revision: 468356

URL: http://svn.apache.org/viewvc?view=rev&rev=468356
Log:
SM-722: White spaces stripped during xml processing

Modified:
    incubator/servicemix/branches/servicemix-3.0/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/StaxSource.java
    incubator/servicemix/branches/servicemix-3.0/servicemix-core/src/test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java

Modified: incubator/servicemix/branches/servicemix-3.0/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/StaxSource.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/branches/servicemix-3.0/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/StaxSource.java?view=diff&rev=468356&r1=468355&r2=468356
==============================================================================
--- incubator/servicemix/branches/servicemix-3.0/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/StaxSource.java
(original)
+++ incubator/servicemix/branches/servicemix-3.0/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/StaxSource.java
Fri Oct 27 05:13:33 2006
@@ -33,6 +33,7 @@
 import org.xml.sax.SAXNotSupportedException;
 import org.xml.sax.SAXParseException;
 import org.xml.sax.XMLReader;
+import org.xml.sax.ext.LexicalHandler;
 import org.xml.sax.helpers.AttributesImpl;
 
 public class StaxSource extends SAXSource implements XMLReader {
@@ -40,6 +41,8 @@
     private XMLStreamReader streamReader;
 
     private ContentHandler contentHandler;
+    
+    private LexicalHandler lexicalHandler;
 
     public StaxSource(XMLStreamReader streamReader) {
         this.streamReader = streamReader;
@@ -52,29 +55,49 @@
 
     protected void parse() throws SAXException {
         try {
-            contentHandler.startDocument();
             while (true) {
                 switch (streamReader.getEventType()) {
+                // Attributes are handled in START_ELEMENT
                 case XMLStreamConstants.ATTRIBUTE:
+                    break;
                 case XMLStreamConstants.CDATA:
+                {
+                    if (lexicalHandler != null) {
+                        lexicalHandler.startCDATA();
+                    }
+                    int length = streamReader.getTextLength();
+                    int start = streamReader.getTextStart();
+                    char[] chars = streamReader.getTextCharacters();
+                    contentHandler.characters(chars, start, length);
+                    if (lexicalHandler != null) {
+                        lexicalHandler.endCDATA();
+                    }
                     break;
+                }
                 case XMLStreamConstants.CHARACTERS:
-                    if (!streamReader.isWhiteSpace()) {
-                        
+                {
+                    int length = streamReader.getTextLength();
+                    int start = streamReader.getTextStart();
+                    char[] chars = streamReader.getTextCharacters();
+                    contentHandler.characters(chars, start, length);
+                    break;
+                }
+                case XMLStreamConstants.SPACE:
+                {
+                    int length = streamReader.getTextLength();
+                    int start = streamReader.getTextStart();
+                    char[] chars = streamReader.getTextCharacters();
+                    contentHandler.ignorableWhitespace(chars, start, length);
+                    break;
+                }
+                case XMLStreamConstants.COMMENT:
+                    if (lexicalHandler != null) {
                         int length = streamReader.getTextLength();
                         int start = streamReader.getTextStart();
                         char[] chars = streamReader.getTextCharacters();
-                        contentHandler.characters(chars, start, length);
-                        /*
-                        for (int textLength = streamReader.getTextLength(); textLength >
0; textLength -= chars.length) {
-                            int l = Math.min(textLength, chars.length);
-                            streamReader.getTextCharacters(0, chars, 0, l);
-                            contentHandler.characters(chars, 0, l);
-                        }
-                        */
+                        lexicalHandler.comment(chars, start, length);
                     }
                     break;
-                case XMLStreamConstants.COMMENT:
                 case XMLStreamConstants.DTD:
                     break;
                 case XMLStreamConstants.END_DOCUMENT:
@@ -95,9 +118,11 @@
                 case XMLStreamConstants.ENTITY_REFERENCE:
                 case XMLStreamConstants.NAMESPACE:
                 case XMLStreamConstants.NOTATION_DECLARATION:
+                    break;
                 case XMLStreamConstants.PROCESSING_INSTRUCTION:
-                case XMLStreamConstants.SPACE:
+                    break;
                 case XMLStreamConstants.START_DOCUMENT:
+                    contentHandler.startDocument();
                     break;
                 case XMLStreamConstants.START_ELEMENT: {
                     for (int i = 0; i < streamReader.getNamespaceCount(); i++) {
@@ -191,6 +216,11 @@
     }
 
     public void setProperty(String name, Object value) throws SAXNotRecognizedException,
SAXNotSupportedException {
+        if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) {
+            lexicalHandler = (LexicalHandler) value;
+        } else {
+            throw new SAXNotRecognizedException(name);
+        }
     }
 
     public void setEntityResolver(EntityResolver resolver) {

Modified: incubator/servicemix/branches/servicemix-3.0/servicemix-core/src/test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/branches/servicemix-3.0/servicemix-core/src/test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java?view=diff&rev=468356&r1=468355&r2=468356
==============================================================================
--- incubator/servicemix/branches/servicemix-3.0/servicemix-core/src/test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java
(original)
+++ incubator/servicemix/branches/servicemix-3.0/servicemix-core/src/test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java
Fri Oct 27 05:13:33 2006
@@ -17,9 +17,9 @@
 package org.apache.servicemix.jbi.jaxp;
 
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
 import java.io.StringWriter;
-import java.util.Arrays;
 
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.stream.XMLInputFactory;
@@ -30,31 +30,44 @@
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 
+import junit.framework.TestCase;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.servicemix.jbi.jaxp.StaxSource;
+import org.apache.servicemix.jbi.util.FileUtil;
 import org.w3c.dom.Document;
-import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
 import org.w3c.dom.Text;
 
-import junit.framework.TestCase;
-
 public class StaxSourceTest extends TestCase {
 
     private static final Log log = LogFactory.getLog(StaxSourceTest.class);
 
     public void testStaxSourceOnStream() throws Exception {
         InputStream is = getClass().getResourceAsStream("test.xml");
-        XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(is);
+        XMLInputFactory factory = XMLInputFactory.newInstance();
+        factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);
+        XMLStreamReader xsr = factory.createXMLStreamReader(is);
         StaxSource ss = new StaxSource(xsr);
         StringWriter buffer = new StringWriter();
         Transformer transformer = TransformerFactory.newInstance().newTransformer();
         transformer.transform(ss, new StreamResult(buffer));
         log.info(buffer.toString());
+        
+        /*
+         * Attribute ordering is not preserved, so we can not compare the strings
+         * 
+        is = getClass().getResourceAsStream("test.xml");
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        FileUtil.copyInputStream(is, baos);
+        compare(baos.toString().replaceAll("\r", ""), buffer.toString().replaceAll("\r",
""));
+        */
+        
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         dbf.setNamespaceAware(true);
         Document doc = dbf.newDocumentBuilder().parse(new ByteArrayInputStream(buffer.toString().getBytes()));
+        checkDomResult(doc);
+        
         StringWriter buffer2 = new StringWriter();
         transformer.transform(new DOMSource(doc), new StreamResult(buffer2));
         log.info(buffer2.toString());
@@ -68,6 +81,7 @@
         DOMResult result = new DOMResult();
         transformer.transform(ss, result);
         assertNotNull(result.getNode());
+        checkDomResult((Document) result.getNode());
     }
 
     public void testStaxToDOM() throws Exception {
@@ -77,11 +91,22 @@
         DOMSource src = new SourceTransformer().toDOMSource(ss);
         assertNotNull(src);
         assertNotNull(src.getNode());
-        NodeList nl = ((Document) src.getNode()).getDocumentElement().getElementsByTagName("long");
-        assertEquals(1, nl.getLength());
-        Text txt = (Text) nl.item(0).getFirstChild();
-        System.out.println(txt.getTextContent());
-        
+        checkDomResult((Document) src.getNode());
+    }
+    
+    protected void checkDomResult(Document doc) {
+        // Whitespace only elements must be preserved
+        NodeList l = doc.getElementsByTagName("child4");
+        assertEquals(1, l.getLength());
+        assertEquals(1, l.item(0).getChildNodes().getLength());
+        Text txt = (Text) l.item(0).getFirstChild();
+        assertEquals("   ", txt.getTextContent());
+
+        // Check long string
+        l = doc.getDocumentElement().getElementsByTagName("long");
+        assertEquals(1, l.getLength());
+        assertEquals(1, l.item(0).getChildNodes().getLength());
+        txt = (Text) l.item(0).getFirstChild();
         StringBuffer expected = new StringBuffer();
         for (int i = 0; i < 4; i++) {
             for (int j = 0; j < 10; j++) {
@@ -95,16 +120,23 @@
                 expected.append("\n");
             }
         }
-        /*
-        char[] c1 = txt.getTextContent().toCharArray();
-        char[] c2 = expected.toString().toCharArray();
+        assertEquals(expected.toString(), txt.getTextContent());
+    }
+    
+    protected void compare(String s1, String s2) {
+        char[] c1 = s1.toCharArray();
+        char[] c2 = s2.toCharArray();
         for (int i = 0; i < c1.length; i++) {
             if (c1[i] != c2[i]) {
-                fail("Expected '" + (int)c2[i] + "' but found '" + (int)c1[i] + "' at index
" + i);
+                fail("Expected '" + (int)c2[i] + "' but found '" + (int)c1[i] + "' at index
" + i + ". Expected '" + build(c2, i) + "' but found '" + build(c1, i) + "'.");
             }
         }
-        */
-        assertEquals(expected.toString(), txt.getTextContent());
+    }
+    
+    protected String build(char[] c, int i) {
+        int min = Math.max(0, i - 10);
+        int cnt = Math.min(20, c.length - min);
+        return new String(c, min, cnt);
     }
 
 }



Mime
View raw message