Java Map Replace replaceOpcodeNames(HashMap replacementValues, final String input)

Here you can find the source of replaceOpcodeNames(HashMap replacementValues, final String input)

Description

replace Opcode Names

License

Open Source License

Declaration

public static String replaceOpcodeNames(HashMap replacementValues, final String input) 

Method Source Code

//package com.java2s;
/**//from  w  ww .  j  ava  2  s .com
 * Title:        NoUnit - Identify Classes that are not being unit Tested
 *
 * Copyright (C) 2001  Paul Browne , FirstPartners.net
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * @author Paul Browne
 * @version 0.6
 *
 * renamed to textUtilities and added to blue by steven yi
 */

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class Main {
    public static String replaceOpcodeNames(HashMap replacementValues, final String input) {

        String retVal = input;

        for (Iterator iter = replacementValues.entrySet().iterator(); iter.hasNext();) {
            Map.Entry entry = (Entry) iter.next();

            String key = (String) entry.getKey();
            String value = (String) entry.getValue();

            retVal = retVal.replaceAll("(^|\\s)" + key + "($|\\s)", "$1" + value + "$2");
        }

        return retVal;
    }

    /**
     * Replaces all occurrences of <code>oldSubstring</code> in
     * <code>string</code>, if there are any, with <code>newSubstring</code>.
     * 
     * @param string -
     *            Replace substrings of this String
     * @param oldSubstring -
     *            The substring of <code>string</code> to be replaced
     * @param newSubstring -
     *            The string to put into <code> string</code>
     * @return A new String which is a copy of <code>string</code> with all
     *         occurrences of <code>oldSubstring</code> in <code>string</code>,
     *         if there are any, with <code>newSubstring</code>. Returns null
     *         if <code>string</code> is null. Returns <code>string</code>
     *         if either substring is null or <code>oldSubstring</code> is
     *         empty
     */
    // TODO - remove and replace in calling code with Java's String replaceAll
    public static String replaceAll(String string, String oldSubstring, String newSubstring) {
        // Local Variables
        String result = string;

        if ((result != null) && (result.length() > 0) && (result.indexOf(oldSubstring) > -1)
                && (oldSubstring.length() > 0) && (!oldSubstring.equals(newSubstring)) && (newSubstring != null)) {

            while (result.indexOf(oldSubstring) > -1) {
                result = replace(result, oldSubstring, newSubstring);
            }
        }

        // result.replaceAll(oldSubstring, newSubstring);

        return result;
    }

    /**
     * Replace the first occurrence of <code>oldSubstring</code> in
     * <code>string</code>, if there is one, with <code>newSubstring</code>.
     * 
     * @param string -
     *            Replace a substring of this String
     * @param oldSubstring -
     *            The substring of <code>string</code> to be replaced
     * @param newSubstring -
     *            The string to put into <code> string</code>
     * @return A new String which is a copy of <code>string</code> with the
     *         first occurrence of <code>oldSubstring</code> in
     *         <code>string</code>, if there is one, with
     *         <code>newSubstring</code>. Returns null if <code>string</code>
     *         is null. Returns <code>string</code> if either substring is
     *         null or <code>oldSubstring</code> is empty
     */

    // TODO - remove and replace in calling code with Java's String replace
    public static String replace(String string, String oldSubstring, String newSubstring) {
        String result = string;

        if ((string != null) && (string.length() > 0) && (oldSubstring != null) && (oldSubstring.length() > 0)
                && (newSubstring != null)) {
            int pos = string.indexOf(oldSubstring);
            result = string.substring(0, pos) + newSubstring + string.substring(pos + oldSubstring.length());
        }

        // result.replaceFirst(oldSubstring, newSubstring);

        return result;
    }
}

Related

  1. replaceKeyword(String messageTemplate, Map args, String key)
  2. replaceMap(String inStr, Map replaceMap)
  3. replaceMethodUriWithValues(String methodUri, Map urlParameterKeyValues)
  4. replaceNode(Map map, Map replacement, List pathItems)
  5. replaceNotContainedWithReplaced(String sentStr, Map mapNotContainedReplaced)
  6. replaceParameters(final String value, final Map paramMap)
  7. replaceParameters(Map map, Map originals, Map defaults, Map hards)
  8. replaceParameters(String query, Map parameters)
  9. replaceParameters(String str, Map parameters)