|
23 | 23 | */
|
24 | 24 | public class CompileHelper {
|
25 | 25 |
|
26 |
| - private File m_tempDir; |
27 |
| - private String m_script; |
| 26 | + private File m_tempDir; |
| 27 | + private String m_script; |
28 | 28 |
|
29 |
| - private Context m_context; |
30 |
| - private Writer m_errorWriter; |
31 |
| - private Writer m_outputWriter; |
| 29 | + private Context m_context; |
| 30 | + private Writer m_errorWriter; |
| 31 | + private Writer m_outputWriter; |
32 | 32 |
|
33 |
| - /** |
34 |
| - * Constructor |
35 |
| - * |
36 |
| - * @param context |
37 |
| - * @param outputWriter |
38 |
| - * @throws IOException |
39 |
| - */ |
40 |
| - public CompileHelper(Context context, Writer errorWriter, |
41 |
| - Writer outputWriter) throws IOException { |
| 33 | + /** |
| 34 | + * Constructor |
| 35 | + * |
| 36 | + * @param context |
| 37 | + * @param outputWriter |
| 38 | + * @throws IOException |
| 39 | + */ |
| 40 | + public CompileHelper(Context context, Writer errorWriter, |
| 41 | + Writer outputWriter) throws IOException { |
42 | 42 |
|
43 |
| - m_errorWriter = errorWriter; |
44 |
| - m_outputWriter = outputWriter; |
45 |
| - m_tempDir = FileUtil.createTempDir("ScriptingNode"); |
| 43 | + m_errorWriter = errorWriter; |
| 44 | + m_outputWriter = outputWriter; |
| 45 | + m_tempDir = FileUtil.createTempDir("ScriptingNode"); |
46 | 46 |
|
47 |
| - m_context = context; |
48 |
| - } |
| 47 | + m_context = context; |
| 48 | + } |
49 | 49 |
|
50 |
| - /* |
51 |
| - * Write script code from the current settings to a file in the temp |
52 |
| - * directory. |
53 |
| - */ |
54 |
| - private void writeScriptToFile(final ScriptLanguage lang) { |
55 |
| - try (Writer w = new FileWriter( |
56 |
| - new File(m_tempDir, "script." + lang.getExtensions().get(0)))) { |
57 |
| - w.write(m_script); |
58 |
| - w.close(); |
59 |
| - } catch (IOException exc) { |
60 |
| - exc.printStackTrace(); |
61 |
| - } |
62 |
| - } |
| 50 | + /* |
| 51 | + * Write script code from the current settings to a file in the temp |
| 52 | + * directory. |
| 53 | + */ |
| 54 | + private void writeScriptToFile(final ScriptLanguage lang) { |
| 55 | + try (Writer w = new FileWriter( |
| 56 | + new File(m_tempDir, "script." + lang.getExtensions().get(0)))) { |
| 57 | + w.write(m_script); |
| 58 | + w.close(); |
| 59 | + } catch (IOException exc) { |
| 60 | + exc.printStackTrace(); |
| 61 | + } |
| 62 | + } |
63 | 63 |
|
64 |
| - /** |
65 |
| - * Compile the script from settings with the given language Note that |
66 |
| - * compilation may be done lazily. |
67 |
| - * |
68 |
| - * @param language |
69 |
| - * for compiling the script |
70 |
| - * @return The resulting scriptInfo. |
71 |
| - * @throws ScriptException |
72 |
| - */ |
73 |
| - public CompileProductHelper compile(final String script, |
74 |
| - final ScriptLanguage language) throws ScriptException { |
75 |
| - setScript(script, language); |
| 64 | + /** |
| 65 | + * Compile the script from settings with the given language Note that |
| 66 | + * compilation may be done lazily. |
| 67 | + * |
| 68 | + * @param language |
| 69 | + * for compiling the script |
| 70 | + * @return The resulting scriptInfo. |
| 71 | + * @throws ScriptException |
| 72 | + */ |
| 73 | + public CompileProductHelper compile(final String script, |
| 74 | + final ScriptLanguage language) throws ScriptException { |
| 75 | + setScript(script, language); |
76 | 76 |
|
77 |
| - if (language instanceof JavaScriptLanguage) { |
78 |
| - JavaEngine scriptEngine = (JavaEngine) language.getScriptEngine(); |
79 |
| - scriptEngine.getContext().setErrorWriter(m_errorWriter); |
80 |
| - scriptEngine.getContext().setWriter(m_outputWriter); |
81 |
| - @SuppressWarnings("unchecked") |
82 |
| - Class<? extends Command> commandClass = (Class<? extends Command>) scriptEngine |
83 |
| - .compile(new StringReader(m_script)); |
84 |
| - if (commandClass == null) { |
85 |
| - throw new ScriptException(""); |
86 |
| - } |
87 |
| - return new CommandCompileProductHelper( |
88 |
| - new CommandInfo(commandClass), m_context); |
89 |
| - } |
| 77 | + if (language instanceof JavaScriptLanguage) { |
| 78 | + JavaEngine scriptEngine = (JavaEngine) language.getScriptEngine(); |
| 79 | + scriptEngine.getContext().setErrorWriter(m_errorWriter); |
| 80 | + scriptEngine.getContext().setWriter(m_outputWriter); |
| 81 | + @SuppressWarnings("unchecked") |
| 82 | + Class<? extends Command> commandClass = (Class<? extends Command>) scriptEngine |
| 83 | + .compile(new StringReader(m_script)); |
| 84 | + if (commandClass == null) { |
| 85 | + throw new ScriptException(""); |
| 86 | + } |
| 87 | + return new CommandCompileProductHelper( |
| 88 | + new CommandInfo(commandClass), m_context); |
| 89 | + } |
90 | 90 |
|
91 |
| - // create script module for execution |
92 |
| - final File scriptFile = new File(m_tempDir, |
93 |
| - "script." + language.getExtensions().get(0)); |
| 91 | + // create script module for execution |
| 92 | + final File scriptFile = new File(m_tempDir, |
| 93 | + "script." + language.getExtensions().get(0)); |
94 | 94 |
|
95 |
| - final ScriptInfo info = new ScriptInfo(m_context, |
96 |
| - scriptFile.getAbsolutePath(), new StringReader(m_script)); |
97 |
| - return new ScriptCompileProductHelper(info, m_context); |
98 |
| - } |
| 95 | + final ScriptInfo info = new ScriptInfo(m_context, |
| 96 | + scriptFile.getAbsolutePath(), new StringReader(m_script)); |
| 97 | + return new ScriptCompileProductHelper(info, m_context); |
| 98 | + } |
99 | 99 |
|
100 |
| - /** |
101 |
| - * Set the script to compile. |
102 |
| - * |
103 |
| - * @param script |
104 |
| - * @param language |
105 |
| - */ |
106 |
| - protected void setScript(final String script, |
107 |
| - final ScriptLanguage language) { |
108 |
| - m_script = script; |
109 |
| - writeScriptToFile(language); |
110 |
| - } |
| 100 | + /** |
| 101 | + * Set the script to compile. |
| 102 | + * |
| 103 | + * @param script |
| 104 | + * @param language |
| 105 | + */ |
| 106 | + protected void setScript(final String script, |
| 107 | + final ScriptLanguage language) { |
| 108 | + m_script = script; |
| 109 | + writeScriptToFile(language); |
| 110 | + } |
111 | 111 | }
|
0 commit comments