KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > util > Source


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.util;
20
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileReader JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.ListIterator JavaDoc;
31
32
33 /**
34  * utility class to access arbitrary source files by line number
35  *
36  * <2do> pcm - should recursively look for sources underneath srcRoot dirs
37  * <2do> pcm - should be more sophisticated, e.g. by using LRU caching
38  */

39 public class Source {
40   static ArrayList JavaDoc srcRoots = new ArrayList JavaDoc();
41   private static Hashtable JavaDoc sources = new Hashtable JavaDoc();
42
43   static {
44     srcRoots.add("src");
45     srcRoots.add("test");
46     srcRoots.add("examples");
47   }
48
49   private List JavaDoc program;
50   private String JavaDoc name;
51
52   protected Source (String JavaDoc fname) {
53     name = fname;
54     program = loadSource(fname);
55   }
56
57   public static void addSourceRoot (String JavaDoc pathName) {
58     srcRoots.add(pathName);
59   }
60
61   public boolean isLineMissing (int line) {
62     return (program == null) || (line <= 0 || line > program.size());
63   }
64
65   public static Source getSource (String JavaDoc fname) {
66     Source s = (Source) sources.get(fname);
67
68     if (s == null) {
69       sources.put(fname, s = new Source(fname));
70     }
71
72     return s;
73   }
74
75   public String JavaDoc getLine (int line) {
76     if (program == null) {
77       return "";
78     }
79
80     if ((line <= 0) || (line > program.size())) {
81       return "";
82     }
83
84     return (String JavaDoc) program.get(line - 1);
85   }
86
87   private static boolean exists (String JavaDoc filename) {
88     return (new File JavaDoc(filename)).exists();
89   }
90
91   private List JavaDoc loadFile (String JavaDoc fname) {
92     try {
93       BufferedReader JavaDoc in = new BufferedReader JavaDoc(new FileReader JavaDoc(fname));
94       List JavaDoc l = new ArrayList JavaDoc();
95       String JavaDoc line;
96
97       while ((line = in.readLine()) != null) {
98         l.add(line);
99       }
100
101       return l;
102     } catch (IOException JavaDoc e) {
103       return null;
104     }
105   }
106
107   private List JavaDoc loadSource (String JavaDoc fname) {
108     ListIterator JavaDoc it = srcRoots.listIterator();
109
110     while (it.hasNext()) {
111       String JavaDoc pn = (String JavaDoc) it.next() + File.separatorChar + fname;
112
113       if (exists(pn)) {
114         return loadFile(pn);
115       }
116     }
117
118     if (exists(fname)) {
119       return loadFile(fname);
120     }
121
122     return null;
123   }
124 }
125
Popular Tags