-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path180.java
More file actions
226 lines (196 loc) · 9.53 KB
/
Copy path180.java
File metadata and controls
226 lines (196 loc) · 9.53 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package io.cloudslang.content.xml.services;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import io.cloudslang.content.xml.entities.inputs.ConvertXmlToJsonInputs;
import io.cloudslang.content.xml.utils.XmlUtils;
import org.apache.commons.lang3.StringUtils;
import org.jdom2.Attribute;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.Namespace;
import org.jdom2.input.SAXBuilder;
import org.jetbrains.annotations.NotNull;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static io.cloudslang.content.xml.utils.Constants.Defaults;
import static io.cloudslang.content.xml.utils.Constants.Defaults.PREFIX_DELIMITER;
import static io.cloudslang.content.xml.utils.Constants.JSON_ATTRIBUTE_PREFIX;
import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
/**
* Created by ursan on 8/2/2016.
*/
public class ConvertXmlToJsonService {
private final StringBuilder namespacesPrefixes;
private final StringBuilder namespacesUris;
public ConvertXmlToJsonService() {
namespacesPrefixes = new StringBuilder();
namespacesUris = new StringBuilder();
}
public String convertToJsonString(final ConvertXmlToJsonInputs inputs) throws JDOMException, IOException, SAXException {
if (StringUtils.isBlank(inputs.getXml())) {
return EMPTY;
}
final InputSource inputSource = new InputSource(new StringReader(inputs.getXml()));
final SAXBuilder builder = new SAXBuilder();
XmlUtils.setFeatures(builder, inputs.getParsingFeatures());
final Element root = builder.build(inputSource).getRootElement();
final List<Element> xmlElements = Collections.singletonList(root);
if (root.getChildren().isEmpty() && !inputs.getIncludeAttributes()) {
return prettyPrint(addJsonObjectsAndPrimitives(new JsonObject(), xmlElements, inputs.getIncludeAttributes(), inputs.getTextElementsName()),
inputs.getPrettyPrint());
}
final JsonObject jsonObjectValue = convertXmlElementsToJsonObject(xmlElements, inputs.getIncludeAttributes(), inputs.getTextElementsName());
final JsonObject jsonObject = getJsonObjectWithRootElement(root, jsonObjectValue, inputs.getIncludeRootElement());
return prettyPrint(jsonObject, inputs.getPrettyPrint());
}
private JsonObject getJsonObjectWithRootElement(final Element rootElement, final JsonObject jsonObject, final boolean addRootElement) {
if (addRootElement) {
final JsonObject rootJson = new JsonObject();
rootJson.add(rootElement.getName(), jsonObject);
return rootJson;
}
return jsonObject;
}
private String prettyPrint(final JsonObject rootJson, final boolean prettyPrint) {
if (prettyPrint) {
final Gson gson = new GsonBuilder().setPrettyPrinting()
.disableHtmlEscaping()
.create();
return gson.toJson(rootJson);
}
return rootJson.toString();
}
private JsonObject addJsonObjectsAndPrimitives(final JsonObject jsonObject, final List<Element> elements, final boolean includeAttributes, final String textPropName) {
for (final Element element : elements) {
final JsonElement jsonElement = isPrimitiveElement(element) ?
new JsonPrimitive(element.getValue()) : convertXmlElementsToJsonObject(Collections.singletonList(element), includeAttributes, textPropName);
jsonObject.add(getElementFullName(element), jsonElement);
}
return jsonObject;
}
private JsonObject convertXmlElementsToJsonObject(final List<Element> xmlElements, final boolean includeAttributes, final String textPropName) {
JsonObject result = new JsonObject();
for (final Element xmlElement : xmlElements) {
result = addXmlElementToJsonObject(result, xmlElement, includeAttributes, textPropName);
}
return result;
}
private JsonArray convertXmlElementsToJsonArray(final List<Element> xmlElements, final boolean includeAttributes, final String textPropName) {
final JsonArray result = new JsonArray();
for (final Element xmlElement : xmlElements) {
result.add(addXmlElementToJsonObject(new JsonObject(), xmlElement, includeAttributes, textPropName));
}
return result;
}
private JsonObject addXmlElementToJsonObject(JsonObject jsonObject, final Element xmlElement, final boolean includeAttributes, final String textPropName) {
addNamespaces(xmlElement.getAdditionalNamespaces());
if (includeAttributes) {
jsonObject = addAttributesToJsonObject(jsonObject, xmlElement.getAttributes());
}
List<Element> children = xmlElement.getChildren();
jsonObject = searchAndAddArrayElementsToJsonObject(jsonObject, children, includeAttributes, textPropName);
children = eliminateArrayElements(children);
jsonObject = addJsonObjectsAndPrimitives(jsonObject, children, includeAttributes, textPropName);
jsonObject = addTextProp(jsonObject, xmlElement.getText(), textPropName);
return jsonObject;
}
private JsonObject searchAndAddArrayElementsToJsonObject(final JsonObject jsonObject, final List<Element> elements, final boolean includeAttributes, final String textPropName) {
final List<String> arrayElementsNames = getListOfArrayElementNames(elements);
for (final String arrayName : arrayElementsNames) {
final List<Element> arrayElements = getElementsByName(arrayName, elements);
jsonObject.add(arrayName, convertXmlElementsToJsonArray(arrayElements, includeAttributes, textPropName));
}
return jsonObject;
}
private List<Element> eliminateArrayElements(List<Element> elements) {
final List<String> arrayElementsNames = getListOfArrayElementNames(elements);
for (final String arrayName : arrayElementsNames) {
elements = eliminateElementsWithName(arrayName, elements);
}
return elements;
}
private JsonObject addTextProp(final JsonObject jsonObject, final String text, final String textPropName) {
if (isNotEmpty(text) && text.matches(".*[a-zA-Z0-9].*")) {
jsonObject.addProperty(textPropName, text);
}
return jsonObject;
}
private void addNamespaces(final List<Namespace> namespaces) {
for (final Namespace namespace : namespaces) {
if (namespacesUris.length() > 0) {
namespacesPrefixes.append(Defaults.DELIMITER);
namespacesUris.append(Defaults.DELIMITER);
}
namespacesPrefixes.append(namespace.getPrefix());
namespacesUris.append(namespace.getURI());
}
}
private JsonObject addAttributesToJsonObject(final JsonObject jsonObject, final List<Attribute> attributes) {
for (final Attribute attribute : attributes) {
jsonObject.addProperty(JSON_ATTRIBUTE_PREFIX + attribute.getName(), attribute.getValue());
}
return jsonObject;
}
@NotNull
private String getElementFullName(final Element element) {
final StringBuilder name = new StringBuilder(element.getNamespacePrefix());
if (!element.getNamespacePrefix().isEmpty())
name.append(PREFIX_DELIMITER);
name.append(element.getName());
return name.toString();
}
private List<String> getElementsFullName(final List<Element> elements) {
final List<String> names = new ArrayList<>();
for (final Element element : elements) {
names.add(getElementFullName(element));
}
return names;
}
private boolean isPrimitiveElement(final Element element) {
return element.getChildren().isEmpty() && element.getAttributes().isEmpty(); //if it doesn't have child and doesn't have attributes it's primitive.
}
private List<String> getListOfArrayElementNames(final List<Element> elements) {
final List<String> names = new ArrayList<>();
final List<String> elementsStr = getElementsFullName(elements);
for (final String name : elementsStr) {
if (elementsStr.indexOf(name) != elementsStr.lastIndexOf(name) && !names.contains(name)) {
names.add(name);
}
}
return names;
}
private List<Element> getElementsByName(final String arrayName, final List<Element> elementsToSearch) {
final List<Element> elements = new ArrayList<>();
for (Element element : elementsToSearch) {
if (getElementFullName(element).equals(arrayName)) {
elements.add(element);
}
}
return elements;
}
private List<Element> eliminateElementsWithName(final String arrayName, final List<Element> elements) {
final List<Element> newElements = new ArrayList<>();
for (final Element element : elements) {
if (!getElementFullName(element).equals(arrayName)) {
newElements.add(element);
}
}
return newElements;
}
public String getNamespacesUris() {
return namespacesUris.toString();
}
public String getNamespacesPrefixes() {
return namespacesPrefixes.toString();
}
}