Project

General

Profile

1
package eu.dnetlib.miscutils.functional.xml;
2

    
3
import java.io.IOException;
4
import java.io.StringReader;
5
import java.util.function.UnaryOperator;
6
import javax.xml.parsers.DocumentBuilder;
7
import javax.xml.parsers.DocumentBuilderFactory;
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;
14

    
15
import org.apache.commons.io.output.ByteArrayOutputStream;
16
import org.apache.commons.logging.Log;
17
import org.apache.commons.logging.LogFactory;
18
import org.w3c.dom.Document;
19
import org.xml.sax.InputSource;
20
import org.xml.sax.SAXException;
21

    
22
/**
23
 * The Class XMLIndenter.
24
 */
25
public class XMLIndenter implements UnaryOperator<String> {
26

    
27
	private static final Log log = LogFactory.getLog(XMLIndenter.class);
28

    
29
//	/**
30
//	 * Static helper Apply.
31
//	 *
32
//	 * @param xml the xml
33
//	 * @return the indented xml string
34
//	 */
35
//	public static String indent(final String xml) {
36
//		return new XMLIndenter().apply(xml);
37
//	}
38
//
39
//	public static String indent(final Document document) throws Exception {
40
//		return new XMLIndenter().doIndent(document);
41
//	}
42

    
43
	public String indent(final Document document) throws Exception {
44
		return doIndent(document);
45
	}
46

    
47
	/*
48
	 * (non-Javadoc)
49
	 *
50
	 * @see eu.dnetlib.miscutils.functional.UnaryFunction#evaluate(java.lang.Object)
51
	 */
52
	@Override
53
	public String apply(final String unformattedXml) {
54
		try {
55
			return doIndent(unformattedXml);
56
		} catch (Throwable e) {
57
			return unformattedXml;
58
		}
59
	}
60

    
61
	/**
62
	 * Do indent.
63
	 *
64
	 * @param unformattedXml
65
	 *            the unformatted xml
66
	 * @return the string
67
	 * @throws IOException
68
	 *             Signals that an I/O exception has occurred.
69
	 */
70
	protected String doIndent(final String unformattedXml) throws Exception {
71
		return doIndent(parseXmlString(unformattedXml));
72
	}
73

    
74
	private String doIndent(final Document document) throws Exception {
75

    
76
		Transformer transformer = TransformerFactory.newInstance().newTransformer();
77
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
78
		transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");
79
		transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
80
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
81
		transformer.transform(new DOMSource(document), new StreamResult(out));
82

    
83
		return out.toString("utf-8");
84
	}
85

    
86
	/**
87
	 * Parses the xml string.
88
	 *
89
	 * @param in
90
	 *            the in
91
	 * @return the document
92
	 */
93
	public Document parseXmlString(final String in) {
94
		try {
95
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
96
			DocumentBuilder db = dbf.newDocumentBuilder();
97
			db.setErrorHandler(null);
98
			InputSource is = new InputSource(new StringReader(in));
99
			return db.parse(is);
100
		} catch (ParserConfigurationException e) {
101
			throw new RuntimeException(e);
102
		} catch (SAXException e) {
103
			throw new RuntimeException(e);
104
		} catch (IOException e) {
105
			throw new RuntimeException(e);
106
		}
107
	}
108

    
109

    
110
}
(7-7/7)