1*cdf0e10cSrcweir import java.io.File;
2*cdf0e10cSrcweir import java.io.InputStream;
3*cdf0e10cSrcweir import java.io.IOException;
4*cdf0e10cSrcweir import java.net.URL;
5*cdf0e10cSrcweir import java.net.URLDecoder;
6*cdf0e10cSrcweir 
7*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
8*cdf0e10cSrcweir import com.sun.star.script.framework.provider.PathUtils;
9*cdf0e10cSrcweir import com.sun.star.script.framework.runtime.XScriptContext;
10*cdf0e10cSrcweir 
11*cdf0e10cSrcweir public class DebugRunner {
12*cdf0e10cSrcweir 
13*cdf0e10cSrcweir     private static final String FILE_URL_PREFIX =
14*cdf0e10cSrcweir             System.getProperty("os.name").startsWith("Windows") == true ?
15*cdf0e10cSrcweir             "file:///" : "file://";
16*cdf0e10cSrcweir 
17*cdf0e10cSrcweir     public void go(final XScriptContext xsctxt, String language, String uri,
18*cdf0e10cSrcweir         String filename) {
19*cdf0e10cSrcweir 
20*cdf0e10cSrcweir         OOScriptDebugger debugger;
21*cdf0e10cSrcweir         String path = "";
22*cdf0e10cSrcweir 
23*cdf0e10cSrcweir         if (language.equals("JavaScript")) {
24*cdf0e10cSrcweir             debugger = new OORhinoDebugger();
25*cdf0e10cSrcweir         }
26*cdf0e10cSrcweir         else if (language.equals("BeanShell")) {
27*cdf0e10cSrcweir             debugger = new OOBeanShellDebugger();
28*cdf0e10cSrcweir         }
29*cdf0e10cSrcweir         else {
30*cdf0e10cSrcweir             return;
31*cdf0e10cSrcweir         }
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir         if (uri.startsWith(FILE_URL_PREFIX)) {
34*cdf0e10cSrcweir             uri = URLDecoder.decode(uri);
35*cdf0e10cSrcweir             String s = uri.substring(FILE_URL_PREFIX.length());
36*cdf0e10cSrcweir             File f = new File(s);
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir             if (f.exists()) {
39*cdf0e10cSrcweir                 if (f.isDirectory()) {
40*cdf0e10cSrcweir                     if (!filename.equals("")) {
41*cdf0e10cSrcweir                         path = new File(f, filename).getAbsolutePath();
42*cdf0e10cSrcweir                     }
43*cdf0e10cSrcweir                 }
44*cdf0e10cSrcweir                 else {
45*cdf0e10cSrcweir                     path = f.getAbsolutePath();
46*cdf0e10cSrcweir                 }
47*cdf0e10cSrcweir             }
48*cdf0e10cSrcweir             debugger.go(xsctxt, path);
49*cdf0e10cSrcweir         }
50*cdf0e10cSrcweir         else {
51*cdf0e10cSrcweir             if (!uri.endsWith("/")) {
52*cdf0e10cSrcweir                 uri += "/";
53*cdf0e10cSrcweir             }
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir             String script = uri + filename;
56*cdf0e10cSrcweir             InputStream is;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir             try {
59*cdf0e10cSrcweir                 is = PathUtils.getScriptFileStream(
60*cdf0e10cSrcweir                     script, xsctxt.getComponentContext());
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir                 if (is != null) {
63*cdf0e10cSrcweir                     debugger.go(xsctxt, is);
64*cdf0e10cSrcweir                 }
65*cdf0e10cSrcweir             }
66*cdf0e10cSrcweir             catch (IOException ioe) {
67*cdf0e10cSrcweir                 System.out.println("Error loading script: " + script);
68*cdf0e10cSrcweir             }
69*cdf0e10cSrcweir         }
70*cdf0e10cSrcweir     }
71*cdf0e10cSrcweir }
72