KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > DataEntryReaderFactory


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard;
22
23 import proguard.io.*;
24 import proguard.util.*;
25
26
27 /**
28  * This class can create DataEntryReader instances based on class path entries.
29  * The readers will unwrap the input data entries from any jars, wars, ears,
30  * and zips, before passing them to a given reader.
31  *
32  * @author Eric Lafortune
33  */

34 public class DataEntryReaderFactory
35 {
36     /**
37      * Creates a DataEntryReader that can read the given class path entry.
38      *
39      * @param messagePrefix a prefix for messages that are printed out.
40      * @param classPathEntry the input class path entry.
41      * @param reader a data entry reader to which the reading of actual
42      * classes and resource files can be delegated.
43      * @return a DataEntryReader for reading the given class path entry.
44      */

45     public static DataEntryReader createDataEntryReader(String JavaDoc messagePrefix,
46                                                         ClassPathEntry classPathEntry,
47                                                         DataEntryReader reader)
48     {
49         String JavaDoc entryName = classPathEntry.getName();
50         boolean isJar = endsWithIgnoreCase(entryName, ".jar");
51         boolean isWar = endsWithIgnoreCase(entryName, ".war");
52         boolean isEar = endsWithIgnoreCase(entryName, ".ear");
53         boolean isZip = endsWithIgnoreCase(entryName, ".zip");
54
55         String JavaDoc filter = classPathEntry.getFilter();
56         String JavaDoc jarFilter = classPathEntry.getJarFilter();
57         String JavaDoc warFilter = classPathEntry.getWarFilter();
58         String JavaDoc earFilter = classPathEntry.getEarFilter();
59         String JavaDoc zipFilter = classPathEntry.getZipFilter();
60
61         System.out.println(messagePrefix +
62                            (isJar ? "jar" :
63                             isWar ? "war" :
64                             isEar ? "ear" :
65                             isZip ? "zip" :
66                                     "directory") +
67                            " [" + classPathEntry.getName() + "]" +
68                            (filter != null ||
69                             jarFilter != null ||
70                             warFilter != null ||
71                             earFilter != null ||
72                             zipFilter != null ? " (filtered)" : ""));
73
74         // Add a filter, if specified.
75
if (filter != null)
76         {
77             reader = new FilteredDataEntryReader(new DataEntryNameFilter(new FileNameListMatcher(filter)),
78                                                  reader);
79         }
80
81         // Unzip any jars, if necessary.
82
reader = wrapInJarReader(reader, isJar, jarFilter, ".jar");
83         if (!isJar)
84         {
85             // Unzip any wars, if necessary.
86
reader = wrapInJarReader(reader, isWar, warFilter, ".war");
87             if (!isWar)
88             {
89                 // Unzip any ears, if necessary.
90
reader = wrapInJarReader(reader, isEar, earFilter, ".ear");
91                 if (!isEar)
92                 {
93                     // Unzip any zips, if necessary.
94
reader = wrapInJarReader(reader, isZip, zipFilter, ".zip");
95                 }
96             }
97         }
98
99         return reader;
100     }
101
102
103     /**
104      * Wraps the given DataEntryReader in a JarReader, filtering it if necessary.
105      */

106     private static DataEntryReader wrapInJarReader(DataEntryReader reader,
107                                                    boolean isJar,
108                                                    String JavaDoc jarFilter,
109                                                    String JavaDoc jarExtension)
110     {
111         // Unzip any jars, if necessary.
112
DataEntryReader jarReader = new JarReader(reader);
113
114         if (isJar)
115         {
116             // Always unzip.
117
return jarReader;
118         }
119         else
120         {
121             // Add a filter, if specified.
122
if (jarFilter != null)
123             {
124                 jarReader = new FilteredDataEntryReader(
125                             new DataEntryNameFilter(
126                             new FileNameListMatcher(jarFilter)),
127                                 jarReader);
128             }
129
130             // Only unzip the right type of jars.
131
return new FilteredDataEntryReader(
132                    new DataEntryNameFilter(
133                    new ExtensionMatcher(jarExtension)),
134                        jarReader,
135                        reader);
136         }
137     }
138
139
140     /**
141      * Returns whether the given string ends with the given suffix, ignoring its
142      * case.
143      */

144     private static boolean endsWithIgnoreCase(String JavaDoc string, String JavaDoc suffix)
145     {
146         int stringLength = string.length();
147         int suffixLength = suffix.length();
148
149         return string.regionMatches(true, stringLength - suffixLength, suffix, 0, suffixLength);
150     }
151 }
152
Popular Tags