diff --git a/README.md b/README.md index 3478e4e..ffc9cc8 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Upstream JDL source code is located under `src/main/jdl-core` folder. You can bundle `target/classes/io/zenwave360/jhipster/jdl/jdl-parser.js` using: - `mvn generate-resources` from project root, or -- `npm generate-resources` from `src/main/jdl-core` folder +- `npm run generate-resources` from `src/main/jdl-core` folder ## Custom Extensions diff --git a/src/main/java/io/zenwave360/jhipster/jdl/JDLParser.java b/src/main/java/io/zenwave360/jhipster/jdl/JDLParser.java index 0e8d264..3bdbf09 100644 --- a/src/main/java/io/zenwave360/jhipster/jdl/JDLParser.java +++ b/src/main/java/io/zenwave360/jhipster/jdl/JDLParser.java @@ -1,20 +1,12 @@ package io.zenwave360.jhipster.jdl; -import static com.oracle.truffle.js.runtime.JSContextOptions.ESM_EVAL_RETURNS_EXPORTS_NAME; - import com.oracle.truffle.js.lang.JavaScriptLanguage; import org.graalvm.polyglot.Context; -import org.graalvm.polyglot.Engine; import org.graalvm.polyglot.Source; import org.graalvm.polyglot.Value; -import javax.swing.plaf.synth.SynthDesktopIconUI; import java.io.IOException; -import java.net.URISyntaxException; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -24,45 +16,20 @@ public class JDLParser { + private static Value parseJDL; + public static Map parseJDL(String JDL) throws IOException { return parseJDL(JDL, Collections.emptyMap()); } public static Map parseJDL(String JDL, Map configuration) throws IOException { - System.setProperty("polyglot.engine.WarnInterpreterOnly", "false"); - String jsCode = loadClassPathFile("io/zenwave360/jhipster/jdl/jdl-parser.js"); - Source jdlParsedJsSource = Source.newBuilder(JavaScriptLanguage.ID, jsCode, "jdl-parser.js").build(); - try (Context context = Context.create(JavaScriptLanguage.ID)) { - context.eval(jdlParsedJsSource); - Value parseJDL = context.getBindings(JavaScriptLanguage.ID).getMember("parseJDL"); - Value returned = null; - try { - returned = parseJDL.execute(JDL, configuration); - } catch (Exception e) { - throw new RuntimeException(e.getMessage()); - } - return (Map) copy(returned.as(Map.class)); - } - } - - public static String jdlToMermaid(String JDL) throws IOException { - return jdlToMermaid(JDL, Collections.emptyMap()); - } - public static String jdlToMermaid(String JDL, Map configuration) throws IOException { - System.setProperty("polyglot.engine.WarnInterpreterOnly", "false"); - String jsCode = loadClassPathFile("io/zenwave360/jhipster/jdl/jdl-parser.js"); - Source jdlParsedJsSource = Source.newBuilder(JavaScriptLanguage.ID, jsCode, "jdl-parser.js").build(); - try (Context context = Context.create(JavaScriptLanguage.ID)) { - context.eval(jdlParsedJsSource); - Value jdlToMermaid = context.getBindings(JavaScriptLanguage.ID).getMember("jdlToMermaid"); - Value returned = null; - try { - returned = jdlToMermaid.execute(JDL, configuration); - } catch (Exception e) { - throw new RuntimeException(e.getMessage()); - } - return returned.asString(); + Value returned = null; + try { + returned = getParseJDL().execute(JDL, configuration); + } catch (Exception e) { + throw new RuntimeException(e.getMessage()); } + return (Map) copy(returned.as(Map.class)); } private static Object copy(Object source) { @@ -75,6 +42,18 @@ private static Object copy(Object source) { return source; } + private static Value getParseJDL() throws IOException { + if(parseJDL == null) { + System.setProperty("polyglot.engine.WarnInterpreterOnly", "false"); + String jsCode = loadClassPathFile("io/zenwave360/jhipster/jdl/jdl-parser.js"); + Source jdlParsedJsSource = Source.newBuilder(JavaScriptLanguage.ID, jsCode, "jdl-parser.js").build(); + Context context = Context.create(JavaScriptLanguage.ID); + context.eval(jdlParsedJsSource); + parseJDL = context.getBindings(JavaScriptLanguage.ID).getMember("parseJDL"); + } + return parseJDL; + } + private static String loadClassPathFile(String fileName) throws IOException { try { return new String(JDLParser.class.getClassLoader().getResourceAsStream(fileName).readAllBytes(), StandardCharsets.UTF_8); diff --git a/src/test/java/io/zenwave360/jhipster/jdl/JDLParserTest.java b/src/test/java/io/zenwave360/jhipster/jdl/JDLParserTest.java index 9221216..4980bab 100644 --- a/src/test/java/io/zenwave360/jhipster/jdl/JDLParserTest.java +++ b/src/test/java/io/zenwave360/jhipster/jdl/JDLParserTest.java @@ -46,8 +46,11 @@ public void testLoadAllJDLs() throws IOException { for (String jdlFile : jdlFiles) { String jdlString = loadClassPathFile(jdlFile); try { - System.out.println("Parsing " + jdlFile + " ..."); + System.out.print("Parsing " + jdlFile + " ..."); + long start = System.currentTimeMillis(); Map parsedJDL = JDLParser.parseJDL(jdlString); + long end = System.currentTimeMillis(); + System.out.println(" took " + (end - start) + " ms"); Assert.assertNotNull(parsedJDL); } catch (Exception e) { exceptions.put(jdlFile, e); diff --git a/src/test/java/io/zenwave360/jhipster/jdl/JDLToMermaidTest.java b/src/test/java/io/zenwave360/jhipster/jdl/JDLToMermaidTest.java deleted file mode 100644 index f1417fe..0000000 --- a/src/test/java/io/zenwave360/jhipster/jdl/JDLToMermaidTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package io.zenwave360.jhipster.jdl; - -import org.junit.Assert; -import org.junit.Test; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -public class JDLToMermaidTest { - - @Test - public void testExportToMermaid21PointsJDL() throws IOException { - String jdlString = loadClassPathFile("jdl-samples-main/21-points.jh"); - String mermaid = JDLParser.jdlToMermaid(jdlString); - Assert.assertNotNull(mermaid); - Assert.assertTrue(mermaid.startsWith("classDiagram")); - } - - @Test - public void testExportToMermaidAllJDLs() throws IOException { - Collection jdlFiles = Stream.of(new File("src/test/resources/jdl-samples-main").listFiles()) - .filter(file -> !file.isDirectory() && (file.getName().endsWith(".jh") || file.getName().endsWith(".jdl"))) - .map(f -> "jdl-samples-main/" + f.getName()) - .collect(Collectors.toSet()); - Map exceptions = new HashMap(); - for (String jdlFile : jdlFiles) { - String jdlString = loadClassPathFile(jdlFile); - try { - System.out.println("Parsing " + jdlFile + " ..."); - String mermaid = JDLParser.jdlToMermaid(jdlString); - Assert.assertNotNull(mermaid); - Assert.assertTrue(mermaid.startsWith("classDiagram")); - } catch (Exception e) { - exceptions.put(jdlFile, e); - } - } - Assert.assertEquals(new ArrayList<>(), new ArrayList<>(exceptions.keySet())); - } - - static String loadClassPathFile(String fileName) { - try { - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - java.net.URL url = classLoader.getResource(fileName); - if (url == null) { - throw new Exception("File not found!"); - } - java.nio.file.Path path = java.nio.file.Paths.get(url.toURI()); - return java.nio.file.Files.readString(path); - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } -}