]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document/src/org/simantics/document/PhantomJSDriver.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.document / src / org / simantics / document / PhantomJSDriver.java
1 package org.simantics.document;\r
2 \r
3 import java.io.File;\r
4 import java.io.IOException;\r
5 import java.io.InputStream;\r
6 import java.io.InputStreamReader;\r
7 import java.net.URL;\r
8 import java.net.URLDecoder;\r
9 import java.nio.charset.Charset;\r
10 import java.nio.file.Files;\r
11 \r
12 import org.eclipse.core.runtime.FileLocator;\r
13 import org.eclipse.core.runtime.IPath;\r
14 import org.eclipse.core.runtime.Path;\r
15 import org.osgi.framework.BundleContext;\r
16 import org.simantics.Simantics;\r
17 \r
18 public class PhantomJSDriver {\r
19 \r
20         public static File fileRef(String fileName) throws IOException {\r
21                 \r
22                 BundleContext context = Activator.getContext();\r
23         IPath path = new Path("/" + fileName);\r
24         URL libURL = FileLocator.find(context.getBundle(), path, null);\r
25         URL fileURL = FileLocator.toFileURL(libURL);\r
26         return new File(URLDecoder.decode(fileURL.getPath(), "UTF-8"));\r
27         \r
28         }\r
29         \r
30         public static String printCommand(String margin, String url, String outFile) throws IOException {\r
31 \r
32                 try {\r
33                         File f = fileRef("print.js");\r
34                         String template = new String(Files.readAllBytes(f.toPath()));\r
35                         template = template.replace("%%url", url);\r
36                         template = template.replace("%%margin", margin);\r
37                         template = template.replace("%%file", outFile.replace("\\", "/"));\r
38                         return template;\r
39                 } catch (IOException e) {\r
40                         return null;\r
41                 }\r
42                 \r
43         }\r
44         \r
45         public static void print(String html, DocumentSettings settings, File output) throws IOException {\r
46                 \r
47         File htmlFile = Simantics.getTempfile("PhantomJSDriver", "html");\r
48                 Files.write(htmlFile.toPath(), html.getBytes(Charset.forName("UTF-8")));\r
49                 \r
50                 String browserUrl = htmlFile.toURI().toURL().toString();\r
51                 \r
52         File script = Simantics.getTempfile("PhantomJSDriver", "script");\r
53         \r
54         String margin = "{left:\"" + settings.marginLeft + "mm\", right:\"" + settings.marginRight + "mm\", top:\"" + settings.marginTop + "mm\", bottom:\"" + settings.marginBottom + "mm\"}";\r
55         \r
56                 String printCommand = PhantomJSDriver.printCommand(margin, browserUrl, output.getAbsolutePath());\r
57         \r
58         Files.write(script.toPath(), printCommand.getBytes());\r
59                 PhantomJSDriver.execute(script);\r
60 \r
61         }\r
62         \r
63         public static boolean execute(File javascript) {\r
64                 \r
65                 try {\r
66                         \r
67                 File filePath = fileRef("phantomjs.exe");\r
68                 \r
69                         String[] args = { filePath.getAbsolutePath(),\r
70                                         javascript.getAbsolutePath()                                    \r
71                         };\r
72                         Process process = new ProcessBuilder(args).start();\r
73                         try {\r
74                                 InputStream input = process.getInputStream();\r
75                                 InputStreamReader reader = new InputStreamReader(input);\r
76                                 //StringBuilder sb = new StringBuilder();\r
77                                 while (true) {\r
78                                         try {\r
79                                                 while (reader.ready()) {\r
80                                                         int r = reader.read();\r
81                                                 }\r
82                                                 \r
83                                                 int error = process.exitValue();\r
84                                                 \r
85                                                 reader.close();\r
86                                                 return error == 0;\r
87                                                 \r
88                                         } catch (IllegalThreadStateException e) {\r
89                                                 Thread.sleep(100);\r
90                                         }\r
91                                 }\r
92                         } finally {\r
93                                 process.destroy();\r
94                         }\r
95                 } catch(IOException e) {\r
96                         e.printStackTrace();\r
97                 } catch (InterruptedException e) {\r
98                         e.printStackTrace();\r
99                 }\r
100                 return false;\r
101 \r
102         }\r
103         \r
104 }\r