"indent-amount" output property ignored when using JAXP API
-----------------------------------------------------------
Key: XALANJ-2551
URL: https://issues.apache.org/jira/browse/XALANJ-2551
Project: XalanJ2
Issue Type: Bug
Security Level: No security risk; visible to anyone (Ordinary problems in
Xalan projects. Anybody can view the issue.)
Components: JAXP
Affects Versions: 2.7.1
Environment: os.name=Linux
java.vm.vendor=Sun Microsystems Inc.
java.runtime.version=1.6.0_24-b07
Reporter: Morten Hattesen
It seems that Xalan 2.7.1 ignores the
{{"{http://xml.apache.org/xslt}indent-amount"}} Transformer output property,
when using the JAXP API, and setting the output property via the
{{TransformerIdentityImpl.setOutputProperties(Properties)}} method.
The attached unit test shows, that when setting the
{{"{http://xml.apache.org/xslt}indent-amount"}} output property on an identity
transform with the source XML {{<root><child/></root>}}, the output indentation
depends on which method is used to set the output property:
*Using {{Transformer.setOutputProperty(String key, String value)}}:*
{code}
<root>
<child/>
</root>
{code}
*Using {{Transformer.setOutputProperties(Properties oformat)}}:*
{code}
<root>
<child/>
</root>
{code}
======================= JUnit 4 Unit Test ========================
package org.softee.xml.transform.xalan;
import static org.junit.Assert.*;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map.Entry;
import java.util.Properties;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
public class XalanIndentationTest {
private static final String TRANSFORMER_FACTORY_INDENT_NUMBER =
"indent-number";
private static final int INDENT_AMOUNT = 4;
private static final String INDENT_AMOUNT_PROPERTY =
"{http://xml.apache.org/xslt}indent-amount";
private static final String SOURCE_XML = "<root><child/></root>";
private static final String OUTPUT_PROPERTY_YES = "yes";
private TransformerFactory factory;
private Properties outputProperties;
@Before
public void before() throws TransformerConfigurationException {
// Force use of Xalan JAXP factory
factory =
TransformerFactory.newInstance("org.apache.xalan.processor.TransformerFactoryImpl",
Thread.currentThread().getContextClassLoader());
try {
// indent-number attribute, that works with Java SE 5/6 embedded
JAXP implementation causes exception in Xalan 2.7.1
factory.setAttribute(TRANSFORMER_FACTORY_INDENT_NUMBER,
Integer.valueOf(INDENT_AMOUNT));
} catch (IllegalArgumentException e) {
System.out.printf("++ JAXP TransformerFactory does not support '%s'
attribute: %s%n",
TRANSFORMER_FACTORY_INDENT_NUMBER, factory.getClass());
}
outputProperties = new Properties();
outputProperties.setProperty(OutputKeys.INDENT, OUTPUT_PROPERTY_YES);
outputProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION,
OUTPUT_PROPERTY_YES);
outputProperties.setProperty(INDENT_AMOUNT_PROPERTY,
String.valueOf(INDENT_AMOUNT));
}
/**
* Test using "{http://xml.apache.org/xslt}indent-amount" calling
setOutputProperty(String name, String value)
*/
@Test
public void testSetOutputProperty() throws TransformerException {
Transformer transformer = factory.newTransformer();
for (Entry<Object, Object> entry : outputProperties.entrySet()) {
transformer.setOutputProperty((String)entry.getKey(),
(String)entry.getValue());
}
transformAndValidate(transformer);
}
/**
* Test using "{http://xml.apache.org/xslt}indent-amount".
* This property is ignored by Xalan 2.7.1 when sent via
setOutputProperties(Properties oformat)
*/
@Test
//@Ignore("Fails when run against Xalan 2.7.1")
public void testSetOutputProperties() throws TransformerException {
Transformer transformer = factory.newTransformer();
transformer.setOutputProperties(outputProperties);
transformAndValidate(transformer);
}
private void transformAndValidate(Transformer transformer) throws
TransformerException {
Source source = new StreamSource(new StringReader(SOURCE_XML));
StringWriter sw = new StringWriter();
Result result = new StreamResult(sw);
transformer.transform(source, result);
String resultXml = sw.toString();
System.out.printf("Result XML (%s):%n%s%n",
transformer.getClass().getName(), resultXml);
// assert that child element has been indented using four spaces
assertTrue("child element not indented", resultXml.contains("
<child"));
}
}
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators:
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]