1 package gov.nasa.jpf.util; 20 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileReader ; 25 import java.io.IOException ; 26 27 import java.util.ArrayList ; 28 import java.util.Hashtable ; 29 import java.util.List ; 30 import java.util.ListIterator ; 31 32 33 39 public class Source { 40 static ArrayList srcRoots = new ArrayList (); 41 private static Hashtable sources = new Hashtable (); 42 43 static { 44 srcRoots.add("src"); 45 srcRoots.add("test"); 46 srcRoots.add("examples"); 47 } 48 49 private List program; 50 private String name; 51 52 protected Source (String fname) { 53 name = fname; 54 program = loadSource(fname); 55 } 56 57 public static void addSourceRoot (String 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 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 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 ) program.get(line - 1); 85 } 86 87 private static boolean exists (String filename) { 88 return (new File (filename)).exists(); 89 } 90 91 private List loadFile (String fname) { 92 try { 93 BufferedReader in = new BufferedReader (new FileReader (fname)); 94 List l = new ArrayList (); 95 String line; 96 97 while ((line = in.readLine()) != null) { 98 l.add(line); 99 } 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 } 125 | Popular Tags |