Project

General

Profile

« Previous | Next » 

Revision 43840

changed internal xerces formatter with a transformer Instance

View differences:

XMLIndenter.java
2 2

  
3 3
import java.io.IOException;
4 4
import java.io.StringReader;
5
import java.io.StringWriter;
6
import java.io.Writer;
7 5
import java.util.function.UnaryOperator;
8 6
import javax.xml.parsers.DocumentBuilder;
9 7
import javax.xml.parsers.DocumentBuilderFactory;
10 8
import javax.xml.parsers.ParserConfigurationException;
9
import javax.xml.transform.OutputKeys;
10
import javax.xml.transform.Transformer;
11
import javax.xml.transform.TransformerFactory;
12
import javax.xml.transform.dom.DOMSource;
13
import javax.xml.transform.stream.StreamResult;
11 14

  
12
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
13
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
15
import org.apache.commons.io.output.ByteArrayOutputStream;
14 16
import org.apache.commons.logging.Log;
15 17
import org.apache.commons.logging.LogFactory;
16 18
import org.w3c.dom.Document;
......
18 20
import org.xml.sax.SAXException;
19 21

  
20 22

  
23

  
21 24
/**
22 25
 * The Class XMLIndenter.
23 26
 */
......
35 38
		return new XMLIndenter().apply(xml);
36 39
	}
37 40

  
38
	public static String indent(final Document document) throws IOException {
41
	public static String indent(final Document document) throws Exception {
39 42
		return new XMLIndenter().doIndent(document);
40 43
	}
41 44

  
......
48 51
	public String apply(final String unformattedXml) {
49 52
		try {
50 53
			return doIndent(unformattedXml);
51
		} catch (IOException e) {
54
		} catch (Throwable e) {
52 55
			return unformattedXml;
53 56
		}
54 57
	}
......
62 65
	 * @throws IOException
63 66
	 *             Signals that an I/O exception has occurred.
64 67
	 */
65
	protected String doIndent(final String unformattedXml) throws IOException {
68
	protected String doIndent(final String unformattedXml) throws Exception {
66 69
		return doIndent(parseXmlString(unformattedXml));
67 70
	}
68 71

  
69
	private String doIndent(final Document document) throws IOException {
72
	private String doIndent(final Document document) throws Exception {
70 73

  
71
		OutputFormat format = new OutputFormat(document);
72
		format.setIndenting(true);
73
		format.setIndent(2);
74
		Writer out = new StringWriter();
75
		XMLSerializer serializer = new XMLSerializer(out, format);
76
		serializer.serialize(document);
74
		TransformerFactory tf = TransformerFactory.newInstance();
75
		Transformer transformer = tf.newTransformer();
76
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
77
		transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");
78
		transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
79
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
80
		transformer.transform(new DOMSource(document), new StreamResult(out));
77 81

  
78
		return out.toString();
82
		return out.toString("utf-8");
79 83
	}
80 84

  
81 85
	/**

Also available in: Unified diff