KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > ant > ConfigurationTask


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 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.ant;
22
23 import proguard.*;
24
25 import org.apache.tools.ant.*;
26
27 import java.io.*;
28 import java.util.*;
29
30 /**
31  * This Task allows to define a ProGuard configuration from Ant.
32  *
33  * @author Eric Lafortune
34  */

35 public class ConfigurationTask extends Task
36 {
37     protected Configuration configuration = new Configuration();
38
39
40     /**
41      * Adds the contents of this configuration task to the given configuration.
42      * @param configuration the configuration to be extended.
43      */

44     public void appendTo(Configuration configuration)
45     {
46         // Append all of these configuration entries to the given configuration.
47
configuration.programJars = extendClassPath(configuration.programJars,
48                                                     this.configuration.programJars);
49
50         configuration.libraryJars = extendClassPath(configuration.libraryJars,
51                                                     this.configuration.libraryJars);
52
53         configuration.keep = extendClassSpecifications(configuration.keep,
54                                                        this.configuration.keep);
55
56         configuration.whyAreYouKeeping = extendClassSpecifications(configuration.whyAreYouKeeping,
57                                                                    this.configuration.whyAreYouKeeping);
58
59         configuration.assumeNoSideEffects = extendClassSpecifications(configuration.assumeNoSideEffects,
60                                                                       this.configuration.assumeNoSideEffects);
61
62         configuration.keepAttributes = extendList(configuration.keepAttributes,
63                                                   this.configuration.keepAttributes);
64     }
65
66
67     // Ant task nested elements.
68

69     public void addConfiguredInjar(ClassPathElement classPathElement)
70     {
71         configuration.programJars = extendClassPath(configuration.programJars,
72                                                     classPathElement,
73                                                     false);
74     }
75
76
77     public void addConfiguredOutjar(ClassPathElement classPathElement)
78     {
79         configuration.programJars = extendClassPath(configuration.programJars,
80                                                     classPathElement,
81                                                     true);
82     }
83
84
85     public void addConfiguredLibraryjar(ClassPathElement classPathElement)
86     {
87         configuration.libraryJars = extendClassPath(configuration.libraryJars,
88                                                     classPathElement,
89                                                     false);
90     }
91
92
93     public void addConfiguredKeep(KeepSpecificationElement keepSpecificationElement)
94     {
95         configuration.keep = extendKeepSpecifications(configuration.keep,
96                                                       keepSpecificationElement,
97                                                       true,
98                                                       false);
99     }
100
101
102     public void addConfiguredKeepclassmembers(KeepSpecificationElement keepSpecificationElement)
103     {
104         configuration.keep = extendKeepSpecifications(configuration.keep,
105                                                       keepSpecificationElement,
106                                                       false,
107                                                       false);
108     }
109
110
111     public void addConfiguredKeepclasseswithmembers(KeepSpecificationElement keepSpecificationElement)
112     {
113         configuration.keep = extendKeepSpecifications(configuration.keep,
114                                                       keepSpecificationElement,
115                                                       true,
116                                                       true);
117     }
118
119
120     public void addConfiguredKeepnames(KeepSpecificationElement keepSpecificationElement)
121     {
122         // Set the shrinking flag, based on the name (backward compatibility).
123
keepSpecificationElement.setAllowshrinking(true);
124
125         configuration.keep = extendKeepSpecifications(configuration.keep,
126                                                       keepSpecificationElement,
127                                                       true,
128                                                       false);
129     }
130
131
132     public void addConfiguredKeepclassmembernames(KeepSpecificationElement keepSpecificationElement)
133     {
134         // Set the shrinking flag, based on the name (backward compatibility).
135
keepSpecificationElement.setAllowshrinking(true);
136
137         configuration.keep = extendKeepSpecifications(configuration.keep,
138                                                       keepSpecificationElement,
139                                                       false,
140                                                       false);
141     }
142
143
144     public void addConfiguredKeepclasseswithmembernames(KeepSpecificationElement keepSpecificationElement)
145     {
146         // Set the shrinking flag, based on the name (backward compatibility).
147
keepSpecificationElement.setAllowshrinking(true);
148
149         configuration.keep = extendKeepSpecifications(configuration.keep,
150                                                       keepSpecificationElement,
151                                                       true,
152                                                       true);
153     }
154
155
156     public void addConfiguredWhyareyoukeeping(ClassSpecificationElement classSpecificationElement)
157     {
158         configuration.whyAreYouKeeping = extendClassSpecifications(configuration.whyAreYouKeeping,
159                                                                    classSpecificationElement);
160     }
161
162
163     public void addConfiguredAssumenosideeffects(ClassSpecificationElement classSpecificationElement)
164     {
165         configuration.assumeNoSideEffects = extendClassSpecifications(configuration.assumeNoSideEffects,
166                                                                       classSpecificationElement);
167     }
168
169
170     public void addConfiguredKeepattribute(KeepAttributeElement keepAttributeElement)
171     {
172         configuration.keepAttributes = extendAttributes(configuration.keepAttributes,
173                                                         keepAttributeElement);
174     }
175
176
177     public void addConfiguredAdaptResourceFileNames(FilterElement filterElement)
178     {
179         configuration.adaptResourceFileNames = extendFilter(configuration.adaptResourceFileNames,
180                                                             filterElement);
181     }
182
183
184     public void addConfiguredAdaptResourceFileContents(FilterElement filterElement)
185     {
186         configuration.adaptResourceFileContents = extendFilter(configuration.adaptResourceFileContents,
187                                                                filterElement);
188     }
189
190
191     public void addConfiguredConfiguration(ConfigurationElement configurationElement)
192     {
193         configurationElement.appendTo(configuration);
194     }
195
196
197     // Implementations for Task.
198

199     public void addText(String JavaDoc text) throws BuildException
200     {
201         try
202         {
203             String JavaDoc arg = getProject().replaceProperties(text);
204
205             ConfigurationParser parser = new ConfigurationParser(new String JavaDoc[] { arg },
206                                                                  getProject().getBaseDir());
207
208             try
209             {
210                 parser.parse(configuration);
211             }
212             catch (ParseException ex)
213             {
214                 throw new BuildException(ex.getMessage());
215             }
216             finally
217             {
218                 parser.close();
219             }
220         }
221         catch (IOException ex)
222         {
223             throw new BuildException(ex.getMessage());
224         }
225     }
226
227
228     // Small utility methods.
229

230     private ClassPath extendClassPath(ClassPath classPath,
231                                       ClassPathElement classPathElement,
232                                       boolean output)
233     {
234         if (classPath == null)
235         {
236             classPath = new ClassPath();
237         }
238
239         classPathElement.appendClassPathEntriesTo(classPath,
240                                                   output);
241
242         return classPath;
243     }
244
245
246     private ClassPath extendClassPath(ClassPath classPath,
247                                       ClassPath additionalClassPath)
248     {
249         if (additionalClassPath != null)
250         {
251             if (classPath == null)
252             {
253                 classPath = new ClassPath();
254             }
255
256             classPath.addAll(additionalClassPath);
257         }
258
259         return classPath;
260     }
261
262
263     private List extendKeepSpecifications(List keepSpecifications,
264                                           KeepSpecificationElement keepSpecificationElement,
265                                           boolean markClasses,
266                                           boolean markClassesConditionally)
267     {
268         if (keepSpecifications == null)
269         {
270             keepSpecifications = new ArrayList();
271         }
272
273         keepSpecificationElement.appendTo(keepSpecifications,
274                                           markClasses,
275                                           markClassesConditionally);
276
277         return keepSpecifications;
278     }
279
280
281     private List extendClassSpecifications(List classSpecifications,
282                                            ClassSpecificationElement classSpecificationElement)
283     {
284         if (classSpecifications == null)
285         {
286             classSpecifications = new ArrayList();
287         }
288
289         classSpecificationElement.appendTo(classSpecifications);
290
291         return classSpecifications;
292     }
293
294
295     private List extendClassSpecifications(List classSpecifications,
296                                            List additionalClassSpecifications)
297     {
298         if (additionalClassSpecifications != null)
299         {
300             if (classSpecifications == null)
301             {
302                 classSpecifications = new ArrayList();
303             }
304
305             classSpecifications.addAll(additionalClassSpecifications);
306         }
307
308         return classSpecifications;
309     }
310
311
312     private List extendAttributes(List attributes,
313                                   KeepAttributeElement keepAttributeElement)
314     {
315         if (attributes == null)
316         {
317             attributes = new ArrayList();
318         }
319
320         keepAttributeElement.appendTo(attributes);
321
322         return attributes;
323     }
324
325
326     private List extendFilter(List filter,
327                               FilterElement filterElement)
328     {
329         if (filter == null)
330         {
331             filter = new ArrayList();
332         }
333
334         filterElement.appendTo(filter);
335
336         return filter;
337     }
338
339
340     private List extendList(List list,
341                             List additionalList)
342     {
343         if (additionalList != null)
344         {
345             if (list == null)
346             {
347                 list = new ArrayList();
348             }
349
350             list.addAll(additionalList);
351         }
352
353         return list;
354     }
355 }
356
Popular Tags