From 4678d0142a3b4f490341baef512bf1a14cdc146a Mon Sep 17 00:00:00 2001
From: rschoene <rene.schoene@tu-dresden.de>
Date: Sat, 21 May 2022 15:42:20 +0200
Subject: [PATCH] Allow svg/pdf/html, cleanup.

---
 grammar2uml/build.gradle                      |  5 +-
 .../grammar2uml/compiler/Compiler.java        | 80 +++++++++++--------
 .../resources/grammar2umlVersion.properties   |  4 +-
 3 files changed, 51 insertions(+), 38 deletions(-)

diff --git a/grammar2uml/build.gradle b/grammar2uml/build.gradle
index fcef177..15e659f 100644
--- a/grammar2uml/build.gradle
+++ b/grammar2uml/build.gradle
@@ -39,7 +39,6 @@ dependencies {
     api group: 'net.sf.beaver', name: 'beaver-rt', version: '0.9.11'
 
     implementation group: 'com.github.spullara.mustache.java', name: 'compiler', version: '0.9.10'
-    // runtime group: 'org.jastadd', name: 'jastadd', version: '2.3.4'
 
     testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.8.2'
     testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.8.2'
@@ -184,13 +183,13 @@ task fatJar(type: Jar) {
     archiveAppendix = "fatjar"
     from sourceSets.main.output
     from {
-        configurations.runtimeClasspath.collect {println it; it.isDirectory() ? it : zipTree(it) }
+        configurations.runtimeClasspath.collect {it.isDirectory() ? it : zipTree(it) }
     }
 
     manifest.attributes "Main-Class": "${mainClassName}"
 }
 
-def versionFile = 'src/main/resources/grammar2umlVersion.properties'
+def versionFile = "src/main/resources/${project.getName()}Version.properties"
 
 try {
     def oldProps = new Properties()
diff --git a/grammar2uml/src/main/java/de/tudresden/inf/st/jastadd/grammar2uml/compiler/Compiler.java b/grammar2uml/src/main/java/de/tudresden/inf/st/jastadd/grammar2uml/compiler/Compiler.java
index 03fcfea..1b6bd40 100644
--- a/grammar2uml/src/main/java/de/tudresden/inf/st/jastadd/grammar2uml/compiler/Compiler.java
+++ b/grammar2uml/src/main/java/de/tudresden/inf/st/jastadd/grammar2uml/compiler/Compiler.java
@@ -7,6 +7,9 @@ import de.tudresden.inf.st.jastadd.grammar2uml.ast.GrammarFile;
 import de.tudresden.inf.st.jastadd.grammar2uml.ast.Program;
 import de.tudresden.inf.st.jastadd.grammar2uml.parser.Grammar2UmlParser;
 import de.tudresden.inf.st.jastadd.grammar2uml.scanner.Grammar2UmlScanner;
+import net.sourceforge.plantuml.FileFormat;
+import net.sourceforge.plantuml.FileFormatOption;
+import net.sourceforge.plantuml.FileUtils;
 import net.sourceforge.plantuml.SourceStringReader;
 import org.jastadd.option.BooleanOption;
 import org.jastadd.option.ValueOption;
@@ -26,25 +29,24 @@ public class Compiler extends AbstractCompiler {
   private ValueOption optionOutputFile;
   private ValueOption optionInputGrammar2Uml;
   private BooleanOption optionDefaultFolders;
-  private BooleanOption optionPrintSource;
   private BooleanOption optionHelp;
   private BooleanOption optionVersion;
   private BooleanOption optionVerbose;
 
   public Compiler() {
-    super("grammar2uml", false);
+    super("grammar2uml" , false);
   }
 
   /**
    * Reads the version string.
-   *
+   * <p>
    * The version string is read from the property file
    * src/main/resources/Version.properties. This
    * file should be generated during the build process. If it is missing
    * then there is some problem in the build script.
    *
-   * @author Jesper Öqvist <jesper.oqvist@cs.lth.se>
    * @return the read version string, or <code>version ?</code>
+   * @author Jesper Öqvist <jesper.oqvist@cs.lth.se>
    */
   private String readVersion() {
     try {
@@ -55,7 +57,7 @@ public class Compiler extends AbstractCompiler {
   }
 
   public static void main(String[] args) {
-    System.setProperty("mustache.debug", "true");
+    System.setProperty("mustache.debug" , "true");
     try {
       new Compiler().run(args);
     } catch (CompilerException e) {
@@ -70,29 +72,26 @@ public class Compiler extends AbstractCompiler {
 
   protected void initOptions() {
     optionOutputFile = addOption(
-        new ValueOption("output", "target file to be generated.")
-            .defaultValue("uml.md")
+        new ValueOption("output" , "target file to be generated.")
+            .defaultValue("uml.png")
             .acceptAnyValue()
             .needsValue(false));
     optionInputGrammar2Uml = addOption(
-        new ValueOption("inputGrammar2Uml", "grammar2uml definition file.")
+        new ValueOption("inputGrammar2Uml" , "grammar2uml definition file.")
             .needsValue(true));
     optionDefaultFolders = addOption(
-        new BooleanOption("defaultFolders",
+        new BooleanOption("defaultFolders" ,
             "Creates a default folder per grammar file.")
-        .defaultValue(false));
-    optionPrintSource = addOption(
-        new BooleanOption("printSource", "The output will be the source code instead of a png.")
             .defaultValue(false));
     optionHelp = addOption(
-        new BooleanOption("help", "Print usage and exit.")
-        .defaultValue(false));
+        new BooleanOption("help" , "Print usage and exit.")
+            .defaultValue(false));
     optionVersion = addOption(
-        new BooleanOption("version", "Print version and exit.")
-        .defaultValue(false));
+        new BooleanOption("version" , "Print version and exit.")
+            .defaultValue(false));
     optionVerbose = addOption(
-        new BooleanOption("verbose", "Print more messages while compiling.")
-        .defaultValue(false));
+        new BooleanOption("verbose" , "Print more messages while compiling.")
+            .defaultValue(false));
   }
 
   private Grammar2Uml parseProgram() throws CompilerException {
@@ -180,28 +179,43 @@ public class Compiler extends AbstractCompiler {
 
     String sourceCode = grammar2uml.generateAspect();
 
-    try {
-      if (optionPrintSource.isMatched()) {
-        Files.writeString(destination, sourceCode);
-      } else {
+    //
+    String extension = fileExtensionOf(destination).toUpperCase();
+    FileFormatOption plantUmlOption = null;
+    switch (extension) {
+      case "MD":
+        try {
+          Files.writeString(destination, sourceCode);
+        } catch (Exception e) {
+          throw new CompilerException("Could not write to file " + destination, e);
+        }
+        break;
+      case "HTML":
+      case "PNG":
+      case "PDF":
+      case "SVG":
+        plantUmlOption = new FileFormatOption(FileFormat.valueOf(extension));
+        break;
+    }
+    if (plantUmlOption != null) {
+      try {
         SourceStringReader reader = new SourceStringReader(sourceCode);
-        reader.outputImage(Files.newOutputStream(destination));
+        reader.outputImage(Files.newOutputStream(destination), plantUmlOption);
+      } catch (Exception e) {
+        throw new CompilerException("Could not write to file " + destination, e);
       }
-    } catch (Exception e) {
-      throw new CompilerException("Could not write to file " + destination, e);
     }
 
     return 0;
   }
 
+  private String fileExtensionOf(Path path) {
+    String fileName = path.toFile().getName();
+    int lastIndexOfDot = fileName.lastIndexOf(".");
+    return fileName.substring(lastIndexOfDot + 1);
+  }
+
   private Path getDestinationPath() {
-    final String outputFileName;
-    if (optionOutputFile.isMatched()) {
-      outputFileName = optionOutputFile.value();
-    } else {
-      outputFileName = optionPrintSource.isMatched() ? "uml.md" : "uml.png";
-      System.out.println("No output output file is set. Assuming '" + outputFileName + "'.");
-    }
-    return Paths.get(outputFileName).toAbsolutePath();
+    return Paths.get(optionOutputFile.value()).toAbsolutePath();
   }
 }
diff --git a/grammar2uml/src/main/resources/grammar2umlVersion.properties b/grammar2uml/src/main/resources/grammar2umlVersion.properties
index c4dd714..04f87fb 100644
--- a/grammar2uml/src/main/resources/grammar2umlVersion.properties
+++ b/grammar2uml/src/main/resources/grammar2umlVersion.properties
@@ -1,2 +1,2 @@
-#Tue Mar 22 10:45:01 CET 2022
-version=0.2.3
+#Sat May 21 15:41:29 CEST 2022
+version=0.2.4
-- 
GitLab