peterreilly    2005/01/18 02:09:37

  Modified:    src/main/org/apache/tools/ant/types Tag: ANT_16_BRANCH
                        PropertySet.java
  Log:
  sync
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.8.2.9   +154 -47   ant/src/main/org/apache/tools/ant/types/PropertySet.java
  
  Index: PropertySet.java
  ===================================================================
  RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/types/PropertySet.java,v
  retrieving revision 1.8.2.8
  retrieving revision 1.8.2.9
  diff -u -r1.8.2.8 -r1.8.2.9
  --- PropertySet.java  10 Jan 2005 08:40:52 -0000      1.8.2.8
  +++ PropertySet.java  18 Jan 2005 10:09:37 -0000      1.8.2.9
  @@ -19,8 +19,10 @@
   
   import java.util.Enumeration;
   import java.util.Iterator;
  +import java.util.Map;
   import java.util.HashSet;
   import java.util.Set;
  +import java.util.TreeMap;
   import java.util.Hashtable;
   import java.util.Properties;
   import java.util.Stack;
  @@ -28,7 +30,6 @@
   
   import org.apache.tools.ant.BuildException;
   import org.apache.tools.ant.Project;
  -import org.apache.tools.ant.PropertyHelper;
   import org.apache.tools.ant.util.FileNameMapper;
   import org.apache.tools.ant.util.regexp.RegexpMatcher;
   import org.apache.tools.ant.util.regexp.RegexpMatcherFactory;
  @@ -45,8 +46,12 @@
       private Set cachedNames;
       private Vector ptyRefs = new Vector();
       private Vector setRefs = new Vector();
  -    private Mapper _mapper;
  +    private Mapper mapper;
   
  +    /**
  +     * this is a nested class containing a reference to some properties
  +     * and optionally a source of properties.
  +     */
       public static class PropertyRef {
   
           private int count;
  @@ -55,25 +60,41 @@
           private String prefix;
           private String builtin;
   
  +        /**
  +         * Set the name.
  +         * @param name a <code>String</code> value
  +         */
           public void setName(String name) {
               assertValid("name", name);
               this.name = name;
           }
   
  +        /**
  +         * Set the regular expression to use to filter the properties.
  +         * @param regex a regular expression
  +         */
           public void setRegex(String regex) {
               assertValid("regex", regex);
               this.regex = regex;
           }
   
  +        /**
  +         * Set the prefix to use.
  +         * @param prefix a <code>String</code> value
  +         */
           public void setPrefix(String prefix) {
               assertValid("prefix", prefix);
               this.prefix = prefix;
           }
   
  +        /**
  +         * Builtin property names - all, system or commandline.
  +         * @param b an enumerated <code>BuildinPropertySetName</code> value
  +         */
           public void setBuiltin(BuiltinPropertySetName b) {
  -            String builtin = b.getValue();
  -            assertValid("builtin", builtin);
  -            this.builtin = builtin;
  +            String pBuiltIn = b.getValue();
  +            assertValid("builtin", pBuiltIn);
  +            this.builtin = pBuiltIn;
           }
   
           private void assertValid(String attr, String value) {
  @@ -87,37 +108,63 @@
               }
           }
   
  +        /**
  +         * a debug toString()
  +         * @return a string version of this object
  +         */
           public String toString() {
               return "name=" + name + ", regex=" + regex + ", prefix=" + prefix
                   + ", builtin=" + builtin;
           }
   
  -    }
  +    } //end nested class
   
  +    /**
  +     * Allow properties of a particular name in the set.
  +     * @param name the property name to allow
  +     */
       public void appendName(String name) {
  -        PropertyRef ref = new PropertyRef();
  -        ref.setName(name);
  -        addPropertyref(ref);
  +        PropertyRef r = new PropertyRef();
  +        r.setName(name);
  +        addPropertyref(r);
       }
   
  +    /**
  +     * Allow properties whose names match a regex in the set.
  +     * @param regex the regular expression to use
  +     */
       public void appendRegex(String regex) {
  -        PropertyRef ref = new PropertyRef();
  -        ref.setRegex(regex);
  -        addPropertyref(ref);
  +        PropertyRef r = new PropertyRef();
  +        r.setRegex(regex);
  +        addPropertyref(r);
       }
   
  +    /**
  +     * Allow properties whose names start with a prefix in the set.
  +     * @param prefix the prefix to use
  +     */
       public void appendPrefix(String prefix) {
  -        PropertyRef ref = new PropertyRef();
  -        ref.setPrefix(prefix);
  -        addPropertyref(ref);
  +        PropertyRef r = new PropertyRef();
  +        r.setPrefix(prefix);
  +        addPropertyref(r);
       }
   
  +    /**
  +     * Allow builtin (all, system or commandline) properties in the set.
  +     * @param b the type of builtin properties
  +     */
       public void appendBuiltin(BuiltinPropertySetName b) {
  -        PropertyRef ref = new PropertyRef();
  -        ref.setBuiltin(b);
  -        addPropertyref(ref);
  +        PropertyRef r = new PropertyRef();
  +        r.setBuiltin(b);
  +        addPropertyref(r);
       }
   
  +    /**
  +     * set a mapper to change property names
  +     * @param type mapper type
  +     * @param from source pattern
  +     * @param to output pattern
  +     */
       public void setMapper(String type, String from, String to) {
           Mapper mapper = createMapper();
           Mapper.MapperType mapperType = new Mapper.MapperType();
  @@ -126,23 +173,35 @@
           mapper.setTo(to);
       }
   
  +    /**
  +     * Add a property reference (nested element) to the references to be 
used.
  +     * @param ref a property reference.
  +     */
       public void addPropertyref(PropertyRef ref) {
           assertNotReference();
           ptyRefs.addElement(ref);
       }
   
  +    /**
  +     * Add another property set to this set.
  +     * @param ref another property set
  +     */
       public void addPropertyset(PropertySet ref) {
           assertNotReference();
           setRefs.addElement(ref);
       }
   
  +    /**
  +     * Create a mapper to map the property names.
  +     * @return a mapper to be configured
  +     */
       public Mapper createMapper() {
           assertNotReference();
  -        if (_mapper != null) {
  +        if (mapper != null) {
               throw new BuildException("Too many <mapper>s!");
           }
  -        _mapper = new Mapper(getProject());
  -        return _mapper;
  +        mapper = new Mapper(getProject());
  +        return mapper;
       }
   
       /**
  @@ -154,22 +213,45 @@
           createMapper().add(fileNameMapper);
       }
   
  +    /**
  +     * 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.
  +     */
       public void setDynamic(boolean dynamic) {
           assertNotReference();
           this.dynamic = dynamic;
       }
   
  +    /**
  +     * 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
  +     */
       public void setNegate(boolean negate) {
           assertNotReference();
           this.negate = negate;
       }
   
  +    /**
  +     * Get the dynamic attribute.
  +     * @return true if the property set is to be evalulated each time it is 
used
  +     */
       public boolean getDynamic() {
           return isReference() ? getRef().dynamic : dynamic;
       }
   
  +    /**
  +     * Get the mapper attribute.
  +     * @return the mapper attribute
  +     */
       public Mapper getMapper() {
  -        return isReference() ? getRef()._mapper : _mapper;
  +        return isReference() ? getRef().mapper : mapper;
       }
   
       /**
  @@ -181,20 +263,20 @@
           Hashtable ret = new Hashtable();
           for (Enumeration e = System.getProperties().propertyNames();
                e.hasMoreElements();) {
  -            Object o = e.nextElement();
  -            if (o instanceof String) {
  -                String name = (String) o;
  -                ret.put(name, System.getProperties().getProperty(name));
  -            }
  +            String name = (String) e.nextElement();
  +            ret.put(name, System.getProperties().getProperty(name));
           }
           return ret;
       }
   
       /**
        * this is the operation to get the existing or recalculated properties.
  -     * @return
  +     * @return the properties for this propertyset
        */
       public Properties getProperties() {
  +        if (isReference()) {
  +            return getRef().getProperties();
  +        }
           Set names = null;
           Project prj = getProject();
           Hashtable props =
  @@ -202,11 +284,7 @@
   
           if (getDynamic() || cachedNames == null) {
               names = new HashSet();
  -            if (isReference()) {
  -                getRef().addPropertyNames(names, props);
  -            } else {
  -                addPropertyNames(names, props);
  -            }
  +            addPropertyNames(names, props);
               // Add this PropertySet's nested PropertySets' property names.
               for (Enumeration e = setRefs.elements(); e.hasMoreElements();) {
                   PropertySet set = (PropertySet) e.nextElement();
  @@ -236,6 +314,7 @@
               String name = (String) iter.next();
               String value = (String) props.get(name);
               if (mapper != null) {
  +                //map the names
                   String[] newname = mapper.mapFileName(name);
                   if (newname != null) {
                       name = newname[0];
  @@ -257,35 +336,35 @@
   
           // Add this PropertySet's property names.
           for (Enumeration e = ptyRefs.elements(); e.hasMoreElements();) {
  -            PropertyRef ref = (PropertyRef) e.nextElement();
  -            if (ref.name != null) {
  -                if (prj != null && prj.getProperty(ref.name) != null) {
  -                    names.add(ref.name);
  +            PropertyRef r = (PropertyRef) e.nextElement();
  +            if (r.name != null) {
  +                if (prj != null && prj.getProperty(r.name) != null) {
  +                    names.add(r.name);
                   }
  -            } else if (ref.prefix != null) {
  +            } else if (r.prefix != null) {
                   for (Enumeration p = properties.keys(); 
p.hasMoreElements();) {
                       String name = (String) p.nextElement();
  -                    if (name.startsWith(ref.prefix)) {
  +                    if (name.startsWith(r.prefix)) {
                           names.add(name);
                       }
                   }
  -            } else if (ref.regex != null) {
  +            } else if (r.regex != null) {
                   RegexpMatcherFactory matchMaker = new RegexpMatcherFactory();
                   RegexpMatcher matcher = matchMaker.newRegexpMatcher();
  -                matcher.setPattern(ref.regex);
  +                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 (ref.builtin != null) {
  +            } else if (r.builtin != null) {
   
  -                if (ref.builtin.equals(BuiltinPropertySetName.ALL)) {
  +                if (r.builtin.equals(BuiltinPropertySetName.ALL)) {
                       names.addAll(properties.keySet());
  -                } else if 
(ref.builtin.equals(BuiltinPropertySetName.SYSTEM)) {
  +                } else if (r.builtin.equals(BuiltinPropertySetName.SYSTEM)) {
                       names.addAll(System.getProperties().keySet());
  -                } else if (ref.builtin.equals(BuiltinPropertySetName
  +                } else if (r.builtin.equals(BuiltinPropertySetName
                                                 .COMMANDLINE)) {
                       names.addAll(getProject().getUserProperties().keySet());
                   } else {
  @@ -300,7 +379,8 @@
   
       /**
        * Performs the check for circular references and returns the
  -     * referenced FileList.
  +     * referenced PropertySet.
  +     * @return the referenced PropertySet
        */
       protected PropertySet getRef() {
           if (!isChecked()) {
  @@ -349,6 +429,11 @@
           }
           noAttributeSet = false;
       }
  +
  +    /**
  +     * flag which tracks whether any attribute has been set; used by
  +     * [EMAIL PROTECTED] #assertNotReference()} and [EMAIL PROTECTED] 
#setRefid(Reference)}
  +     */
       private boolean noAttributeSet = true;
   
       /**
  @@ -358,9 +443,31 @@
           static final String ALL = "all";
           static final String SYSTEM = "system";
           static final String COMMANDLINE = "commandline";
  +        /** @see EnumeratedAttribute#getValues() */
           public String[] getValues() {
               return new String[] {ALL, SYSTEM, COMMANDLINE};
           }
       }
  -} // END class PropertySet
   
  +    /**
  +     * 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' <i>natural 
order</i>.
  +     * @return a string rep of this object
  +     */
  +    public String toString() {
  +        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();
  +    }
  +}
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to