Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6354965

SAXResult does not support CDATA

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 5.0
    • xml

      FULL PRODUCT VERSION :
      java version "1.4.2_06"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_06-b03)
      Java HotSpot(TM) Client VM (build 1.4.2_06-b03, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]
      SunOS sascs5 5.7 Generic_106541-20 sun4u sparc SUNW,Ultra-Enterprise

      A DESCRIPTION OF THE PROBLEM :
      I enclose a simple demonstration which shows inconsistent behaviour when using SAXResult compared to using the default SAX Reader in JAXP.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the program Generator which reads the classes.xml file describing prototype data for generating a Java class definition. Applying the classes.xsl stylesheet to this data generates an intermediary XML document with separate elements for each Java class file and a CDATA section to protect the Java code from interpretation by an XML parser.

      The program transofrms to an intermediary byte buffer and then reads the data from this buffer using the default XML reader. The program then applies the same transformation and writes the output to an XMLResult object using the same XML content handler as in the previous step.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The generated output listing the Java class filenames and contents should be identical.
      ACTUAL -
      The output from the SAXResult includes the <![CDATA[ ... ]]> characters which should have been recognised parsed as they are standard XML content.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.*;

      import javax.xml.parsers.*;
      import javax.xml.transform.*;
      import javax.xml.transform.sax.*;
      import javax.xml.transform.stream.*;
      import org.xml.sax.*;
      import org.xml.sax.helpers.*;

      public final class Generator extends DefaultHandler {
          
          public void transform(String xml, String xsl, PrintStream out)
          throws Exception
          {
              System.out.println("@Separate transform and parse results@");
      TransformerFactory tf = TransformerFactory.newInstance();
      Transformer t1 = tf.newTransformer(new StreamSource(xsl));
      InputSource input1 = new InputSource(new FileReader(xml));
      ByteArrayOutputStream buffer = new ByteArrayOutputStream();
      t1.transform(new SAXSource(input1), new StreamResult(buffer));
      System.out.println("\n@Contents of intermediary buffer@");
      buffer.writeTo(System.out);
      System.out.println("\n\n@Parsed using default SAX Reader@");
      SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
      XMLReader reader = parser.getXMLReader();
      reader.setContentHandler(this);
      reader.parse(new InputSource(new ByteArrayInputStream(buffer.toByteArray())));
              System.out.println("\n@Transformed and parsed using SAX Result@");
      Transformer t2 = tf.newTransformer(new StreamSource(xsl));
      InputSource input2 = new InputSource(new FileReader(xml));
      t2.transform(new SAXSource(input2), new SAXResult(this));
          }
          
          String filename;

          public void characters(char[] ch, int start, int length)
          {
              if (filename != null)
                  System.out.print(new String(ch,start,length));
          }


          public void endElement(String uri, String localName, String qName)
          {
              if (!"class-definition".equals(qName)) return;
              if (filename != null)
                  System.out.println();
              filename = null;
          }


          public void startElement(String uri, String localName, String qName, Attributes attributes)
          {
              if (!"class-definition".equals(qName)) return;
              filename = attributes.getValue("file");
              System.out.println("*"+filename+"*");
          }
          
          public static void main(String[] args) {
              try {
      Generator generator = new Generator();
      generator.transform("classes.xml","classes.xsl",System.out);
              }
              catch (Exception ex) {
                  ex.printStackTrace();
              }
          }
      }

      --classes.xml

      <?xml version='1.0' ?>
      <classes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="./classes.xsd">
        <class name="Book"/>
        <class name="DVD"/>
      </classes>

      --classes.xsl
      <?xml version="1.0"?>
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="/">
        <classes>
         <xsl:apply-templates/>
        </classes>
      </xsl:template>
      <xsl:template match="class">
        <class-definition>
         <xsl:attribute name="file"><xsl:value-of select="concat(@name,'.java')"/></xsl:attribute>
      <xsl:text>public class </xsl:text>
      <xsl:value-of select="@name"/>
      <xsl:text disable-output-escaping="yes"><![CDATA[ {} ]]></xsl:text>
        </class-definition>
      </xsl:template>
      </xsl:stylesheet>


      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Using the temporary buffer as shown in the example code.

            spericas Santiago Pericasgeertsen
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: