ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@locus.apache.org
Subject cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs Property.java
Date Thu, 13 Jul 2000 15:23:11 GMT
bodewig     00/07/13 08:23:09

  Modified:    src/main/org/apache/tools/ant ProjectHelper.java
               src/main/org/apache/tools/ant/taskdefs Property.java
  Log:
  Resolve properties loaded from files in a more predictable way.
  Submitted by:	Glenn McAllister <glennm@ca.ibm.com>
  
  Revision  Changes    Path
  1.20      +2 -1      jakarta-ant/src/main/org/apache/tools/ant/ProjectHelper.java
  
  Index: ProjectHelper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/ProjectHelper.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- ProjectHelper.java	2000/07/12 12:40:28	1.19
  +++ ProjectHelper.java	2000/07/13 15:23:03	1.20
  @@ -480,7 +480,8 @@
                                                value );
                   }
                   String n=value.substring( pos+2, endName );
  -                String v=(String) keys.get( n );
  +                String v= (keys.containsKey(n)) ? (String) keys.get( n ) 
  +                    : "${"+n+"}";
                   //System.out.println("N: " + n + " " + " V:" + v);
                   sb.append( v );
                   prev=endName+1;
  
  
  
  1.8       +89 -29    jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Property.java
  
  Index: Property.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Property.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- Property.java	2000/07/06 16:48:18	1.7
  +++ Property.java	2000/07/13 15:23:07	1.8
  @@ -64,6 +64,7 @@
    *
    * @author costin@dnt.ro
    * @author Sam Ruby <rubys@us.ibm.com>
  + * @author Glenn McAllister <glennm@ca.ibm.com>
    */
   public class Property extends Task {
   
  @@ -101,20 +102,7 @@
       public void init() throws BuildException {
           try {
               if ((name != null) && (value != null)) {
  -		if( userProperty )
  -                    if (project.getUserProperty(name) == null) {
  -		        project.setUserProperty(name, value);
  -                    } else {
  -                        log("Override ignored for " + name, 
  -                            Project.MSG_VERBOSE);
  -                    }
  -		else
  -                    if (project.getProperty(name) == null) {
  -		        project.setProperty(name, value);
  -                    } else {
  -                        log("Override ignored for " + name, 
  -                            Project.MSG_VERBOSE);
  -                    }
  +                addProperty(name, value);
               }
   
               if (file != null) loadFile(file);
  @@ -126,16 +114,18 @@
           }
       }
   
  -    private void loadFile (String name) {
  +    private void loadFile (String name) throws BuildException {
           Properties props = new Properties();
           log("Loading " + name, Project.MSG_VERBOSE);
           try {
               if (new File(name).exists()) {
                   props.load(new FileInputStream(name));
                   addProperties(props);
  +            } else {
  +                log("Unable to find " + name, Project.MSG_VERBOSE);
               }
           } catch(Exception ex) {
  -            ex.printStackTrace();
  +            throw new BuildException(ex.getMessage(), ex, location);
           }
       }
   
  @@ -154,29 +144,99 @@
       }
   
       private void addProperties(Properties props) {
  +        resolveAllProperties(props);
           Enumeration e = props.keys();
           while (e.hasMoreElements()) {
               String name = (String) e.nextElement();
               String value = (String) props.getProperty(name);
               String v = ProjectHelper.replaceProperties(value, project.getProperties());
  -            if( userProperty )
  -                if (project.getUserProperty(name) == null) {
  -		    project.setUserProperty(name, v);
  -                } else {
  -                    log("Override ignored for " + name, 
  -                        Project.MSG_VERBOSE);
  +            addProperty(name, value);
  +        }
  +    }
  +
  +    public void setUserProperty( boolean userP ) {
  +	userProperty=userP;
  +    }
  +
  +    private void addProperty(String n, String v) {
  +        if( userProperty ) {
  +            if (project.getUserProperty(n) == null) {
  +                project.setUserProperty(n, v);
  +            } else {
  +                log("Override ignored for " + name, Project.MSG_VERBOSE);
  +            } 
  +        } else {
  +            if (project.getProperty(n) == null) {
  +                project.setProperty(n, v);
  +            } else {
  +                log("Override ignored for " + name, Project.MSG_VERBOSE);
  +            }
  +        }
  +    }
  +
  +    private void resolveAllProperties(Hashtable props) {
  +        Hashtable toResolve = new Hashtable();
  +        Enumeration e = props.keys();
  +        boolean more = true;
  +        
  +        while (more) {
  +            while (e.hasMoreElements()) {
  +                Vector propsInValue = new Vector();
  +                String name = (String) e.nextElement();
  +                String value = (String) props.get(name);
  +
  +                if (extractProperties(value, propsInValue)) {
  +                    for (int i=0; i < propsInValue.size(); i++) {
  +                        String elem = (String) propsInValue.elementAt(i);
  +                        if (project.getProperties().containsKey(elem) ||
  +                            props.containsKey(elem)) {
  +                            toResolve.put(name, value);
  +                            break;
  +                        }
  +                    }
                   }
  -            else
  -                if (project.getProperty(name) == null) {
  -		    project.setProperty(name, v);
  +
  +                if (toResolve.size() > 0) {
  +                    Enumeration tre = toResolve.keys();
  +                    while (tre.hasMoreElements()) {
  +                        String name2 = (String) tre.nextElement();
  +                        String value2 = (String) toResolve.get(name2);
  +                        String v = ProjectHelper.replaceProperties(value2,
  +                                                                   project.getProperties());
  +                        v = ProjectHelper.replaceProperties(v, props);
  +                        props.put(name, v);
  +                    }
  +
  +                    toResolve.clear();
  +                    e = props.keys();
                   } else {
  -                    log("Override ignored for " + name, 
  -                        Project.MSG_VERBOSE);
  +                    more = false;
                   }
  +            }
           }
       }
   
  -    public void setUserProperty( boolean userP ) {
  -	userProperty=userP;
  +    private boolean extractProperties(String source, Vector properties) {
  +        // This is an abreviated version of 
  +        // ProjectHelper.replaceProperties method
  +        int i=0;
  +        int prev=0;
  +        int pos;
  +
  +        while( (pos=source.indexOf( "$", prev )) >= 0 ) {
  +            if( pos == (source.length() - 1)) {
  +                prev = pos + 1;
  +            } else if (source.charAt( pos + 1 ) != '{' ) {
  +                prev=pos+2;
  +            } else {
  +                int endName=source.indexOf( '}', pos );
  +                String n=source.substring( pos+2, endName );
  +                properties.addElement(n);
  +                prev=endName+1;
  +            }
  +        }
  +        
  +        return (properties.size() > 0);
       }
  +    
   }
  
  
  

Mime
View raw message