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