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 import java.io.File;
23 import java.io.InputStream;
24 import java.io.IOException;
25 import java.net.URL;
26 import java.net.URLDecoder;
27 
28 import com.sun.star.uno.XComponentContext;
29 import com.sun.star.script.framework.provider.PathUtils;
30 import com.sun.star.script.framework.runtime.XScriptContext;
31 
32 public class DebugRunner {
33 
34     private static final String FILE_URL_PREFIX =
35             System.getProperty("os.name").startsWith("Windows") == true ?
36             "file:///" : "file://";
37 
go(final XScriptContext xsctxt, String language, String uri, String filename)38     public void go(final XScriptContext xsctxt, String language, String uri,
39         String filename) {
40 
41         OOScriptDebugger debugger;
42         String path = "";
43 
44         if (language.equals("JavaScript")) {
45             debugger = new OORhinoDebugger();
46         }
47         else if (language.equals("BeanShell")) {
48             debugger = new OOBeanShellDebugger();
49         }
50         else {
51             return;
52         }
53 
54         if (uri.startsWith(FILE_URL_PREFIX)) {
55             uri = URLDecoder.decode(uri);
56             String s = uri.substring(FILE_URL_PREFIX.length());
57             File f = new File(s);
58 
59             if (f.exists()) {
60                 if (f.isDirectory()) {
61                     if (!filename.equals("")) {
62                         path = new File(f, filename).getAbsolutePath();
63                     }
64                 }
65                 else {
66                     path = f.getAbsolutePath();
67                 }
68             }
69             debugger.go(xsctxt, path);
70         }
71         else {
72             if (!uri.endsWith("/")) {
73                 uri += "/";
74             }
75 
76             String script = uri + filename;
77             InputStream is;
78 
79             try {
80                 is = PathUtils.getScriptFileStream(
81                     script, xsctxt.getComponentContext());
82 
83                 if (is != null) {
84                     debugger.go(xsctxt, is);
85                 }
86             }
87             catch (IOException ioe) {
88                 System.out.println("Error loading script: " + script);
89             }
90         }
91     }
92 }
93