1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 package com.sun.star.script.framework.provider.beanshell; 28 29 import java.io.InputStream; 30 import java.io.IOException; 31 32 import java.net.URL; 33 34 import com.sun.star.script.provider.XScriptContext; 35 36 public class ScriptSourceModel { 37 38 private int currentPosition = -1; 39 private URL file = null; 40 private ScriptSourceView view = null; 41 42 public ScriptSourceModel(URL file ) { 43 this.file = file; 44 } 45 46 private String load() throws IOException { 47 StringBuffer buf = new StringBuffer(); 48 InputStream in = file.openStream(); 49 50 byte[] contents = new byte[1024]; 51 int len = 0; 52 53 while ((len = in.read(contents, 0, 1024)) != -1) { 54 buf.append(new String(contents, 0, len)); 55 } 56 57 try { 58 in.close(); 59 } 60 catch (IOException ignore) { 61 } 62 63 return buf.toString(); 64 } 65 66 public String getText() { 67 String result = ""; 68 69 try { 70 result = load(); 71 } 72 catch (IOException ioe) { 73 // do nothing, empty string will be returned 74 } 75 76 return result; 77 } 78 79 public int getCurrentPosition() { 80 return this.currentPosition; 81 } 82 83 public void setView(ScriptSourceView view) { 84 this.view = view; 85 } 86 87 public Object execute(final XScriptContext context, ClassLoader cl ) 88 throws Exception 89 { 90 Object result = null; 91 // Thread execThread = new Thread() { 92 // public void run() { 93 if ( cl != null ) 94 { 95 // sets this threads class loader 96 // hopefully any threads spawned by this 97 // will inherit this cl 98 // this enables any class files imported 99 // from the interpreter to be loaded 100 // note: setting the classloader on the 101 // interpreter has a slightly different 102 // meaning in that the classloader for 103 // the interpreter seems only to look for 104 // source files ( bla.java ) in the classpath 105 Thread.currentThread().setContextClassLoader(cl); 106 } 107 bsh.Interpreter interpreter = new bsh.Interpreter(); 108 if ( cl != null ) 109 { 110 // additionally set class loader on the interpreter 111 // to allow it to load java classes defined in source 112 // files e.g. bla.java 113 interpreter.getNameSpace().clear(); 114 } 115 116 117 // reset position 118 currentPosition = -1; 119 view.update(); 120 121 interpreter.set("XSCRIPTCONTEXT", context); 122 interpreter.set("ARGUMENTS", new Object[0]); 123 124 if (view.isModified()) { 125 result = interpreter.eval(view.getText()); 126 } 127 else { 128 result = interpreter.eval(getText()); 129 } 130 // } 131 // }; 132 // execThread.start(); 133 return result; 134 } 135 public void indicateErrorLine( int lineNum ) 136 { 137 System.out.println("Beanshell indicateErrorLine " + lineNum ); 138 currentPosition = lineNum - 1; 139 view.update(); 140 } 141 } 142