KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > DataEntryWriterFactory


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 import java.io.*;
27
28
29 /**
30  * This class can create DataEntryWriter instances based on class paths. The
31  * writers will wrap the output in the proper jars, wars, ears, and zips.
32  *
33  * @author Eric Lafortune
34  */

35 public class DataEntryWriterFactory
36 {
37     /**
38      * Creates a DataEntryWriter that can write to the given class path entries.
39      *
40      * @param classPath the output class path.
41      * @param fromIndex the start index in the class path.
42      * @param toIndex the end index in the class path.
43      * @return a DataEntryWriter for writing to the given class path entries.
44      */

45     public static DataEntryWriter createDataEntryWriter(ClassPath classPath,
46                                                         int fromIndex,
47                                                         int toIndex)
48     {
49         DataEntryWriter writer = null;
50
51         // Create a chain of writers, one for each class path entry.
52
for (int index = toIndex - 1; index >= fromIndex; index--)
53         {
54             ClassPathEntry entry = classPath.get(index);
55             writer = createClassPathEntryWriter(entry, writer);
56         }
57
58         return writer;
59     }
60
61
62     /**
63      * Creates a DataEntryWriter that can write to the given class path entry,
64      * or delegate to another DataEntryWriter if its filters don't match.
65      */

66     private static DataEntryWriter createClassPathEntryWriter(ClassPathEntry classPathEntry,
67                                                               DataEntryWriter alternativeWriter)
68     {
69         String JavaDoc entryName = classPathEntry.getName();
70         boolean isJar = endsWithIgnoreCase(entryName, ".jar");
71         boolean isWar = endsWithIgnoreCase(entryName, ".war");
72         boolean isEar = endsWithIgnoreCase(entryName, ".ear");
73         boolean isZip = endsWithIgnoreCase(entryName, ".zip");
74
75         String JavaDoc filter = classPathEntry.getFilter();
76         String JavaDoc jarFilter = classPathEntry.getJarFilter();
77         String JavaDoc warFilter = classPathEntry.getWarFilter();
78         String JavaDoc earFilter = classPathEntry.getEarFilter();
79         String JavaDoc zipFilter = classPathEntry.getZipFilter();
80
81         System.out.println("Preparing output " +
82                            (isJar ? "jar" :
83                             isWar ? "war" :
84                             isWar ? "ear" :
85                             isZip ? "zip" :
86                                     "directory") +
87                            " [" + entryName + "]" +
88                            (filter != null ||
89                             jarFilter != null ||
90                             warFilter != null ||
91                             earFilter != null ||
92                             zipFilter != null ? " (filtered)" : ""));
93
94         DataEntryWriter writer = new DirectoryWriter(classPathEntry.getFile(),
95                                                      isJar ||
96                                                      isWar ||
97                                                      isEar ||
98                                                      isZip);
99
100         // Set up the filtered jar writers.
101
writer = wrapInJarWriter(writer, isZip, zipFilter, ".zip", isJar || isWar || isEar);
102         writer = wrapInJarWriter(writer, isEar, earFilter, ".ear", isJar || isWar);
103         writer = wrapInJarWriter(writer, isWar, warFilter, ".war", isJar);
104         writer = wrapInJarWriter(writer, isJar, jarFilter, ".jar", false);
105
106         // Add a filter, if specified.
107
writer = filter != null?
108             new FilteredDataEntryWriter(
109             new DataEntryNameFilter(
110             new FileNameListMatcher(filter)),
111                 writer) :
112             writer;
113
114         // Let the writer cascade, if specified.
115
return alternativeWriter != null ?
116             new CascadingDataEntryWriter(writer, alternativeWriter) :
117             writer;
118     }
119
120
121     /**
122      * Wraps the given DataEntryWriter in a JarWriter, filtering if necessary.
123      */

124     private static DataEntryWriter wrapInJarWriter(DataEntryWriter writer,
125                                                    boolean isJar,
126                                                    String JavaDoc jarFilter,
127                                                    String JavaDoc jarExtension,
128                                                    boolean dontWrap)
129     {
130         // Zip up jars, if necessary.
131
DataEntryWriter jarWriter = dontWrap ?
132             (DataEntryWriter)new ParentDataEntryWriter(writer) :
133             (DataEntryWriter)new JarWriter(writer, null, ProGuard.VERSION);
134
135         // Add a filter, if specified.
136
DataEntryWriter filteredJarWriter = jarFilter != null?
137             new FilteredDataEntryWriter(
138             new DataEntryParentFilter(
139             new DataEntryNameFilter(
140             new FileNameListMatcher(jarFilter))),
141                  jarWriter) :
142             jarWriter;
143
144         // Only zip up jars, unless the output is a jar file itself.
145
return new FilteredDataEntryWriter(
146                new DataEntryParentFilter(
147                new DataEntryNameFilter(
148                new ExtensionMatcher(jarExtension))),
149                    filteredJarWriter,
150                    isJar ? jarWriter : writer);
151     }
152
153
154     /**
155      * Returns whether the given string ends with the given suffix, ignoring its
156      * case.
157      */

158     private static boolean endsWithIgnoreCase(String JavaDoc string, String JavaDoc suffix)
159     {
160         int stringLength = string.length();
161         int suffixLength = suffix.length();
162
163         return string.regionMatches(true, stringLength - suffixLength, suffix, 0, suffixLength);
164     }
165 }
166
Popular Tags