-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathNDBXWriter.java
More file actions
287 lines (258 loc) · 12.8 KB
/
NDBXWriter.java
File metadata and controls
287 lines (258 loc) · 12.8 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package nodebox.node;
import nodebox.util.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
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.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
/**
* Writes the ndbx file format.
*/
public class NDBXWriter {
public static void write(NodeLibrary library, File file) {
StreamResult streamResult = new StreamResult(file);
write(library, streamResult);
}
public static void write(NodeLibrary library, Writer writer) {
StreamResult streamResult = new StreamResult(writer);
write(library, streamResult);
}
public static void write(NodeLibrary library, StreamResult streamResult) {
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.newDocument();
// Build the header.
Element rootElement = doc.createElement("ndbx");
doc.appendChild(rootElement);
rootElement.setAttribute("type", "file");
rootElement.setAttribute("formatVersion", "0.9");
// Write out all the variables.
for (String variableName : library.getVariableNames()) {
String variableValue = library.getVariable(variableName);
Element varElement = doc.createElement("var");
rootElement.appendChild(varElement);
varElement.setAttribute("name", variableName);
varElement.setAttribute("value", variableValue);
}
// Write out all the nodes (skip the root)
List<Node> children = library.getRootNode().getChildren();
// Sort the children by name.
Collections.sort(children, new NodeNameCompator());
// The order in which the nodes are written is important!
// Since a library can potentially store an instance and its prototype, make sure that the prototype gets
// stored sequentially before its instance.
// The NDBXHandler class expects prototypes to be defined before their instances.
while (!children.isEmpty()) {
Node child = children.get(0);
writeOrderedChild(library, doc, rootElement, children, child);
}
// Write out all the child connections.
for (Connection conn : library.getRootNode().getConnections()) {
writeConnection(doc, rootElement, conn);
}
// Convert the document to XML.
DOMSource domSource = new DOMSource(doc);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.transform(domSource, streamResult);
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (TransformerException e) {
throw new RuntimeException(e);
}
}
public static String asString(NodeLibrary library) {
StringWriter writer = new StringWriter();
write(library, writer);
return writer.toString();
}
/**
* Write out the child. If the prototype of the child is also in this library, write that out first, recursively.
*
* @param library the node library
* @param doc the XML document
* @param parent the parent element
* @param children a list of children that were written already.
* When a child is written, we remove it from the list.
* @param child the child to write
*/
private static void writeOrderedChild(NodeLibrary library, Document doc, Element parent, List<Node> children, Node child) {
Node prototype = child.getPrototype();
if (prototype.getLibrary() == library && children.contains(prototype))
writeOrderedChild(library, doc, parent, children, prototype);
writeNode(doc, parent, child);
children.remove(child);
}
private static void writeNode(Document doc, Element parent, Node node) {
String xPosition = String.format(Locale.US, "%.0f", node.getX());
String yPosition = String.format(Locale.US, "%.0f", node.getY());
Element el = doc.createElement("node");
parent.appendChild(el);
el.setAttribute("name", node.getName());
el.setAttribute("prototype", node.getPrototype().getRelativeIdentifier(node));
el.setAttribute("x", xPosition);
el.setAttribute("y", yPosition);
if (node.isRendered())
el.setAttribute("rendered", "true");
if (node.isExported())
el.setAttribute("exported", "true");
// Add the output type if it is different from the prototype.
if (node.getDataClass() != node.getPrototype().getDataClass())
el.setAttribute("type", node.getDataClass().getName());
// Add the description
if (!node.getDescription().equals(node.getPrototype().getDescription())) {
Element desc = doc.createElement("description");
el.appendChild(desc);
Text descText = doc.createTextNode(node.getDescription());
desc.appendChild(descText);
}
// Add the ports
for (Port port : node.getPorts()) {
writePort(doc, el, port);
}
// Add the parameters
for (Parameter param : node.getParameters()) {
// We've written the description above in the <description> tag.
if (param.getName().equals("_description")) continue;
writeParameter(doc, el, param);
}
// Add all child nodes
for (Node child : node.getChildren()) {
writeNode(doc, el, child);
}
// Add all child connections
for (Connection conn : node.getConnections()) {
writeConnection(doc, el, conn);
}
}
private static void writeParameter(Document doc, Element parent, Parameter param) {
// We only write out the attributes that have changed with regards to the prototype.
Parameter protoParam = param.getPrototype();
// If the parameter and its prototype are completely equal, don't write anything.
if (param.prototypeEquals(protoParam)) return;
Element el = doc.createElement("param");
parent.appendChild(el);
// The parameters are not equal, so we can start writing the name.
el.setAttribute("name", param.getName());
// Write parameter type
if (protoParam == null || !param.getType().equals(protoParam.getType()))
el.setAttribute(NDBXHandler.PARAMETER_TYPE, param.getType().toString().toLowerCase(Locale.US));
// Write parameter attributes
attributeToXml(param, el, "widget", NDBXHandler.PARAMETER_WIDGET, protoParam, Parameter.WIDGET_MAPPING.get(param.getType()));
attributeToXml(param, el, "label", NDBXHandler.PARAMETER_LABEL, protoParam, StringUtils.humanizeName(param.getName()));
attributeToXml(param, el, "helpText", NDBXHandler.PARAMETER_HELP_TEXT, protoParam, null);
attributeToXml(param, el, "displayLevel", NDBXHandler.PARAMETER_DISPLAY_LEVEL, protoParam, Parameter.DisplayLevel.HUD);
attributeToXml(param, el, "enableExpression", NDBXHandler.PARAMETER_ENABLE_EXPRESSION, protoParam, "");
attributeToXml(param, el, "boundingMethod", NDBXHandler.PARAMETER_BOUNDING_METHOD, protoParam, Parameter.BoundingMethod.NONE);
attributeToXml(param, el, "minimumValue", NDBXHandler.PARAMETER_MINIMUM_VALUE, protoParam, null);
attributeToXml(param, el, "maximumValue", NDBXHandler.PARAMETER_MAXIMUM_VALUE, protoParam, null);
// Write parameter value / expression
if (param.hasExpression()) {
appendText(doc, el, "expression", param.getExpression());
} else {
switch (param.getType()) {
case INT:
appendText(doc, el, "value", param.asInt());
break;
case FLOAT:
appendText(doc, el, "value", param.asFloat());
break;
case STRING:
appendText(doc, el, "value", param.asString());
break;
case COLOR:
appendText(doc, el, "value", param.asColor());
break;
case CODE:
Element value = doc.createElement("value");
value.setAttribute("type", param.asCode().getType());
value.appendChild(doc.createCDATASection(param.asCode().getSource()));
el.appendChild(value);
break;
}
}
// Write menu items
List<Parameter.MenuItem> items = param.getMenuItems();
if (items.size() > 0) {
List<Parameter.MenuItem> protoItems = protoParam == null ? null : protoParam.getMenuItems();
if (!items.equals(protoItems)) {
for (Parameter.MenuItem item : items) {
Element menu = doc.createElement("menu");
el.appendChild(menu);
menu.setAttribute("key", item.getKey());
Text menuText = doc.createTextNode(item.getLabel());
menu.appendChild(menuText);
}
}
}
}
private static void writePort(Document doc, Element parent, Port port) {
// We only write out the ports that have changed with regards to the prototype.
Node protoNode = port.getNode().getPrototype();
Port protoPort = null;
if (protoNode != null)
protoPort = protoNode.getPort(port.getName());
// If the port and its prototype are equal, don't write anything.
if (protoPort != null
&& protoPort.getName().equals(port.getName())
&& protoPort.getDirection().equals(port.getDirection())
&& protoPort.getCardinality().equals(port.getCardinality())) return;
Element el = doc.createElement("port");
el.setAttribute("name", port.getName());
if (port.getCardinality() != Port.Cardinality.SINGLE)
el.setAttribute("cardinality", port.getCardinality().toString().toLowerCase(Locale.US));
parent.appendChild(el);
}
private static void writeConnection(Document doc, Element parent, Connection conn) {
Port output = conn.getOutput();
Element connElement = doc.createElement("conn");
connElement.setAttribute("output", output.getNode().getName());
connElement.setAttribute("input", conn.getInputNode().getName());
connElement.setAttribute("port", conn.getInput().getName());
parent.appendChild(connElement);
}
private static void attributeToXml(Parameter param, Element el, String attrName, String xmlName, Parameter protoParam, Object defaultValue) {
try {
String methodName = "get" + attrName.substring(0, 1).toUpperCase(Locale.US) + attrName.substring(1);
Method m = param.getClass().getMethod(methodName);
Object myValue = m.invoke(param);
if (myValue == null) return;
Object protoValue = protoParam != null ? m.invoke(protoParam) : null;
if (!myValue.equals(protoValue) && !myValue.equals(defaultValue)) {
// Values that are already strings are written as is.
// Other values, such as enums, are written as lowercase.
String stringValue = myValue instanceof String ? (String) myValue : myValue.toString().toLowerCase(Locale.US);
el.setAttribute(xmlName, stringValue);
}
} catch (Exception e) {
throw new RuntimeException("Exception while trying to get " + attrName + " for parameter " + param, e);
}
}
private static void appendText(Document doc, Element parent, String name, Object text) {
Element el = doc.createElement(name);
el.appendChild(doc.createTextNode(text.toString()));
parent.appendChild(el);
}
private static class NodeNameCompator implements Comparator<Node> {
public int compare(Node node1, Node node2) {
return node1.getName().compareTo(node2.getName());
}
}
}