|
1 | | -/* Copyright 2018 Philipp Salvisberg <philipp.salvisberg@trivadis.com> |
| 1 | +/* |
| 2 | + * Copyright 2018 Philipp Salvisberg <philipp.salvisberg@trivadis.com> |
2 | 3 | * |
3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 5 | * you may not use this file except in compliance with the License. |
|
12 | 13 | * See the License for the specific language governing permissions and |
13 | 14 | * limitations under the License. |
14 | 15 | */ |
15 | | -package org.utplsql.sqldev.model |
| 16 | +package org.utplsql.sqldev.model; |
16 | 17 |
|
17 | | -import java.io.StringWriter |
18 | | -import javax.xml.transform.OutputKeys |
19 | | -import javax.xml.transform.TransformerFactory |
20 | | -import javax.xml.transform.dom.DOMSource |
21 | | -import javax.xml.transform.stream.StreamResult |
22 | | -import javax.xml.xpath.XPathConstants |
23 | | -import javax.xml.xpath.XPathFactory |
24 | | -import org.w3c.dom.Element |
25 | | -import org.w3c.dom.Node |
26 | | -import org.w3c.dom.NodeList |
| 18 | +import java.io.StringWriter; |
| 19 | +import java.util.logging.Logger; |
27 | 20 |
|
28 | | -class XMLTools { |
29 | | - val xpathFactory = XPathFactory.newInstance() |
30 | | - val xpath = xpathFactory.newXPath() |
31 | | - |
32 | | - def getNodeList(Node doc, String xpathString) { |
33 | | - val expr = xpath.compile(xpathString); |
34 | | - val NodeList nodeList = expr.evaluate(doc, XPathConstants.NODESET) as NodeList |
35 | | - return nodeList |
36 | | - } |
37 | | - |
38 | | - def getNode(Node doc, String xpathString) { |
39 | | - val expr = xpath.compile(xpathString); |
40 | | - val Node node = expr.evaluate(doc, XPathConstants.NODE) as Node |
41 | | - return node |
42 | | - } |
43 | | - |
44 | | - def void trimWhitespace(Node node) { |
45 | | - val children = node.childNodes |
46 | | - for (i : 0 ..< children.length) { |
47 | | - val child = children.item(i) |
48 | | - if (child.nodeType == Node.TEXT_NODE) { |
49 | | - child.textContent = child.textContent.trim |
50 | | - } |
51 | | - trimWhitespace(child); |
52 | | - } |
53 | | - } |
54 | | - |
55 | | - def nodeToString(Node node, String cdataSectionElements) { |
56 | | - node.trimWhitespace |
57 | | - val writer = new StringWriter() |
58 | | - val factory = TransformerFactory.newInstance().newTransformer() |
59 | | - factory.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes") |
60 | | - factory.setOutputProperty(OutputKeys.INDENT, "yes") |
61 | | - factory.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3"); |
62 | | - factory.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, cdataSectionElements) |
63 | | - factory.transform(new DOMSource(node), new StreamResult(writer)) |
64 | | - val result = writer.toString() |
65 | | - val fixedResult = result.replaceAll('''<!\[CDATA\[\s*\]\]>''',"") |
66 | | - return fixedResult |
67 | | - } |
68 | | - |
69 | | - def getAttributeValue(Node node, String namedItem) { |
70 | | - var String value = null |
71 | | - if (node instanceof Element) { |
72 | | - value = node.attributes?.getNamedItem(namedItem)?.nodeValue; |
73 | | - } |
74 | | - return value |
75 | | - } |
76 | | - |
77 | | - def getElementValue(Node node, String tagName) { |
78 | | - return getElementNode(node, tagName)?.textContent |
79 | | - } |
| 21 | +import javax.xml.XMLConstants; |
| 22 | +import javax.xml.transform.OutputKeys; |
| 23 | +import javax.xml.transform.Transformer; |
| 24 | +import javax.xml.transform.TransformerException; |
| 25 | +import javax.xml.transform.TransformerFactory; |
| 26 | +import javax.xml.transform.dom.DOMSource; |
| 27 | +import javax.xml.transform.stream.StreamResult; |
| 28 | +import javax.xml.xpath.XPath; |
| 29 | +import javax.xml.xpath.XPathConstants; |
| 30 | +import javax.xml.xpath.XPathExpression; |
| 31 | +import javax.xml.xpath.XPathExpressionException; |
| 32 | +import javax.xml.xpath.XPathFactory; |
80 | 33 |
|
81 | | - def getElementNode(Node node, String tagName) { |
82 | | - var Node resultNode = null |
83 | | - if (node instanceof Element) { |
84 | | - resultNode = node.getElementsByTagName(tagName)?.item(0) |
85 | | - } |
86 | | - return resultNode |
87 | | - } |
| 34 | +import org.utplsql.sqldev.exception.GenericRuntimeException; |
| 35 | +import org.w3c.dom.Element; |
| 36 | +import org.w3c.dom.NamedNodeMap; |
| 37 | +import org.w3c.dom.Node; |
| 38 | +import org.w3c.dom.NodeList; |
| 39 | + |
| 40 | +public class XMLTools { |
| 41 | + private static final Logger logger = Logger.getLogger(XMLTools.class.getName()); |
| 42 | + private final XPathFactory xpathFactory = XPathFactory.newInstance(); |
| 43 | + private final XPath xpath = xpathFactory.newXPath(); |
| 44 | + |
| 45 | + public NodeList getNodeList(final Node doc, final String xpathString) { |
| 46 | + try { |
| 47 | + final XPathExpression expr = xpath.compile(xpathString); |
| 48 | + return ((NodeList) expr.evaluate(doc, XPathConstants.NODESET)); |
| 49 | + } catch (XPathExpressionException e) { |
| 50 | + final String msg = "XPathExpressionException for " + xpathString + " due to " + e.getMessage(); |
| 51 | + logger.severe(() -> msg); |
| 52 | + throw new GenericRuntimeException(msg, e); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public Node getNode(final Node doc, final String xpathString) { |
| 57 | + try { |
| 58 | + final XPathExpression expr = xpath.compile(xpathString); |
| 59 | + return ((Node) expr.evaluate(doc, XPathConstants.NODE)); |
| 60 | + } catch (XPathExpressionException e) { |
| 61 | + final String msg = "XPathExpressionException for " + xpathString + " due to " + e.getMessage(); |
| 62 | + logger.severe(() -> msg); |
| 63 | + throw new GenericRuntimeException(msg, e); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public void trimWhitespace(final Node node) { |
| 68 | + final NodeList children = node.getChildNodes(); |
| 69 | + for (int i = 0; i < children.getLength(); i++) { |
| 70 | + final Node child = children.item(i); |
| 71 | + if (child.getNodeType() == Node.TEXT_NODE) { |
| 72 | + child.setTextContent(child.getTextContent().trim()); |
| 73 | + } |
| 74 | + trimWhitespace(child); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public String nodeToString(final Node node, final String cdataSectionElements) { |
| 79 | + try { |
| 80 | + trimWhitespace(node); |
| 81 | + final StringWriter writer = new StringWriter(); |
| 82 | + TransformerFactory factory = TransformerFactory.newInstance(); |
| 83 | + factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); |
| 84 | + final Transformer transformer = factory.newTransformer(); |
| 85 | + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 86 | + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
| 87 | + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3"); |
| 88 | + transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, cdataSectionElements); |
| 89 | + transformer.transform( new DOMSource(node), new StreamResult(writer)); |
| 90 | + final String result = writer.toString(); |
| 91 | + return result.replaceAll("<!\\[CDATA\\[\\s*\\]\\]>", ""); |
| 92 | + } catch (TransformerException e) { |
| 93 | + final String msg = "TransformerException for " + cdataSectionElements + " due to " + e.getMessage(); |
| 94 | + logger.severe(() -> msg); |
| 95 | + throw new GenericRuntimeException(msg, e); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public String getAttributeValue(final Node node, final String namedItem) { |
| 100 | + String value = null; |
| 101 | + if (node instanceof Element) { |
| 102 | + final NamedNodeMap attributes = ((Element) node).getAttributes(); |
| 103 | + if (attributes != null) { |
| 104 | + final Node item = attributes.getNamedItem(namedItem); |
| 105 | + if (item != null) { |
| 106 | + value = item.getNodeValue(); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + return value; |
| 111 | + } |
| 112 | + |
| 113 | + public String getElementValue(final Node node, final String tagName) { |
| 114 | + String value = null; |
| 115 | + final Node item = getElementNode(node, tagName); |
| 116 | + if (item != null) { |
| 117 | + value = item.getTextContent(); |
| 118 | + } |
| 119 | + return value; |
| 120 | + } |
| 121 | + |
| 122 | + public Node getElementNode(final Node node, final String tagName) { |
| 123 | + Node resultNode = null; |
| 124 | + if (node instanceof Element) { |
| 125 | + NodeList list = ((Element) node).getElementsByTagName(tagName); |
| 126 | + if (list != null && list.getLength() > 0) { |
| 127 | + resultNode = list.item(0); |
| 128 | + } |
| 129 | + } |
| 130 | + return resultNode; |
| 131 | + } |
88 | 132 | } |
0 commit comments