Debian Codegraph @

texk/tex4htk/java/xtpipes/FileInfo.java

raw
1 package xtpipes;
2 /*
3 FileInfo.java (2009-01-27-22:19)
4 */
5 import java.io.File;
6 import java.io.PrintWriter;
7
8 public class FileInfo{
9 static String [] classPaths = null;
10 static String [] scriptPaths = null;
11 static java.util.HashMap <String,String> registry =
12 new java.util.HashMap <String,String>();
13 static String slash = System.getProperty("file.separator");
14 static String ii_scriptDir;
15 static PrintWriter log;
16 static boolean trace;
17
18 public FileInfo(PrintWriter log, String iii_scriptDir, boolean trace){
19 FileInfo.log = log;
20 FileInfo.ii_scriptDir = iii_scriptDir;
21 FileInfo.trace = trace;
22 classPaths = FileInfo.getPaths( System.getProperty("java.class.path") );
23 if( iii_scriptDir != null ){
24 scriptPaths = FileInfo.getPaths( iii_scriptDir );
25 }
26
27
28 }
29 public static String searchFile( String file ){
30 String key = ((ii_scriptDir == null)? "" : ii_scriptDir )
31 + "!" + file;
32 String result = (String) registry.get( key );
33 if( result == null ){
34 for(int i=0; i<2; i++){
35 if( trace ){
36 log.println( "Searching: " + file );
37 }
38 if( (new File(file)).exists() ){
39 result = ( file.indexOf(slash) == -1 )?
40 (System.getProperty("user.dir") + slash + file)
41 :
42 file;
43 }
44 else {
45 if( ii_scriptDir != null ){
46 int k = scriptPaths.length;
47 while( k>0 ){
48 k--;
49 if( trace ){
50 log.println( "Searching: " + file
51 + ", recursively in directory: " + scriptPaths[k] );
52 }
53 result = searchDirectory( new File(scriptPaths[k]), file);
54 if( result != null ){ break; }
55 }
56 String s = ii_scriptDir + file;
57 if( (new File( s )).exists() ){ result = s; }
58
59 }
60 if( result == null ){
61 int k = classPaths.length;
62 String toFile = "xtpipes" + slash + "lib" + slash + file;
63 while( k>0 ){
64 k--;
65 String s = classPaths[k] + toFile;
66 if( trace ){ log.println( "Searching: " + s ); }
67 if( new File(s).exists() ){ result = s; break; }
68 }
69
70 } }
71 if( result != null ){ break; }
72 file = new File(file).getName();
73 }
74 if( result != null ){
75 result = FileInfo.cleanPath(result);
76 registry.put(key, result);
77 }
78 }
79 if( trace ){
80 if( result == null ){
81 log.println(
82 "Directory paths from xtpipes command line option -i: "
83 + ii_scriptDir );
84 } else { log.println( "Found: " + result + "\n" ); }
85 log.flush();
86 }
87 return result;
88 }
89
90 static String [] getPaths( String dirs ){
91 String [] paths = null;
92 paths = dirs.split( System.getProperty("path.separator") );
93 int k = paths.length;
94 while( k>0 ){
95 k--;
96 paths[k] = cleanPath( paths[k] );
97
98 int len = paths[k].length();
99 if( (len>1) && (paths[k].lastIndexOf(slash + ".") == (len-1)) ){
100 paths[k] = paths[k].substring(0,len-1);
101 } else if( (len>0) && ((len-1) != paths[k].lastIndexOf( slash )) ){
102 paths[k] += slash;
103 } }
104 return paths;
105 }
106
107 public static String cleanPath( String path ){
108 String slash = System.getProperty("file.separator");
109 String userDir = System.getProperty( "user.dir" );
110 if( (path.length() > 0) && (path.charAt(0) == '~') ){
111 if( (path.length() == 1) || (path.charAt(1) != '~') ){
112 path = System.getProperty( "user.home" )
113 + path.substring(1);
114 } }
115
116 if( path.startsWith("..") ){
117 path = userDir.substring(0,
118 Math.max(0,Math.max(
119 userDir.lastIndexOf("/")
120 ,
121 userDir.lastIndexOf("\\")
122 )))
123 + path.substring(2);
124 }
125 if( path.startsWith(".") ){
126 path = userDir + slash + path.substring(1);
127 }
128
129 int i;
130 while(
131 ((i=path.indexOf("/..")) != -1)
132 ||
133 ((i=path.indexOf("\\..")) != -1)
134 ){
135 String s = path.substring(0,i);
136 int j = Math.max(s.lastIndexOf("/"), s.lastIndexOf("\\"));
137 path = path.substring(0,j) + path.substring(i+3);
138 }
139 while(
140 ((i=path.indexOf("/.")) != -1)
141 ||
142 ((i=path.indexOf("\\.")) != -1)
143 ){
144 String s = path.substring(0,i);
145 int j = Math.max(s.indexOf("/"), s.indexOf("\\"));
146 path = path.substring(0,j) + path.substring(i+2);
147 }
148
149 return path;
150 }
151
152 static String searchDirectory(File dir, String file) {
153 String result = null;
154 if( dir.isDirectory() ){
155 String [] children = dir.list();
156 for (int i=0; i<children.length; i++) {
157 result = searchDirectory( new File(dir,children[i]), file);
158 if( result != null ) { break; }
159 }
160 } else {
161 String s = dir.toString();
162 if( s.equals(file) || s.endsWith(slash + file) ){
163 result = s;
164 }
165 }
166 return result;
167 }
168
169 }
170