1*cd519653SAndrew Rist /**************************************************************
2*cd519653SAndrew Rist  *
3*cd519653SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cd519653SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cd519653SAndrew Rist  * distributed with this work for additional information
6*cd519653SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cd519653SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cd519653SAndrew Rist  * "License"); you may not use this file except in compliance
9*cd519653SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*cd519653SAndrew Rist  *
11*cd519653SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*cd519653SAndrew Rist  *
13*cd519653SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cd519653SAndrew Rist  * software distributed under the License is distributed on an
15*cd519653SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cd519653SAndrew Rist  * KIND, either express or implied.  See the License for the
17*cd519653SAndrew Rist  * specific language governing permissions and limitations
18*cd519653SAndrew Rist  * under the License.
19*cd519653SAndrew Rist  *
20*cd519653SAndrew Rist  *************************************************************/
21*cd519653SAndrew Rist 
22cdf0e10cSrcweir import java.io.File;
23cdf0e10cSrcweir import java.io.InputStream;
24cdf0e10cSrcweir import java.io.IOException;
25cdf0e10cSrcweir import java.net.URL;
26cdf0e10cSrcweir import java.net.URLDecoder;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
29cdf0e10cSrcweir import com.sun.star.script.framework.provider.PathUtils;
30cdf0e10cSrcweir import com.sun.star.script.framework.runtime.XScriptContext;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir public class DebugRunner {
33cdf0e10cSrcweir 
34cdf0e10cSrcweir     private static final String FILE_URL_PREFIX =
35cdf0e10cSrcweir             System.getProperty("os.name").startsWith("Windows") == true ?
36cdf0e10cSrcweir             "file:///" : "file://";
37cdf0e10cSrcweir 
go(final XScriptContext xsctxt, String language, String uri, String filename)38cdf0e10cSrcweir     public void go(final XScriptContext xsctxt, String language, String uri,
39cdf0e10cSrcweir         String filename) {
40cdf0e10cSrcweir 
41cdf0e10cSrcweir         OOScriptDebugger debugger;
42cdf0e10cSrcweir         String path = "";
43cdf0e10cSrcweir 
44cdf0e10cSrcweir         if (language.equals("JavaScript")) {
45cdf0e10cSrcweir             debugger = new OORhinoDebugger();
46cdf0e10cSrcweir         }
47cdf0e10cSrcweir         else if (language.equals("BeanShell")) {
48cdf0e10cSrcweir             debugger = new OOBeanShellDebugger();
49cdf0e10cSrcweir         }
50cdf0e10cSrcweir         else {
51cdf0e10cSrcweir             return;
52cdf0e10cSrcweir         }
53cdf0e10cSrcweir 
54cdf0e10cSrcweir         if (uri.startsWith(FILE_URL_PREFIX)) {
55cdf0e10cSrcweir             uri = URLDecoder.decode(uri);
56cdf0e10cSrcweir             String s = uri.substring(FILE_URL_PREFIX.length());
57cdf0e10cSrcweir             File f = new File(s);
58cdf0e10cSrcweir 
59cdf0e10cSrcweir             if (f.exists()) {
60cdf0e10cSrcweir                 if (f.isDirectory()) {
61cdf0e10cSrcweir                     if (!filename.equals("")) {
62cdf0e10cSrcweir                         path = new File(f, filename).getAbsolutePath();
63cdf0e10cSrcweir                     }
64cdf0e10cSrcweir                 }
65cdf0e10cSrcweir                 else {
66cdf0e10cSrcweir                     path = f.getAbsolutePath();
67cdf0e10cSrcweir                 }
68cdf0e10cSrcweir             }
69cdf0e10cSrcweir             debugger.go(xsctxt, path);
70cdf0e10cSrcweir         }
71cdf0e10cSrcweir         else {
72cdf0e10cSrcweir             if (!uri.endsWith("/")) {
73cdf0e10cSrcweir                 uri += "/";
74cdf0e10cSrcweir             }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir             String script = uri + filename;
77cdf0e10cSrcweir             InputStream is;
78cdf0e10cSrcweir 
79cdf0e10cSrcweir             try {
80cdf0e10cSrcweir                 is = PathUtils.getScriptFileStream(
81cdf0e10cSrcweir                     script, xsctxt.getComponentContext());
82cdf0e10cSrcweir 
83cdf0e10cSrcweir                 if (is != null) {
84cdf0e10cSrcweir                     debugger.go(xsctxt, is);
85cdf0e10cSrcweir                 }
86cdf0e10cSrcweir             }
87cdf0e10cSrcweir             catch (IOException ioe) {
88cdf0e10cSrcweir                 System.out.println("Error loading script: " + script);
89cdf0e10cSrcweir             }
90cdf0e10cSrcweir         }
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir }
93