1 /* 2 * ProGuard -- shrinking, optimization, obfuscation, and preverification 3 * of Java bytecode. 4 * 5 * Copyright (c) 2002-2014 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.util.List; 27 28 29 /** 30 * This class can create DataEntryReader instances based on class path entries. 31 * The readers will unwrap the input data entries from any jars, wars, ears, 32 * and zips, before passing them to a given reader. 33 * 34 * @author Eric Lafortune 35 */ 36 public class DataEntryReaderFactory 37 { 38 /** 39 * Creates a DataEntryReader that can read the given class path entry. 40 * 41 * @param messagePrefix a prefix for messages that are printed out. 42 * @param classPathEntry the input class path entry. 43 * @param reader a data entry reader to which the reading of actual 44 * classes and resource files can be delegated. 45 * @return a DataEntryReader for reading the given class path entry. 46 */ createDataEntryReader(String messagePrefix, ClassPathEntry classPathEntry, DataEntryReader reader)47 public static DataEntryReader createDataEntryReader(String messagePrefix, 48 ClassPathEntry classPathEntry, 49 DataEntryReader reader) 50 { 51 boolean isApk = classPathEntry.isApk(); 52 boolean isJar = classPathEntry.isJar(); 53 boolean isAar = classPathEntry.isAar(); 54 boolean isWar = classPathEntry.isWar(); 55 boolean isEar = classPathEntry.isEar(); 56 boolean isZip = classPathEntry.isZip(); 57 58 List filter = classPathEntry.getFilter(); 59 List apkFilter = classPathEntry.getApkFilter(); 60 List jarFilter = classPathEntry.getJarFilter(); 61 List aarFilter = classPathEntry.getAarFilter(); 62 List warFilter = classPathEntry.getWarFilter(); 63 List earFilter = classPathEntry.getEarFilter(); 64 List zipFilter = classPathEntry.getZipFilter(); 65 66 System.out.println(messagePrefix + 67 (isApk ? "apk" : 68 isJar ? "jar" : 69 isAar ? "aar" : 70 isWar ? "war" : 71 isEar ? "ear" : 72 isZip ? "zip" : 73 "directory") + 74 " [" + classPathEntry.getName() + "]" + 75 (filter != null || 76 apkFilter != null || 77 jarFilter != null || 78 aarFilter != null || 79 warFilter != null || 80 earFilter != null || 81 zipFilter != null ? " (filtered)" : "")); 82 83 // Add a filter, if specified. 84 if (filter != null) 85 { 86 reader = new FilteredDataEntryReader( 87 new DataEntryNameFilter( 88 new ListParser(new FileNameParser()).parse(filter)), 89 reader); 90 } 91 92 // Unzip any apks, if necessary. 93 reader = wrapInJarReader(reader, isApk, apkFilter, ".apk"); 94 if (!isApk) 95 { 96 // Unzip any jars, if necessary. 97 reader = wrapInJarReader(reader, isJar, jarFilter, ".jar"); 98 if (!isJar) 99 { 100 // Unzip any aars, if necessary. 101 reader = wrapInJarReader(reader, isAar, aarFilter, ".aar"); 102 if (!isAar) 103 { 104 // Unzip any wars, if necessary. 105 reader = wrapInJarReader(reader, isWar, warFilter, ".war"); 106 if (!isWar) 107 { 108 // Unzip any ears, if necessary. 109 reader = wrapInJarReader(reader, isEar, earFilter, ".ear"); 110 if (!isEar) 111 { 112 // Unzip any zips, if necessary. 113 reader = wrapInJarReader(reader, isZip, zipFilter, ".zip"); 114 } 115 } 116 } 117 } 118 } 119 120 return reader; 121 } 122 123 124 /** 125 * Wraps the given DataEntryReader in a JarReader, filtering it if necessary. 126 */ wrapInJarReader(DataEntryReader reader, boolean isJar, List jarFilter, String jarExtension)127 private static DataEntryReader wrapInJarReader(DataEntryReader reader, 128 boolean isJar, 129 List jarFilter, 130 String jarExtension) 131 { 132 // Unzip any jars, if necessary. 133 DataEntryReader jarReader = new JarReader(reader); 134 135 if (isJar) 136 { 137 // Always unzip. 138 return jarReader; 139 } 140 else 141 { 142 // Add a filter, if specified. 143 if (jarFilter != null) 144 { 145 jarReader = new FilteredDataEntryReader( 146 new DataEntryNameFilter( 147 new ListParser(new FileNameParser()).parse(jarFilter)), 148 jarReader); 149 } 150 151 // Only unzip the right type of jars. 152 return new FilteredDataEntryReader( 153 new DataEntryNameFilter( 154 new ExtensionMatcher(jarExtension)), 155 jarReader, 156 reader); 157 } 158 } 159 } 160