• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  *             of Java bytecode.
4  *
5  * Copyright (c) 2002-2013 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 org.apache.tools.ant.*;
24 import org.apache.tools.ant.types.*;
25 import proguard.*;
26 import proguard.util.ListUtil;
27 
28 import java.io.*;
29 import java.util.Properties;
30 
31 /**
32  * This DataType represents a reference to an XML-style ProGuard configuration
33  * in Ant, or a file set of ProGuard-style configuration files.
34  *
35  * @author Eric Lafortune
36  */
37 public class ConfigurationElement extends FileSet
38 {
39     /**
40      * Adds the contents of this configuration element to the given
41      * configuration.
42      * @param configuration the configuration to be extended.
43      */
appendTo(Configuration configuration)44     public void appendTo(Configuration configuration)
45     {
46         File     baseDir;
47         String[] fileNames;
48 
49         if (isReference())
50         {
51             // Get the referenced path or file set.
52             Object referencedObject = getCheckedRef(Object.class,
53                                                     Object.class.getName());
54 
55             if (referencedObject instanceof ConfigurationTask)
56             {
57                 // The reference doesn't point to a file set, but to a
58                 // configuration task.
59                 ConfigurationTask configurationTask =
60                     (ConfigurationTask)referencedObject;
61 
62                 // Append the contents of the referenced configuration to the
63                 // current configuration.
64                 configurationTask.appendTo(configuration);
65 
66                 return;
67             }
68             else if (referencedObject instanceof AbstractFileSet)
69             {
70                 AbstractFileSet fileSet = (AbstractFileSet)referencedObject;
71 
72                 // Get the names of the existing input files in the referenced file set.
73                 DirectoryScanner scanner = fileSet.getDirectoryScanner(getProject());
74                 baseDir   = scanner.getBasedir();
75                 fileNames = scanner.getIncludedFiles();
76             }
77             else
78             {
79                 throw new BuildException("The refid attribute doesn't point to a <proguardconfiguration> element or a <fileset> element");
80             }
81         }
82         else
83         {
84             // Get the names of the existing input files in the referenced file set.
85             DirectoryScanner scanner = getDirectoryScanner(getProject());
86             baseDir   = scanner.getBasedir();
87             fileNames = scanner.getIncludedFiles();
88         }
89 
90         // Get the combined system properties and Ant properties, for
91         // replacing ProGuard-style properties ('<...>').
92         Properties properties = new Properties();
93         properties.putAll(getProject().getProperties());
94 
95         try
96         {
97             // Append the contents of the configuration files to the current
98             // configuration.
99             for (int index = 0; index < fileNames.length; index++)
100             {
101                 File configurationFile = new File(baseDir, fileNames[index]);
102 
103                 ConfigurationParser parser =
104                     new ConfigurationParser(configurationFile, properties);
105                 try
106                 {
107                     parser.parse(configuration);
108                 }
109                 catch (ParseException ex)
110                 {
111                     throw new BuildException(ex.getMessage());
112                 }
113                 finally
114                 {
115                     parser.close();
116                 }
117             }
118         }
119         catch (IOException ex)
120         {
121             throw new BuildException(ex.getMessage());
122         }
123     }
124 }
125