1
1
package com .osiris .headlessbrowser ;
2
2
3
3
import com .osiris .headlessbrowser .javascript .JS_API_Console ;
4
- import com .osiris .headlessbrowser .javascript .defaults . JavaScriptAPI ;
4
+ import com .osiris .headlessbrowser .javascript .JavaScriptAPI ;
5
5
import com .osiris .headlessbrowser .javascript .exceptions .DuplicateRegisteredId ;
6
6
import de .undercouch .citeproc .VariableWrapper ;
7
7
import de .undercouch .citeproc .VariableWrapperParams ;
20
20
public class JSContext extends AbstractScriptRunner {
21
21
private HeadlessWindow window ;
22
22
private Context rawContext = Context .newBuilder ("js" ).allowAllAccess (true ).build ();
23
- private List <JavaScriptAPI > loadedJSWebAPIs = new ArrayList <>();
23
+ private Map <JavaScriptAPI , Boolean > loadedJSWebAPIs = new HashMap <>();
24
24
25
25
public JSContext (HeadlessWindow window ) {
26
26
this .window = window ;
27
27
28
28
// APIs in this list get loaded into this JSContext in the order they were added to this list.
29
29
// If you want to add an api that depends on another one make sure to add it after that one.
30
30
//loadedJSWebAPIs.add(new JS_API_Console(System.out));
31
- rawContext . getBindings ( "js" ). getMemberKeys (). forEach ( key -> System .out . println ( key ));
31
+ loadedJSWebAPIs . put ( new JS_API_Console ( System .out ), true ); // If true overrides any existing variable with the same name
32
32
33
33
// Register all JavaScript Web-APIs:
34
- try {
35
- for (JavaScriptAPI api :
36
- loadedJSWebAPIs ) {
37
- registerAndLoad (api .getJSVarName (), api .getObject ());
34
+ loadedJSWebAPIs .forEach ((api , override ) -> {
35
+ try {
36
+ registerAndLoad (api .getJSVarName (), api .getObject (), override );
37
+ } catch (Exception exception ) {
38
+ System .err .println ("Failed to load one/multiple JavaScript Web-API(s) into the current JavaScript-Context! Details:" );
39
+ throw new RuntimeException (exception );
38
40
}
39
- } catch (Exception exception ) {
40
- System .err .println ("Failed to load one/multiple JavaScript Web-API(s) into the current JavaScript-Context! Details:" );
41
- throw new RuntimeException (exception );
42
- }
41
+ });
42
+
43
43
}
44
44
45
45
/**
46
46
* Registers and loads this API into the provided {@link JSContext}. <br>
47
+ * @param id
47
48
*/
48
- public void registerAndLoad (String id , Object object ) throws DuplicateRegisteredId , IOException {
49
- if (rawContext .getBindings ("js" ).getMember (id ) != null )
49
+ public void registerAndLoad (String id , Object object , boolean override ) throws DuplicateRegisteredId , IOException {
50
+ if (! override && rawContext .getBindings ("js" ).getMember (id ) != null )
50
51
throw new DuplicateRegisteredId ("Failed to register because of already existing/registered id '" +id +"'." );
51
52
rawContext .getBindings ("js" ).putMember (id , object );
52
53
}
@@ -60,11 +61,11 @@ public void registerAndLoad(String id, Object object) throws DuplicateRegistered
60
61
* @throws IOException
61
62
*/
62
63
public void eval (String jsCode ) throws IOException {
63
- eval (new BufferedReader ( new StringReader ( jsCode )) );
64
+ rawContext . eval ("js" , jsCode );
64
65
}
65
66
66
67
public void eval (InputStream jsCodesInputStream ) throws IOException {
67
- eval (new BufferedReader ( new InputStreamReader (jsCodesInputStream ) ));
68
+ eval (new InputStreamReader (jsCodesInputStream ));
68
69
}
69
70
70
71
public HeadlessWindow getWindow () {
@@ -95,6 +96,14 @@ public String getVersion() {
95
96
96
97
@ Override
97
98
public void eval (Reader reader ) throws IOException {
99
+ String jsCode = null ;
100
+ try (BufferedReader bufferedReader = new BufferedReader (reader )) {
101
+ while ((jsCode = bufferedReader .readLine ()) != null ) {
102
+ jsCode = jsCode +jsCode ;
103
+ }
104
+ } catch (IOException e ) {
105
+ throw e ;
106
+ }
98
107
rawContext .eval (Source .newBuilder ("js" , reader , null ).cached (false ).build ());
99
108
}
100
109
0 commit comments