KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > common > Template


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  */

23 package org.enhydra.tool.common;
24
25 // standard imports
26
import java.io.File JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.FileNotFoundException JavaDoc;
30 import java.net.JarURLConnection JavaDoc;
31 import java.net.MalformedURLException JavaDoc;
32 import java.net.URL JavaDoc;
33 import java.util.jar.JarEntry JavaDoc;
34 import java.util.jar.JarFile JavaDoc;
35 import java.util.Arrays JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Enumeration JavaDoc;
38
39 //
40
public class Template {
41     private File JavaDoc output = null;
42     private URL JavaDoc url = null;
43     private String JavaDoc base = new String JavaDoc();
44
45     //
46
public Template(URL JavaDoc u, String JavaDoc b) {
47         url = u;
48         base = b.replace('\\', '/');
49     }
50
51     public Template(File JavaDoc f, String JavaDoc b) {
52         try {
53             url = f.toURL();
54             base = b.replace('\\', '/');
55         } catch (MalformedURLException JavaDoc e) {
56             e.printStackTrace(System.err);
57         }
58     }
59
60     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
61         return url.openStream();
62     }
63
64     public Template[] listTemplates() {
65         Template[] children = new Template[0];
66
67         children = listTemplates(null);
68         return children;
69     }
70
71     public Template[] listTemplates(TemplateFilter filter) {
72         Template[] children = new Template[0];
73         ArrayList JavaDoc list = null;
74         Template[] jarTemplates = new Template[0];
75         Template[] fileTemplates = new Template[0];
76
77         jarTemplates = listJarTemplates(filter);
78         fileTemplates = listFileTemplates(filter);
79         list = new ArrayList JavaDoc(Arrays.asList(fileTemplates));
80         for (int j = 0; j < jarTemplates.length; j++) {
81             boolean override = false;
82
83             for (int f = 0; f < fileTemplates.length; f++) {
84                 if (jarTemplates[j].getRelativePath().equals(fileTemplates[f].getRelativePath())) {
85                     override = true;
86                     break;
87                 }
88             }
89             if (!override) {
90                 list.add(jarTemplates[j]);
91             }
92         }
93         jarTemplates = new Template[0];
94         fileTemplates = new Template[0];
95         list.trimToSize();
96         if (list.size() > 0) {
97             children = new Template[list.size()];
98             children = (Template[]) list.toArray(children);
99         }
100         list.clear();
101         return children;
102     }
103
104     private Template[] listJarTemplates(TemplateFilter filter) {
105         Template[] children = new Template[0];
106         ArrayList JavaDoc list = new ArrayList JavaDoc();
107         JarURLConnection JavaDoc connect = null;
108         JarFile JavaDoc jar = null;
109         JarEntry JavaDoc entry = null;
110         Enumeration JavaDoc entries = null;
111         URL JavaDoc baseUrl = null;
112         int index = -1;
113
114         if (url.getProtocol().equals("jar")) {
115             baseUrl = url;
116         } else {
117             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
118
119             buf.append(getClass().getName().replace('.', '/'));
120             buf.append(".class");
121             baseUrl = ClassLoader.getSystemResource(buf.toString());
122             buf.setLength(0);
123             index = baseUrl.toString().indexOf('!');
124             if (index > -1) {
125                 buf.append(baseUrl.toString().substring(0, index + 1));
126                 buf.append('/');
127                 buf.append(getBasePath());
128                 if (!buf.toString().endsWith("/")) {
129                     buf.append('/');
130                 }
131                 try {
132                     baseUrl = new URL JavaDoc(buf.toString());
133                 } catch (MalformedURLException JavaDoc e) {
134                     baseUrl = null;
135                     e.printStackTrace(System.err);
136                 }
137             } else {
138                 baseUrl = null;
139             }
140         }
141         if (baseUrl == null) {
142             entries = null;
143         } else {
144             try {
145                 connect = (JarURLConnection JavaDoc) baseUrl.openConnection();
146                 jar = connect.getJarFile();
147                 entries = jar.entries();
148             } catch (FileNotFoundException JavaDoc e) {
149                 System.err.println(e.getMessage());
150                 entries = null;
151             } catch (IOException JavaDoc e) {
152                 e.printStackTrace(System.err);
153                 entries = null;
154             }
155         }
156         if (entries == null) {
157
158             //
159
} else {
160             while (entries.hasMoreElements()) {
161                 String JavaDoc entryName = new String JavaDoc();
162                 String JavaDoc parentPath = new String JavaDoc();
163
164                 entry = (JarEntry JavaDoc) entries.nextElement();
165                 entryName = entry.getName();
166
167                 // Exclude our own directory
168
if (entryName.equals(getBasePath())) {
169                     continue;
170                 }
171                 index = entryName.lastIndexOf('/');
172                 if (index > -1 && entry.isDirectory()) {
173                     parentPath = entryName.substring(0, index);
174                 } else {
175                     parentPath = entryName;
176                 }
177                 index = parentPath.lastIndexOf('/');
178                 if (index > -1) {
179                     parentPath = parentPath.substring(0, index + 1);
180                 }
181                 if (!parentPath.equals(getBasePath())) {
182                     continue;
183                 }
184                 Template temp = null;
185                 URL JavaDoc newUrl = null;
186                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
187
188                 buf.append(extractJarPath(baseUrl));
189                 buf.append(entryName);
190                 try {
191                     newUrl = new URL JavaDoc(buf.toString());
192                     temp = new Template(newUrl, base);
193                     if (filter == null) {
194                         list.add(temp);
195                     } else if (filter.accept(temp)) {
196                         list.add(temp);
197                     }
198                 } catch (MalformedURLException JavaDoc e) {
199                     e.printStackTrace(System.err);
200                 }
201             }
202         }
203         list.trimToSize();
204         if (list.size() > 0) {
205             children = new Template[list.size()];
206             children = (Template[]) list.toArray(children);
207         }
208         list.clear();
209         return children;
210     }
211
212     private Template[] listFileTemplates(TemplateFilter filter) {
213         Template[] children = new Template[0];
214         ArrayList JavaDoc list = new ArrayList JavaDoc();
215         URL JavaDoc baseUrl = null;
216         File JavaDoc dir = null;
217         File JavaDoc[] files = new File JavaDoc[0];
218
219         if (url.getProtocol().equalsIgnoreCase("file")) {
220             baseUrl = url;
221         } else if (url.toString().startsWith("jar:file:")
222                    && url.toString().indexOf('!') > -1) {
223             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
224             String JavaDoc path = url.toString();
225
226             path = path.substring(4, url.toString().indexOf('!'));
227             path = path.substring(0, path.lastIndexOf('/'));
228             path = path.substring(0, path.lastIndexOf('/') + 1);
229             buf.append(path);
230             buf.append(getBasePath());
231             try {
232                 baseUrl = new URL JavaDoc(buf.toString());
233             } catch (MalformedURLException JavaDoc e) {
234                 e.printStackTrace(System.err);
235                 baseUrl = null;
236             }
237         }
238         if (baseUrl == null) {
239             list.clear();
240         } else {
241             dir = new File JavaDoc(baseUrl.getFile());
242             if (dir.isDirectory()) {
243                 files = dir.listFiles();
244                 for (int i = 0; i < files.length; i++) {
245                     Template temp = new Template(files[i], base);
246
247                     if (filter == null) {
248                         list.add(temp);
249                     } else if (filter.accept(temp)) {
250                         list.add(temp);
251                     }
252                 }
253             }
254         }
255         list.trimToSize();
256         if (list.size() > 0) {
257             children = new Template[list.size()];
258             children = (Template[]) list.toArray(children);
259         }
260         list.clear();
261         return children;
262     }
263
264     public int endsWith(String JavaDoc[] list) {
265         int ew = -1;
266
267         for (int i = 0; i < list.length; i++) {
268             if (endsWith(list[i])) {
269                 ew = i;
270                 break;
271             }
272         }
273         return ew;
274     }
275
276     public boolean endsWith(String JavaDoc in) {
277         boolean ew = false;
278
279         if (in == null || url == null) {
280             ew = false;
281         } else {
282             String JavaDoc compEnd = new String JavaDoc(in);
283             String JavaDoc compUrl = new String JavaDoc(url.toString());
284
285             compEnd = compEnd.replace('\\', '/');
286             if (compUrl.endsWith("/") && (!compEnd.endsWith("/"))) {
287                 compEnd = compEnd + '/';
288             }
289             compEnd = compEnd.toLowerCase();
290             compUrl = compUrl.toLowerCase();
291             ew = compUrl.endsWith(compEnd);
292         }
293         return ew;
294     }
295
296     public String JavaDoc getExtension() {
297         String JavaDoc ext = new String JavaDoc();
298         int index = -1;
299
300         if (url == null) {
301             ext = new String JavaDoc();
302         } else {
303             ext = new String JavaDoc(url.toString());
304             ext = ext.replace('\\', '/');
305             index = ext.lastIndexOf('/');
306             if (index > -1) {
307                 ext = ext.substring(index);
308             }
309             index = ext.lastIndexOf('.');
310             if (index > -1 && (ext.length() > index)) {
311                 ext = ext.substring(index + 1);
312             } else {
313                 ext = new String JavaDoc();
314             }
315         }
316         return ext;
317     }
318
319     public String JavaDoc getName() {
320         String JavaDoc n = new String JavaDoc();
321         int index = -1;
322
323         if (url == null) {
324             n = new String JavaDoc();
325         } else {
326             n = url.getFile();
327             index = n.lastIndexOf('/');
328             if (index == n.length()) {
329                 n = n.substring(0, n.length() - 1);
330             }
331             index = n.lastIndexOf('/');
332             n = n.substring(index);
333         }
334         return n;
335     }
336
337     private String JavaDoc getBasePath() {
338         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
339
340         buf.append(base);
341         buf.append(getRelativePath());
342         return buf.toString();
343     }
344
345     private String JavaDoc extractJarPath(URL JavaDoc jarUrl) {
346         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
347         int index = -1;
348
349         if (jarUrl.getProtocol().equalsIgnoreCase("jar")) {
350             index = jarUrl.toString().indexOf('!');
351             if (index > -1) {
352                 buf.append(jarUrl.toString().substring(0, index + 1));
353                 buf.append('/');
354             }
355         }
356         return buf.toString();
357     }
358
359     public String JavaDoc getRelativePath() {
360         String JavaDoc path = new String JavaDoc();
361         String JavaDoc file = new String JavaDoc();
362         int index = -1;
363
364         if (base == null || url == null) {
365             path = new String JavaDoc();
366         } else {
367             file = url.getFile();
368             index = file.indexOf(base);
369             if (index > -1) {
370                 path = file.substring(index + base.length());
371             } else {
372                 index = file.lastIndexOf('/');
373                 if (index > -1) {
374                     path = file.substring(index);
375                 } else {
376                     path = file;
377                 }
378             }
379         }
380         if (path.startsWith("/")) {
381             if (path.length() == 1) {
382                 path = ".";
383             } else {
384                 path = path.substring(1, path.length());
385             }
386         }
387         return path;
388     }
389
390     public boolean isDirectory() {
391         boolean isd = false;
392
393         if (url == null) {
394             isd = false;
395         } else if (url.getProtocol().equals("file")) {
396             File JavaDoc file = new File JavaDoc(url.getFile());
397
398             isd = file.isDirectory();
399         } else if (url.getProtocol().equals("jar")) {
400             JarURLConnection JavaDoc connect;
401             JarEntry JavaDoc entry = null;
402
403             try {
404                 connect = (JarURLConnection JavaDoc) url.openConnection();
405                 entry = connect.getJarEntry();
406                 isd = (entry.getSize() == 0) || entry.isDirectory()
407                       || entry.getName().endsWith("/");
408             } catch (IOException JavaDoc e) {
409                 e.printStackTrace(System.err);
410                 isd = false;
411             }
412         }
413         return isd;
414     }
415
416     public boolean isFile() {
417         boolean isf = false;
418
419         if (url == null) {
420             isf = false;
421         } else if (url.getProtocol().equals("file")) {
422             File JavaDoc file = new File JavaDoc(url.getFile());
423
424             isf = file.isFile();
425         } else if (url.getProtocol().equals("jar")) {
426             JarURLConnection JavaDoc connect;
427
428             try {
429                 connect = (JarURLConnection JavaDoc) url.openConnection();
430                 isf = (connect.getJarEntry().getSize() > 0);
431             } catch (IOException JavaDoc e) {
432                 e.printStackTrace(System.err);
433                 isf = false;
434             }
435         }
436         return isf;
437     }
438
439     public File JavaDoc getOutput() {
440         return output;
441     }
442
443     public void setOutput(File JavaDoc f) {
444         output = f;
445     }
446
447     public String JavaDoc toString() {
448         return url.toString();
449     }
450
451 }
452
Popular Tags