1 21 package proguard; 22 23 import proguard.classfile.ClassPool; 24 import proguard.io.*; 25 import proguard.util.FileNameListMatcher; 26 27 import java.io.IOException ; 28 29 34 public class OutputWriter 35 { 36 private Configuration configuration; 37 38 39 43 public OutputWriter(Configuration configuration) 44 { 45 this.configuration = configuration; 46 } 47 48 49 53 public void execute(ClassPool programClassPool) throws IOException 54 { 55 ClassPath programJars = configuration.programJars; 56 57 ClassPathEntry firstEntry = programJars.get(0); 59 if (firstEntry.isOutput()) 60 { 61 throw new IOException ("The output jar [" + firstEntry.getName() + 62 "] must be specified after an input jar, or it will be empty."); 63 } 64 65 for (int index = 0; index < programJars.size() - 1; index++) 67 { 68 ClassPathEntry entry = programJars.get(index); 69 if (entry.isOutput()) 70 { 71 if (entry.getFilter() == null && 73 entry.getJarFilter() == null && 74 entry.getWarFilter() == null && 75 entry.getEarFilter() == null && 76 entry.getZipFilter() == null && 77 programJars.get(index + 1).isOutput()) 78 { 79 throw new IOException ("The output jar [" + entry.getName() + 80 "] must have a filter, or all subsequent jars will be empty."); 81 } 82 83 for (int inIndex = 0; inIndex < programJars.size(); inIndex++) 85 { 86 ClassPathEntry otherEntry = programJars.get(inIndex); 87 88 if (!otherEntry.isOutput() && 89 entry.getFile().equals(otherEntry.getFile())) 90 { 91 throw new IOException ("The output jar [" + entry.getName() + 92 "] must be different from all input jars."); 93 } 94 } 95 } 96 } 97 98 int firstInputIndex = 0; 99 int lastInputIndex = 0; 100 101 for (int index = 0; index < programJars.size(); index++) 103 { 104 ClassPathEntry entry = programJars.get(index); 106 if (!entry.isOutput()) 107 { 108 lastInputIndex = index; 110 } 111 else 112 { 113 int nextIndex = index + 1; 115 if (nextIndex == programJars.size() || 116 !programJars.get(nextIndex).isOutput()) 117 { 118 writeOutput(programClassPool, 120 programJars, 121 firstInputIndex, 122 lastInputIndex + 1, 123 nextIndex); 124 125 firstInputIndex = nextIndex; 127 } 128 } 129 } 130 } 131 132 133 136 private void writeOutput(ClassPool programClassPool, 137 ClassPath classPath, 138 int fromInputIndex, 139 int fromOutputIndex, 140 int toOutputIndex) 141 throws IOException 142 { 143 try 144 { 145 DataEntryWriter writer = 148 DataEntryWriterFactory.createDataEntryWriter(classPath, 149 fromOutputIndex, 150 toOutputIndex); 151 152 DataEntryReader classRewriter = 154 new ClassRewriter(programClassPool, writer); 155 156 DataEntryReader resourceRewriter = 158 new DataEntryCopier(writer); 159 160 if (configuration.adaptResourceFileNames != null) 163 { 164 resourceRewriter = 165 new FilteredDataEntryReader( 166 new DataEntryNameFilter(new FileNameListMatcher(configuration.adaptResourceFileNames)), 167 new DataEntryRenamer(programClassPool, resourceRewriter), 168 resourceRewriter); 169 } 170 171 if (configuration.adaptResourceFileContents != null) 174 { 175 resourceRewriter = 176 new FilteredDataEntryReader( 177 new DataEntryNameFilter(new FileNameListMatcher(configuration.adaptResourceFileContents)), 178 new DataEntryRewriter(programClassPool, writer), 179 resourceRewriter); 180 } 181 182 DataEntryReader reader = 185 new ClassFilter(classRewriter, resourceRewriter); 186 187 new InputReader(configuration).readInput(" Copying resources from program ", 190 classPath, 191 fromInputIndex, 192 fromOutputIndex, 193 reader); 194 195 writer.close(); 197 } 198 catch (IOException ex) 199 { 200 throw new IOException ("Can't write [" + classPath.get(fromOutputIndex).getName() + "] (" + ex.getMessage() + ")"); 201 } 202 } 203 } 204 | Popular Tags |