forked from uber/NullAway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XMLUtil.java
183 lines (168 loc) · 7.28 KB
/
XMLUtil.java
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
/*
* Copyright (c) 2022 Uber Technologies, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.uber.nullaway.fixserialization;
import java.io.File;
import javax.annotation.Nullable;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
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.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.jetbrains.annotations.Contract;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
/** Helper for class for parsing/writing xml files. */
public class XMLUtil {
/**
* Helper method for reading attributes of node located at /key_1/key_2/.../key_n (in the form of
* {@code Xpath} query) from a {@link Document}.
*
* @param doc XML object to read values from.
* @param key Key to locate the value, can be nested in the form of {@code Xpath} query (e.g.
* /key1/key2:.../key_n).
* @param klass Class type of the value in doc.
* @return The value in the specified keychain cast to the class type given in parameter.
*/
public static <T> DefaultXMLValueProvider<T> getValueFromAttribute(
Document doc, String key, String attr, Class<T> klass) {
try {
XPath xPath = XPathFactory.newInstance().newXPath();
Node node = (Node) xPath.compile(key).evaluate(doc, XPathConstants.NODE);
if (node != null && node.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) node;
return new DefaultXMLValueProvider<>(eElement.getAttribute(attr), klass);
}
} catch (XPathExpressionException ignored) {
return new DefaultXMLValueProvider<>(null, klass);
}
return new DefaultXMLValueProvider<>(null, klass);
}
/**
* Helper method for reading value of a node located at /key_1/key_2/.../key_n (in the form of
* {@code Xpath} query) from a {@link Document}.
*
* @param doc XML object to read values from.
* @param key Key to locate the value, can be nested in the form of {@code Xpath} query (e.g.
* /key1/key2/.../key_n).
* @param klass Class type of the value in doc.
* @return The value in the specified keychain cast to the class type given in parameter.
*/
public static <T> DefaultXMLValueProvider<T> getValueFromTag(
Document doc, String key, Class<T> klass) {
try {
XPath xPath = XPathFactory.newInstance().newXPath();
Node node = (Node) xPath.compile(key).evaluate(doc, XPathConstants.NODE);
if (node != null && node.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) node;
return new DefaultXMLValueProvider<>(eElement.getTextContent(), klass);
}
} catch (XPathExpressionException ignored) {
return new DefaultXMLValueProvider<>(null, klass);
}
return new DefaultXMLValueProvider<>(null, klass);
}
/**
* Writes the {@link FixSerializationConfig} in {@code XML} format.
*
* @param config Config file to write.
* @param path Path to write the config at.
*/
public static void writeInXMLFormat(FixSerializationConfig config, String path) {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
// Root
Element rootElement = doc.createElement("serialization");
doc.appendChild(rootElement);
// Suggest
Element suggestElement = doc.createElement("suggest");
suggestElement.setAttribute("active", String.valueOf(config.suggestEnabled));
suggestElement.setAttribute("enclosing", String.valueOf(config.suggestEnclosing));
rootElement.appendChild(suggestElement);
// Field Initialization
Element fieldInitInfoEnabled = doc.createElement("fieldInitInfo");
fieldInitInfoEnabled.setAttribute("active", String.valueOf(config.fieldInitInfoEnabled));
rootElement.appendChild(fieldInitInfoEnabled);
// Method Parameter Protection Test
Element paramTestElement = doc.createElement("paramTest");
paramTestElement.setAttribute(
"active", String.valueOf(config.methodParamProtectionTestEnabled));
paramTestElement.setAttribute("index", String.valueOf(config.paramTestIndex));
rootElement.appendChild(paramTestElement);
// Output dir
Element outputDir = doc.createElement("path");
outputDir.setTextContent(config.outputDirectory);
rootElement.appendChild(outputDir);
// Writings
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(path));
transformer.transform(source, result);
} catch (ParserConfigurationException | TransformerException e) {
throw new RuntimeException("Error happened in writing config.", e);
}
}
/** Helper class for setting default values when the key is not found. */
static class DefaultXMLValueProvider<T> {
@Nullable final Object value;
final Class<T> klass;
DefaultXMLValueProvider(@Nullable Object value, Class<T> klass) {
this.klass = klass;
if (value == null) {
this.value = null;
} else {
String content = value.toString();
switch (klass.getSimpleName()) {
case "Integer":
this.value = Integer.valueOf(content);
break;
case "Boolean":
this.value = Boolean.valueOf(content);
break;
case "String":
this.value = String.valueOf(content);
break;
default:
throw new IllegalArgumentException(
"Cannot extract values of type: "
+ klass
+ ", only Double|Boolean|String accepted.");
}
}
}
@Contract("!null -> !null")
@Nullable
T orElse(@Nullable T other) {
return value == null ? other : klass.cast(this.value);
}
}
}