KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > builder > SourceFileList


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.source.builder;
20
21 import java.io.*;
22 import java.util.*;
23 import java.util.logging.*;
24 import java.util.zip.ZipFile JavaDoc;
25 import java.util.zip.ZipEntry JavaDoc;
26 import org.netbeans.modules.java.source.engine.ElapsedTimer;
27
28 /**
29  * File and Jar file expansion utility routines.
30  */

31 public final class SourceFileList {
32
33     public static final String JavaDoc JAR_URL_PREFIX = "jar:/file:/";
34
35     public static String JavaDoc[] findFiles(String JavaDoc [] args) {
36     return findFiles(args, allFilter, null);
37     }
38
39     public static String JavaDoc[] findFiles(String JavaDoc [] args, Logger logger) {
40     return findFiles(args, allFilter, logger);
41     }
42
43     public static String JavaDoc[] findFiles(String JavaDoc[] args, SourceFileFilter filter) {
44     return findFiles(args, filter, null);
45     }
46
47     public static String JavaDoc[] findFiles(String JavaDoc [] args, SourceFileFilter filter,
48                      Logger logger) {
49     ElapsedTimer timer = new ElapsedTimer();
50     List<String JavaDoc> nargs = new ArrayList<String JavaDoc>();
51     int limit = args.length;
52     for(int i = 0; i<limit; i++)
53         expand(null, args[i], filter, nargs);
54     String JavaDoc[] files = nargs.toArray(new String JavaDoc[0]);
55     if (logger != null) {
56         logger.fine("found " + Integer.toString(files.length) +
57                " source files in " + timer.toString());
58     }
59     return files;
60     }
61
62     private static void expand(File JavaDoc dir, String JavaDoc name, SourceFileFilter filter,
63                    List<String JavaDoc> nargs) {
64     File JavaDoc f = dir != null ? new File JavaDoc(dir, name) : new File JavaDoc(name);
65     if (filter.acceptFile(name)) {
66             String JavaDoc path = f.getPath();
67             if (!nargs.contains(path))
68                 nargs.add(path);
69         return;
70     }
71     if(name.endsWith(".jar") || name.endsWith(".zip")) {
72         /* No one has asked for this feature yet, and several NetBeans unit
73          * tests have source jars with bad files in them.
74          */

75         //expandZip(f.getPath(), filter, nargs);
76
return;
77     }
78     String JavaDoc[] subnames = f.list();
79     if (subnames == null)
80         return;
81     int limit = subnames.length;
82     for(int i = 0; i<limit; i++) {
83         String JavaDoc sn = subnames[i];
84         if (filter.acceptDirectory(sn))
85         expand(f, sn, filter, nargs);
86     }
87     }
88
89     private static void expandZip(String JavaDoc zipFileName, SourceFileFilter filter,
90                   List<String JavaDoc> nargs) {
91     try {
92         ZipFile JavaDoc zf = new ZipFile JavaDoc(zipFileName);
93         Enumeration files = zf.entries();
94         while (files.hasMoreElements()) {
95         ZipEntry JavaDoc entry = (ZipEntry JavaDoc)files.nextElement();
96         String JavaDoc name = entry.getName();
97         if (filter.acceptFile(name)) {
98                     name = JAR_URL_PREFIX + zipFileName + '!' + name;
99                     if (!nargs.contains(name))
100                         nargs.add(name);
101                 }
102         }
103     } catch (IOException e) {
104         System.err.println("failed reading zip file " + zipFileName +
105                    ": " + e);
106     }
107     }
108
109     private static final SourceFileFilter allFilter =
110     new DefaultSourceFileFilter();
111 }
112
Popular Tags