001: //
002: // Copyright (C) 2005 United States Government as represented by the
003: // Administrator of the National Aeronautics and Space Administration
004: // (NASA). All Rights Reserved.
005: //
006: // This software is distributed under the NASA Open Source Agreement
007: // (NOSA), version 1.3. The NOSA has been approved by the Open Source
008: // Initiative. See the file NOSA-1.3-JPF at the top of the distribution
009: // directory tree for the complete NOSA document.
010: //
011: // THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
012: // KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
013: // LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
014: // SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
015: // A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
016: // THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
017: // DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
018: //
019: package gov.nasa.jpf.util;
020:
021: import java.io.BufferedReader;
022: import java.io.File;
023: import java.io.FileReader;
024: import java.io.IOException;
025:
026: import java.util.ArrayList;
027: import java.util.Hashtable;
028: import java.util.List;
029: import java.util.ListIterator;
030:
031: /**
032: * utility class to access arbitrary source files by line number
033: *
034: * <2do> pcm - should recursively look for sources underneath srcRoot dirs
035: * <2do> pcm - should be more sophisticated, e.g. by using LRU caching
036: */
037: public class Source {
038: static ArrayList srcRoots = new ArrayList();
039: private static Hashtable sources = new Hashtable();
040:
041: static {
042: srcRoots.add("src");
043: srcRoots.add("test");
044: srcRoots.add("examples");
045: }
046:
047: private List program;
048: private String name;
049:
050: protected Source(String fname) {
051: name = fname;
052: program = loadSource(fname);
053: }
054:
055: public static void addSourceRoot(String pathName) {
056: srcRoots.add(pathName);
057: }
058:
059: public boolean isLineMissing(int line) {
060: return (program == null)
061: || (line <= 0 || line > program.size());
062: }
063:
064: public static Source getSource(String fname) {
065: Source s = (Source) sources.get(fname);
066:
067: if (s == null) {
068: sources.put(fname, s = new Source(fname));
069: }
070:
071: return s;
072: }
073:
074: public String getLine(int line) {
075: if (program == null) {
076: return "";
077: }
078:
079: if ((line <= 0) || (line > program.size())) {
080: return "";
081: }
082:
083: return (String) program.get(line - 1);
084: }
085:
086: private static boolean exists(String filename) {
087: return (new File(filename)).exists();
088: }
089:
090: private List loadFile(String fname) {
091: try {
092: BufferedReader in = new BufferedReader(
093: new FileReader(fname));
094: List l = new ArrayList();
095: String line;
096:
097: while ((line = in.readLine()) != null) {
098: l.add(line);
099: }
100:
101: return l;
102: } catch (IOException e) {
103: return null;
104: }
105: }
106:
107: private List loadSource(String fname) {
108: ListIterator it = srcRoots.listIterator();
109:
110: while (it.hasNext()) {
111: String pn = (String) 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: }
|