KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > Mapper


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.types;
20
21 import java.util.Properties JavaDoc;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.util.FileNameMapper;
25 import org.apache.tools.ant.util.CompositeMapper;
26 import org.apache.tools.ant.util.ContainerMapper;
27
28 /**
29  * Element to define a FileNameMapper.
30  *
31  */

32 public class Mapper extends DataType implements Cloneable JavaDoc {
33     // CheckStyle:VisibilityModifier OFF - bc
34

35     protected MapperType type = null;
36     protected String JavaDoc classname = null;
37     protected Path classpath = null;
38     protected String JavaDoc from = null;
39     protected String JavaDoc to = null;
40
41     // CheckStyle:VisibilityModifier ON
42

43     private ContainerMapper container = null;
44
45     /**
46      * Construct a new <code>Mapper</code> element.
47      * @param p the owning Ant <code>Project</code>.
48      */

49     public Mapper(Project p) {
50         setProject(p);
51     }
52
53     /**
54      * Set the type of <code>FileNameMapper</code> to use.
55      * @param type the <code>MapperType</code> enumerated attribute.
56      */

57     public void setType(MapperType type) {
58         if (isReference()) {
59             throw tooManyAttributes();
60         }
61         this.type = type;
62     }
63
64     /**
65      * Cannot mix add and addconfigured in same type, so
66      * provide this to override the add method.
67      * @param fileNameMapper the <code>FileNameMapper</code> to add.
68      */

69     public void addConfigured(FileNameMapper fileNameMapper) {
70         add(fileNameMapper);
71     }
72
73     /**
74      * Add a nested <code>FileNameMapper</code>.
75      * @param fileNameMapper the <code>FileNameMapper</code> to add.
76      */

77     public void add(FileNameMapper fileNameMapper) {
78         if (isReference()) {
79             throw noChildrenAllowed();
80         }
81         if (container == null) {
82             if (type == null && classname == null) {
83                 container = new CompositeMapper();
84             } else {
85                 FileNameMapper m = getImplementation();
86                 if (m instanceof ContainerMapper) {
87                     container = (ContainerMapper) m;
88                 } else {
89                     throw new BuildException(String.valueOf(m)
90                         + " mapper implementation does not support nested mappers!");
91                 }
92             }
93         }
94         container.add(fileNameMapper);
95     }
96
97     /**
98      * Add a Mapper
99      * @param mapper the mapper to add
100      */

101     public void addConfiguredMapper(Mapper mapper) {
102         add(mapper.getImplementation());
103     }
104
105     /**
106      * Set the class name of the FileNameMapper to use.
107      * @param classname the name of the class
108      */

109     public void setClassname(String JavaDoc classname) {
110         if (isReference()) {
111             throw tooManyAttributes();
112         }
113         this.classname = classname;
114     }
115
116     /**
117      * Set the classpath to load the FileNameMapper through (attribute).
118      * @param classpath the classpath
119      */

120     public void setClasspath(Path classpath) {
121         if (isReference()) {
122             throw tooManyAttributes();
123         }
124         if (this.classpath == null) {
125             this.classpath = classpath;
126         } else {
127             this.classpath.append(classpath);
128         }
129     }
130
131     /**
132      * Set the classpath to load the FileNameMapper through (nested element).
133      * @return a path object to be configured
134      */

135     public Path createClasspath() {
136         if (isReference()) {
137             throw noChildrenAllowed();
138         }
139         if (this.classpath == null) {
140             this.classpath = new Path(getProject());
141         }
142         return this.classpath.createPath();
143     }
144
145     /**
146      * Set the classpath to load the FileNameMapper through via
147      * reference (attribute).
148      * @param ref the reference to the FileNameMapper
149      */

150     public void setClasspathRef(Reference ref) {
151         if (isReference()) {
152             throw tooManyAttributes();
153         }
154         createClasspath().setRefid(ref);
155     }
156
157     /**
158      * Set the argument to FileNameMapper.setFrom
159      * @param from the from attribute to pass to the FileNameMapper
160      */

161     public void setFrom(String JavaDoc from) {
162         if (isReference()) {
163             throw tooManyAttributes();
164         }
165         this.from = from;
166     }
167
168     /**
169      * Set the argument to FileNameMapper.setTo
170      * @param to the to attribute to pass to the FileNameMapper
171      */

172     public void setTo(String JavaDoc to) {
173         if (isReference()) {
174             throw tooManyAttributes();
175         }
176         this.to = to;
177     }
178
179     /**
180      * Make this Mapper instance a reference to another Mapper.
181      *
182      * <p>You must not set any other attribute if you make it a
183      * reference.</p>
184      * @param r the reference to another mapper
185      * @throws BuildException if other attributes are set
186      */

187     public void setRefid(Reference r) throws BuildException {
188         if (type != null || from != null || to != null) {
189             throw tooManyAttributes();
190         }
191         super.setRefid(r);
192     }
193
194     /**
195      * Returns a fully configured FileNameMapper implementation.
196      * @return a FileNameMapper object to be configured
197      * @throws BuildException on error
198      */

199     public FileNameMapper getImplementation() throws BuildException {
200         if (isReference()) {
201             return getRef().getImplementation();
202         }
203
204         if (type == null && classname == null && container == null) {
205             throw new BuildException(
206                 "nested mapper or "
207                 + "one of the attributes type or classname is required");
208         }
209
210         if (container != null) {
211             return container;
212         }
213
214         if (type != null && classname != null) {
215             throw new BuildException(
216                 "must not specify both type and classname attribute");
217         }
218
219         try {
220             FileNameMapper m
221                 = (FileNameMapper) (getImplementationClass().newInstance());
222             final Project p = getProject();
223             if (p != null) {
224                 p.setProjectReference(m);
225             }
226             m.setFrom(from);
227             m.setTo(to);
228
229             return m;
230         } catch (BuildException be) {
231             throw be;
232         } catch (Throwable JavaDoc t) {
233             throw new BuildException(t);
234         }
235     }
236
237      /**
238      * Gets the Class object associated with the mapper implementation.
239      * @return <code>Class</code>.
240      * @throws ClassNotFoundException if the class cannot be found
241      */

242     protected Class JavaDoc getImplementationClass() throws ClassNotFoundException JavaDoc {
243
244         String JavaDoc cName = this.classname;
245         if (type != null) {
246             cName = type.getImplementation();
247         }
248
249         ClassLoader JavaDoc loader = (classpath == null)
250             ? getClass().getClassLoader()
251             : getProject().createClassLoader(classpath);
252
253         return Class.forName(cName, true, loader);
254     }
255
256     /**
257      * Performs the check for circular references and returns the
258      * referenced Mapper.
259      * @return the referenced Mapper
260      */

261     protected Mapper getRef() {
262         return (Mapper) getCheckedRef();
263     }
264
265     /**
266      * Class as Argument to FileNameMapper.setType.
267      */

268     public static class MapperType extends EnumeratedAttribute {
269         private Properties JavaDoc implementations;
270
271         /** Constructor for the MapperType enumeration */
272         public MapperType() {
273             implementations = new Properties JavaDoc();
274             implementations.put("identity",
275                                 "org.apache.tools.ant.util.IdentityMapper");
276             implementations.put("flatten",
277                                 "org.apache.tools.ant.util.FlatFileNameMapper");
278             implementations.put("glob",
279                                 "org.apache.tools.ant.util.GlobPatternMapper");
280             implementations.put("merge",
281                                 "org.apache.tools.ant.util.MergingMapper");
282             implementations.put("regexp",
283                                 "org.apache.tools.ant.util.RegexpPatternMapper");
284             implementations.put("package",
285                                 "org.apache.tools.ant.util.PackageNameMapper");
286             implementations.put("unpackage",
287                                 "org.apache.tools.ant.util.UnPackageNameMapper");
288         }
289
290         /**
291          * @return the filenamemapper names
292          */

293         public String JavaDoc[] getValues() {
294             return new String JavaDoc[] {"identity", "flatten", "glob",
295                                  "merge", "regexp", "package", "unpackage"};
296         }
297
298         /**
299          * @return the classname for the filenamemapper name
300          */

301         public String JavaDoc getImplementation() {
302             return implementations.getProperty(getValue());
303         }
304     }
305
306 }
307
Popular Tags