001 /* Copyright 2000, 2001, Compaq Computer Corporation */ 002 003 package javafe.filespace; 004 005 006 import java.io.IOException; 007 import java.util.Enumeration; 008 009 import javafe.genericfile.*; 010 011 012 /** 013 * This module implements the Query "interface" by using the Java 014 * filespace classes (ClassPath, PathComponent, etc.) provided by the 015 * javafe.filespace package. 016 */ 017 018 public class SlowQuery extends Query { 019 020 /*************************************************** 021 * * 022 * Creation: * 023 * * 024 **************************************************/ 025 026 /** 027 * The Java file space that corresponds to our classpath. 028 */ 029 private Tree javaFileSpace; 030 031 032 /** 033 * Create an query engine that may be queried about packages and 034 * classes in the classpath classpath. 035 * 036 */ 037 //@ requires classpath != null; 038 public SlowQuery(String classpath) throws java.io.IOException { 039 javaFileSpace = ClassPath.open(classpath, false); 040 } 041 042 /** 043 * Create an query engine that may be queried about packages and 044 * classes in the current Java classpath (cf. ClassPath) at the 045 * time the engine was created. (I.e., later changes to the 046 * current classpath have no effect on the query engine.) 047 */ 048 public SlowQuery() throws java.io.IOException { 049 javaFileSpace = ClassPath.open(false); 050 } 051 052 053 /*************************************************** 054 * * 055 * Locating files: * 056 * * 057 **************************************************/ 058 059 /** 060 * Return true iff the package P in the Java filespace is 061 * "accessible".<p> 062 * 063 * Warning: the definition of accessible is host system dependent 064 * and may in fact be defined as always true.<p> 065 */ 066 //@ also 067 //@ requires P != null; 068 public boolean accessable(String[] P) { 069 return (getPackage(P) != null); 070 } 071 072 073 /** 074 * Attempt to locate the file typename+"."+extension in the package 075 * P in the Java filespace.<p> 076 * 077 * If such a file is found, then a (non-null) GenericFile 078 * representing it is returned. Otherwise, null is returned.<p> 079 */ 080 //@ also 081 //@ requires P != null; 082 //@ requires typename != null; 083 //@ requires extension != null; 084 public GenericFile findFile(String[] P, String typename, 085 String extension) { 086 return findFile(P,typename+"."+extension); 087 } 088 089 //@ also 090 //@ requires P != null; 091 //@ requires filename != null; 092 public GenericFile findFile(String[] P, String filename) { 093 Tree Package = getPackage(P); 094 if (Package==null) 095 return null; 096 097 Tree node = Package.getChild(filename); 098 if (node==null) 099 return null; 100 101 return (GenericFile)node.data; //@ nowarn Cast; 102 } 103 104 //@ also 105 //@ requires P != null; 106 //@ requires typename != null; 107 //@ requires extensions != null; 108 public GenericFile findFile(String[] P, String typename, 109 String[] extensions) { 110 // FIXME - only utilizes the first package 111 Tree Package = getPackage(P); 112 if (Package==null) 113 return null; 114 115 for (int i=0; i<extensions.length; ++i) { 116 String extension = extensions[i]; 117 Tree node = Package.getChild(typename+"."+extension); 118 if (node != null) return (GenericFile)node.data; //@ nowarn Cast; 119 } 120 return null; 121 } 122 123 //@ also 124 //@ requires P != null; 125 public Enumeration findFiles(String[] P) { 126 Tree Package = getPackage(P); 127 if (Package==null) 128 return null; 129 130 return Package.children(); 131 } 132 133 /* 134 * Helper function: return the node corresponding to package P in 135 * the Java filespace or null if there is no such corresponding 136 * package. 137 */ 138 //@ requires P != null; 139 //@ requires \nonnullelements(P); 140 private Tree getPackage(String[] P) { 141 Tree Package = javaFileSpace; 142 143 for (int i=0; i<P.length; i++) { 144 if (Package != null) 145 Package = Package.getChild(P[i]); 146 } 147 148 return Package; 149 } 150 151 152 /*************************************************** 153 * * 154 * Debugging functions: * 155 * * 156 **************************************************/ 157 158 /** A simple test driver */ 159 //@ requires args != null; 160 //@ requires \nonnullelements(args); 161 public static void main(String[] args) throws IOException { 162 /* 163 * Parse command arguments: 164 */ 165 if (args.length != 3) { 166 System.out.println( 167 "Query: usage <package name> <typename> <extension>"); 168 return; 169 } 170 171 Query Q = new SlowQuery(); 172 String[] P = StringUtil.parseList(args[0],'.'); 173 174 if (!Q.accessable(P)) 175 System.out.println("Package not accessible."); 176 177 GenericFile result = Q.findFile(P, args[1], args[2]); 178 if (result==null) 179 System.out.println("Type not found."); 180 else 181 System.out.println("Type found @ " + result.getHumanName()); 182 } 183 }