001: /*
002: * ProGuard -- shrinking, optimization, obfuscation, and preverification
003: * of Java bytecode.
004: *
005: * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the Free
009: * Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful, but WITHOUT
013: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
015: * more details.
016: *
017: * You should have received a copy of the GNU General Public License along
018: * with this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: package proguard.ant;
022:
023: import org.apache.tools.ant.*;
024: import proguard.*;
025:
026: import java.io.IOException;
027: import java.util.*;
028:
029: /**
030: * This Task allows to define a ProGuard configuration from Ant.
031: *
032: * @author Eric Lafortune
033: */
034: public class ConfigurationTask extends Task {
035: protected final Configuration configuration = new Configuration();
036:
037: /**
038: * Adds the contents of this configuration task to the given configuration.
039: * @param configuration the configuration to be extended.
040: */
041: public void appendTo(Configuration configuration) {
042: // Append all of these configuration entries to the given configuration.
043: configuration.programJars = extendClassPath(
044: configuration.programJars,
045: this .configuration.programJars);
046:
047: configuration.libraryJars = extendClassPath(
048: configuration.libraryJars,
049: this .configuration.libraryJars);
050:
051: configuration.keep = extendClassSpecifications(
052: configuration.keep, this .configuration.keep);
053:
054: configuration.whyAreYouKeeping = extendClassSpecifications(
055: configuration.whyAreYouKeeping,
056: this .configuration.whyAreYouKeeping);
057:
058: configuration.assumeNoSideEffects = extendClassSpecifications(
059: configuration.assumeNoSideEffects,
060: this .configuration.assumeNoSideEffects);
061:
062: configuration.keepAttributes = extendList(
063: configuration.keepAttributes,
064: this .configuration.keepAttributes);
065: }
066:
067: // Ant task nested elements.
068:
069: public void addConfiguredInjar(ClassPathElement classPathElement) {
070: configuration.programJars = extendClassPath(
071: configuration.programJars, classPathElement, false);
072: }
073:
074: public void addConfiguredOutjar(ClassPathElement classPathElement) {
075: configuration.programJars = extendClassPath(
076: configuration.programJars, classPathElement, true);
077: }
078:
079: public void addConfiguredLibraryjar(
080: ClassPathElement classPathElement) {
081: configuration.libraryJars = extendClassPath(
082: configuration.libraryJars, classPathElement, false);
083: }
084:
085: public void addConfiguredKeep(
086: KeepSpecificationElement keepSpecificationElement) {
087: configuration.keep = extendKeepSpecifications(
088: configuration.keep, keepSpecificationElement, true,
089: false);
090: }
091:
092: public void addConfiguredKeepclassmembers(
093: KeepSpecificationElement keepSpecificationElement) {
094: configuration.keep = extendKeepSpecifications(
095: configuration.keep, keepSpecificationElement, false,
096: false);
097: }
098:
099: public void addConfiguredKeepclasseswithmembers(
100: KeepSpecificationElement keepSpecificationElement) {
101: configuration.keep = extendKeepSpecifications(
102: configuration.keep, keepSpecificationElement, true,
103: true);
104: }
105:
106: public void addConfiguredKeepnames(
107: KeepSpecificationElement keepSpecificationElement) {
108: // Set the shrinking flag, based on the name (backward compatibility).
109: keepSpecificationElement.setAllowshrinking(true);
110:
111: configuration.keep = extendKeepSpecifications(
112: configuration.keep, keepSpecificationElement, true,
113: false);
114: }
115:
116: public void addConfiguredKeepclassmembernames(
117: KeepSpecificationElement keepSpecificationElement) {
118: // Set the shrinking flag, based on the name (backward compatibility).
119: keepSpecificationElement.setAllowshrinking(true);
120:
121: configuration.keep = extendKeepSpecifications(
122: configuration.keep, keepSpecificationElement, false,
123: false);
124: }
125:
126: public void addConfiguredKeepclasseswithmembernames(
127: KeepSpecificationElement keepSpecificationElement) {
128: // Set the shrinking flag, based on the name (backward compatibility).
129: keepSpecificationElement.setAllowshrinking(true);
130:
131: configuration.keep = extendKeepSpecifications(
132: configuration.keep, keepSpecificationElement, true,
133: true);
134: }
135:
136: public void addConfiguredWhyareyoukeeping(
137: ClassSpecificationElement classSpecificationElement) {
138: configuration.whyAreYouKeeping = extendClassSpecifications(
139: configuration.whyAreYouKeeping,
140: classSpecificationElement);
141: }
142:
143: public void addConfiguredAssumenosideeffects(
144: ClassSpecificationElement classSpecificationElement) {
145: configuration.assumeNoSideEffects = extendClassSpecifications(
146: configuration.assumeNoSideEffects,
147: classSpecificationElement);
148: }
149:
150: public void addConfiguredKeepattribute(
151: KeepAttributeElement keepAttributeElement) {
152: configuration.keepAttributes = extendAttributes(
153: configuration.keepAttributes, keepAttributeElement);
154: }
155:
156: public void addConfiguredAdaptresourcefilenames(
157: FilterElement filterElement) {
158: configuration.adaptResourceFileNames = extendFilter(
159: configuration.adaptResourceFileNames, filterElement);
160: }
161:
162: public void addConfiguredAdaptresourcefilecontents(
163: FilterElement filterElement) {
164: configuration.adaptResourceFileContents = extendFilter(
165: configuration.adaptResourceFileContents, filterElement);
166: }
167:
168: public void addConfiguredConfiguration(
169: ConfigurationElement configurationElement) {
170: configurationElement.appendTo(configuration);
171: }
172:
173: // Implementations for Task.
174:
175: public void addText(String text) throws BuildException {
176: try {
177: String arg = getProject().replaceProperties(text);
178:
179: ConfigurationParser parser = new ConfigurationParser(
180: new String[] { arg }, getProject().getBaseDir());
181:
182: try {
183: parser.parse(configuration);
184: } catch (ParseException ex) {
185: throw new BuildException(ex.getMessage());
186: } finally {
187: parser.close();
188: }
189: } catch (IOException ex) {
190: throw new BuildException(ex.getMessage());
191: }
192: }
193:
194: // Small utility methods.
195:
196: private ClassPath extendClassPath(ClassPath classPath,
197: ClassPathElement classPathElement, boolean output) {
198: if (classPath == null) {
199: classPath = new ClassPath();
200: }
201:
202: classPathElement.appendClassPathEntriesTo(classPath, output);
203:
204: return classPath;
205: }
206:
207: private ClassPath extendClassPath(ClassPath classPath,
208: ClassPath additionalClassPath) {
209: if (additionalClassPath != null) {
210: if (classPath == null) {
211: classPath = new ClassPath();
212: }
213:
214: classPath.addAll(additionalClassPath);
215: }
216:
217: return classPath;
218: }
219:
220: private List extendKeepSpecifications(List keepSpecifications,
221: KeepSpecificationElement keepSpecificationElement,
222: boolean markClasses, boolean markClassesConditionally) {
223: if (keepSpecifications == null) {
224: keepSpecifications = new ArrayList();
225: }
226:
227: keepSpecificationElement.appendTo(keepSpecifications,
228: markClasses, markClassesConditionally);
229:
230: return keepSpecifications;
231: }
232:
233: private List extendClassSpecifications(List classSpecifications,
234: ClassSpecificationElement classSpecificationElement) {
235: if (classSpecifications == null) {
236: classSpecifications = new ArrayList();
237: }
238:
239: classSpecificationElement.appendTo(classSpecifications);
240:
241: return classSpecifications;
242: }
243:
244: private List extendClassSpecifications(List classSpecifications,
245: List additionalClassSpecifications) {
246: if (additionalClassSpecifications != null) {
247: if (classSpecifications == null) {
248: classSpecifications = new ArrayList();
249: }
250:
251: classSpecifications.addAll(additionalClassSpecifications);
252: }
253:
254: return classSpecifications;
255: }
256:
257: private List extendAttributes(List attributes,
258: KeepAttributeElement keepAttributeElement) {
259: if (attributes == null) {
260: attributes = new ArrayList();
261: }
262:
263: keepAttributeElement.appendTo(attributes);
264:
265: return attributes;
266: }
267:
268: private List extendFilter(List filter, FilterElement filterElement) {
269: if (filter == null) {
270: filter = new ArrayList();
271: }
272:
273: filterElement.appendTo(filter);
274:
275: return filter;
276: }
277:
278: private List extendList(List list, List additionalList) {
279: if (additionalList != null) {
280: if (list == null) {
281: list = new ArrayList();
282: }
283:
284: list.addAll(additionalList);
285: }
286:
287: return list;
288: }
289: }
|