Esempio n. 1
0
  /**
   * Replaces <code>${xxx}</code> style constructions in the given value with the string value of
   * the corresponding data types.
   *
   * @param value The string to be scanned for property references. May be <code>null</code>, in
   *     which case this method returns immediately with no effect.
   * @param keys Mapping (String to String) of property names to their values. If <code>null</code>,
   *     only project properties will be used.
   * @exception BuildException if the string contains an opening <code>${</code> without a closing
   *     <code>}</code>
   * @return the original string with the properties replaced, or <code>null</code> if the original
   *     string is <code>null</code>.
   */
  public String replaceProperties(String ns, String value, Hashtable keys) throws BuildException {
    if (value == null) {
      return null;
    }

    Vector fragments = new Vector();
    Vector propertyRefs = new Vector();
    parsePropertyString(value, fragments, propertyRefs);

    StringBuffer sb = new StringBuffer();
    Enumeration i = fragments.elements();
    Enumeration j = propertyRefs.elements();

    while (i.hasMoreElements()) {
      String fragment = (String) i.nextElement();
      if (fragment == null) {
        String propertyName = (String) j.nextElement();
        Object replacement = null;

        // try to get it from the project or keys
        // Backward compatibility
        if (keys != null) {
          replacement = keys.get(propertyName);
        }
        if (replacement == null) {
          replacement = getProperty(ns, propertyName);
        }

        if (replacement == null) {
          project.log("Property ${" + propertyName + "} has not been set", Project.MSG_VERBOSE);
        }
        fragment = (replacement != null) ? replacement.toString() : "${" + propertyName + "}";
      }
      sb.append(fragment);
    }

    return sb.toString();
  }