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