1 18 19 package org.apache.tools.ant.types; 20 21 import java.util.Enumeration ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.HashSet ; 25 import java.util.Set ; 26 import java.util.TreeMap ; 27 import java.util.Hashtable ; 28 import java.util.Properties ; 29 import java.util.Vector ; 30 31 import org.apache.tools.ant.BuildException; 32 import org.apache.tools.ant.Project; 33 import org.apache.tools.ant.types.resources.PropertyResource; 34 import org.apache.tools.ant.util.FileNameMapper; 35 import org.apache.tools.ant.util.regexp.RegexpMatcher; 36 import org.apache.tools.ant.util.regexp.RegexpMatcherFactory; 37 38 43 public class PropertySet extends DataType implements ResourceCollection { 44 45 private boolean dynamic = true; 46 private boolean negate = false; 47 private Set cachedNames; 48 private Vector ptyRefs = new Vector (); 49 private Vector setRefs = new Vector (); 50 private Mapper mapper; 51 52 56 public static class PropertyRef { 57 58 private int count; 59 private String name; 60 private String regex; 61 private String prefix; 62 private String builtin; 63 64 68 public void setName(String name) { 69 assertValid("name", name); 70 this.name = name; 71 } 72 73 77 public void setRegex(String regex) { 78 assertValid("regex", regex); 79 this.regex = regex; 80 } 81 82 86 public void setPrefix(String prefix) { 87 assertValid("prefix", prefix); 88 this.prefix = prefix; 89 } 90 91 95 public void setBuiltin(BuiltinPropertySetName b) { 96 String pBuiltIn = b.getValue(); 97 assertValid("builtin", pBuiltIn); 98 this.builtin = pBuiltIn; 99 } 100 101 private void assertValid(String attr, String value) { 102 if (value == null || value.length() < 1) { 103 throw new BuildException("Invalid attribute: " + attr); 104 } 105 106 if (++count != 1) { 107 throw new BuildException("Attributes name, regex, and " 108 + "prefix are mutually exclusive"); 109 } 110 } 111 112 116 public String toString() { 117 return "name=" + name + ", regex=" + regex + ", prefix=" + prefix 118 + ", builtin=" + builtin; 119 } 120 121 } 123 127 public void appendName(String name) { 128 PropertyRef r = new PropertyRef(); 129 r.setName(name); 130 addPropertyref(r); 131 } 132 133 137 public void appendRegex(String regex) { 138 PropertyRef r = new PropertyRef(); 139 r.setRegex(regex); 140 addPropertyref(r); 141 } 142 143 147 public void appendPrefix(String prefix) { 148 PropertyRef r = new PropertyRef(); 149 r.setPrefix(prefix); 150 addPropertyref(r); 151 } 152 153 157 public void appendBuiltin(BuiltinPropertySetName b) { 158 PropertyRef r = new PropertyRef(); 159 r.setBuiltin(b); 160 addPropertyref(r); 161 } 162 163 169 public void setMapper(String type, String from, String to) { 170 Mapper m = createMapper(); 171 Mapper.MapperType mapperType = new Mapper.MapperType(); 172 mapperType.setValue(type); 173 m.setType(mapperType); 174 m.setFrom(from); 175 m.setTo(to); 176 } 177 178 182 public void addPropertyref(PropertyRef ref) { 183 assertNotReference(); 184 ptyRefs.addElement(ref); 185 } 186 187 191 public void addPropertyset(PropertySet ref) { 192 assertNotReference(); 193 setRefs.addElement(ref); 194 } 195 196 200 public Mapper createMapper() { 201 assertNotReference(); 202 if (mapper != null) { 203 throw new BuildException("Too many <mapper>s!"); 204 } 205 mapper = new Mapper(getProject()); 206 return mapper; 207 } 208 209 214 public void add(FileNameMapper fileNameMapper) { 215 createMapper().add(fileNameMapper); 216 } 217 218 227 public void setDynamic(boolean dynamic) { 228 assertNotReference(); 229 this.dynamic = dynamic; 230 } 231 232 238 public void setNegate(boolean negate) { 239 assertNotReference(); 240 this.negate = negate; 241 } 242 243 247 public boolean getDynamic() { 248 return isReference() ? getRef().dynamic : dynamic; 249 } 250 251 255 public Mapper getMapper() { 256 return isReference() ? getRef().mapper : mapper; 257 } 258 259 264 private Hashtable getAllSystemProperties() { 265 Hashtable ret = new Hashtable (); 266 for (Enumeration e = System.getProperties().propertyNames(); 267 e.hasMoreElements();) { 268 String name = (String ) e.nextElement(); 269 ret.put(name, System.getProperties().getProperty(name)); 270 } 271 return ret; 272 } 273 274 278 public Properties getProperties() { 279 if (isReference()) { 280 return getRef().getProperties(); 281 } 282 Set names = null; 283 Project prj = getProject(); 284 Hashtable props = 285 prj == null ? getAllSystemProperties() : prj.getProperties(); 286 287 for (Enumeration e = setRefs.elements(); e.hasMoreElements();) { 289 PropertySet set = (PropertySet) e.nextElement(); 290 props.putAll(set.getProperties()); 291 } 292 293 if (getDynamic() || cachedNames == null) { 294 names = new HashSet (); 295 addPropertyNames(names, props); 296 for (Enumeration e = setRefs.elements(); e.hasMoreElements();) { 298 PropertySet set = (PropertySet) e.nextElement(); 299 names.addAll(set.getProperties().keySet()); 300 } 301 if (negate) { 302 HashSet complement = new HashSet (props.keySet()); 304 complement.removeAll(names); 305 names = complement; 306 } 307 if (!getDynamic()) { 308 cachedNames = names; 309 } 310 } else { 311 names = cachedNames; 312 } 313 FileNameMapper m = null; 314 Mapper myMapper = getMapper(); 315 if (myMapper != null) { 316 m = myMapper.getImplementation(); 317 } 318 Properties properties = new Properties (); 319 for (Iterator iter = names.iterator(); iter.hasNext();) { 321 String name = (String ) iter.next(); 322 String value = (String ) props.get(name); 323 if (value != null) { 324 if (m != null) { 327 String [] newname = m.mapFileName(name); 329 if (newname != null) { 330 name = newname[0]; 331 } 332 } 333 properties.setProperty(name, value); 334 } 335 } 336 return properties; 337 } 338 339 345 private void addPropertyNames(Set names, Hashtable properties) { 346 for (Enumeration e = ptyRefs.elements(); e.hasMoreElements();) { 348 PropertyRef r = (PropertyRef) e.nextElement(); 349 if (r.name != null) { 350 if (properties.get(r.name) != null) { 351 names.add(r.name); 352 } 353 } else if (r.prefix != null) { 354 for (Enumeration p = properties.keys(); p.hasMoreElements();) { 355 String name = (String ) p.nextElement(); 356 if (name.startsWith(r.prefix)) { 357 names.add(name); 358 } 359 } 360 } else if (r.regex != null) { 361 RegexpMatcherFactory matchMaker = new RegexpMatcherFactory(); 362 RegexpMatcher matcher = matchMaker.newRegexpMatcher(); 363 matcher.setPattern(r.regex); 364 for (Enumeration p = properties.keys(); p.hasMoreElements();) { 365 String name = (String ) p.nextElement(); 366 if (matcher.matches(name)) { 367 names.add(name); 368 } 369 } 370 } else if (r.builtin != null) { 371 372 if (r.builtin.equals(BuiltinPropertySetName.ALL)) { 373 names.addAll(properties.keySet()); 374 } else if (r.builtin.equals(BuiltinPropertySetName.SYSTEM)) { 375 names.addAll(System.getProperties().keySet()); 376 } else if (r.builtin.equals(BuiltinPropertySetName 377 .COMMANDLINE)) { 378 names.addAll(getProject().getUserProperties().keySet()); 379 } else { 380 throw new BuildException("Impossible: Invalid builtin " 381 + "attribute!"); 382 } 383 } else { 384 throw new BuildException("Impossible: Invalid PropertyRef!"); 385 } 386 } 387 } 388 389 394 protected PropertySet getRef() { 395 return (PropertySet) getCheckedRef(PropertySet.class, "propertyset"); 396 } 397 398 405 public final void setRefid(Reference r) { 406 if (!noAttributeSet) { 407 throw tooManyAttributes(); 408 } 409 super.setRefid(r); 410 } 411 412 422 protected final void assertNotReference() { 423 if (isReference()) { 424 throw tooManyAttributes(); 425 } 426 noAttributeSet = false; 427 } 428 429 433 private boolean noAttributeSet = true; 434 435 438 public static class BuiltinPropertySetName extends EnumeratedAttribute { 439 static final String ALL = "all"; 440 static final String SYSTEM = "system"; 441 static final String COMMANDLINE = "commandline"; 442 443 public String [] getValues() { 444 return new String [] {ALL, SYSTEM, COMMANDLINE}; 445 } 446 } 447 448 455 public String toString() { 456 StringBuffer b = new StringBuffer (); 457 TreeMap sorted = new TreeMap (getProperties()); 458 for (Iterator i = sorted.entrySet().iterator(); i.hasNext();) { 459 Map.Entry e = (Map.Entry ) i.next(); 460 if (b.length() != 0) { 461 b.append(", "); 462 } 463 b.append(e.getKey().toString()); 464 b.append("="); 465 b.append(e.getValue().toString()); 466 } 467 return b.toString(); 468 } 469 470 475 public Iterator iterator() { 476 final Enumeration e = getProperties().propertyNames(); 477 return new Iterator () { 478 public boolean hasNext() { 479 return e.hasMoreElements(); 480 } 481 public Object next() { 482 return new PropertyResource(getProject(), (String ) e.nextElement()); 483 } 484 public void remove() { 485 throw new UnsupportedOperationException (); 486 } 487 }; 488 } 489 490 494 public int size() { 495 return isReference() ? getRef().size() : getProperties().size(); 496 } 497 498 502 public boolean isFilesystemOnly() { 503 return isReference() && getRef().isFilesystemOnly(); 504 } 505 506 } 507 | Popular Tags |