javascript - Save an SVG image from a website using Java (desktop) -


i'm generating website jdenticon using code (test.html):

<!doctype html>    <html>      <head>          <title>test</title>      </head>      <body>          <script src="https://cdn.jsdelivr.net/jdenticon/1.3.2/jdenticon.min.js" async></script>            <svg width="200" height="200" data-jdenticon-hash="ff8adece0631821959f443c9d956fc39">          fallback text browsers not supporting inline svg</svg>        </body>  </html>

i want image generated in code accesible java desktop application , want saved on pc. possible , maybe changing hascode (ff8adece0631821959f443c9d956fc39)

and whats code it?

edit #1:

i've found in api of jdenticon looks , used .net framework:

var engine = new jurassic.scriptengine(); engine.executefile("<path jdenticon.js>"); engine.setglobalvalue("size", 200); engine.setglobalvalue("hash", "ff8adece0631821959f443c9d956fc39");  var svg = engine.evaluate<string>("jdenticon.tosvg(hash, size)"); file.writealltext("testicon.svg", svg); 

i wanted in java , found java scriptengine (tutorials: oracle). don't know how use it, right code looks this:

scriptenginemanager factory = new scriptenginemanager(); scriptengine engine = factory.getenginebyname("javascript");  engine.put("size", 200); engine.put("hash", "ff8adece0631821959f443c9d956fc39");  string svg = (string) engine.eval(new java.io.filereader("c:/jdenticon.js")); 

this won't work because not call jdenticon.tosvg(hash, size) method in jdenticon.js file. if has idea on how solve problem thisit nice.

your updated question including interesting idea run original code inside javascript interpreter apparently built in java se, made me pursue approach further, resulting in following code have tested generate svg document text, @ least on computer generates expected svg icon image.

import java.io.filenotfoundexception; import java.io.filereader; import javax.script.*;  public class jdenticonclient {     public static void main(string args[]) throws filenotfoundexception, scriptexception {         string svgtext = new jdenticonclient().getsvgdocumenttext("ff8adece0631821959f443c9d956fc39", 200);         system.out.print(svgtext);     }     public string getsvgdocumenttext(string hash, int size) throws filenotfoundexception, scriptexception {         scriptenginemanager factory = new scriptenginemanager();         scriptengine scriptengine = factory.getenginebyname("javascript");         scriptengine.eval(new filereader("jdenticon.min.js"));         return scriptengine.eval("jdenticon.tosvg(\"" + hash + "\", \"" + size + "\")").tostring();     } } 

even though approached same solution, have consider following:

  1. the put method exposes additional script host objects scripts being evaluated. entire point using script engines custom host not web browser set of apis or node.js or that. basically, can create own apis using put or simulate existing ones -- yes, replicate entire browser api set, making application scripting host compatible modern browser.
  2. i compose evaluated string myself, technicality -- exposing native hash , size string , int objects respectively, script using put have worked well, have globals, not smart thing do, given how these natural one-shot parameters tosvg procedure.
  3. even though correctly included jdenticon script evaluation, may have not taken account default, merely including (and running) script calls jdenticon procedure looks canvas , svg elements inside dom tree, in case not exist. in fact, impressed evaluation of contents of script file did not abort mid-way due exception assumptions dom made. that's because script made work node.js well, not have dom either.
  4. the code above can optimized not have create new factory , new script engine every time needs generate svg. leave exercise.
  5. the getsvgdocumenttext returns entire generated svg document text. you'd have add logic if want e.g. save said text *.svg file.

p.s. pointing me out in direction of scriptengine. didn't know java had one. nice know, never know.


Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -