JDK-6337981 : indent-number doesn't have any effect when a StreamResult uses an OutputStream
  • Type: Bug
  • Component: xml
  • Sub-Component: org.xml.sax
  • Affected Version: 5.0
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2005-10-18
  • Updated: 2012-04-25
Description
FULL PRODUCT VERSION :
java version "1.5.0_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_01-b08)
Java HotSpot(TM) Client VM (build 1.5.0_01-b08, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
It is not possible to use a TransformerHandler with an OutputStream based StreamResult as a pretty-printer. The indentation amount is lost.

The method com.sun.org.apache.xml.internal.serializer.ToStream.setOutputStream  will reset the m_indentAmount field, thus invalidating the setting inposed when the serializer was first created.

The method ToStream.setOutputStream will call init, typically with a default property setting. init will call another init that will attempt to set the indent amount from the properites. The indent amount that was set when the ToStream was first created is then lost.

Using a writer, the setOutputStream is not called on the new ToStream instance but setWriter is. setWriter will not call init.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the supplied Java class. The output is self explanatory.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The indent-number should work regardless if an OutputStream or Writer based StreamResult is used.
ACTUAL -
The output is not indented when using an OutputStream based StreamResult. The output is correctly indented when using a Writer based StreamResult.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.sax.*;
import javax.xml.transform.stream.*;
import org.xml.sax.*;
import org.xml.sax.helpers.XMLReaderFactory;

public class TestIndent
{
	static String testDoc = "<?xml version=\"1.0\"?><project><target/></project>";

	public static void main(String[] argv)
	throws Exception
	{
		SAXTransformerFactory stf = (SAXTransformerFactory)TransformerFactory.newInstance();
		stf.setAttribute("indent-number", 4);
		TransformerHandler serializer = stf.newTransformerHandler();
		Transformer t = serializer.getTransformer();
		t.setOutputProperty(OutputKeys.METHOD, "xml");
		t.setOutputProperty(OutputKeys.INDENT, "yes");

		ByteArrayOutputStream of = new ByteArrayOutputStream();
		StreamResult out = new StreamResult(of);
		serializer.setResult(out);
		
		XMLReader rdr = XMLReaderFactory.createXMLReader();
		rdr.setContentHandler(serializer);
		rdr.parse(new InputSource(new StringReader(testDoc)));
		
		System.out.println(new String(of.toByteArray()));

		// Do it again, this time with a Writer.
		//
		StringWriter wf = new StringWriter();
		out = new StreamResult(wf);
		serializer.setResult(out);

		rdr.parse(new InputSource(new StringReader(testDoc)));
		System.out.println(wf.toString());
	}
}

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

CUSTOMER SUBMITTED WORKAROUND :
Wrap your OutputStream in a Writer (using correct encoding).

Comments
EVALUATION bug is reproducibile and is being accepted.
10-12-2005