View Javadoc
1 package net.sf.plb4jedit.plb; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.util.StringTokenizer; 6 /*** 7 * Handles the PLB_PATH to search for includes. 8 * @author Peter Schaefer 9 */ 10 public class PathBroker { 11 private String[] pathElements; 12 13 /*** 14 * no public access 15 */ 16 private PathBroker() { 17 } 18 19 /*** 20 * Constructs a path broker for a path and a program 21 * 22 * @param plbpath general plb path as defined in env PLB_PATH 23 * @param program absolute path of the actual program has priority over default path 24 * + programName itself, e.g. /opt/cvsroot/procars/utils/test.dbs 25 */ 26 public PathBroker(String plbpath, String program) { 27 if (program == null || plbpath == null) { 28 throw new IllegalArgumentException("arguments null"); 29 } 30 String delimiter; 31 if (File.separator.equals("/")) { // unix 32 delimiter = ":"; 33 } else { // windows 34 delimiter = ";"; 35 } 36 File programFile = new File(program); 37 String programPath = programFile.getParent(); 38 StringTokenizer tokenizer = new StringTokenizer(plbpath, delimiter); 39 int size = tokenizer.countTokens() + 1; 40 //System.out.println("*****"+size); 41 pathElements = new String[size]; 42 pathElements[0] = makePath(programPath); 43 for (int i = 1; i < size; i++) { 44 pathElements[i] = makePath(tokenizer.nextToken()); 45 } 46 } 47 48 /*** 49 * little helper to ensure that path always ends with the system 50 * specific File.separator. If path is already in proper format, nothing is done.<br/> 51 * E.g. /opt/cvsroot/procars/incs/rcd ->/opt/cvsroot/procars/incs/rcd/ 52 * @param path a path 53 * @return path which ends with the File separator. 54 */ 55 private String makePath(String path) { 56 //System.out.println("*****"+path); 57 if (path.endsWith(File.separator)) { 58 return path; 59 } else { 60 return path + File.separator; 61 } 62 } 63 64 /*** 65 * get elements of the PLB_PATH as String array. 66 * @return 67 */ 68 public String[] getPathElements() { 69 return pathElements; 70 } 71 72 /*** 73 * search in path elements according to their order for the given include name. 74 * The first path-element which matches is used to construct the file. Include name 75 * must not contain the file suffix .dbs 76 * @param include include name without file suffix (.dbs)- 77 * @return where this include is found in the PLB_PATH. 78 * @throws FileNotFoundException if include is not found in any place in the path 79 */ 80 public File searchInclude(String include) throws FileNotFoundException { 81 for (int i = 0; i < pathElements.length; i++) { 82 File probe = new File(pathElements[i] + include + ".dbs"); 83 if (probe.exists()) { 84 return probe; 85 } 86 } 87 throw new FileNotFoundException( 88 "include " + include + " not found in path"); 89 } 90 91 /*** 92 * for testing: Gets a PLB_PATH and an include name as input and prints out 93 * where the include is found in the path 94 * @param args PLB_PATH, include name without file-extension, e.g. nadrcd3 (but not nadrcd3.dbs) 95 * @throws FileNotFoundException if include can not be found in path. 96 */ 97 public static void main(String[] args) throws FileNotFoundException { 98 PathBroker broker = new PathBroker(args[0], args[1]); 99 String[] elements = broker.getPathElements(); 100 for (int i = 0; i < elements.length; i++) { 101 System.out.println(elements[i]); 102 } 103 File inc = broker.searchInclude(args[2]); 104 System.out.println( 105 "Include " + args[2] + " found at " + inc.getAbsolutePath()); 106 } 107 }

This page was automatically generated by Maven