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