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