1/* 2 * ProGuard -- shrinking, optimization, obfuscation, and preverification 3 * of Java bytecode. 4 * 5 * Copyright (c) 2002-2009 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 */ 21package proguard; 22 23import proguard.io.*; 24import proguard.util.*; 25 26import java.util.List; 27 28/** 29 * This class can create DataEntryWriter instances based on class paths. The 30 * writers will wrap the output in the proper jars, wars, ears, and zips. 31 * 32 * @author Eric Lafortune 33 */ 34public class DataEntryWriterFactory 35{ 36 /** 37 * Creates a DataEntryWriter that can write to the given class path entries. 38 * 39 * @param classPath the output class path. 40 * @param fromIndex the start index in the class path. 41 * @param toIndex the end index in the class path. 42 * @return a DataEntryWriter for writing to the given class path entries. 43 */ 44 public static DataEntryWriter createDataEntryWriter(ClassPath classPath, 45 int fromIndex, 46 int toIndex) 47 { 48 DataEntryWriter writer = null; 49 50 // Create a chain of writers, one for each class path entry. 51 for (int index = toIndex - 1; index >= fromIndex; index--) 52 { 53 ClassPathEntry entry = classPath.get(index); 54 writer = createClassPathEntryWriter(entry, writer); 55 } 56 57 return writer; 58 } 59 60 61 /** 62 * Creates a DataEntryWriter that can write to the given class path entry, 63 * or delegate to another DataEntryWriter if its filters don't match. 64 */ 65 private static DataEntryWriter createClassPathEntryWriter(ClassPathEntry classPathEntry, 66 DataEntryWriter alternativeWriter) 67 { 68 String entryName = classPathEntry.getName(); 69 boolean isJar = endsWithIgnoreCase(entryName, ".jar"); 70 boolean isWar = endsWithIgnoreCase(entryName, ".war"); 71 boolean isEar = endsWithIgnoreCase(entryName, ".ear"); 72 boolean isZip = endsWithIgnoreCase(entryName, ".zip"); 73 74 List filter = classPathEntry.getFilter(); 75 List jarFilter = classPathEntry.getJarFilter(); 76 List warFilter = classPathEntry.getWarFilter(); 77 List earFilter = classPathEntry.getEarFilter(); 78 List zipFilter = classPathEntry.getZipFilter(); 79 80 System.out.println("Preparing output " + 81 (isJar ? "jar" : 82 isWar ? "war" : 83 isEar ? "ear" : 84 isZip ? "zip" : 85 "directory") + 86 " [" + entryName + "]" + 87 (filter != null || 88 jarFilter != null || 89 warFilter != null || 90 earFilter != null || 91 zipFilter != null ? " (filtered)" : "")); 92 93 DataEntryWriter writer = new DirectoryWriter(classPathEntry.getFile(), 94 isJar || 95 isWar || 96 isEar || 97 isZip); 98 99 // Set up the filtered jar writers. 100 writer = wrapInJarWriter(writer, isZip, zipFilter, ".zip", isJar || isWar || isEar); 101 writer = wrapInJarWriter(writer, isEar, earFilter, ".ear", isJar || isWar); 102 writer = wrapInJarWriter(writer, isWar, warFilter, ".war", isJar); 103 writer = wrapInJarWriter(writer, isJar, jarFilter, ".jar", false); 104 105 // Add a filter, if specified. 106 writer = filter != null? 107 new FilteredDataEntryWriter( 108 new DataEntryNameFilter( 109 new ListParser(new FileNameParser()).parse(filter)), 110 writer) : 111 writer; 112 113 // Let the writer cascade, if specified. 114 return alternativeWriter != null ? 115 new CascadingDataEntryWriter(writer, alternativeWriter) : 116 writer; 117 } 118 119 120 /** 121 * Wraps the given DataEntryWriter in a JarWriter, filtering if necessary. 122 */ 123 private static DataEntryWriter wrapInJarWriter(DataEntryWriter writer, 124 boolean isJar, 125 List jarFilter, 126 String jarExtension, 127 boolean dontWrap) 128 { 129 // Zip up jars, if necessary. 130 DataEntryWriter jarWriter = dontWrap ? 131 (DataEntryWriter)new ParentDataEntryWriter(writer) : 132 (DataEntryWriter)new JarWriter(writer); 133 134 // Add a filter, if specified. 135 DataEntryWriter filteredJarWriter = jarFilter != null? 136 new FilteredDataEntryWriter( 137 new DataEntryParentFilter( 138 new DataEntryNameFilter( 139 new ListParser(new FileNameParser()).parse(jarFilter))), 140 jarWriter) : 141 jarWriter; 142 143 // Only zip up jars, unless the output is a jar file itself. 144 return new FilteredDataEntryWriter( 145 new DataEntryParentFilter( 146 new DataEntryNameFilter( 147 new ExtensionMatcher(jarExtension))), 148 filteredJarWriter, 149 isJar ? jarWriter : writer); 150 } 151 152 153 /** 154 * Returns whether the given string ends with the given suffix, ignoring its 155 * case. 156 */ 157 private static boolean endsWithIgnoreCase(String string, String suffix) 158 { 159 int stringLength = string.length(); 160 int suffixLength = suffix.length(); 161 162 return string.regionMatches(true, stringLength - suffixLength, suffix, 0, suffixLength); 163 } 164} 165