Skip to content

Commit 6a5c79c

Browse files
authored
[xml2js] add support for quadTo and stroke (#31)
In addition - log management improvement: when running Svg2Js, underlying Svg2Xml and Xml2Js are using the same log levels - Xml2Js produce debug logs when processing unsupported elements - add tests for Svg2Js - avoid logs in Svg2Xml tests
1 parent 7e1a06c commit 6a5c79c

File tree

8 files changed

+118
-37
lines changed

8 files changed

+118
-37
lines changed

src/main/java/com/mxgraph/svg2js/Svg2Js.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,20 @@ public static void main(String[] args) throws IOException {
2020

2121
File svg = new File(args[0]);
2222
File currentDirectory = new File(System.getProperty("user.dir"));
23-
File destinationDirectory = new File(currentDirectory, "mxgraph-stencil-from-svg_" + System.currentTimeMillis());
23+
File tempDirectory = new File(currentDirectory, "mxgraph-stencil-from-svg_" + System.currentTimeMillis());
2424

25-
new Svg2Js().process(svg, destinationDirectory);
25+
// TODO make log level configurable
26+
String code = new Svg2Js().infoLog(true).debugLog(false).convertToJsCode(svg, tempDirectory);
27+
FileUtils.deleteQuietly(tempDirectory);
28+
System.out.println(code);
29+
System.out.println();
2630
}
2731

28-
public void process(File svg, File tempDirectory) throws IOException {
32+
public String convertToJsCode(File svg, File tempDirectory) throws IOException {
2933
logInfo("Start generating mxgraph JS code from SVG " + svg);
3034
logInfo("mxgraph stencil will be generated into " + tempDirectory);
3135

32-
Svg2Xml svg2Xml = new Svg2Xml();
36+
Svg2Xml svg2Xml = new Svg2Xml().infoLog(isInfoLogActivated);
3337
svg2Xml.convertToXml(new File[]{svg}, tempDirectory);
3438
logInfo("SVG converted into mxgraph stencil");
3539

@@ -39,14 +43,22 @@ public void process(File svg, File tempDirectory) throws IOException {
3943
}
4044

4145
logInfo("Converting SVG into JS code");
42-
String code = new Xml2Js().infoLog(true).debugLog(false).parse(expectedGeneratedStencilFile);
43-
System.out.println(code);
44-
System.out.println();
45-
FileUtils.deleteQuietly(tempDirectory);
46-
logInfo("mxgraph JS code from SVG produced");
46+
String code = new Xml2Js().infoLog(isInfoLogActivated).debugLog(isDebugLogActivated).parse(expectedGeneratedStencilFile);
47+
logInfo("Conversion to JS completed");
48+
return code;
4749
}
4850

51+
// TODO log management duplicated with Xml2Js
4952
private boolean isInfoLogActivated = true;
53+
private boolean isDebugLogActivated = true;
54+
public Svg2Js infoLog(boolean activate) {
55+
this.isInfoLogActivated = activate;
56+
return this;
57+
}
58+
public Svg2Js debugLog(boolean activate) {
59+
this.isDebugLogActivated = activate;
60+
return this;
61+
}
5062
private void logInfo(String msg) {
5163
if (isInfoLogActivated) {
5264
System.out.println(format("Svg2Js [INFO] %s", msg));

src/main/java/com/mxgraph/svg2xml/Svg2Xml.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,12 @@ public static void main(String[] args)
106106
svg2Xml.convertToXml(sourceFiles.toArray(new File[0]), new File(args[1]));
107107
}
108108

109+
// TODO log management duplicated with Xml2Js
109110
private boolean isInfoLogActivated = true;
111+
public Svg2Xml infoLog(boolean activate) {
112+
this.isInfoLogActivated = activate;
113+
return this;
114+
}
110115
private void logInfo(String msg) {
111116
if (isInfoLogActivated) {
112117
System.out.println(format("Svg2Xml [INFO] %s", msg));

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public static void main(String[] args) {
2323
}
2424

2525
File source = new File(args[0]);
26+
// TODO make log level configurable
2627
String code = new Xml2Js().infoLog(true).debugLog(false).parse(source);
2728
System.out.println(code);
2829
}
@@ -92,8 +93,11 @@ public void parseShape(Node shape) {
9293
for (int i = 0; i < shapeChildren.getLength(); i++) {
9394
Node item = shapeChildren.item(i);
9495
if (item.getNodeType() == Node.ELEMENT_NODE) {
95-
if (item.getNodeName().equals("foreground")) {
96+
String nodeName = item.getNodeName();
97+
if (nodeName.equals("foreground")) {
9698
parseForeground(item);
99+
} else {
100+
logDebug("Unsupported element: " + nodeName);
97101
}
98102
}
99103
}
@@ -127,6 +131,13 @@ private void parseForeground(Node foreground) {
127131
break;
128132
case "path":
129133
parsePath((Element) item);
134+
break;
135+
case "stroke":
136+
logDebug("Parsing stroke");
137+
generateCanvasMethodCall("stroke()");
138+
break;
139+
default:
140+
logDebug("Unsupported element: " + nodeName);
130141
}
131142
}
132143
}
@@ -183,7 +194,13 @@ private void parsePathElement(Element element) {
183194
case "move":
184195
generateCanvasMethodCall(format("moveTo(%s, %s)", element.getAttribute("x"), element.getAttribute("y")));
185196
break;
186-
197+
case "quad":
198+
generateCanvasMethodCall(format("quadTo(%s, %s, %s, %s)",
199+
element.getAttribute("x1"), element.getAttribute("y1"),
200+
element.getAttribute("x2"), element.getAttribute("y2")));
201+
break;
202+
default:
203+
logDebug("Unsupported element: " + nodeName);
187204
}
188205
}
189206

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.mxgraph;
2+
3+
import java.io.File;
4+
import java.util.Arrays;
5+
6+
public class FileTooling {
7+
8+
public static File[] svgSourceFiles(String... fileNames) {
9+
File parent = new File(System.getProperty("user.dir"), "src/test/resources/svg"); // ensure we pass absolute path
10+
return Arrays.stream(fileNames)
11+
.map(fileName -> new File(parent, fileName))
12+
.toArray(File[]::new);
13+
}
14+
15+
public static File svgSourceFile(String fileName) {
16+
return svgSourceFiles(fileName)[0];
17+
}
18+
19+
public static File destinationFolder(String folderName) {
20+
return new File("target/test/output/", folderName);
21+
}
22+
23+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.mxgraph.svg2js;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
8+
import static com.mxgraph.FileTooling.*;
9+
import static com.mxgraph.utils.FileUtils.EOL;
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
12+
class Svg2JsTest {
13+
14+
private final Svg2Js xml2Js = new Svg2Js().infoLog(false).debugLog(false);
15+
16+
@Test
17+
void process_svg_file() throws IOException {
18+
File workingDirectory = destinationFolder("Svg2Js/work");
19+
20+
assertThat(xml2Js.convertToJsCode(svgSourceFile("simple-02/path-blue.svg"), workingDirectory))
21+
.isEqualTo(toCode("// shape: path-blue",
22+
"// width: 70",
23+
"// height: 50",
24+
"// foreground",
25+
"canvas.begin();",
26+
"canvas.moveTo(0, 25);",
27+
"canvas.quadTo(20, 0, 30, 25);",
28+
"canvas.quadTo(40, 50, 70, 25);",
29+
"canvas.stroke();"
30+
)
31+
);
32+
}
33+
34+
private String toCode(String... lines) {
35+
return String.join(EOL, lines);
36+
}
37+
38+
}

src/test/java/com/mxgraph/svg2xml/Svg2XmlTest.java

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

3+
import static com.mxgraph.FileTooling.destinationFolder;
4+
import static com.mxgraph.FileTooling.svgSourceFiles;
35
import static com.mxgraph.utils.FileUtils.EOL;
46
import static com.mxgraph.utils.FileUtils.fileContent;
57
import static org.assertj.core.api.Assertions.assertThat;
@@ -10,9 +12,13 @@
1012

1113
class Svg2XmlTest {
1214

15+
private static Svg2Xml newSvg2Xml() {
16+
return new Svg2Xml().infoLog(false);
17+
}
18+
1319
@Test
1420
void convertToXml_single_file() {
15-
Svg2Xml svg2Xml = new Svg2Xml();
21+
Svg2Xml svg2Xml = newSvg2Xml();
1622

1723
File destPath = destinationFolder("Simple-Single file");
1824
svg2Xml.convertToXml(svgSourceFiles("simple-01/circle-green.svg"), destPath);
@@ -29,7 +35,7 @@ void convertToXml_single_file() {
2935

3036
@Test
3137
void convertToXml_two_files_from_the_same_folder() {
32-
Svg2Xml svg2Xml = new Svg2Xml();
38+
Svg2Xml svg2Xml = newSvg2Xml();
3339

3440
File destPath = destinationFolder("Simple-Two files");
3541
svg2Xml.convertToXml(svgSourceFiles("simple-01/circle-green.svg", "simple-01/rectangle-blue.svg"), destPath);
@@ -48,7 +54,7 @@ void convertToXml_two_files_from_the_same_folder() {
4854

4955
@Test
5056
void convertToXml_files_from_two_folders_without_subfolders_files_given_ordered_by_folders() {
51-
Svg2Xml svg2Xml = new Svg2Xml();
57+
Svg2Xml svg2Xml = newSvg2Xml();
5258

5359
File destPath = destinationFolder("files from 2 folders - no subfolders");
5460
// in the current implementation, the files are supposed to be passed ordered by folder
@@ -72,26 +78,4 @@ void convertToXml_files_from_two_folders_without_subfolders_files_given_ordered_
7278
);
7379
}
7480

75-
// =================================================================================================================
76-
// UTILS
77-
// =================================================================================================================
78-
79-
private static void assertFirstLine(String fileContent, String expectedStart, String expectedEnd) {
80-
String firstLine = fileContent.substring(0, fileContent.indexOf(EOL));
81-
assertThat(firstLine).describedAs("1st line of the generated file")
82-
.startsWith(expectedStart)
83-
.endsWith(expectedEnd);
84-
}
85-
86-
private static File[] svgSourceFiles(String... fileNames) {
87-
File parent = new File(System.getProperty("user.dir"), "src/test/resources/svg"); // ensure we pass absolute path
88-
return Arrays.stream(fileNames)
89-
.map(fileName -> new File(parent, fileName))
90-
.toArray(File[]::new);
91-
}
92-
93-
private static File destinationFolder(String folderName) {
94-
return new File("target/test/output/", folderName);
95-
}
96-
9781
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ void parse_code_lines() {
1515
.element("<curve x1='85.52' x2='39.66' x3='39.66' y1='0' y2='45.87' y3='102.25'/>")
1616
.element("<line x='39.66' y='131.4'/>")
1717
.element("<arc large-arc-flag='0' rx='6.43' ry='6.43' sweep-flag='1' x='95.51' x-axis-rotation='0' y='135.03'/>")
18+
.element("<quad x1='20' x2='30' y1='0' y2='25'/>")
1819
.element("<close/>")
1920
.end()
2021
;
@@ -30,6 +31,7 @@ void parse_code_lines() {
3031
"canvas.curveTo(85.52, 0, 39.66, 45.87, 39.66, 102.25);",
3132
"canvas.lineTo(39.66, 131.4);",
3233
"canvas.arcTo(6.43, 6.43, 0, 0, 1, 95.51, 135.03);",
34+
"canvas.quadTo(20, 0, 30, 25);",
3335
"canvas.close();",
3436
"canvas.fillAndStroke();"
3537
);
Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)