Skip to content

Commit

Permalink
Merge pull request #76 from metanorma/issue/73
Browse files Browse the repository at this point in the history
Issue/73
  • Loading branch information
Intelligent2013 authored Oct 9, 2023
2 parents 1759691 + f901b4b commit 51f7177
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 70 deletions.
6 changes: 4 additions & 2 deletions src/main/java/org/metanorma/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
public final class Constants {
public static final int ERROR_EXIT_CODE = -1;

static final String FORMAT = "adoc";
static final String DOCUMENT_ADOC = "document." + FORMAT;
public static final String XML_EXTENSION = ".xml";

public static final String FORMAT = "adoc";
public static final String DOCUMENT_ADOC = "document." + FORMAT;
}
81 changes: 81 additions & 0 deletions src/main/java/org/metanorma/SVGGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.metanorma;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

public class SVGGenerator {

final String SVG_EXTENSION = ".svg";

public void generateSVG(String xmlFile, String image, String outPath, boolean isSVGmap) {
try {
String content = new String(Files.readAllBytes(Paths.get(xmlFile)));
//if (content.contains("img.area")) {
if (content.contains("imgfile.content")) {
String folder = new File(xmlFile).getParent() + File.separator;
//String svgFilename = xmlFile.substring(0, xmlFile.length() - XML_EXTENSION.length()) + SVG_EXTENSION;
String svgFilename = Util.changeFileExtension(xmlFile, SVG_EXTENSION);
if (outPath != null && !outPath.isEmpty()) {
if (!(outPath.toLowerCase().endsWith(SVG_EXTENSION) || outPath.toLowerCase().endsWith(Constants.XML_EXTENSION))) { // if folder

String xmlFilename = Paths.get(xmlFile).getFileName().toString();
svgFilename = Util.changeFileExtension(xmlFilename, SVG_EXTENSION);

String schemaName = "";
if (!isSVGmap) { //no need to add schamaName for [.svgmap] SVG
schemaName = new File(xmlFile).getParentFile().getName();
Files.createDirectories(Paths.get(outPath, schemaName));
}

svgFilename = Paths.get(outPath, schemaName, svgFilename).toString();
} else {
svgFilename = outPath;
String parentFolder = new File(svgFilename).getParent();
if (parentFolder != null && !parentFolder.isEmpty()) {
Files.createDirectories(Paths.get(parentFolder));
}
}
}
System.out.println("Generate SVG file for " + xmlFile + "...");


// get linearized XML with default attributes substitution from DTD
String linearizedXML = XMLUtils.processLinearizedXML(xmlFile);
// load linearized xml
Source src = new StreamSource(new StringReader(linearizedXML));

Source srcXSL = new StreamSource(Util.getStreamFromResources(getClass().getClassLoader(), "map2svg.xsl"));

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer = factory.newTransformer(srcXSL);

transformer.setParameter("path", folder);
if (image != null) {
transformer.setParameter("image", image);
}

StringWriter resultWriter = new StringWriter();
StreamResult sr = new StreamResult(resultWriter);

transformer.transform(src, sr);
String xmlSVG = resultWriter.toString();
new File(svgFilename).getParentFile().mkdirs();
try (
BufferedWriter writer = Files.newBufferedWriter(Paths.get(svgFilename))) {
writer.write(xmlSVG);
}
System.out.println("SVG saved in " + svgFilename + ".");
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
72 changes: 4 additions & 68 deletions src/main/java/org/metanorma/stepmod2mn.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class stepmod2mn {
static final String OUTPUT_LOG = "Output: %s (%s)";

static final String SVG_EXTENSION = ".svg";
static final String XML_EXTENSION = ".xml";

static boolean DEBUG = false;

static String VER = Util.getAppVersion();
Expand Down Expand Up @@ -559,78 +559,14 @@ public void generateSVG(String xmlFilePath, String image, String outPath, boolea
xmlFiles = walk
.filter(p -> !Files.isDirectory(p))
.map(p -> p.toString())
.filter(f -> f.toLowerCase().endsWith(XML_EXTENSION))
.filter(f -> f.toLowerCase().endsWith(Constants.XML_EXTENSION))
.collect(Collectors.toList());
} catch (Exception e)
{
} catch (Exception e) {
System.out.println("Can't generate SVG file(s) for " + xmlFilePath + "...");
e.printStackTrace();
}
for(String xmlFile: xmlFiles) {
try
{
String content = new String(Files.readAllBytes(Paths.get(xmlFile)));
if (content.contains("img.area")) {
String folder = new File(xmlFile).getParent() + File.separator;
//String svgFilename = xmlFile.substring(0, xmlFile.length() - XML_EXTENSION.length()) + SVG_EXTENSION;
String svgFilename = Util.changeFileExtension(xmlFile, SVG_EXTENSION);
if (outPath != null && !outPath.isEmpty()) {
if (!(outPath.toLowerCase().endsWith(SVG_EXTENSION) || outPath.toLowerCase().endsWith(XML_EXTENSION))) { // if folder

String xmlFilename = Paths.get(xmlFile).getFileName().toString();
svgFilename = Util.changeFileExtension(xmlFilename,SVG_EXTENSION);

String schemaName = "";
if (!isSVGmap) { //no need to add schamaName for [.svgmap] SVG
schemaName = new File(xmlFile).getParentFile().getName();
Files.createDirectories(Paths.get(outPath, schemaName));
}

svgFilename = Paths.get(outPath, schemaName, svgFilename).toString();
} else {
svgFilename = outPath;
String parentFolder = new File(svgFilename).getParent();
if (parentFolder != null && !parentFolder.isEmpty()) {
Files.createDirectories(Paths.get(parentFolder));
}
}
}
System.out.println("Generate SVG file for " + xmlFile + "...");


// get linearized XML with default attributes substitution from DTD
String linearizedXML = XMLUtils.processLinearizedXML(xmlFile);
// load linearized xml
Source src = new StreamSource(new StringReader(linearizedXML));

Source srcXSL = new StreamSource(Util.getStreamFromResources(getClass().getClassLoader(), "map2svg.xsl"));

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer = factory.newTransformer(srcXSL);

transformer.setParameter("path", folder);
if (image != null) {
transformer.setParameter("image", image);
}

StringWriter resultWriter = new StringWriter();
StreamResult sr = new StreamResult(resultWriter);

transformer.transform(src, sr);
String xmlSVG = resultWriter.toString();
new File(svgFilename).getParentFile().mkdirs();
try (
BufferedWriter writer = Files.newBufferedWriter(Paths.get(svgFilename))) {
writer.write(xmlSVG);
}
System.out.println("SVG saved in " + svgFilename + ".");
}
}
catch (Exception e)
{
e.printStackTrace();
}
new SVGGenerator().generateSVG(xmlFile, image, outPath, isSVGmap);
}
//xmlFiles.forEach(x -> System.out.println(x));
}
Expand Down

0 comments on commit 51f7177

Please sign in to comment.