├── COPYING ├── LICENSE ├── README.md ├── javascript └── viz.js ├── src └── ch │ └── braincell │ └── viz │ ├── NashornVizJS.java │ ├── V8VizJS.java │ ├── VizJS.java │ └── VizJSException.java └── vizjs.jar /COPYING: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS MODIFIED BSD 2 | LICENSE ("AGREEMENT"). 3 | 4 | ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES 5 | RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 6 | 7 | ======================================================================== 8 | 9 | All rights reserved. 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are met: 13 | * Redistributions of source code must retain the above copyright 14 | notice, this list of conditions and the following disclaimer. 15 | * Redistributions in binary form must reproduce the above copyright 16 | notice, this list of conditions and the following disclaimer in the 17 | documentation and/or other materials provided with the distribution. 18 | * Neither the name of the University of California, Berkeley nor the 19 | names of its contributors may be used to endorse or promote products 20 | derived from this software without specific prior written permission. 21 | 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 23 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 26 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 18 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 21 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vizjs 2 | 3 | Integration of the magic project https://github.com/mdaines/viz.js for PlantUML. 4 | 5 | See http://plantuml.com/vizjs -------------------------------------------------------------------------------- /src/ch/braincell/viz/NashornVizJS.java: -------------------------------------------------------------------------------- 1 | /* Licensed under the Revised BSD License (the Revised Berkeley Software Distribution) 2 | * 3 | * Redistribution and use in source and binary forms, with or without 4 | * modification, are permitted provided that the following conditions are met: 5 | * 6 | * * Redistributions of source code must retain the above copyright 7 | * notice, this list of conditions and the following disclaimer. 8 | * * Redistributions in binary form must reproduce the above copyright 9 | * notice, this list of conditions and the following disclaimer in the 10 | * documentation and/or other materials provided with the distribution. 11 | * * Neither the name of the University of California, Berkeley nor the 12 | * names of its contributors may be used to endorse or promote products 13 | * derived from this software without specific prior written permission. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY 16 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY 19 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | * 26 | * 27 | * Original Author: Andreas Studer 28 | */ 29 | package ch.braincell.viz; 30 | 31 | import java.io.IOException; 32 | import java.util.Collection; 33 | import java.util.Map; 34 | 35 | import javax.script.Invocable; 36 | import javax.script.ScriptEngine; 37 | import javax.script.ScriptEngineManager; 38 | import javax.script.ScriptException; 39 | 40 | /** 41 | * Nashorn implementation for viz.js. At the moment not active since it is 42 | * awfully slow with bigger .dot files. 43 | * 44 | * @author Andreas Studer 45 | * 46 | */ 47 | class NashornVizJS extends VizJS { 48 | // JS variable to collect messages form viz.js in case of fatal errors. 49 | private static final String JSMESSAGESARRAY = "messages"; 50 | // JS array initialization. 51 | private static final String JSMESSAGESARRAYINIT = "messages=[];"; 52 | // Overwrite JS print function for collecting messages out of viz.js. 53 | private static final String JSPRINTFUNCTION = "var messages=[]; print=function(s){messages.push(s);};"; 54 | 55 | private ScriptEngine engine; 56 | private Invocable invoke; 57 | 58 | /** 59 | * Initialization of Nashorn scripting engine. 60 | * ScriptExceptions will be wrapped into VizJSException. 61 | * @throws IOException 62 | */ 63 | public NashornVizJS() throws IOException { 64 | 65 | engine = new ScriptEngineManager().getEngineByName("nashorn"); 66 | try { 67 | engine.eval(JSPRINTFUNCTION); 68 | engine.eval(getVizCode()); 69 | } catch (ScriptException e) { 70 | throw new VizJSException("Initializing of Nashorn failed (script error).", e); 71 | } 72 | invoke = (Invocable) engine; 73 | } 74 | 75 | @Override 76 | public String execute(String dot) { 77 | try { 78 | engine.eval(JSMESSAGESARRAYINIT); 79 | return invoke.invokeFunction("Viz", dot).toString(); 80 | } catch (NoSuchMethodException | ScriptException e) { 81 | try { 82 | @SuppressWarnings("unchecked") 83 | Collection messages = ((Map) engine.eval(JSMESSAGESARRAY)).values(); 84 | if (messages != null && !messages.isEmpty()) { 85 | // Now something really bad happened: viz.js tells us 86 | // something 87 | // with printing (e.g. Abort). 88 | String summary = ""; 89 | for (Object line : messages) { 90 | summary += line + "\n"; 91 | } 92 | throw new VizJSException(summary, e); 93 | } 94 | } catch (ScriptException e1) { 95 | throw new VizJSException("Problems getting messages in engine: " + getVersion(), e1); 96 | } 97 | throw new VizJSException("Problems executing function viz.js in engine: " + getVersion(), e); 98 | } 99 | } 100 | 101 | @SuppressWarnings("static-access") 102 | @Override 103 | public String getVersion() { 104 | return engine.ENGINE + " " + engine.ENGINE_VERSION; 105 | } 106 | 107 | @Override 108 | protected void release() { 109 | // Nothing to release for Nashorn. 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /src/ch/braincell/viz/V8VizJS.java: -------------------------------------------------------------------------------- 1 | /* Licensed under the Revised BSD License (the Revised Berkeley Software Distribution) 2 | * 3 | * Redistribution and use in source and binary forms, with or without 4 | * modification, are permitted provided that the following conditions are met: 5 | * 6 | * * Redistributions of source code must retain the above copyright 7 | * notice, this list of conditions and the following disclaimer. 8 | * * Redistributions in binary form must reproduce the above copyright 9 | * notice, this list of conditions and the following disclaimer in the 10 | * documentation and/or other materials provided with the distribution. 11 | * * Neither the name of the University of California, Berkeley nor the 12 | * names of its contributors may be used to endorse or promote products 13 | * derived from this software without specific prior written permission. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY 16 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY 19 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | * 26 | * 27 | * Original Author: Andreas Studer 28 | */ 29 | package ch.braincell.viz; 30 | 31 | import java.io.IOException; 32 | 33 | import com.eclipsesource.v8.V8; 34 | import com.eclipsesource.v8.V8Array; 35 | import com.eclipsesource.v8.V8Function; 36 | import com.eclipsesource.v8.utils.V8ObjectUtils; 37 | 38 | /** 39 | * Create a wrapper with J2V8 and viz.js. 40 | * 41 | * @author Andreas Studer 42 | * 43 | */ 44 | class V8VizJS extends VizJS { 45 | // JS variable to collect messages form viz.js in case of fatal errors. 46 | private static final String JSMESSAGESARRAY = "messages"; 47 | // Splice JS array to reduce size. 48 | private static final String JSMESSAGESARRAYSPLICE = "messages.splice(0, 100);"; 49 | // Overwrite JS print function for collecting messages out of viz.js. 50 | private static final String JSPRINTFUNCTION = "var messages=[]; print=function(s){messages.push(s);};"; 51 | 52 | private V8 runtime; 53 | private V8Array messages; 54 | private V8Function vizFunction; 55 | 56 | /** 57 | * creates and initializes V8 engine with viz.js 58 | * 59 | * @throws IOException 60 | */ 61 | public V8VizJS() throws IOException { 62 | runtime = V8.createV8Runtime(); 63 | runtime.executeVoidScript(JSPRINTFUNCTION); 64 | messages = runtime.getArray(JSMESSAGESARRAY); 65 | runtime.executeVoidScript(getVizCode()); 66 | vizFunction = (V8Function) runtime.getObject("Viz"); 67 | } 68 | 69 | @Override 70 | public String execute(String dot) { 71 | V8Array parameters = new V8Array(runtime).push(dot); 72 | try { 73 | runtime.executeVoidScript(JSMESSAGESARRAYSPLICE); 74 | return (String) vizFunction.call(runtime, parameters); 75 | } catch (Exception e) { 76 | 77 | if (messages.length() > 0) { 78 | // Now something really bad happened: viz.js tells us something 79 | // with printing (e.g. Abort). 80 | String summary = ""; 81 | for (int i = 0; i < messages.length(); i++) { 82 | summary += V8ObjectUtils.getValue(messages, i) + "\n"; 83 | } 84 | throw new VizJSException(summary, e); 85 | } 86 | 87 | throw new VizJSException("Problems executing function viz.js in engine: " + getVersion(), e); 88 | } finally { 89 | parameters.release(); 90 | } 91 | } 92 | 93 | @Override 94 | public void release() { 95 | vizFunction.release(); 96 | messages.release(); 97 | runtime.release(true); 98 | } 99 | 100 | @Override 101 | public String getVersion() { 102 | return "J2V8 build: " + runtime.getBuildID(); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/ch/braincell/viz/VizJS.java: -------------------------------------------------------------------------------- 1 | /* Licensed under the Revised BSD License (the Revised Berkeley Software Distribution) 2 | * 3 | * Redistribution and use in source and binary forms, with or without 4 | * modification, are permitted provided that the following conditions are met: 5 | * 6 | * * Redistributions of source code must retain the above copyright 7 | * notice, this list of conditions and the following disclaimer. 8 | * * Redistributions in binary form must reproduce the above copyright 9 | * notice, this list of conditions and the following disclaimer in the 10 | * documentation and/or other materials provided with the distribution. 11 | * * Neither the name of the University of California, Berkeley nor the 12 | * names of its contributors may be used to endorse or promote products 13 | * derived from this software without specific prior written permission. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY 16 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY 19 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | * 26 | * 27 | * Original Author: Andreas Studer 28 | */ 29 | package ch.braincell.viz; 30 | 31 | import java.io.BufferedReader; 32 | import java.io.IOException; 33 | import java.io.InputStreamReader; 34 | import java.util.stream.Collectors; 35 | 36 | /** 37 | * Simple access class for viz.js; either J2V8 if available for the platform or 38 | * Nashorn (JRE8). 39 | * 40 | * @author Andreas Studer 41 | * 42 | */ 43 | public abstract class VizJS { 44 | 45 | /** 46 | * creates an instance of the wrapper with the JS-engine. Every thread needs 47 | * one engine instance. It's not possible to use one instance in multiple 48 | * treads. 49 | * 50 | * @return JS-engine wrapper, pre-loaded with viz.js 51 | */ 52 | public static VizJS create() { 53 | try { 54 | return new V8VizJS(); 55 | } catch (IOException e) { 56 | throw new VizJSException("Loading of viz.js resource in .jar failed!", e); 57 | } 58 | } 59 | 60 | /** 61 | * Execute dot string. 62 | * 63 | * @param dot 64 | * definition in the dot language (see GraphViz). 65 | * @return svg presentation. 66 | */ 67 | public abstract String execute(String dot); 68 | 69 | /** 70 | * Returns engine initiated. 71 | * @return engine description 72 | */ 73 | public abstract String getVersion(); 74 | 75 | /** 76 | * Release all resources kept by the JavaScript engine. This is mainly 77 | * important for native bound engines (e.g. J2V8). 78 | */ 79 | protected abstract void release(); 80 | 81 | /** 82 | * Close resources if class gets finalized through GC (Not sure if this is 83 | * the right way to do though). 84 | */ 85 | @Override 86 | protected void finalize() throws Throwable { 87 | super.finalize(); 88 | release(); 89 | System.out.println("released..."); 90 | }; 91 | 92 | /** 93 | * Load the viz.js library as String. 94 | * 95 | * @return viz.js as String 96 | * @throws IOException 97 | * Exception if viz.js as resource is not found in classpath. 98 | */ 99 | protected String getVizCode() throws IOException { 100 | try (final BufferedReader read = new BufferedReader( 101 | new InputStreamReader(getClass().getResourceAsStream("/javascript/viz.js"), "UTF-8"))) { 102 | return read.lines().collect(Collectors.joining("\n")); 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/ch/braincell/viz/VizJSException.java: -------------------------------------------------------------------------------- 1 | /* Licensed under the Revised BSD License (the Revised Berkeley Software Distribution) 2 | * 3 | * Redistribution and use in source and binary forms, with or without 4 | * modification, are permitted provided that the following conditions are met: 5 | * 6 | * * Redistributions of source code must retain the above copyright 7 | * notice, this list of conditions and the following disclaimer. 8 | * * Redistributions in binary form must reproduce the above copyright 9 | * notice, this list of conditions and the following disclaimer in the 10 | * documentation and/or other materials provided with the distribution. 11 | * * Neither the name of the University of California, Berkeley nor the 12 | * names of its contributors may be used to endorse or promote products 13 | * derived from this software without specific prior written permission. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY 16 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY 19 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | * 26 | * 27 | * Original Author: Andreas Studer 28 | */ 29 | package ch.braincell.viz; 30 | 31 | /** 32 | * Runtime exception for anything related to viz js. 33 | * @author Andreas Studer 34 | * 35 | */ 36 | public class VizJSException extends RuntimeException { 37 | private static final long serialVersionUID = 2213268325822980962L; 38 | 39 | public VizJSException(String message) { 40 | super(message); 41 | } 42 | 43 | public VizJSException(String message, Throwable cause) { 44 | super(message, cause); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /vizjs.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plantuml/vizjs/8fa2266f8ca8fe0595c33f495106e77571e4308e/vizjs.jar --------------------------------------------------------------------------------