Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
<artifactId>jgraphx</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
41 changes: 31 additions & 10 deletions src/main/java/com/mxgraph/svg2xml/Svg2Xml.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.commons.io.FileUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
Expand Down Expand Up @@ -159,6 +160,26 @@ public void convertToXml(File[] sourceFiles, File destPath) {
// {
// sourceFolder = gui.sourceFileListComponent.getSelectedFiles()[0].getParent();
// }
// Basic implementation to mimic upstream/master
// only used to compute the stencil groupName
File rootSourceFolder = null;
for (File sourceFile : sourceFiles) {
File parent = sourceFile.getParentFile().getParentFile();
if (rootSourceFolder == null) {
rootSourceFolder = parent;
continue;
}

try {
if (FileUtils.directoryContains(parent, rootSourceFolder)) {
rootSourceFolder = parent;
}
} catch (IOException e) {
// TODO error management
e.printStackTrace();
}
}
sourceFolder = rootSourceFolder.getAbsolutePath();
// =============================================================================================================
// END OF "NOTE"
// =============================================================================================================
Expand Down Expand Up @@ -652,24 +673,24 @@ else if (aspectRatio.toLowerCase().equals("variable"))

try
{
String currentDestPath = destPath.getAbsolutePath() + File.separator + lastGroupName.replace(".", File.separator) + ".xml";
// TODO fail if unable to mkdirs (or use commons-io)
destPath.mkdirs();

currentDestPath = currentDestPath.toLowerCase();
currentDestPath = currentDestPath.replaceAll("\\s", "_");
File myDestFile = new File(currentDestPath);
System.out.println("Prepare writing to " + myDestFile);
String fileName = sourceFiles[i].getName();
fileName = fileName.substring(0, fileName.lastIndexOf('.')) + ".xml";
File destFile = new File(destPath, fileName);
System.out.println("Prepare writing to " + destFile);

File myDestRoot = new File(myDestFile.getParent());
myDestRoot.mkdirs();
FileWriter fileWriter = new FileWriter(myDestFile);
// TODO try-with-resource to improve resources management
FileWriter fileWriter = new FileWriter(destFile);
BufferedWriter writer = new BufferedWriter(fileWriter);
writer.write(groupXml);
writer.close();
System.out.println("File written");

if (!destPaths.contains(myDestRoot))
if (!destPaths.contains(destPath))
{
destPaths.add(myDestRoot);
destPaths.add(destPath);
}

}
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/mxgraph/utils/FileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mxgraph.utils;

import java.io.File;
import java.io.IOException;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.Files.lines;
import static java.util.stream.Collectors.joining;

public class FileUtils {

public static final String EOL = System.lineSeparator();

// when switching to JDK11+, use Files#readString instead
public static String fileContent(File file) {
try {
return lines(file.toPath(), UTF_8).collect(joining(EOL));
} catch (IOException e) {
throw new RuntimeException("Unable to read the content of " + file, e);
}
}

}
16 changes: 2 additions & 14 deletions src/main/java/com/mxgraph/xml2js/Xml2Js.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

import static com.mxgraph.utils.FileUtils.EOL;
import static com.mxgraph.utils.FileUtils.fileContent;
import static java.lang.String.format;

public class Xml2Js {
Expand Down Expand Up @@ -53,18 +53,6 @@ public String parse(File source) {
return code;
}

public static final String EOL = System.getProperty("line.separator");

private static String fileContent(File file) {
// TODO java 11 use readString
try {
List<String> strings = Files.readAllLines(file.toPath());
return String.join(EOL, strings);
} catch (IOException e) {
throw new RuntimeException("Unable to read the content of " + file, e);
}
}

private String parse(String shapeStencilXml) {
List<String> lines = parseCodeLines(shapeStencilXml);
return String.join(EOL, lines);
Expand Down
97 changes: 97 additions & 0 deletions src/test/java/com/mxgraph/svg2xml/Svg2XmlTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.mxgraph.svg2xml;

import static com.mxgraph.utils.FileUtils.EOL;
import static com.mxgraph.utils.FileUtils.fileContent;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.Arrays;

class Svg2XmlTest {

@Test
void convertToXml_single_file() {
Svg2Xml svg2Xml = new Svg2Xml();

File destPath = destinationFolder("Simple-Single file");
svg2Xml.convertToXml(svgSourceFiles("simple-01/circle-green.svg"), destPath);

File expectedGeneratedFile = new File(destPath, "circle-green.xml");
assertThat(expectedGeneratedFile).isFile();
String fileContent = fileContent(expectedGeneratedFile);
assertThat(fileContent).startsWith("<shapes name=\"mxgraph.simple-01\">");
assertThat(fileContent).contains(
"<fillcolor color=\"green\"/>",
"<ellipse h=\"200\" w=\"200\" x=\"0\" y=\"0\"/>"
);
}

@Test
void convertToXml_two_files_from_the_same_folder() {
Svg2Xml svg2Xml = new Svg2Xml();

File destPath = destinationFolder("Simple-Two files");
svg2Xml.convertToXml(svgSourceFiles("simple-01/circle-green.svg", "simple-01/rectangle-blue.svg"), destPath);

File expectedGeneratedFile = new File(destPath, "rectangle-blue.xml"); // use base name of the latest svg file in the source folder
assertThat(expectedGeneratedFile).isFile();
String fileContent = fileContent(expectedGeneratedFile);
assertThat(fileContent).startsWith("<shapes name=\"mxgraph.simple-01\">");
assertThat(fileContent).contains(
// 1st shape
"<shape aspect=\"variable\" h=\"200\" name=\"circle-green\"",
// 2nde shape
"<shape aspect=\"variable\" h=\"100\" name=\"rectangle-blue\""
);
}

@Test
void convertToXml_files_from_two_folders_without_subfolders_files_given_ordered_by_folders() {
Svg2Xml svg2Xml = new Svg2Xml();

File destPath = destinationFolder("files from 2 folders - no subfolders");
// in the current implementation, the files are supposed to be passed ordered by folder
svg2Xml.convertToXml(svgSourceFiles("simple-01/circle-green.svg", "simple-01/rectangle-blue.svg", "simple-02/path-blue.svg"), destPath);


// File generated from source files in 'simple-01'
File expected1stGeneratedFile = new File(destPath, "rectangle-blue.xml");
assertThat(expected1stGeneratedFile).isFile();
String contentOfFirstFile = fileContent(expected1stGeneratedFile);
assertThat(contentOfFirstFile).startsWith("<shapes name=\"mxgraph.simple-01\">");

// File generated from source files in 'simple-02'
File expected2ndGeneratedFile = new File(destPath, "path-blue.xml");
assertThat(expected2ndGeneratedFile).isFile();
String contentOf2ndFile = fileContent(expected2ndGeneratedFile);
assertThat(contentOf2ndFile).startsWith("<shapes name=\"mxgraph.simple-02\">");
assertThat(contentOf2ndFile).describedAs("Content of the 2nd generated file").contains(
"<quad x1=\"20\" x2=\"30\" y1=\"0\" y2=\"25\"/>",
"<quad x1=\"40\" x2=\"70\" y1=\"50\" y2=\"25\"/>"
);
}

// =================================================================================================================
// UTILS
// =================================================================================================================

private static void assertFirstLine(String fileContent, String expectedStart, String expectedEnd) {
String firstLine = fileContent.substring(0, fileContent.indexOf(EOL));
assertThat(firstLine).describedAs("1st line of the generated file")
.startsWith(expectedStart)
.endsWith(expectedEnd);
}

private static File[] svgSourceFiles(String... fileNames) {
File parent = new File(System.getProperty("user.dir"), "src/test/resources/svg"); // ensure we pass absolute path
return Arrays.stream(fileNames)
.map(fileName -> new File(parent, fileName))
.toArray(File[]::new);
}

private static File destinationFolder(String folderName) {
return new File("target/test/output/", folderName);
}

}
3 changes: 3 additions & 0 deletions src/test/resources/svg/simple-01/circle-green.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/test/resources/svg/simple-01/rectangle-blue.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/test/resources/svg/simple-02/path-blue.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/test/resources/svg/simple-02/subfolder-02/line-orange.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/test/resources/svg/simple-02/subfolder-02/star-green.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.