~ubuntu-branches/ubuntu/breezy/proguard/breezy

« back to all changes in this revision

Viewing changes to src/proguard/DataEntryReaderFactory.java

  • Committer: Bazaar Package Importer
  • Author(s): Sam Clegg
  • Date: 2005-06-17 14:25:24 UTC
  • Revision ID: james.westby@ubuntu.com-20050617142524-thz2yfa3vemy3j9d
Tags: upstream-3.2
ImportĀ upstreamĀ versionĀ 3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: DataEntryReaderFactory.java,v 1.2 2004/08/15 12:39:30 eric Exp $
 
2
 *
 
3
 * ProGuard -- shrinking, optimization, and obfuscation of Java class files.
 
4
 *
 
5
 * Copyright (c) 2002-2004 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
     *                       class files and resource files can be delegated.
 
43
     * @return a DataEntryReader for reading the given class path entry.
 
44
     */
 
45
    public static DataEntryReader createDataEntryReader(String          messagePrefix,
 
46
                                                        ClassPathEntry  classPathEntry,
 
47
                                                        DataEntryReader reader)
 
48
    {
 
49
        String entryName = classPathEntry.getName();
 
50
        boolean isJar = entryName.endsWith(".jar");
 
51
        boolean isWar = entryName.endsWith(".war");
 
52
        boolean isEar = entryName.endsWith(".ear");
 
53
        boolean isZip = entryName.endsWith(".zip");
 
54
 
 
55
        String filter    = classPathEntry.getFilter();
 
56
        String jarFilter = classPathEntry.getJarFilter();
 
57
        String warFilter = classPathEntry.getWarFilter();
 
58
        String earFilter = classPathEntry.getEarFilter();
 
59
        String 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          jarFilter,
 
109
                                                   String          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
}