KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > ant > ClassPathElement


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 org.apache.tools.ant.*;
24 import org.apache.tools.ant.types.*;
25
26 import proguard.*;
27
28 import java.io.*;
29 import java.lang.ref.Reference JavaDoc;
30 import java.util.Stack JavaDoc;
31
32 /**
33  * This FileSet represents a class path entry (or a set of class path entries)
34  * in Ant.
35  *
36  * @author Eric Lafortune
37  */

38 public class ClassPathElement extends Path
39 {
40     private String JavaDoc filter;
41     private String JavaDoc jarFilter;
42     private String JavaDoc warFilter;
43     private String JavaDoc earFilter;
44     private String JavaDoc zipFilter;
45
46
47     /**
48      * @see Path#Path(Project)
49      */

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      */

61     public void appendClassPathEntriesTo(ClassPath classPath, boolean output)
62     {
63         File baseDir = getProject().getBaseDir();
64         String JavaDoc[] fileNames;
65
66         if (isReference())
67         {
68             // Get the referenced path or file set.
69
Object JavaDoc 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 JavaDoc 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(filter);
125             entry.setJarFilter(jarFilter);
126             entry.setWarFilter(warFilter);
127             entry.setEarFilter(earFilter);
128             entry.setZipFilter(zipFilter);
129
130             // Add it to the class path.
131
classPath.add(entry);
132         }
133     }
134
135
136     // Ant task attributes.
137

138     /**
139      * @deprecated Use {@link #setLocation(File)} instead.
140      */

141     public void setFile(File file)
142     {
143         setLocation(file);
144     }
145
146
147     /**
148      * @deprecated Use {@link #setLocation(File)} instead.
149      */

150     public void setDir(File file)
151     {
152         setLocation(file);
153     }
154
155
156     /**
157      * @deprecated Use {@link #setLocation(File)} instead.
158      */

159     public void setName(File file)
160     {
161         setLocation(file);
162     }
163
164
165     public void setFilter(String JavaDoc filter)
166     {
167         this.filter = filter;
168     }
169
170
171     public void setJarFilter(String JavaDoc jarFilter)
172     {
173         this.jarFilter = jarFilter;
174     }
175
176
177     public void setWarFilter(String JavaDoc warFilter)
178     {
179         this.warFilter = warFilter;
180     }
181
182
183     public void setEarFilter(String JavaDoc earFilter)
184     {
185         this.earFilter = earFilter;
186     }
187
188
189     public void setZipFilter(String JavaDoc zipFilter)
190     {
191         this.zipFilter = zipFilter;
192     }
193 }
194
Popular Tags