Mega Code Archive

 
Categories / Java / Data Type
 

Substitute sub-strings in side of a string

import java.util.Map; /*   * JBoss, Home of Professional Open Source   * Copyright 2005, JBoss Inc., and individual contributors as indicated   * by the @authors tag. See the copyright.txt in the distribution for a   * full listing of individual contributors.   *   * This is free software; you can redistribute it and/or modify it   * under the terms of the GNU Lesser General Public License as   * published by the Free Software Foundation; either version 2.1 of   * the License, or (at your option) any later version.   *   * This software is distributed in the hope that it will be useful,   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   * Lesser General Public License for more details.   *   * You should have received a copy of the GNU Lesser General Public   * License along with this software; if not, write to the Free   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.   */ public class Main{   /** An empty string constant */   public static final String EMPTY = "";   /////////////////////////////////////////////////////////////////////////   //                         Substitution Methods                        //   /////////////////////////////////////////////////////////////////////////   /**    * Substitute sub-strings in side of a string.    *    * @param buff    Stirng buffer to use for substitution (buffer is not reset)    * @param from    String to substitute from    * @param to      String to substitute to    * @param string  String to look for from in    * @return        Substituted string    */   public static String subst(final StringBuffer buff, final String from,      final String to, final String string)   {      int begin = 0, end = 0;      while ((end = string.indexOf(from, end)) != -1)      {         // append the first part of the string         buff.append(string.substring(begin, end));         // append the replaced string         buff.append(to);         // update positions         begin = end + from.length();         end = begin;      }      // append the rest of the string      buff.append(string.substring(begin, string.length()));      return buff.toString();   }   /**    * Substitute sub-strings in side of a string.    *    * @param from    String to substitute from    * @param to      String to substitute to    * @param string  String to look for from in    * @return        Substituted string    */   public static String subst(final String from, final String to,      final String string)   {      return subst(new StringBuffer(), from, to, string);   }   /**    * Substitute sub-strings in side of a string.    *    * @param buff       String buffer to use for substitution (buffer is not reset)    * @param string     String to subst mappings in    * @param map        Map of from->to strings    * @param beginToken Beginning token    * @param endToken   Ending token    * @return           Substituted string    */   public static String subst(final StringBuffer buff, final String string,      final Map map, final String beginToken,      final String endToken)   {      int begin = 0, rangeEnd = 0;      Range range;      while ((range = rangeOf(beginToken, endToken, string, rangeEnd)) != null)      {         // append the first part of the string         buff.append(string.substring(begin, range.begin));         // Get the string to replace from the map         String key = string.substring(range.begin + beginToken.length(),            range.end);         Object value = map.get(key);         // if mapping does not exist then use empty;         if (value == null) value = EMPTY;         // append the replaced string         buff.append(value);         // update positions         begin = range.end + endToken.length();         rangeEnd = begin;      }      // append the rest of the string      buff.append(string.substring(begin, string.length()));      return buff.toString();   }   /**    * Substitute sub-strings in side of a string.    *    * @param string     String to subst mappings in    * @param map        Map of from->to strings    * @param beginToken Beginning token    * @param endToken   Ending token    * @return           Substituted string    */   public static String subst(final String string, final Map map,      final String beginToken, final String endToken)   {      return subst(new StringBuffer(), string, map, beginToken, endToken);   }   /**    * Substitute index identifiers with the replacement value from the    * given array for the corresponding index.    *    * @param buff       The string buffer used for the substitution    *                   (buffer is not reset).    * @param string     String substitution format.    * @param replace    Array of strings whose values will be used as     *                   replacements in the given string when a token with    *                   their index is found.    * @param token      The character token to specify the start of an index    *                   reference.    * @return           Substituted string.    */   public static String subst(final StringBuffer buff, final String string,      final String replace[], final char token)   {      int i = string.length();      for (int j = 0; j >= 0 && j < i; j++)      {         char c = string.charAt(j);         // if the char is the token, then get the index         if (c == token)         {            // if we aren't at the end of the string, get the index            if (j != i)            {               int k = Character.digit(string.charAt(j + 1), 10);               if (k == -1)               {                  buff.append(string.charAt(j + 1));               }               else if (k < replace.length)               {                  buff.append(replace[k]);               }               j++;            }         }         else         {            buff.append(c);         }      }      return buff.toString();   }   /**    * Substitute index identifiers with the replacement value from the    * given array for the corresponding index.    *    * @param string     String substitution format.    * @param replace    Array of strings whose values will be used as     *                   replacements in the given string when a token with    *                   their index is found.    * @param token      The character token to specify the start of an index    *                   reference.    * @return           Substituted string.    */   public static String subst(final String string, final String replace[],      final char token)   {      return subst(new StringBuffer(), string, replace, token);   }   /**    * Substitute index identifiers (with <code>%</code> for the index token)    * with the replacement value from the given array for the corresponding    * index.    *    * @param string     String substitution format.    * @param replace    Array of strings whose values will be used as     *                   replacements in the given string when a token with    *                   their index is found.    * @return           Substituted string.    */   public static String subst(final String string, final String replace[])   {      return subst(new StringBuffer(), string, replace, '%');   }   /////////////////////////////////////////////////////////////////////////   //                             Range Methods                           //   /////////////////////////////////////////////////////////////////////////   /**    * Represents a range between two integers.    */   public static class Range   {      /** The beginning of the range. */      public int begin;      /** The end of the range. */      public int end;      /**       * Construct a new range.       *       * @param begin   The beginning of the range.       * @param end     The end of the range.       */      public Range(int begin, int end)      {         this.begin = begin;         this.end = end;      }      /**       * Default constructor.       */      public Range()      {      }   }   /**    * Return the range from a begining token to an ending token.    *    * @param beginToken String to indicate begining of range.    * @param endToken   String to indicate ending of range.    * @param string     String to look for range in.    * @param fromIndex  Beginning index.    * @return           (begin index, end index) or <i>null</i>.    */   public static Range rangeOf(final String beginToken, final String endToken,      final String string, final int fromIndex)   {      int begin = string.indexOf(beginToken, fromIndex);      if (begin != -1)      {         int end = string.indexOf(endToken, begin + 1);         if (end != -1)         {            return new Range(begin, end);         }      }      return null;   }   /**    * Return the range from a begining token to an ending token.    *    * @param beginToken String to indicate begining of range.    * @param endToken   String to indicate ending of range.    * @param string     String to look for range in.    * @return           (begin index, end index) or <i>null</i>.    */   public static Range rangeOf(final String beginToken, final String endToken,      final String string)   {      return rangeOf(beginToken, endToken, string, 0);   } }