1 | /*
|
---|
2 | * Copyright 2000-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.types;
|
---|
19 |
|
---|
20 | import java.util.Properties;
|
---|
21 | import java.util.Stack;
|
---|
22 | import org.apache.tools.ant.AntClassLoader;
|
---|
23 | import org.apache.tools.ant.BuildException;
|
---|
24 | import org.apache.tools.ant.Project;
|
---|
25 | import org.apache.tools.ant.util.FileNameMapper;
|
---|
26 | import org.apache.tools.ant.util.CompositeMapper;
|
---|
27 | import org.apache.tools.ant.util.ContainerMapper;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Element to define a FileNameMapper.
|
---|
31 | *
|
---|
32 | */
|
---|
33 | public class Mapper extends DataType implements Cloneable {
|
---|
34 |
|
---|
35 | protected MapperType type = null;
|
---|
36 | protected String classname = null;
|
---|
37 | protected Path classpath = null;
|
---|
38 | protected String from = null;
|
---|
39 | protected String to = null;
|
---|
40 |
|
---|
41 | private ContainerMapper container = null;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Construct a new <CODE>Mapper</CODE> element.
|
---|
45 | * @param p the owning Ant <CODE>Project</CODE>.
|
---|
46 | */
|
---|
47 | public Mapper(Project p) {
|
---|
48 | setProject(p);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Set the type of <code>FileNameMapper</code> to use.
|
---|
53 | * @param type the <CODE>MapperType</CODE> enumerated attribute.
|
---|
54 | */
|
---|
55 | public void setType(MapperType type) {
|
---|
56 | if (isReference()) {
|
---|
57 | throw tooManyAttributes();
|
---|
58 | }
|
---|
59 | this.type = type;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Add a nested <CODE>FileNameMapper</CODE>.
|
---|
64 | * @param fileNameMapper the <CODE>FileNameMapper</CODE> to add.
|
---|
65 | */
|
---|
66 | public void add(FileNameMapper fileNameMapper) {
|
---|
67 | if (isReference()) {
|
---|
68 | throw noChildrenAllowed();
|
---|
69 | }
|
---|
70 | if (container == null) {
|
---|
71 | if (type == null && classname == null) {
|
---|
72 | container = new CompositeMapper();
|
---|
73 | } else {
|
---|
74 | FileNameMapper m = getImplementation();
|
---|
75 | if (m instanceof ContainerMapper) {
|
---|
76 | container = (ContainerMapper)m;
|
---|
77 | } else {
|
---|
78 | throw new BuildException(String.valueOf(m)
|
---|
79 | + " mapper implementation does not support nested mappers!");
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 | container.add(fileNameMapper);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Add a Mapper
|
---|
88 | * @param mapper the mapper to add
|
---|
89 | */
|
---|
90 | public void addConfiguredMapper(Mapper mapper) {
|
---|
91 | add(mapper.getImplementation());
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Set the class name of the FileNameMapper to use.
|
---|
96 | */
|
---|
97 | public void setClassname(String classname) {
|
---|
98 | if (isReference()) {
|
---|
99 | throw tooManyAttributes();
|
---|
100 | }
|
---|
101 | this.classname = classname;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Set the classpath to load the FileNameMapper through (attribute).
|
---|
106 | */
|
---|
107 | public void setClasspath(Path classpath) {
|
---|
108 | if (isReference()) {
|
---|
109 | throw tooManyAttributes();
|
---|
110 | }
|
---|
111 | if (this.classpath == null) {
|
---|
112 | this.classpath = classpath;
|
---|
113 | } else {
|
---|
114 | this.classpath.append(classpath);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Set the classpath to load the FileNameMapper through (nested element).
|
---|
120 | */
|
---|
121 | public Path createClasspath() {
|
---|
122 | if (isReference()) {
|
---|
123 | throw noChildrenAllowed();
|
---|
124 | }
|
---|
125 | if (this.classpath == null) {
|
---|
126 | this.classpath = new Path(getProject());
|
---|
127 | }
|
---|
128 | return this.classpath.createPath();
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Set the classpath to load the FileNameMapper through via
|
---|
133 | * reference (attribute).
|
---|
134 | */
|
---|
135 | public void setClasspathRef(Reference r) {
|
---|
136 | if (isReference()) {
|
---|
137 | throw tooManyAttributes();
|
---|
138 | }
|
---|
139 | createClasspath().setRefid(r);
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Set the argument to FileNameMapper.setFrom
|
---|
144 | */
|
---|
145 | public void setFrom(String from) {
|
---|
146 | if (isReference()) {
|
---|
147 | throw tooManyAttributes();
|
---|
148 | }
|
---|
149 | this.from = from;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Set the argument to FileNameMapper.setTo
|
---|
154 | */
|
---|
155 | public void setTo(String to) {
|
---|
156 | if (isReference()) {
|
---|
157 | throw tooManyAttributes();
|
---|
158 | }
|
---|
159 | this.to = to;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Make this Mapper instance a reference to another Mapper.
|
---|
164 | *
|
---|
165 | * <p>You must not set any other attribute if you make it a
|
---|
166 | * reference.</p>
|
---|
167 | */
|
---|
168 | public void setRefid(Reference r) throws BuildException {
|
---|
169 | if (type != null || from != null || to != null) {
|
---|
170 | throw tooManyAttributes();
|
---|
171 | }
|
---|
172 | super.setRefid(r);
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Returns a fully configured FileNameMapper implementation.
|
---|
177 | */
|
---|
178 | public FileNameMapper getImplementation() throws BuildException {
|
---|
179 | if (isReference()) {
|
---|
180 | return getRef().getImplementation();
|
---|
181 | }
|
---|
182 |
|
---|
183 | if (type == null && classname == null && container == null) {
|
---|
184 | throw new BuildException(
|
---|
185 | "nested mapper or "
|
---|
186 | + "one of the attributes type or classname is required");
|
---|
187 | }
|
---|
188 |
|
---|
189 | if (container != null) {
|
---|
190 | return container;
|
---|
191 | }
|
---|
192 |
|
---|
193 | if (type != null && classname != null) {
|
---|
194 | throw new BuildException(
|
---|
195 | "must not specify both type and classname attribute");
|
---|
196 | }
|
---|
197 |
|
---|
198 | try {
|
---|
199 | FileNameMapper m
|
---|
200 | = (FileNameMapper)(getImplementationClass().newInstance());
|
---|
201 | final Project project = getProject();
|
---|
202 | if (project != null) {
|
---|
203 | project.setProjectReference(m);
|
---|
204 | }
|
---|
205 | m.setFrom(from);
|
---|
206 | m.setTo(to);
|
---|
207 |
|
---|
208 | return m;
|
---|
209 | } catch (BuildException be) {
|
---|
210 | throw be;
|
---|
211 | } catch (Throwable t) {
|
---|
212 | throw new BuildException(t);
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Gets the Class object associated with the mapper implementation.
|
---|
218 | * @return <CODE>Class</CODE>.
|
---|
219 | */
|
---|
220 | protected Class getImplementationClass() throws ClassNotFoundException {
|
---|
221 |
|
---|
222 | String classname = this.classname;
|
---|
223 | if (type != null) {
|
---|
224 | classname = type.getImplementation();
|
---|
225 | }
|
---|
226 |
|
---|
227 | ClassLoader loader = (classpath == null)
|
---|
228 | ? getClass().getClassLoader()
|
---|
229 | : getProject().createClassLoader(classpath);
|
---|
230 |
|
---|
231 | return Class.forName(classname, true, loader);
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Performs the check for circular references and returns the
|
---|
236 | * referenced Mapper.
|
---|
237 | */
|
---|
238 | protected Mapper getRef() {
|
---|
239 | if (!isChecked()) {
|
---|
240 | Stack stk = new Stack();
|
---|
241 | stk.push(this);
|
---|
242 | dieOnCircularReference(stk, getProject());
|
---|
243 | }
|
---|
244 |
|
---|
245 | Object o = getRefid().getReferencedObject(getProject());
|
---|
246 | if (!(o instanceof Mapper)) {
|
---|
247 | String msg = getRefid().getRefId() + " doesn\'t denote a mapper";
|
---|
248 | throw new BuildException(msg);
|
---|
249 | } else {
|
---|
250 | return (Mapper) o;
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * Class as Argument to FileNameMapper.setType.
|
---|
256 | */
|
---|
257 | public static class MapperType extends EnumeratedAttribute {
|
---|
258 | private Properties implementations;
|
---|
259 |
|
---|
260 | public MapperType() {
|
---|
261 | implementations = new Properties();
|
---|
262 | implementations.put("identity",
|
---|
263 | "org.apache.tools.ant.util.IdentityMapper");
|
---|
264 | implementations.put("flatten",
|
---|
265 | "org.apache.tools.ant.util.FlatFileNameMapper");
|
---|
266 | implementations.put("glob",
|
---|
267 | "org.apache.tools.ant.util.GlobPatternMapper");
|
---|
268 | implementations.put("merge",
|
---|
269 | "org.apache.tools.ant.util.MergingMapper");
|
---|
270 | implementations.put("regexp",
|
---|
271 | "org.apache.tools.ant.util.RegexpPatternMapper");
|
---|
272 | implementations.put("package",
|
---|
273 | "org.apache.tools.ant.util.PackageNameMapper");
|
---|
274 | implementations.put("unpackage",
|
---|
275 | "org.apache.tools.ant.util.UnPackageNameMapper");
|
---|
276 | }
|
---|
277 |
|
---|
278 | public String[] getValues() {
|
---|
279 | return new String[] {"identity", "flatten", "glob",
|
---|
280 | "merge", "regexp", "package", "unpackage"};
|
---|
281 | }
|
---|
282 |
|
---|
283 | public String getImplementation() {
|
---|
284 | return implementations.getProperty(getValue());
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | }
|
---|