Skip to content

Commit c0f40c0

Browse files
authored
[svg2js] 1st implementation of the tool (#24)
Bridge Svg2Xml to Xml2Js.
1 parent 4c49307 commit c0f40c0

4 files changed

Lines changed: 91 additions & 11 deletions

File tree

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ This repository is based on mxGraph svg2xml tooling: `SVG to XML mxGraph stencil
1010
created for internal use, so there are lots of things unfinished.`
1111

1212
It contains the original svg2xml tool; it aims to improve it and to add the following features
13-
- modernize `svg2xml` and contribute improvements to the upstream repository (if it is still active and accept
13+
- modernize [svg2xml](#svg2xml) and contribute improvements to the upstream repository (if it is still active and accept
1414
contributions): CLI, UI look&feel updates, implement to completed features....
15-
- add `xml2js` tool: translate an XML mxGraph stencil definition into a set of corresponding javascript command for an
15+
- add [xml2js](#xml2js): translate an XML mxGraph stencil definition into a set of corresponding javascript command for an
1616
easy integration in JS programs
17-
- add `svg2js`: directly convert an SVG file into a set of javascript commands
17+
- add [svg2js](#svg2js): convert an SVG file into a set of `mxGraph` javascript commands
18+
xml2js
1819

1920

2021
## Build
@@ -78,8 +79,9 @@ If you want to reduce the size, with some compromise to precision, use rounding.
7879

7980
## `xml2js`
8081

81-
**DISCLAIMER**: this tool is at its early stage and miss a lot of features. See the GitHub issues (create one for
82-
any questions and prior submitting a Pull Request for discussions)
82+
**DISCLAIMER**: this tool is at its early stage and misses a lot of features. See the GitHub issues (create one for
83+
any questions and prior submitting a Pull Request for discussions). It is mainly developed to provide the foundation for `svg2js`
84+
8385

8486
**GOAL**: translate an XML mxGraph stencil definition into a set of corresponding javascript calls for an easy integration in JS programs
8587

@@ -104,3 +106,16 @@ canvas.lineTo(5.55, 18.4);
104106
canvas.close();
105107
canvas.fillAndStroke();
106108
```
109+
110+
## `svg2js`
111+
112+
**GOAL**: convert an SVG file into a set of `mxGraph` javascript commands, by calling `svg2xml` then `xml2js`
113+
114+
### Run
115+
116+
`svg2js` required the following arguments
117+
- `<path_to_source>` path to the SVG file to convert to javascript code
118+
```
119+
java -cp target/mxgraph-svg2shape-*-jar-with-dependencies.jar com.mxgraph.svg2js.Svg2Js <path_to_source>
120+
```
121+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.mxgraph.svg2js;
2+
3+
import com.mxgraph.svg2xml.Svg2Xml;
4+
import com.mxgraph.xml2js.Xml2Js;
5+
import org.apache.commons.io.FileUtils;
6+
import org.apache.commons.io.FilenameUtils;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
11+
import static java.lang.String.format;
12+
13+
public class Svg2Js {
14+
15+
public static void main(String[] args) throws IOException {
16+
if (args.length < 1) {
17+
System.err.println("svg2js requires at least one argument (path to the svg file)");
18+
System.exit(-1);
19+
}
20+
21+
File svg = new File(args[0]);
22+
File currentDirectory = new File(System.getProperty("user.dir"));
23+
File destinationDirectory = new File(currentDirectory, "mxgraph-stencil-from-svg_" + System.currentTimeMillis());
24+
25+
new Svg2Js().process(svg, destinationDirectory);
26+
}
27+
28+
public void process(File svg, File tempDirectory) throws IOException {
29+
logInfo("Start generating mxgraph JS code from SVG " + svg);
30+
logInfo("mxgraph stencil will be generated into " + tempDirectory);
31+
32+
Svg2Xml svg2Xml = new Svg2Xml();
33+
svg2Xml.convertToXml(new File[]{svg}, tempDirectory);
34+
logInfo("SVG converted into mxgraph stencil");
35+
36+
File expectedGeneratedStencilFile = new File(tempDirectory, FilenameUtils.getBaseName(svg.getName()) + ".xml");
37+
if (!expectedGeneratedStencilFile.isFile()) {
38+
throw new IOException(expectedGeneratedStencilFile + " has not been generated, see logs below");
39+
}
40+
41+
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");
47+
}
48+
49+
private boolean isInfoLogActivated = true;
50+
private void logInfo(String msg) {
51+
if (isInfoLogActivated) {
52+
System.out.println(format("Svg2Js [INFO] %s", msg));
53+
}
54+
}
55+
56+
}

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
import com.mxgraph.shape.mxStencilShape;
4848
import com.mxgraph.svg2xml.XmlConfig.aspectType;
4949

50+
import static java.lang.String.format;
51+
5052
/**
5153
* Executes what is defined in Svg2XmlGui
5254
*/
@@ -104,6 +106,13 @@ public static void main(String[] args)
104106
svg2Xml.convertToXml(sourceFiles.toArray(new File[0]), new File(args[1]));
105107
}
106108

109+
private boolean isInfoLogActivated = true;
110+
private void logInfo(String msg) {
111+
if (isInfoLogActivated) {
112+
System.out.println(format("Svg2Xml [INFO] %s", msg));
113+
}
114+
}
115+
107116
public void convertToXml(File[] sourceFiles, File destPath) {
108117
// order of actions:
109118
//1. Config settings are given default values combined with the settings from the UI.
@@ -190,7 +199,7 @@ public void convertToXml(File[] sourceFiles, File destPath) {
190199
// construct destConfigDoc based on default values, groupConfigDoc and stencilConfigDoc
191200
for (int i = 0; i < sourceFiles.length; i++)
192201
{
193-
System.out.println("Processing " + sourceFiles[i].getAbsolutePath());
202+
logInfo("Processing " + sourceFiles[i].getAbsolutePath());
194203
groupBaos = new ByteArrayOutputStream();
195204
isLastInGroup = false;
196205
isNewGroup = true;
@@ -234,7 +243,7 @@ public void convertToXml(File[] sourceFiles, File destPath) {
234243
testFile = null;
235244

236245
// now we have potentially both config files in String format
237-
System.out.println("parsing " + shapeName + " using " + configCount + " configs");
246+
logInfo("parsing " + shapeName + " using " + configCount + " configs");
238247

239248
ArrayList<Connection> groupConnection = null;
240249

@@ -679,14 +688,14 @@ else if (aspectRatio.toLowerCase().equals("variable"))
679688
String fileName = sourceFiles[i].getName();
680689
fileName = fileName.substring(0, fileName.lastIndexOf('.')) + ".xml";
681690
File destFile = new File(destPath, fileName);
682-
System.out.println("Prepare writing to " + destFile);
691+
logInfo("Prepare writing to " + destFile);
683692

684693
// TODO try-with-resource to improve resources management
685694
FileWriter fileWriter = new FileWriter(destFile);
686695
BufferedWriter writer = new BufferedWriter(fileWriter);
687696
writer.write(groupXml);
688697
writer.close();
689-
System.out.println("File written");
698+
logInfo("File written");
690699

691700
if (!destPaths.contains(destPath))
692701
{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,13 @@ private void parsePathElement(Element element) {
170170

171171
private void logInfo(String msg) {
172172
if (isInfoLogActivated) {
173-
System.out.println(format("[INFO] %s", msg));
173+
System.out.println(format("Xml2Js [INFO] %s", msg));
174174
}
175175
}
176176

177177
private void logDebug(String msg) {
178178
if (isDebugLogActivated) {
179-
System.out.println(format("[DEBUG] %s", msg));
179+
System.out.println(format("Xml2Js [DEBUG] %s", msg));
180180
}
181181
}
182182

0 commit comments

Comments
 (0)