1 18 19 package org.apache.tools.ant.types; 20 21 import java.util.Properties ; 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 32 public class Mapper extends DataType implements Cloneable { 33 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 43 private ContainerMapper container = null; 44 45 49 public Mapper(Project p) { 50 setProject(p); 51 } 52 53 57 public void setType(MapperType type) { 58 if (isReference()) { 59 throw tooManyAttributes(); 60 } 61 this.type = type; 62 } 63 64 69 public void addConfigured(FileNameMapper fileNameMapper) { 70 add(fileNameMapper); 71 } 72 73 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 101 public void addConfiguredMapper(Mapper mapper) { 102 add(mapper.getImplementation()); 103 } 104 105 109 public void setClassname(String classname) { 110 if (isReference()) { 111 throw tooManyAttributes(); 112 } 113 this.classname = classname; 114 } 115 116 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 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 150 public void setClasspathRef(Reference ref) { 151 if (isReference()) { 152 throw tooManyAttributes(); 153 } 154 createClasspath().setRefid(ref); 155 } 156 157 161 public void setFrom(String from) { 162 if (isReference()) { 163 throw tooManyAttributes(); 164 } 165 this.from = from; 166 } 167 168 172 public void setTo(String to) { 173 if (isReference()) { 174 throw tooManyAttributes(); 175 } 176 this.to = to; 177 } 178 179 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 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 t) { 233 throw new BuildException(t); 234 } 235 } 236 237 242 protected Class getImplementationClass() throws ClassNotFoundException { 243 244 String cName = this.classname; 245 if (type != null) { 246 cName = type.getImplementation(); 247 } 248 249 ClassLoader loader = (classpath == null) 250 ? getClass().getClassLoader() 251 : getProject().createClassLoader(classpath); 252 253 return Class.forName(cName, true, loader); 254 } 255 256 261 protected Mapper getRef() { 262 return (Mapper) getCheckedRef(); 263 } 264 265 268 public static class MapperType extends EnumeratedAttribute { 269 private Properties implementations; 270 271 272 public MapperType() { 273 implementations = new Properties (); 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 293 public String [] getValues() { 294 return new String [] {"identity", "flatten", "glob", 295 "merge", "regexp", "package", "unpackage"}; 296 } 297 298 301 public String getImplementation() { 302 return implementations.getProperty(getValue()); 303 } 304 } 305 306 } 307 | Popular Tags |