FileDocCategorySizeDatePackage
PropertySet.javaAPI DocApache Ant 1.7016733Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.types

PropertySet

public class PropertySet extends DataType implements ResourceCollection
A set of properties.
since
Ant 1.6

Fields Summary
private boolean
dynamic
private boolean
negate
private Set
cachedNames
private Vector
ptyRefs
private Vector
setRefs
private Mapper
mapper
private boolean
noAttributeSet
Flag which tracks whether any attribute has been set; used by {@link #assertNotReference()} and {@link #setRefid(Reference)}.
Constructors Summary
Methods Summary
public voidadd(org.apache.tools.ant.util.FileNameMapper fileNameMapper)
Add a nested FileNameMapper.

param
fileNameMapper the mapper to add.
since
Ant 1.6.3

        createMapper().add(fileNameMapper);
    
private voidaddPropertyNames(java.util.Set names, java.util.Hashtable properties)

param
names the output Set to fill with the property names matching this PropertySet selection criteria.
param
properties the current Project properties, passed in to avoid needless duplication of the Hashtable during recursion.

        // Add this PropertySet's property names.
        for (Enumeration e = ptyRefs.elements(); e.hasMoreElements();) {
            PropertyRef r = (PropertyRef) e.nextElement();
            if (r.name != null) {
                if (properties.get(r.name) != null) {
                    names.add(r.name);
                }
            } else if (r.prefix != null) {
                for (Enumeration p = properties.keys(); p.hasMoreElements();) {
                    String name = (String) p.nextElement();
                    if (name.startsWith(r.prefix)) {
                        names.add(name);
                    }
                }
            } else if (r.regex != null) {
                RegexpMatcherFactory matchMaker = new RegexpMatcherFactory();
                RegexpMatcher matcher = matchMaker.newRegexpMatcher();
                matcher.setPattern(r.regex);
                for (Enumeration p = properties.keys(); p.hasMoreElements();) {
                    String name = (String) p.nextElement();
                    if (matcher.matches(name)) {
                        names.add(name);
                    }
                }
            } else if (r.builtin != null) {

                if (r.builtin.equals(BuiltinPropertySetName.ALL)) {
                    names.addAll(properties.keySet());
                } else if (r.builtin.equals(BuiltinPropertySetName.SYSTEM)) {
                    names.addAll(System.getProperties().keySet());
                } else if (r.builtin.equals(BuiltinPropertySetName
                                              .COMMANDLINE)) {
                    names.addAll(getProject().getUserProperties().keySet());
                } else {
                    throw new BuildException("Impossible: Invalid builtin "
                                             + "attribute!");
                }
            } else {
                throw new BuildException("Impossible: Invalid PropertyRef!");
            }
        }
    
public voidaddPropertyref(org.apache.tools.ant.types.PropertySet$PropertyRef ref)
Add a property reference (nested element) to the references to be used.

param
ref a property reference.

        assertNotReference();
        ptyRefs.addElement(ref);
    
public voidaddPropertyset(org.apache.tools.ant.types.PropertySet ref)
Add another property set to this set.

param
ref another property set.

        assertNotReference();
        setRefs.addElement(ref);
    
public voidappendBuiltin(org.apache.tools.ant.types.PropertySet$BuiltinPropertySetName b)
Allow builtin (all, system or commandline) properties in the set.

param
b the type of builtin properties.

        PropertyRef r = new PropertyRef();
        r.setBuiltin(b);
        addPropertyref(r);
    
public voidappendName(java.lang.String name)
Allow properties of a particular name in the set.

param
name the property name to allow.

        PropertyRef r = new PropertyRef();
        r.setName(name);
        addPropertyref(r);
    
public voidappendPrefix(java.lang.String prefix)
Allow properties whose names start with a prefix in the set.

param
prefix the prefix to use.

        PropertyRef r = new PropertyRef();
        r.setPrefix(prefix);
        addPropertyref(r);
    
public voidappendRegex(java.lang.String regex)
Allow properties whose names match a regex in the set.

param
regex the regular expression to use.

        PropertyRef r = new PropertyRef();
        r.setRegex(regex);
        addPropertyref(r);
    
protected final voidassertNotReference()
Ensures this data type is not a reference.

Calling this method as the first line of every bean method of this data type (setXyz, addXyz, createXyz) ensure proper handling of the refid attribute.

throws
BuildException if the refid attribute was already set, since refid and all other attributes are mutually exclusive.

        if (isReference()) {
            throw tooManyAttributes();
        }
        noAttributeSet = false;
    
public MappercreateMapper()
Create a mapper to map the property names.

return
a mapper to be configured.

        assertNotReference();
        if (mapper != null) {
            throw new BuildException("Too many <mapper>s!");
        }
        mapper = new Mapper(getProject());
        return mapper;
    
private java.util.HashtablegetAllSystemProperties()
Convert the system properties to a hashtable. Use propertynames to get the list of properties (including default ones).

        Hashtable ret = new Hashtable();
        for (Enumeration e = System.getProperties().propertyNames();
             e.hasMoreElements();) {
            String name = (String) e.nextElement();
            ret.put(name, System.getProperties().getProperty(name));
        }
        return ret;
    
public booleangetDynamic()
Get the dynamic attribute.

return
true if the property set is to be evalulated each time it is used.

        return isReference() ? getRef().dynamic : dynamic;
    
public MappergetMapper()
Get the mapper attribute.

return
the mapper attribute.

        return isReference() ? getRef().mapper : mapper;
    
public java.util.PropertiesgetProperties()
This is the operation to get the existing or recalculated properties.

return
the properties for this propertyset.

        if (isReference()) {
            return getRef().getProperties();
        }
        Set names = null;
        Project prj = getProject();
        Hashtable props =
            prj == null ? getAllSystemProperties() : prj.getProperties();

        //quick & dirty, to make nested mapped p-sets work:
        for (Enumeration e = setRefs.elements(); e.hasMoreElements();) {
            PropertySet set = (PropertySet) e.nextElement();
            props.putAll(set.getProperties());
        }

        if (getDynamic() || cachedNames == null) {
            names = new HashSet();
            addPropertyNames(names, props);
            // Add this PropertySet's nested PropertySets' property names.
            for (Enumeration e = setRefs.elements(); e.hasMoreElements();) {
                PropertySet set = (PropertySet) e.nextElement();
                names.addAll(set.getProperties().keySet());
            }
            if (negate) {
                //make a copy...
                HashSet complement = new HashSet(props.keySet());
                complement.removeAll(names);
                names = complement;
            }
            if (!getDynamic()) {
                cachedNames = names;
            }
        } else {
            names = cachedNames;
        }
        FileNameMapper m = null;
        Mapper myMapper = getMapper();
        if (myMapper != null) {
            m = myMapper.getImplementation();
        }
        Properties properties = new Properties();
        //iterate through the names, get the matching values
        for (Iterator iter = names.iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String value = (String) props.get(name);
            if (value != null) {
                // may be null if a system property has been added
                // after the project instance has been initialized
                if (m != null) {
                    //map the names
                    String[] newname = m.mapFileName(name);
                    if (newname != null) {
                        name = newname[0];
                    }
                }
                properties.setProperty(name, value);
            }
        }
        return properties;
    
protected org.apache.tools.ant.types.PropertySetgetRef()
Performs the check for circular references and returns the referenced PropertySet.

return
the referenced PropertySet.

        return (PropertySet) getCheckedRef(PropertySet.class, "propertyset");
    
public booleanisFilesystemOnly()
Fulfill the ResourceCollection contract.

return
whether this is a filesystem-only resource collection.

        return isReference() && getRef().isFilesystemOnly();
    
public java.util.Iteratoriterator()
Fulfill the ResourceCollection interface.

return
an Iterator of Resources.
since
Ant 1.7

        final Enumeration e = getProperties().propertyNames();
        return new Iterator() {
            public boolean hasNext() {
                return e.hasMoreElements();
            }
            public Object next() {
                return new PropertyResource(getProject(), (String) e.nextElement());
            }
            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    
public voidsetDynamic(boolean dynamic)
Set whether to reevaluate the set everytime the set is used. Default is true.

param
dynamic if true, reevaluate the property set each time the set is used. if false cache the property set the first time and use the cached set on subsequent occasions.

        assertNotReference();
        this.dynamic = dynamic;
    
public voidsetMapper(java.lang.String type, java.lang.String from, java.lang.String to)
Set a mapper to change property names.

param
type mapper type.
param
from source pattern.
param
to output pattern.

        Mapper m = createMapper();
        Mapper.MapperType mapperType = new Mapper.MapperType();
        mapperType.setValue(type);
        m.setType(mapperType);
        m.setFrom(from);
        m.setTo(to);
    
public voidsetNegate(boolean negate)
Set whether to negate results. If "true", all properties not selected by nested elements will be returned. Default is "false".

param
negate if true, negate the selection criteria.

        assertNotReference();
        this.negate = negate;
    
public final voidsetRefid(Reference r)
Sets the value of the refid attribute.

param
r the reference this datatype should point to.
throws
BuildException if another attribute was set, since refid and all other attributes are mutually exclusive.

        if (!noAttributeSet) {
            throw tooManyAttributes();
        }
        super.setRefid(r);
    
public intsize()
Fulfill the ResourceCollection contract.

return
the size of this ResourceCollection.

        return isReference() ? getRef().size() : getProperties().size();
    
public java.lang.StringtoString()
A debug toString. This gets a comma separated list of key=value pairs for the properties in the set. The output order is sorted according to the keys' natural order.

return
a string rep of this object.

        StringBuffer b = new StringBuffer();
        TreeMap sorted = new TreeMap(getProperties());
        for (Iterator i = sorted.entrySet().iterator(); i.hasNext();) {
            Map.Entry e = (Map.Entry) i.next();
            if (b.length() != 0) {
                b.append(", ");
            }
            b.append(e.getKey().toString());
            b.append("=");
            b.append(e.getValue().toString());
        }
        return b.toString();