You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by mo...@apache.org on 2002/09/30 19:35:04 UTC

cvs commit: jakarta-jetspeed/src/java/org/apache/jetspeed/util StringUtils.java

morciuch    2002/09/30 10:35:04

  Modified:    src/java/org/apache/jetspeed/util StringUtils.java
  Log:
  Added method replaceVars
  
  Revision  Changes    Path
  1.7       +40 -1     jakarta-jetspeed/src/java/org/apache/jetspeed/util/StringUtils.java
  
  Index: StringUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/util/StringUtils.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- StringUtils.java	26 Jul 2002 01:47:21 -0000	1.6
  +++ StringUtils.java	30 Sep 2002 17:35:04 -0000	1.7
  @@ -56,12 +56,14 @@
   
   import org.apache.turbine.util.Log;
   import java.util.StringTokenizer;
  +import java.util.Map;
   
   /**
    * This class provides static util methods for String manaipulation that 
    * aren't part of the default JDK functionalities.
    *
    * @author <a href="mailto:raphael@apache.org">Rapha�l Luta</a>
  + * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a> 
    * @version $Id$
    */
   public class StringUtils 
  @@ -245,5 +247,42 @@
           }
           return filename;
       }               
  +
  +    /**
  +     * Performs variable substitution for a string. String is scanned for ${variable_name} and if one is found,
  +     * it is replaced with corresponding value from the vars hashtable.
  +     * 
  +     * @param origString unmodified string
  +     * @param vars Hashtable of replacement values
  +     * @return modified string
  +     * @exception Exception
  +     */
  +    public static String replaceVars(String origString, Map vars) 
  +    {
  +
  +        StringBuffer finalString = new StringBuffer();
  +        int index = 0;
  +        int i = 0;
  +        String key = null;
  +        String value = null;
  +        while ((index = origString.indexOf("${", i)) > -1) 
  +        {
  +            key = origString.substring(index + 2, origString.indexOf("}", index+3));
  +            value = (String) vars.get(key);
  +            finalString.append(origString.substring(i, index));
  +            if (value != null) 
  +            {
  +                finalString.append(value);
  +            } 
  +            else 
  +            {
  +                finalString.append("${"+key+"}");
  +            }
  +            i = index + 3 + key.length();
  +        }
  +        finalString.append (origString.substring(i));
  +
  +        return finalString.toString();
  +    }
   
   }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>