Debian Codegraph @

scripting/java/com/sun/star/script/framework/provider/beanshell/ScriptSourceModel.java

raw
1 /*
2 * This file is part of the LibreOffice project.
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 *
8 * This file incorporates work covered by the following license notice:
9 *
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17 */
18 package com.sun.star.script.framework.provider.beanshell;
19
20 import com.sun.star.script.provider.XScriptContext;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24
25 import java.net.URL;
26
27 public class ScriptSourceModel {
28
29 private int currentPosition = -1;
30 private final URL file;
31 private ScriptSourceView view = null;
32
33 public ScriptSourceModel(URL file) {
34 this.file = file;
35 }
36
37 private String load() throws IOException {
38 StringBuilder buf = new StringBuilder();
39 InputStream in = file.openStream();
40
41 byte[] contents = new byte[1024];
42 int len;
43
44 while ((len = in.read(contents, 0, 1024)) != -1) {
45 buf.append(new String(contents, 0, len));
46 }
47
48 try {
49 in.close();
50 } catch (IOException ignore) {
51 }
52
53 return buf.toString();
54 }
55
56 public String getText() {
57 String result = "";
58
59 try {
60 result = load();
61 } catch (IOException ioe) {
62 // do nothing, empty string will be returned
63 }
64
65 return result;
66 }
67
68 public int getCurrentPosition() {
69 return this.currentPosition;
70 }
71
72 public void setView(ScriptSourceView view) {
73 this.view = view;
74 }
75
76 public Object execute(final XScriptContext context, ClassLoader cl)
77 throws Exception {
78 if (cl != null) {
79 // sets this threads class loader
80 // hopefully any threads spawned by this
81 // will inherit this cl
82 // this enables any class files imported
83 // from the interpreter to be loaded
84 // note: setting the classloader on the
85 // interpreter has a slightly different
86 // meaning in that the classloader for
87 // the interpreter seems only to look for
88 // source files ( bla.java ) in the classpath
89 Thread.currentThread().setContextClassLoader(cl);
90 }
91
92 bsh.Interpreter interpreter = new bsh.Interpreter();
93
94 if (cl != null) {
95 // additionally set class loader on the interpreter
96 // to allow it to load java classes defined in source
97 // files e.g. bla.java
98 interpreter.getNameSpace().clear();
99 }
100
101
102 // reset position
103 currentPosition = -1;
104 view.update();
105
106 interpreter.set("XSCRIPTCONTEXT", context);
107 interpreter.set("ARGUMENTS", new Object[0]);
108
109 Object result;
110
111 if (view.isModified()) {
112 String s = view.getText();
113 result = interpreter.eval(s != null ? s : "");
114 } else {
115 result = interpreter.eval(getText());
116 }
117
118 return result;
119 }
120
121 public void indicateErrorLine(int lineNum) {
122 System.out.println("Beanshell indicateErrorLine " + lineNum);
123 currentPosition = lineNum - 1;
124 view.update();
125 }
126 }