Skip to content

Commit

Permalink
svg2xml: allow to run without showing the GUI (#8)
Browse files Browse the repository at this point in the history
Adapted the main methods for the Gui and the console app to prevent the Gui from
showing up when the app is launched with arguments.

Based on alex-hall-idener@7b757e5

Co-authored-by: Alex Hall Freaza <alex.hall@idener.es>
  • Loading branch information
tbouffard and alex-hall-idener committed Apr 26, 2020
1 parent ad4b865 commit 4f7f0cd
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 43 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,27 @@ Note about branches in this repository
- `master` is not intended to receive any changes except updates from the upstream repository
- the `develop` branch is the target for new features, fixes, .... and merge commits from `master`

## Run

After having built the project, run
## `svg2xml`

### Run

You can run `svg2xml` with or without GUI.
- with the GUI (more details below)
```
java -jar target/mxgraph-svg2shape-*-jar-with-dependencies.jar
```
- without the GUI
- `<path_to_source>` path to the svg file to convert or path to a folder to walk through and find svg files (see the
GUI explanation below)
- `<path_to_destination>`
```
java -jar target/mxgraph-svg2shape-*-jar-with-dependencies.jar <path_to_source> <path_to_destination>
```

## Svg2Xml

### Quick start guide
### GUI Quick start guide

Run Svg2XmlGui. The left file system defines what files or folders you want to convert. The right one, defines the destination.
The left file system defines what files or folders you want to convert. The right one, defines the destination.

If you select one file, a single stencil XML file will be generated for just that one stencil.

Expand Down
97 changes: 62 additions & 35 deletions src/main/java/com/mxgraph/svg2xml/Svg2Xml.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
Expand Down Expand Up @@ -67,6 +68,39 @@ public class Svg2Xml
public Svg2Xml(Svg2XmlGui gui)
{
destConfigDoc = new XmlConfig(gui);
convertToXml(gui.sourceFiles, gui.destPath);
}

public Svg2Xml(){
destConfigDoc = new XmlConfig();
}

public static void main(String[] args)
{
if(args.length < 2)
{
System.err.println("Incorrect number of arguments: you must pass the svg source file/folder and the destination folder");
// TODO exit
return;
}
File sourceArg = new File(args[0]);
List<File> sourceFiles = new ArrayList<>();
if(sourceArg.isDirectory())
{
// TODO extract this code as it is not related to the GUI
Svg2XmlGui gui = new Svg2XmlGui();
sourceFiles.addAll(gui.walk(sourceArg.getAbsolutePath()));
}
else
{
sourceFiles.add(sourceArg);
}

Svg2Xml svg2Xml = new Svg2Xml();
svg2Xml.convertToXml(sourceFiles.toArray(new File[0]), new File(args[1]));
}

public void convertToXml(File[] sourceFiles, File destPath) {
// order of actions:
//1. Config settings are given default values combined with the settings from the UI.
//2. check if additional config files exist. Those are the group config and individual stencil config files in XML format. If they exist, they are combined and the config settings are altered accordingly.
Expand All @@ -89,29 +123,29 @@ public Svg2Xml(Svg2XmlGui gui)
//20. add the new element to the XML
//21. write the document to a file

// TODO add SVG viewbox support, or manual setting of viewbox
// TODO add SVG viewbox support, or manual setting of viewbox

boolean isLastInGroup = true;
boolean isNewGroup = true;
String groupXml = new String();
ByteArrayOutputStream groupBaos = new ByteArrayOutputStream();
ByteArrayOutputStream groupBaos = new ByteArrayOutputStream();

// construct destConfigDoc based on default values, groupConfigDoc and stencilConfigDoc
for (int i = 0; i < gui.sourceFiles.length; i++)
for (int i = 0; i < sourceFiles.length; i++)
{
System.out.println("Processing " + gui.sourceFiles[i].getAbsolutePath());
System.out.println("Processing " + sourceFiles[i].getAbsolutePath());
groupBaos = new ByteArrayOutputStream();
isLastInGroup = false;
isNewGroup = false;

String shapeName = gui.sourceFiles[i].getName();
String shapeName = sourceFiles[i].getName();
shapeName = shapeName.substring(0, shapeName.lastIndexOf("."));
int configCount = 0;

// looking for a group config file
String groupConfigString = null;

String configNameString = gui.sourceFiles[i].getParent();
String configNameString = sourceFiles[i].getParent();
configNameString += "_config.xml";
File testFile = new File(configNameString);

Expand All @@ -126,7 +160,7 @@ public Svg2Xml(Svg2XmlGui gui)
// looking for a stencil config file
String stencilConfigString = null;

configNameString = gui.sourceFiles[i].getAbsolutePath();
configNameString = sourceFiles[i].getAbsolutePath();
int pointIndex = configNameString.lastIndexOf('.');
configNameString = configNameString.substring(0, pointIndex) + "_config.xml";

Expand Down Expand Up @@ -267,7 +301,7 @@ else if (aspectRatio.toLowerCase().equals("variable"))
//TODO probably done more elegantly via DOM
String srcXmlString;

srcXmlString = readFile(gui.sourceFiles[i].getAbsolutePath());
srcXmlString = readFile(sourceFiles[i].getAbsolutePath());
int doctypeIndex = srcXmlString.indexOf("<!DOCTYPE");

if (doctypeIndex>-1)
Expand All @@ -280,7 +314,7 @@ else if (aspectRatio.toLowerCase().equals("variable"))

srcSVGDoc = parseXml(srcXmlString);
srcSVGDoc = flattenSvg(srcSVGDoc);

//TODO remove connection points
Connection svgConnects = getConnections(srcSVGDoc);
srcSVGDoc = removeConnections(srcSVGDoc);
Expand All @@ -295,11 +329,11 @@ else if (aspectRatio.toLowerCase().equals("variable"))

//recalculate connections to relative coords
ArrayList<Constraint> constraints = svgConnects.getConstraints();

for (int j=0; j < constraints.size(); j++)
{
Constraint currConstraint = constraints.get(j);

double x = currConstraint.getX() - bounds.getMinX();
x = Math.round(x * 100.0 / bounds.getWidth()) / 100.0;
currConstraint.setX(x);
Expand All @@ -308,7 +342,7 @@ else if (aspectRatio.toLowerCase().equals("variable"))
y = Math.round(y * 100.0 / bounds.getHeight()) / 100.0;
currConstraint.setY(y);
}

double stencilBoundsMinX = 0;
double stencilBoundsMinY = 0;
double stencilBoundsMaxX = 5;
Expand All @@ -332,16 +366,16 @@ else if (aspectRatio.toLowerCase().equals("variable"))
stencilBoundsX = 5;
double oldStencilBoundsMaxX = stencilBoundsMaxX;
double oldStencilBoundsMinX = stencilBoundsMinX;
stencilBoundsMaxX = (oldStencilBoundsMaxX + oldStencilBoundsMinX) / 2.0d + 2.5;
stencilBoundsMinX = (oldStencilBoundsMaxX + oldStencilBoundsMinX) / 2.0d - 2.5;
stencilBoundsMaxX = (oldStencilBoundsMaxX + oldStencilBoundsMinX) / 2.0d + 2.5;
stencilBoundsMinX = (oldStencilBoundsMaxX + oldStencilBoundsMinX) / 2.0d - 2.5;
}
if (stencilBoundsY<5)
{
stencilBoundsY = 5;
double oldStencilBoundsMaxY = stencilBoundsMaxY;
double oldStencilBoundsMinY = stencilBoundsMinY;
stencilBoundsMaxY = (oldStencilBoundsMaxY + oldStencilBoundsMinY) / 2.0d + 2.5;
stencilBoundsMinY = (oldStencilBoundsMaxY + oldStencilBoundsMinY) / 2.0d - 2.5;
stencilBoundsMaxY = (oldStencilBoundsMaxY + oldStencilBoundsMinY) / 2.0d + 2.5;
stencilBoundsMinY = (oldStencilBoundsMaxY + oldStencilBoundsMinY) / 2.0d - 2.5;
}

destConfigDoc.setStencilBoundsX(stencilBoundsX);
Expand Down Expand Up @@ -440,7 +474,7 @@ else if (aspectRatio.toLowerCase().equals("variable"))

if (isRestoreNeeded(oldStyle, currStyle))
{
Node bg = destDoc.getElementsByTagName("background").item(0);
Node bg = destDoc.getElementsByTagName("background").item(0);
Node fg = destDoc.getElementsByTagName("foreground").item(0);

if (bg != null)
Expand Down Expand Up @@ -503,8 +537,8 @@ else if (aspectRatio.toLowerCase().equals("variable"))
}
else
{
String currParent = gui.sourceFiles[i].getParent();
String oldParent = gui.sourceFiles[i-1].getParent();
String currParent = sourceFiles[i].getParent();
String oldParent = sourceFiles[i-1].getParent();

if(currParent.equals(oldParent))
{
Expand All @@ -517,14 +551,14 @@ else if (aspectRatio.toLowerCase().equals("variable"))
}

//check if this is the last file in the group
if (i + 1 == gui.sourceFiles.length)
if (i + 1 == sourceFiles.length)
{
isLastInGroup = true;
}
else
{
String currParent = gui.sourceFiles[i].getParent();
String nextParent = gui.sourceFiles[i+1].getParent();
String currParent = sourceFiles[i].getParent();
String nextParent = sourceFiles[i+1].getParent();

if(currParent.equals(nextParent))
{
Expand All @@ -537,14 +571,14 @@ else if (aspectRatio.toLowerCase().equals("variable"))
}

// here we need some group naming check
String currentPath = gui.sourceFiles[i].getAbsolutePath();
String currentPath = sourceFiles[i].getAbsolutePath();
currentPath = currentPath.substring(2, currentPath.lastIndexOf("."));

if (isNewGroup)
{
// if new group then we save the old file and open a new one
String groupName = stencilUserMarker;
File currFile = new File(gui.sourceFiles[i].getAbsolutePath());
File currFile = new File(sourceFiles[i].getAbsolutePath());
ArrayList <String> folders = new ArrayList <String>();

while (!currFile.getParentFile().getName().equals("svgroot") && currFile.getParent().length() > 4)
Expand Down Expand Up @@ -578,8 +612,8 @@ else if (aspectRatio.toLowerCase().equals("variable"))

try
{
String currentDestPath = gui.destPath.getAbsolutePath();
currentDestPath += gui.sourceFiles[i].getParent().substring(2, gui.sourceFiles[i].getParent().length()) + ".xml";
String currentDestPath = destPath.getAbsolutePath();
currentDestPath += sourceFiles[i].getParent().substring(2, sourceFiles[i].getParent().length()) + ".xml";
currentDestPath = currentDestPath.toLowerCase();
currentDestPath = currentDestPath.replaceAll("\\s", "_");
File myDestFile = new File(currentDestPath);
Expand All @@ -592,8 +626,8 @@ else if (aspectRatio.toLowerCase().equals("variable"))
writer.write(groupXml);
writer.close();
System.out.println("File written");
}
catch(Exception ex)
}
catch(Exception ex)
{
ex.printStackTrace();
}
Expand Down Expand Up @@ -1188,13 +1222,6 @@ public static String readFile(String filename)
return null;
}

/**
* @param args
*/
public static void main(String[] args)
{
}

/**
* Removes all defs and makes them inline, if visuals are in question, gradient fills are removed and a single color is put inline.
* Group style is inherited into individual element and groups are removed.
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/mxgraph/svg2xml/Svg2XmlGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class Svg2XmlGui implements ActionListener{
/**
* @param args
*/
public static void main(String[] args)
public static void main(String[] args)
{
Svg2XmlGui gui = new Svg2XmlGui();
gui.startHere(args, gui);
Expand All @@ -89,6 +89,7 @@ public void startHere(String[] args, Svg2XmlGui gui)
if (args.length>0)
{
Svg2Xml.main(args);
return; // prevent the GUI to show up after processing
}

JFrame frame = new JFrame();
Expand Down Expand Up @@ -202,7 +203,7 @@ public void actionPerformed(ActionEvent e) {

}

// the parser is started with args. They are parsed and sent to the svg parser class
// the parser is started with args. They are parsed and sent to the svg parser class
public void parseArgs(String args[])
{
if (args.length>0)
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/mxgraph/svg2xml/XmlConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public XmlConfig(Svg2XmlGui gui)
setRoundCoords(gui.isRoundCoords());
setDecimalsToRound(gui.getDecimalsToRound());
}

public XmlConfig()
{

}

public boolean isBackground()
{
Expand Down

0 comments on commit 4f7f0cd

Please sign in to comment.