Debian Codegraph @

scripting/examples/java/debugger/DebugRunner.java

raw
1 /*
2 * This file is part of the LibreOffice project.
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 *
8 * This file incorporates work covered by the following license notice:
9 *
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17 */
18
19 package org.libreoffice.example.java_scripts;
20
21 import java.io.File;
22 import java.io.InputStream;
23 import java.io.IOException;
24 import java.net.URL;
25 import java.net.URLDecoder;
26
27 import com.sun.star.uno.XComponentContext;
28 import com.sun.star.script.framework.provider.PathUtils;
29 import com.sun.star.script.provider.XScriptContext;
30
31 public class DebugRunner {
32
33 private static final String FILE_URL_PREFIX =
34 System.getProperty("os.name").startsWith("Windows") == true ?
35 "file:///" : "file://";
36
37 public void go(final XScriptContext xsctxt, String language, String uri,
38 String filename) {
39
40 OOScriptDebugger debugger;
41 String path = "";
42
43 if (language.equals("JavaScript")) {
44 debugger = new OORhinoDebugger();
45 } else if (language.equals("BeanShell")) {
46 debugger = new OOBeanShellDebugger();
47 } else {
48 return;
49 }
50
51 if (uri.startsWith(FILE_URL_PREFIX)) {
52 uri = URLDecoder.decode(uri);
53 String s = uri.substring(FILE_URL_PREFIX.length());
54 File f = new File(s);
55
56 if (f.exists()) {
57 if (f.isDirectory()) {
58 if (!filename.equals("")) {
59 path = new File(f, filename).getAbsolutePath();
60 }
61 } else {
62 path = f.getAbsolutePath();
63 }
64 }
65
66 debugger.go(xsctxt, path);
67 } else {
68 if (!uri.endsWith("/")) {
69 uri += "/";
70 }
71
72 String script = uri + filename;
73 InputStream is;
74
75 try {
76 is = PathUtils.getScriptFileStream(
77 script, xsctxt.getComponentContext());
78
79 if (is != null) {
80 debugger.go(xsctxt, is);
81 }
82 } catch (IOException ioe) {
83 System.out.println("Error loading script: " + script);
84 }
85 }
86 }
87 }