Java Map Replace unescape(String cooked, char escapeChar, Map replacements)

Here you can find the source of unescape(String cooked, char escapeChar, Map replacements)

Description

Unescape the String cooked .

License

Open Source License

Parameter

Parameter Description
cooked The escaped String .
escapeChar The escape character.
replacements A map of escaped entities to their unescaped representation.

Return

The unescaped .

Declaration

public static String unescape(String cooked, char escapeChar, Map<Integer, Integer> replacements) 

Method Source Code

//package com.java2s;
/*//from   ww  w  .j av  a 2s . c o m
 * dmfs - http://dmfs.org/
 *
 * Copyright (C) 2011 Marten Gajda <marten@dmfs.org>
 *
 * 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
 */

import java.util.Map;

public class Main {
    /**
     * Unescape the {@link String} {@code cooked}.
     * 
     * @param cooked
     *            The escaped {@link String}.
     * 
     * @param escapeChar
     *            The escape character.
     * 
     * @param replacements
     *            A map of escaped entities to their unescaped representation.
     * 
     * @return The unescaped {@link String}.
     * 
     */
    public static String unescape(String cooked, char escapeChar, Map<Integer, Integer> replacements) {
        if (cooked == null) {
            return null;
        }

        int length = cooked.length();

        if (length <= 1) {
            return cooked;
        }

        StringBuilder result = null;

        int pos;
        int start = 0;

        while ((pos = cooked.indexOf(escapeChar, start)) >= 0) {
            if (result == null) {
                // assume the raw string is not longer than the cooked string
                result = new StringBuilder(length);
            }

            result.append(cooked.substring(start, pos));
            if (pos + 1 < length) {
                char c = cooked.charAt(pos + 1);
                if (replacements != null && replacements.containsKey((int) c)) {
                    Integer replacement = replacements.get((int) c);
                    if (replacement != null) {
                        result.append((char) (int) replacement);
                    }
                } else {
                    result.append(c);
                }
            } else {
                // was the last character of cooked, we keep it since it doesn't escape any other character
                result.append(escapeChar);
            }
            // skip escape character and escaped character
            start = pos + 2;
        }

        if (start == 0) {
            // no escaped characters found, return original string
            return cooked;
        } else {
            if (start < length) {
                // append remainder
                result.append(cooked.substring(start));
            }

            return result.toString();
        }
    }
}

Related

  1. replaceVariable(final String src, final Map value)
  2. replaceVariables(String chart, Map variables)
  3. replaceVars(String origString, Map vars)
  4. replaceVars(String s, Map m)
  5. stringReplace(Map map, String line)
  6. zipToMap(String[] searchStrings, String[] replacements)