DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=17345>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=17345

java.lang.RuntimeException: java.lang.NullPointerException while reuse 
TransformerHandler on pipe processing

           Summary: java.lang.RuntimeException:
                    java.lang.NullPointerException while reuse
                    TransformerHandler on pipe processing
           Product: XalanJ2
           Version: 2.4
          Platform: PC
        OS/Version: Windows NT/2K
            Status: NEW
          Severity: Normal
          Priority: Other
         Component: Xalan
        AssignedTo: [EMAIL PROTECTED]
        ReportedBy: [EMAIL PROTECTED]


I've got a process chaining two transformations by piping SAX events. I created 
Xalan wrapper which has to initialize Xalan and prepare everything for 
transformation of series of XMLs. Here is main methods of the wrapper below:

public class PipeConverter
{
  private SAXTransformerFactory saxTFactory;
  private XMLReader reader;
  private Serializer serializer;

  private TransformerHandler[] tHandlers;
  private StreamSource[] tSources;

  /** Inits main components */
  public PipeConverter() throws Exception
  {
    saxTFactory = (SAXTransformerFactory)TransformerFactory.newInstance();
    reader = XMLReaderFactory.createXMLReader();
    serializer = SerializerFactory.getSerializer
                 (OutputProperties.getDefaultMethodProperties("xml"));
  }

  /** 
   * Reads and prepares content handlers.
   * @param _fileNames array of XSL file names
   */
  public void setTemplates(String[] _fileNames) throws Exception
  {
    num = _fileNames.length;
    tSources = new StreamSource[num];
    for(int i = 0; i < num; i++)
    {
      tSources[i] = new StreamSource(_fileNames[i]);
    }
    tHandlers = new TransformerHandler[num];
    for(int i = 0; i < num; i++)
    {
      tHandlers[i] = saxTFactory.newTransformerHandler(tSources[i]);
    }
    reader.setContentHandler(tHandlers[0]);
    reader.setProperty("http://xml.org/sax/properties/lexical-handler";,
                       tHandlers[0]);
    for(int i = 0; i < num-1; i++)
    {
      tHandlers[i].setResult(new SAXResult(tHandlers[i+1]));
    }
    tHandlers[num-1].setResult(new SAXResult(serializer.asContentHandler()));
  }

  /**
   * Transforms XML string.
   * @param
   */
  public String transform(String _xmlSource)
  {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try
    {
      serializer.setOutputStream(baos);
      InputSource source = new InputSource
                           (new ByteArrayInputStream(_xmlSource.getBytes()));
      reader.parse(source);
    }
    catch(Exception ex)
    {
      ex.printStackTrace();
    }
    return baos.toString();
  }

  /**
   * Test
   */
  public static void main(String[] args) throws Exception
  {
    // Read source HTML files
    FileInputStream fis = new FileInputStream("file1.html");
    byte[] content1 = new byte[fis.available()];
    fis.read(content);
    String source1 = new String(content);

    // ...Same steps to read file content omitted...
    String source2 = new String(content);

    // Prepare array of templates
    String[] templates = new String[2];
    templates[0] = "template1.xsl";
    templates[1] = "template2.xsl";

    // Create and init converter
    PipeConverter conv = new PipeConverter();
    conv.setTemplates(templates);

    // Process
    conv.transform(source1);
    conv.transform(source2); // <-- NullPointerException
  }
}

First transformation executed successfully.
Second transformation threw exception:

java.lang.RuntimeException: java.lang.NullPointerException
        at org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:524)
        at org.apache.crimson.parser.Parser2.parse(Parser2.java:305)
        at org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:442)
        at epost.html.trax.PipeConverter.transform(PipeConverter.java:123)
        at epost.html.trax.PipeConverter.main(PipeConverter.java:181)

If I recreate transformer handlers for the second transformation then it gets 
executed. So, I have to obtain new handlers from the SAX factory: "tHandlers[i] 
= saxTFactory.newTransformerHandler(tSources[i]);" But this is what I am trying 
to avoid in order to save time on transformation process.

Why do transformation handler cannot be reused? It seems to me like a bug in a 
Xalan's SAX parser.

Reply via email to