View Javadoc
1 package net.sf.plb4jedit.filelocator;
2 import java.io.*;
3 import java.util.*;
4 import org.gjt.sp.util.Log;
5
6 public class FileLocator {
7 private final String DATABASENAME=".FileLocator.map";
8 private String storeName;
9 private HashMap store; // map of Maps (key,MapforThisKey)
10
11 public FileLocator() throws Exception {
12 storeName = System.getProperty("user.home") + File.separator + DATABASENAME;
13 loadMap();
14 }
15
16 public FileLocator(String storeName) throws Exception {
17 this.storeName = storeName;
18 loadMap();
19 }
20
21 public void insertMap(String mapName, String path, String extension) throws Exception {
22 // try to append to an existing map, not overwrite!
23 Map m = (Map)store.get(mapName);
24 if (m == null) {
25 m = new HashMap();
26 }
27 store.put(mapName,m);
28 try {
29 File f = new File(path);
30 if (f != null && f.isDirectory()) {
31 processDirectory(f,m,extension);
32 }
33 } catch (Exception e) {
34 e.printStackTrace();
35 }
36 // store map in users home directory
37 FileOutputStream ostream = new FileOutputStream(storeName);
38 ObjectOutputStream p = new ObjectOutputStream(ostream);
39 p.writeObject(store);
40 p.flush();
41 ostream.flush();
42 ostream.close();
43 debug("stored " +storeName);
44 }
45
46 private void loadMap() throws Exception {
47 File f = new File(storeName);
48 debug("storeName "+storeName);
49 if (f != null && f.exists()) {
50 FileInputStream istream = new FileInputStream(f);
51 ObjectInputStream p = new ObjectInputStream(istream);
52 store = (HashMap)p.readObject();
53 istream.close();
54 debug("map loaded");
55 } else {
56 debug("no map loaded");
57 store = new HashMap();
58 }
59 }
60
61 public void reload() throws Exception {
62 store.clear();
63 loadMap();
64 }
65
66 public void delete() {
67 store = null;
68 try {
69 File f = new File(System.getProperty("user.home") + File.separator + DATABASENAME);
70 if (f != null && f.exists()) {
71 f.delete();
72 loadMap();
73 System.out.println(f.getAbsolutePath() + " deleted.");
74 } else {
75 System.out.println(f.getAbsolutePath() + " not deleted.");
76 }
77 } catch (Exception ex) {
78 ex.printStackTrace();
79 System.out.println("delete failed.");
80 }
81 }
82
83 public void delete(String mapName) {
84 if (store.containsKey(mapName)) {
85 store.remove(mapName);
86 }
87 }
88
89 public String[] locate(String mapName, String source) throws Exception {
90 debug("locate("+mapName+","+source+")" + " size of store "+ store.size());
91 Map m = (Map)store.get(mapName);
92 if (m != null) {
93 debug("found map");
94 Object o = m.get(source);
95 if (o == null) {
96 throw new IllegalStateException("didn't find '" + source + "' in map " + mapName);
97 }
98 if (o instanceof String) {
99 String[] s = new String[1];
100 s[0] = (String)o;
101 return s;
102 } else if (o instanceof Set) {
103 Set set = (Set)o;
104 String[] s = new String[set.size()];
105 int i = 0;
106 for (Iterator it = set.iterator(); it.hasNext(); ) {
107 s[i++] = (String) it.next();
108 }
109 return s;
110 } else {
111 return null;
112 }
113 } else {
114 throw new IllegalStateException("no map defined for " + mapName);
115 }
116 }
117
118 public String[] locatePartially(String mapName, String source) throws Exception {
119 debug("locatePartially("+mapName+","+source+")" + " size of store "+ store.size());
120 String[] s = null;
121 ArrayList a = new ArrayList();
122 Map m = (Map)store.get(mapName);
123 if (m == null) {
124 throw new IllegalStateException("no map defined for " + mapName);
125 }
126 for (Iterator it = m.keySet().iterator(); it.hasNext();) {
127 String key = (String) it.next();
128 if (key.indexOf(source) > -1) {
129 Object o = m.get(key);
130 if (o instanceof String) {
131 a.add(o);
132 } else if (o instanceof Set) {
133 Set set = (Set)o;
134 for (Iterator it2 = set.iterator(); it2.hasNext(); ) {
135 a.add(it2.next());
136 }
137 }
138 }
139 }
140 if (a.size() > 0) {
141 a.trimToSize();
142 s = new String[a.size()];
143 s = (String[]) a.toArray(s);
144 }
145 return s;
146 }
147
148 public String dump() throws Exception {
149 StringBuffer out = new StringBuffer("File: " + storeName + "\n");
150 if (store == null || store.size() == 0) {
151 out.append("database empty\n");
152 return out.toString();
153 }
154
155 for (Iterator it = store.keySet().iterator(); it.hasNext();) {
156 String mapName = (String) it.next();
157 out.append("Map " + mapName + "\n");
158 if (mapName == null || mapName.length() == 0) continue;
159 Map m = (Map)store.get(mapName);
160 if (m != null) {
161 for (Iterator it2 = m.keySet().iterator(); it2.hasNext(); ) {
162 String key = (String) it2.next();
163 String[] s = locate(mapName,key);
164 for (int i=0; i<s.length;i++) {
165 out.append(key+"\t\t"+s[i]+ "\n");
166 }
167 }
168 }
169 }
170 return out.toString();
171 }
172
173 public String[] getMaps() {
174 Set keys = store.keySet();
175 if (keys.size() == 0) {
176 return null;
177 }
178 String[] mapNames = new String[keys.size()];
179 int i = 0;
180 for (Iterator it = keys.iterator(); it.hasNext();) {
181 mapNames[i++] = (String) it.next();
182 }
183 return mapNames;
184 }
185
186 private void processDirectory(File d, Map m, String extension) throws Exception {
187 if (d == null || !d.isDirectory() || !d.exists() || !d.canRead()) return;
188 File[] files = d.listFiles();
189 if (files == null || files.length == 0) return;
190
191 for (int i = 0; i < files.length; i++) {
192 try {
193 if (files[i].isDirectory()) {
194 processDirectory(files[i], m, extension);
195 } else {
196 String path = files[i].getCanonicalPath();
197 String fileName = files[i].getName();
198 if (extension != null && extension.length() > 0 && fileName.endsWith(extension)) {
199 String key = getKey(fileName,extension);
200 if (!m.containsKey(key)) {
201 m.put(key,path);
202 debug("added "+key+" in " + path);
203 } else {
204 Object entry = m.get(key);
205 if (entry instanceof String) {
206 Set set = new HashSet(2);
207 set.add(entry);
208 set.add(path);
209 m.put(key,set);
210 } else if (entry instanceof Set) {
211 ((Set)entry).add(path);
212 }
213 }
214 }
215 }
216 } catch (Exception e) {
217 System.err.println("Catched exception. Trying to proceed");
218 e.printStackTrace();
219 //return;
220 }
221 }
222 }
223
224 private String getKey(String fileName,String extension) {
225 return fileName.substring(0,fileName.indexOf(extension));
226 }
227
228 private void debug(String s) {
229 if (false) {
230 Log.log(Log.DEBUG,this,s);
231 }
232 }
233
234 public static void main(String[] args) throws Exception {
235 if (args.length < 1) {
236 System.out.println("usage FileLocator mode modespecific");
237 System.out.println(" modes: updatedb | locate | partial | delete | dump");
238 System.exit(0);
239 }
240 FileLocator fl = new FileLocator();
241 if (args[0].equals("updatedb")) {
242 if (args.length == 4) {
243 fl.insertMap(args[1],args[2],args[3]);
244 } else {
245 System.out.println("FileLocator updatedb mapName path extension");
246 }
247 } else if (args[0].equals("locate")) {
248 if (args.length == 3) {
249 String[] s = fl.locate(args[1],args[2]);
250 if (s != null) {
251 for (int i = 0; i < s.length ; i++) {
252 System.out.println(s[i]);
253 }
254 } else {
255 System.out.println("Nothing found");
256 }
257 } else {
258 System.out.println("FileLocator locate mapName file");
259 }
260 } else if (args[0].equals("delete")) {
261 fl.delete();
262 } else if (args[0].equals("dump")) {
263 System.out.println(fl.dump());
264 } else if (args[0].equals("partial")) {
265 if (args.length == 3) {
266 String[] s = fl.locatePartially(args[1],args[2]);
267 if (s != null) {
268 for (int i = 0; i < s.length ; i++) {
269 System.out.println(s[i]);
270 }
271 } else {
272 System.out.println("Nothing found");
273 }
274 } else {
275 System.out.println("FileLocator partial mapName file");
276 }
277 } else {
278 System.out.println("unknown mode");
279 }
280 }
281 }
282
283
This page was automatically generated by Maven