• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
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.File;
29 
30 /**
31  * This FileSet represents a class path entry (or a set of class path entries)
32  * in Ant.
33  *
34  * @author Eric Lafortune
35  */
36 public class ClassPathElement extends Path
37 {
38     private String filter;
39     private String apkFilter;
40     private String jarFilter;
41     private String aarFilter;
42     private String warFilter;
43     private String earFilter;
44     private String zipFilter;
45 
46 
47     /**
48      * @see Path#Path(Project)
49      */
ClassPathElement(Project project)50     public ClassPathElement(Project project)
51     {
52         super(project);
53     }
54 
55 
56     /**
57      * Adds the contents of this class path element to the given class path.
58      * @param classPath the class path to be extended.
59      * @param output    specifies whether this is an output entry or not.
60      */
appendClassPathEntriesTo(ClassPath classPath, boolean output)61     public void appendClassPathEntriesTo(ClassPath classPath, boolean output)
62     {
63         File     baseDir = getProject().getBaseDir();
64         String[] fileNames;
65 
66         if (isReference())
67         {
68             // Get the referenced path or file set.
69             Object referencedObject = getCheckedRef(DataType.class,
70                                                     DataType.class.getName());
71 
72             if (referencedObject instanceof Path)
73             {
74                 Path path = (Path)referencedObject;
75 
76                 // Get the names of the files in the referenced path.
77                 fileNames = path.list();
78             }
79             else if (referencedObject instanceof AbstractFileSet)
80             {
81                 AbstractFileSet fileSet = (AbstractFileSet)referencedObject;
82 
83                 // Get the names of the existing input files in the referenced file set.
84                 DirectoryScanner scanner = fileSet.getDirectoryScanner(getProject());
85                 baseDir   = scanner.getBasedir();
86                 fileNames = scanner.getIncludedFiles();
87             }
88             else
89             {
90                 throw new BuildException("The refid attribute doesn't point to a <path> element or a <fileset> element");
91             }
92         }
93         else
94         {
95             // Get the names of the files in this path.
96             fileNames = list();
97         }
98 
99         if (output)
100         {
101             if (fileNames.length != 1)
102             {
103                 throw new BuildException("The <outjar> element must specify exactly one file or directory ["+fileNames.length+"]");
104             }
105         }
106         //else
107         //{
108         //    if (fileNames.length < 1)
109         //    {
110         //        throw new BuildException("The <injar> element must specify at least one file or directory");
111         //    }
112         //}
113 
114         for (int index = 0; index < fileNames.length; index++)
115         {
116             // Create a new class path entry, with the proper file name and
117             // any filters.
118             String fileName = fileNames[index];
119             File   file     = new File(fileName);
120 
121             ClassPathEntry entry =
122                 new ClassPathEntry(file.isAbsolute() ? file : new File(baseDir, fileName),
123                                    output);
124             entry.setFilter(ListUtil.commaSeparatedList(filter));
125             entry.setApkFilter(ListUtil.commaSeparatedList(apkFilter));
126             entry.setJarFilter(ListUtil.commaSeparatedList(jarFilter));
127             entry.setAarFilter(ListUtil.commaSeparatedList(aarFilter));
128             entry.setWarFilter(ListUtil.commaSeparatedList(warFilter));
129             entry.setEarFilter(ListUtil.commaSeparatedList(earFilter));
130             entry.setZipFilter(ListUtil.commaSeparatedList(zipFilter));
131 
132             // Add it to the class path.
133             classPath.add(entry);
134         }
135     }
136 
137 
138     // Ant task attributes.
139 
140     /**
141      * @deprecated Use {@link #setLocation(File)} instead.
142      */
setFile(File file)143     public void setFile(File file)
144     {
145         setLocation(file);
146     }
147 
148 
149     /**
150      * @deprecated Use {@link #setLocation(File)} instead.
151      */
setDir(File file)152     public void setDir(File file)
153     {
154         setLocation(file);
155     }
156 
157 
158     /**
159      * @deprecated Use {@link #setLocation(File)} instead.
160      */
setName(File file)161     public void setName(File file)
162     {
163         setLocation(file);
164     }
165 
166 
setFilter(String filter)167     public void setFilter(String filter)
168     {
169         this.filter = filter;
170     }
171 
172 
setApkfilter(String apkFilter)173     public void setApkfilter(String apkFilter)
174     {
175         this.apkFilter = apkFilter;
176     }
177 
178 
setJarfilter(String jarFilter)179     public void setJarfilter(String jarFilter)
180     {
181         this.jarFilter = jarFilter;
182     }
183 
184 
setAarfilter(String aarFilter)185     public void setAarfilter(String aarFilter)
186     {
187         this.aarFilter = aarFilter;
188     }
189 
190 
setWarfilter(String warFilter)191     public void setWarfilter(String warFilter)
192     {
193         this.warFilter = warFilter;
194     }
195 
196 
setEarfilter(String earFilter)197     public void setEarfilter(String earFilter)
198     {
199         this.earFilter = earFilter;
200     }
201 
202 
setZipfilter(String zipFilter)203     public void setZipfilter(String zipFilter)
204     {
205         this.zipFilter = zipFilter;
206     }
207 }
208