summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/util/XmlHelper.java
blob: 90d282bbb5ff5454df4807e57cfd70cacee407c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package org.openslx.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XmlHelper
{
	private final static Logger LOGGER = LogManager.getLogger( XmlHelper.class );

	// TODO check thread-safety
	private static final XPath XPath = XPathFactory.newInstance().newXPath();
	private static DocumentBuilder dBuilder;
	static {
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		dbFactory.setNamespaceAware( true );
		dbFactory.setIgnoringComments( true );
		try {
			dBuilder = dbFactory.newDocumentBuilder();
		} catch ( ParserConfigurationException e ) {
			LOGGER.error( "Failed to initalize DOM parser with default configurations." );
		}
	}

	public static String globalXPathToLocalXPath( String xPath )
	{
		final StringBuilder exprBuilder = new StringBuilder();
		final String[] elements = xPath.split( "/" );

		for ( final String element : elements ) {
			if ( !element.isEmpty() ) {
				final Pattern arraySpecifierRegex = Pattern.compile( "^(.*)\\[(.*)\\]$" );
				final Matcher arraySpecifierMatcher = arraySpecifierRegex.matcher( element );
				final String elementName;
				final String elementSpecifier;

				if ( arraySpecifierMatcher.find() ) {
					elementName = arraySpecifierMatcher.group( 1 );
					elementSpecifier = arraySpecifierMatcher.group( 2 );
				} else {
					elementName = element;
					elementSpecifier = null;
				}

				if ( !elementName.startsWith( "@" ) && !elementName.equals( "*" ) ) {
					exprBuilder.append( "/*[local-name()='" + elementName + "']" );

				} else {
					exprBuilder.append( "/" + elementName );
				}

				if ( elementSpecifier != null && !elementSpecifier.isEmpty() ) {
					exprBuilder.append( "[" + elementSpecifier + "]" );
				}
			}
		}

		return exprBuilder.toString();
	}

	public static XPathExpression compileXPath( String xPath ) throws XPathExpressionException
	{
		final String localXPath = XmlHelper.globalXPathToLocalXPath( xPath );
		return XPath.compile( localXPath );
	}

	public static Document parseDocumentFromStream( InputStream is )
	{
		Document doc = null;

		// read document from stream
		try {
			doc = dBuilder.parse( is );
		} catch ( SAXException | IOException e ) {
			doc = null;
		}

		// normalize parsed document
		if ( doc != null ) {
			doc.getDocumentElement().normalize();
		}

		return doc;
	}

	public static Document removeFormattingNodes( Document doc )
	{
		NodeList empty;
		try {
			empty = (NodeList)XPath.evaluate( "//text()[normalize-space(.) = '']",
					doc, XPathConstants.NODESET );
		} catch ( XPathExpressionException e ) {
			LOGGER.error( "Bad XPath expression to find all empty text nodes." );
			return null;
		}

		for ( int i = 0; i < empty.getLength(); i++ ) {
			Node node = empty.item( i );
			node.getParentNode().removeChild( node );
		}
		return doc;
	}

	public static String getUnformattedXml( InputStream is )
	{
		// prune empty text nodes, essentially removing all formatting
		Document doc = parseDocumentFromStream( is );
		return getXmlFromDocument( removeFormattingNodes( doc ), false );
	}

	public static String getFormattedXml( InputStream is )
	{
		Document doc = parseDocumentFromStream( is );
		return getXmlFromDocument( doc, true );
	}

	public static String getXmlFromDocument( Document doc, boolean humanReadable )
	{
		try {
			StringWriter writer = new StringWriter();
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer transformer = tf.newTransformer();
			transformer.setOutputProperty( OutputKeys.OMIT_XML_DECLARATION, "no" );
			transformer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
			transformer.setOutputProperty( OutputKeys.METHOD, "xml" );
			if ( humanReadable ) {
				transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
				transformer.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount", "2" );
			}
			transformer.transform( new DOMSource( doc ), new StreamResult( writer ) );
			return writer.toString();
		} catch ( Exception ex ) {
			LOGGER.error( "Failed to transform XML to String: ", ex );
			return null;
		}
	}

	public static Element getOrCreateElement( Document doc, Element parent, String nsUri, String nsName,
			String name, String attrName, String attrValue )
	{
		final NodeList childList;
		if ( nsUri == null ) {
			childList = parent.getElementsByTagName( name );
		} else {
			childList = parent.getElementsByTagNameNS( nsUri, name );
		}
		Element element = null;
		outer: for ( int i = 0; i < childList.getLength(); ++i ) {
			Node n = childList.item( i );
			if ( n.getNodeType() != Node.ELEMENT_NODE )
				continue;
			if ( attrName != null && attrValue != null ) {
				for ( int ai = 0; ai < n.getAttributes().getLength(); ++ai ) {
					Node attr = n.getAttributes().item( ai );
					if ( attr.getNodeType() != Attr.ELEMENT_NODE )
						continue;
					Attr a = (Attr)attr;
					if ( !attrName.equals( a.getLocalName() ) || !attrValue.equals( a.getValue() ) )
						continue;
					element = (Element)n;
					break outer;
				}
			} else {
				element = (Element)n;
				break;
			}
		}
		if ( element == null ) {
			// Need a new <qemu:device alias="hostdev0">
			if ( nsUri == null || nsName == null ) {
				element = doc.createElement( name );
			} else {
				element = doc.createElementNS( nsUri, name );
				element.setPrefix( nsName );
			}
			if ( attrName != null && attrValue != null ) {
				element.setAttribute( attrName, attrValue );
			}
			parent.appendChild( element );
		}
		return element;
	}

}