United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6337981 : indent-number doesn't have any effect when a StreamResult uses an OutputStream

Details
Type:
Bug
Submit Date:
2005-10-18
Status:
Open
Updated Date:
2012-04-25
Project Name:
JDK
Resolved Date:
Component:
xml
OS:
windows_xp
Sub-Component:
org.xml.sax
CPU:
x86
Priority:
P4
Resolution:
Unresolved
Affected Versions:
5.0
Targeted Versions:

Related Reports

Sub Tasks

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.
                                     
2005-12-10



Hardware and Software, Engineered to Work Together