00001 package gov.nasa.arc.ase.jpf.jvm;
00002
00003 import gov.nasa.arc.ase.util.Debug;
00004 import java.io.File;
00005 import java.io.BufferedReader;
00006 import java.io.FileReader;
00007 import java.io.IOException;
00008 import java.io.FileNotFoundException;
00009 import java.util.Hashtable;
00010
00011
00012
00013
00014 import java.util.ArrayList;
00015 import java.util.List;
00016
00017
00018 public class Source {
00019 private static Hashtable sources = new Hashtable();
00020 private List program;
00021 private String name;
00022
00023 public Source(String fname) {
00024 name = fname;
00025 program = loadSource(fname);
00026 }
00027 private static boolean exists(String filename) {
00028 try{
00029 new FileReader(filename);
00030 return true;
00031 } catch(FileNotFoundException e) {
00032 return false;
00033 }
00034 }
00035 public String get(int line) {
00036 if(program == null)
00037 return "-- missing source file --";
00038
00039 if(line <= 0 || line > program.size())
00040 return "-- invalid line number --";
00041
00042 return (String)program.get(line-1);
00043 }
00044 public static Source getSource(String fname) {
00045 Source s = (Source)sources.get(fname);
00046
00047 if(s == null)
00048 sources.put(fname, s = new Source(fname));
00049
00050 return s;
00051 }
00052 private List loadFile(String fname) {
00053 try {
00054 BufferedReader in = new BufferedReader(new FileReader(fname));
00055 List l = new ArrayList();
00056 String line;
00057
00058 while((line = in.readLine()) != null)
00059 l.add(line);
00060
00061 return l;
00062 } catch(IOException e) {
00063 return null;
00064 }
00065 }
00066 private List loadSource(String fname) {
00067 for(int i = 0, l = ClassPath.length(); i < l; i++) {
00068 String filename = ClassPath.get(i) + File.separator + fname;
00069 if(exists(filename))
00070 return loadFile(filename);
00071 }
00072
00073 if(exists(fname))
00074 return loadFile(fname);
00075
00076 return null;
00077 }
00078 public static String locateSourceFile(String fname) {
00079 for(int i = 0, l = ClassPath.length(); i < l; i++) {
00080 String filename = ClassPath.get(i) + File.separator + fname;
00081 if(exists(filename))
00082 return filename;
00083 }
00084
00085 if(exists(fname))
00086 return fname;
00087
00088 return null;
00089 }
00090 }