Skip to content

Commit 7e1a06c

Browse files
authored
[xml2js] provide the shape name and dimensions as comments (#30)
Even if not directly used in the js code, this information can be helpful to get context.
1 parent 6399df4 commit 7e1a06c

3 files changed

Lines changed: 31 additions & 6 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ java -cp target/mxgraph-svg2shape-*-jar-with-dependencies.jar com.mxgraph.xml2js
111111

112112
The tool writes the generated code in the console
113113
``` javascript
114+
// shape: shape-01
115+
// width: 30.5
116+
// height: 40.2
114117
// foreground
115118
canvas.begin();
116119
canvas.moveTo(19.87, 0);

src/main/java/com/mxgraph/xml2js/Xml2Js.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.mxgraph.xml2js;
22

3-
import org.w3c.dom.Document;
4-
import org.w3c.dom.Element;
5-
import org.w3c.dom.Node;
6-
import org.w3c.dom.NodeList;
3+
import org.w3c.dom.*;
74
import org.xml.sax.InputSource;
85

96
import javax.xml.parsers.DocumentBuilder;
@@ -84,7 +81,13 @@ private static Document parseXml(String xml) {
8481
}
8582

8683
public void parseShape(Node shape) {
87-
logInfo(format("Parsing shape '%s'", shape.getAttributes().getNamedItem("name").getTextContent()));
84+
AttributesTextContent shapeAttributes = new AttributesTextContent(shape.getAttributes());
85+
String name = shapeAttributes.text("name");
86+
logInfo(format("Parsing shape '%s'", name));
87+
generateComment(format("shape: %s", name));
88+
generateComment(format("width: %s", shapeAttributes.text("w")));
89+
generateComment(format("height: %s", shapeAttributes.text("h")));
90+
8891
NodeList shapeChildren = shape.getChildNodes();
8992
for (int i = 0; i < shapeChildren.getLength(); i++) {
9093
Node item = shapeChildren.item(i);
@@ -96,9 +99,21 @@ public void parseShape(Node shape) {
9699
}
97100
}
98101

102+
private static class AttributesTextContent {
103+
private final NamedNodeMap attributes;
104+
105+
public AttributesTextContent(NamedNodeMap attributes) {
106+
this.attributes = attributes;
107+
}
108+
109+
public String text(String attributeName) {
110+
return attributes.getNamedItem(attributeName).getTextContent();
111+
}
112+
}
113+
99114
private void parseForeground(Node foreground) {
100115
logDebug("Parsing foreground");
101-
generateCode("// foreground");
116+
generateComment("foreground");
102117
NodeList children = foreground.getChildNodes();
103118

104119
for (int i = 0; i < children.getLength(); i++) {
@@ -126,6 +141,10 @@ private void generateCode(String code) {
126141
codeLines.add(code);
127142
}
128143

144+
private void generateComment(String comment) {
145+
generateCode("// " + comment);
146+
}
147+
129148
private void parsePath(Node path) {
130149
logDebug("Parsing path");
131150
generateCanvasMethodCall("begin()");

src/test/java/com/mxgraph/xml2js/Xml2JsTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ void parse_code_lines() {
2121
String xml = builder.build();
2222

2323
assertThat(xml2Js.parseCodeLines(xml)).containsExactly(
24+
"// shape: shape-01",
25+
"// width: 284.03",
26+
"// height: 327.7",
2427
"// foreground",
2528
"canvas.begin();",
2629
"canvas.moveTo(141.9, 0);",

0 commit comments

Comments
 (0)