1/*
2 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26/*
27 * This source code is provided to illustrate the usage of a given feature
28 * or technique and has been deliberately simplified. Additional steps
29 * required for a production-quality application, such as security checks,
30 * input validation and proper error handling, might not be present in
31 * this sample code.
32 */
33
34
35package com.sun.tools.example.debug.tty;
36
37import com.sun.jdi.Location;
38import com.sun.jdi.AbsentInformationException;
39import java.util.List;
40import java.util.ArrayList;
41import java.util.StringTokenizer;
42import java.io.*;
43
44class SourceMapper {
45
46    private final String[] dirs;
47
48    SourceMapper(List<String> sourcepath) {
49        /*
50         * sourcepath can arrive from the debugee as a List.
51         * (via PathSearchingVirtualMachine.classPath())
52         */
53        List<String> dirList = new ArrayList<String>();
54        for (String element : sourcepath) {
55            //XXX remove .jar and .zip files; we want only directories on
56            //the source path. (Bug ID 4186582)
57            if ( ! (element.endsWith(".jar") ||
58                    element.endsWith(".zip"))) {
59                dirList.add(element);
60            }
61        }
62        dirs = dirList.toArray(new String[0]);
63    }
64
65    SourceMapper(String sourcepath) {
66        /*
67         * sourcepath can also arrive from the command line
68         * as a String.  (via "-sourcepath")
69         *
70         * Using File.pathSeparator as delimiter below is OK
71         * because we are on the same machine as the command
72         * line originiated.
73         */
74        StringTokenizer st = new StringTokenizer(sourcepath,
75                                                 File.pathSeparator);
76        List<String> dirList = new ArrayList<String>();
77        while (st.hasMoreTokens()) {
78            String s = st.nextToken();
79            //XXX remove .jar and .zip files; we want only directories on
80            //the source path. (Bug ID 4186582)
81            if ( ! (s.endsWith(".jar") ||
82                    s.endsWith(".zip"))) {
83                dirList.add(s);
84            }
85        }
86        dirs = dirList.toArray(new String[0]);
87    }
88
89    /*
90     * Return the current sourcePath as a String.
91     */
92    String getSourcePath() {
93        int i = 0;
94        StringBuffer sp;
95        if (dirs.length < 1) {
96            return "";          //The source path is empty.
97        } else {
98            sp = new StringBuffer(dirs[i++]);
99        }
100        for (; i < dirs.length; i++) {
101            sp.append(File.pathSeparator);
102            sp.append(dirs[i]);
103        }
104        return sp.toString();
105    }
106
107    /**
108     * Return a File cooresponding to the source of this location.
109     * Return null if not available.
110     */
111    File sourceFile(Location loc) {
112        try {
113            String filename = loc.sourceName();
114            String refName = loc.declaringType().name();
115            int iDot = refName.lastIndexOf('.');
116            String pkgName = (iDot >= 0)? refName.substring(0, iDot+1) : "";
117            String full = pkgName.replace('.', File.separatorChar) + filename;
118            for (int i= 0; i < dirs.length; ++i) {
119                File path = new File(dirs[i], full);
120                if (path.exists()) {
121                    return path;
122                }
123            }
124            return null;
125        } catch (AbsentInformationException e) {
126            return null;
127        }
128    }
129
130    /**
131     * Return a BufferedReader cooresponding to the source
132     * of this location.
133     * Return null if not available.
134     * Note: returned reader must be closed.
135     */
136    BufferedReader sourceReader(Location loc) {
137        File sourceFile = sourceFile(loc);
138        if (sourceFile == null) {
139            return null;
140        }
141        try {
142            return new BufferedReader(new FileReader(sourceFile));
143        } catch(IOException exc) {
144        }
145        return null;
146    }
147}
148