-package org.simantics.document;\r
-\r
-import java.io.File;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.io.InputStreamReader;\r
-import java.net.URL;\r
-import java.net.URLDecoder;\r
-import java.nio.charset.Charset;\r
-import java.nio.file.Files;\r
-\r
-import org.eclipse.core.runtime.FileLocator;\r
-import org.eclipse.core.runtime.IPath;\r
-import org.eclipse.core.runtime.Path;\r
-import org.osgi.framework.BundleContext;\r
-import org.simantics.Simantics;\r
-\r
-public class PhantomJSDriver {\r
-\r
- public static File fileRef(String fileName) throws IOException {\r
- \r
- BundleContext context = Activator.getContext();\r
- IPath path = new Path("/" + fileName);\r
- URL libURL = FileLocator.find(context.getBundle(), path, null);\r
- URL fileURL = FileLocator.toFileURL(libURL);\r
- return new File(URLDecoder.decode(fileURL.getPath(), "UTF-8"));\r
- \r
- }\r
- \r
- public static String printCommand(String margin, String url, String outFile) throws IOException {\r
-\r
- try {\r
- File f = fileRef("print.js");\r
- String template = new String(Files.readAllBytes(f.toPath()));\r
- template = template.replace("%%url", url);\r
- template = template.replace("%%margin", margin);\r
- template = template.replace("%%file", outFile.replace("\\", "/"));\r
- return template;\r
- } catch (IOException e) {\r
- return null;\r
- }\r
- \r
- }\r
- \r
- public static void print(String html, DocumentSettings settings, File output) throws IOException {\r
- \r
- File htmlFile = Simantics.getTempfile("PhantomJSDriver", "html");\r
- Files.write(htmlFile.toPath(), html.getBytes(Charset.forName("UTF-8")));\r
- \r
- String browserUrl = htmlFile.toURI().toURL().toString();\r
- \r
- File script = Simantics.getTempfile("PhantomJSDriver", "script");\r
- \r
- String margin = "{left:\"" + settings.marginLeft + "mm\", right:\"" + settings.marginRight + "mm\", top:\"" + settings.marginTop + "mm\", bottom:\"" + settings.marginBottom + "mm\"}";\r
- \r
- String printCommand = PhantomJSDriver.printCommand(margin, browserUrl, output.getAbsolutePath());\r
- \r
- Files.write(script.toPath(), printCommand.getBytes());\r
- PhantomJSDriver.execute(script);\r
-\r
- }\r
- \r
- public static boolean execute(File javascript) {\r
- \r
- try {\r
- \r
- File filePath = fileRef("phantomjs.exe");\r
- \r
- String[] args = { filePath.getAbsolutePath(),\r
- javascript.getAbsolutePath() \r
- };\r
- Process process = new ProcessBuilder(args).start();\r
- try {\r
- InputStream input = process.getInputStream();\r
- InputStreamReader reader = new InputStreamReader(input);\r
- //StringBuilder sb = new StringBuilder();\r
- while (true) {\r
- try {\r
- while (reader.ready()) {\r
- int r = reader.read();\r
- }\r
- \r
- int error = process.exitValue();\r
- \r
- reader.close();\r
- return error == 0;\r
- \r
- } catch (IllegalThreadStateException e) {\r
- Thread.sleep(100);\r
- }\r
- }\r
- } finally {\r
- process.destroy();\r
- }\r
- } catch(IOException e) {\r
- e.printStackTrace();\r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- return false;\r
-\r
- }\r
- \r
-}\r
+package org.simantics.document;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.net.URLDecoder;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.osgi.framework.BundleContext;
+import org.simantics.Simantics;
+
+public class PhantomJSDriver {
+
+ public static File fileRef(String fileName) throws IOException {
+
+ BundleContext context = Activator.getContext();
+ IPath path = new Path("/" + fileName);
+ URL libURL = FileLocator.find(context.getBundle(), path, null);
+ URL fileURL = FileLocator.toFileURL(libURL);
+ return new File(URLDecoder.decode(fileURL.getPath(), "UTF-8"));
+
+ }
+
+ public static String printCommand(String margin, String url, String outFile) throws IOException {
+
+ try {
+ File f = fileRef("print.js");
+ String template = new String(Files.readAllBytes(f.toPath()));
+ template = template.replace("%%url", url);
+ template = template.replace("%%margin", margin);
+ template = template.replace("%%file", outFile.replace("\\", "/"));
+ return template;
+ } catch (IOException e) {
+ return null;
+ }
+
+ }
+
+ public static void print(String html, DocumentSettings settings, File output) throws IOException {
+
+ File htmlFile = Simantics.getTempfile("PhantomJSDriver", "html");
+ Files.write(htmlFile.toPath(), html.getBytes(Charset.forName("UTF-8")));
+
+ String browserUrl = htmlFile.toURI().toURL().toString();
+
+ File script = Simantics.getTempfile("PhantomJSDriver", "script");
+
+ String margin = "{left:\"" + settings.marginLeft + "mm\", right:\"" + settings.marginRight + "mm\", top:\"" + settings.marginTop + "mm\", bottom:\"" + settings.marginBottom + "mm\"}";
+
+ String printCommand = PhantomJSDriver.printCommand(margin, browserUrl, output.getAbsolutePath());
+
+ Files.write(script.toPath(), printCommand.getBytes());
+ PhantomJSDriver.execute(script);
+
+ }
+
+ public static boolean execute(File javascript) {
+
+ try {
+
+ File filePath = fileRef("phantomjs.exe");
+
+ String[] args = { filePath.getAbsolutePath(),
+ javascript.getAbsolutePath()
+ };
+ Process process = new ProcessBuilder(args).start();
+ try {
+ InputStream input = process.getInputStream();
+ InputStreamReader reader = new InputStreamReader(input);
+ //StringBuilder sb = new StringBuilder();
+ while (true) {
+ try {
+ while (reader.ready()) {
+ int r = reader.read();
+ }
+
+ int error = process.exitValue();
+
+ reader.close();
+ return error == 0;
+
+ } catch (IllegalThreadStateException e) {
+ Thread.sleep(100);
+ }
+ }
+ } finally {
+ process.destroy();
+ }
+ } catch(IOException e) {
+ e.printStackTrace();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ return false;
+
+ }
+
+}