-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowConverter.java
More file actions
200 lines (176 loc) · 6.6 KB
/
FlowConverter.java
File metadata and controls
200 lines (176 loc) · 6.6 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
package org.example;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.Marshaller;
import jakarta.xml.bind.Unmarshaller;
import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
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 java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FlowConverter {
@XmlRootElement(name = "zflow")
public static class ZFlow {
@XmlElement(name = "config")
public List<Config> configs;
@XmlElement(name = "node")
public List<Node> nodes;
}
@XmlRootElement(name = "config")
public static class Config {
@XmlAttribute
public String name;
@XmlAttribute
public String value;
}
@XmlRootElement(name = "node")
public static class Node {
@XmlAttribute
public String name;
@XmlAttribute
public String id;
@XmlAttribute
public String x;
@XmlAttribute
public String y;
@XmlElement(name = "line")
public List<Line> lines;
}
@XmlRootElement(name = "line")
public static class Line {
@XmlAttribute
public String name;
@XmlAttribute
public String id;
@XmlAttribute
public String target;
}
public static class JsonFlow {
public Map<String, Object> areas;
public int initNum;
public Map<String, JsonLine> lines;
public Map<String, JsonNode> nodes;
public String title;
}
public static class JsonLine {
public boolean alt;
public String from;
public String name;
public String to;
public String type;
}
public static class JsonNode {
public boolean alt;
public int height;
public int left;
public String name;
public int top;
public String type;
public int width;
}
public static String xmlToJson(String xml) throws Exception {
JAXBContext context = JAXBContext.newInstance(ZFlow.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
ZFlow zflow = (ZFlow) unmarshaller.unmarshal(new StringReader(xml));
JsonFlow jsonFlow = new JsonFlow();
jsonFlow.areas = new HashMap<>();
jsonFlow.initNum = 100;
jsonFlow.lines = new HashMap<>();
jsonFlow.nodes = new HashMap<>();
jsonFlow.title = "newFlow_1";
for (int i = 0; i < zflow.nodes.size(); i++) {
Node node = zflow.nodes.get(i);
JsonNode jsonNode = new JsonNode();
jsonNode.alt = true;
jsonNode.height = 24;
jsonNode.left = Integer.parseInt(node.x);
jsonNode.name = node.name;
jsonNode.top = Integer.parseInt(node.y);
jsonNode.type = "start round";
jsonNode.width = 24;
jsonFlow.nodes.put("node_" + (i + 1), jsonNode);
if (node.lines != null) {
for (int j = 0; j < node.lines.size(); j++) {
Line line = node.lines.get(j);
JsonLine jsonLine = new JsonLine();
jsonLine.alt = true;
jsonLine.from = "node_" + (i + 1);
jsonLine.name = line.name;
jsonLine.to = "node_" + (Integer.parseInt(line.target.replaceAll("\\D", "")) + 1);
jsonLine.type = "sl";
jsonFlow.lines.put("line_" + (jsonFlow.lines.size() + 1), jsonLine);
}
}
}
ObjectMapper mapper = new ObjectMapper();
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonFlow);
}
public static String jsonToXml(String json) throws Exception {
ObjectMapper mapper = new ObjectMapper();
JsonFlow jsonFlow = mapper.readValue(json, JsonFlow.class);
ZFlow zflow = new ZFlow();
zflow.configs = new ArrayList<>();
zflow.nodes = new ArrayList<>();
for (Map.Entry<String, JsonNode> entry : jsonFlow.nodes.entrySet()) {
JsonNode jsonNode = entry.getValue();
Node node = new Node();
node.name = jsonNode.name;
node.id = entry.getKey();
node.x = String.valueOf(jsonNode.left);
node.y = String.valueOf(jsonNode.top);
node.lines = new ArrayList<>();
for (Map.Entry<String, JsonLine> lineEntry : jsonFlow.lines.entrySet()) {
JsonLine jsonLine = lineEntry.getValue();
if (jsonLine.from.equals(entry.getKey())) {
Line line = new Line();
line.name = jsonLine.name;
line.id = lineEntry.getKey();
line.target = jsonLine.to.replaceAll("node_", "Node");
node.lines.add(line);
}
}
zflow.nodes.add(node);
}
JAXBContext context = JAXBContext.newInstance(ZFlow.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter writer = new StringWriter();
marshaller.marshal(zflow, writer);
return writer.toString();
}
private static String readFile(String filePath) throws IOException {
return new String(Files.readAllBytes(Paths.get(filePath)));
}
public static void main(String[] args) {
try {
String xml = readFile("C:\\work\\temp\\test.xml"); // Your XML string here
String json = xmlToJson(xml);
System.out.println("XML to JSON:");
System.out.println(json);
// String json = readFile("C:\\work\\temp\\test.json");
// String convertedXml = jsonToXml(json);
// System.out.println("\nJSON back to XML:");
// System.out.println(convertedXml);
} catch (Exception e) {
e.printStackTrace();
}
}
}