From fe18e34ce4fd04719ff408f20ccde9137d9621e1 Mon Sep 17 00:00:00 2001
From: rschoene <rene.schoene@tu-dresden.de>
Date: Thu, 24 Feb 2022 16:10:53 +0100
Subject: [PATCH] 0.2

- use packaged plantuml to create png files per default
- add option "--printSource" to create a md file instead
---
 grammar2uml/build.gradle                      |  1 +
 .../grammar2uml/compiler/Compiler.java        | 62 +++++++++++--------
 .../resources/grammar2umlVersion.properties   |  4 +-
 3 files changed, 40 insertions(+), 27 deletions(-)

diff --git a/grammar2uml/build.gradle b/grammar2uml/build.gradle
index 3522119..fc69ae6 100644
--- a/grammar2uml/build.gradle
+++ b/grammar2uml/build.gradle
@@ -24,6 +24,7 @@ jar {
 
 dependencies {
     implementation project(':relast.preprocessor')
+    implementation fileTree(include: ['plantuml.jar'], dir: '../libs')
 
     implementation group: 'com.github.spullara.mustache.java', name: 'compiler', version: "${mustache_java_version}"
     runtime group: 'org.jastadd', name: 'jastadd', version: '2.3.4'
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 f318da1..03fcfea 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
@@ -1,20 +1,20 @@
 package de.tudresden.inf.st.jastadd.grammar2uml.compiler;
 
 import beaver.Parser;
-import org.jastadd.option.BooleanOption;
-import org.jastadd.option.ValueOption;
-import org.jastadd.relast.compiler.AbstractCompiler;
-import org.jastadd.relast.compiler.CompilerException;
 import de.tudresden.inf.st.jastadd.grammar2uml.ast.ErrorMessage;
+import de.tudresden.inf.st.jastadd.grammar2uml.ast.Grammar2Uml;
 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.ast.Grammar2Uml;
 import de.tudresden.inf.st.jastadd.grammar2uml.parser.Grammar2UmlParser;
 import de.tudresden.inf.st.jastadd.grammar2uml.scanner.Grammar2UmlScanner;
+import net.sourceforge.plantuml.SourceStringReader;
+import org.jastadd.option.BooleanOption;
+import org.jastadd.option.ValueOption;
+import org.jastadd.relast.compiler.AbstractCompiler;
+import org.jastadd.relast.compiler.CompilerException;
 
 import java.io.BufferedReader;
 import java.io.IOException;
-import java.io.PrintWriter;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -26,6 +26,7 @@ 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;
@@ -67,16 +68,6 @@ public class Compiler extends AbstractCompiler {
     System.out.println(message);
   }
 
-  private void writeToFile(String filename, String str) throws CompilerException {
-    try {
-      PrintWriter writer = new PrintWriter(filename);
-      writer.print(str);
-      writer.close();
-    } catch (Exception e) {
-      throw new CompilerException("Could not write to file " + filename, e);
-    }
-  }
-
   protected void initOptions() {
     optionOutputFile = addOption(
         new ValueOption("output", "target file to be generated.")
@@ -90,6 +81,9 @@ public class Compiler extends AbstractCompiler {
         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));
@@ -160,13 +154,7 @@ public class Compiler extends AbstractCompiler {
 
     printMessage("Running grammar2uml " + readVersion());
 
-    String output;
-    if (optionOutputFile.isMatched()) {
-      output = optionOutputFile.value();
-    } else {
-      output = "uml.md";
-      System.out.println("No output output file is set. Assuming '" + output + "'.");
-    }
+    Path destination = getDestinationPath();
     Path parent = Paths.get(optionOutputFile.value()).toAbsolutePath().getParent();
     try {
       Files.createDirectories(parent);
@@ -188,8 +176,32 @@ public class Compiler extends AbstractCompiler {
       System.exit(1);
     }
 
-    printMessage("Writing output file " + output);
-    writeToFile(output, grammar2uml.generateAspect());
+    printMessage("Writing output file " + destination);
+
+    String sourceCode = grammar2uml.generateAspect();
+
+    try {
+      if (optionPrintSource.isMatched()) {
+        Files.writeString(destination, sourceCode);
+      } else {
+        SourceStringReader reader = new SourceStringReader(sourceCode);
+        reader.outputImage(Files.newOutputStream(destination));
+      }
+    } catch (Exception e) {
+      throw new CompilerException("Could not write to file " + destination, e);
+    }
+
     return 0;
   }
+
+  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();
+  }
 }
diff --git a/grammar2uml/src/main/resources/grammar2umlVersion.properties b/grammar2uml/src/main/resources/grammar2umlVersion.properties
index 3a40866..b3b2086 100644
--- a/grammar2uml/src/main/resources/grammar2umlVersion.properties
+++ b/grammar2uml/src/main/resources/grammar2umlVersion.properties
@@ -1,2 +1,2 @@
-#Fri Jan 15 20:11:10 CET 2021
-version=0.1.1
+#Thu Feb 24 16:08:53 CET 2022
+version=0.2
-- 
GitLab