Java String Escape escape(final String value)

Here you can find the source of escape(final String value)

Description

Escape a value when writing XML.

License

Open Source License

Parameter

Parameter Description
value the value to escape

Return

the escaped value.

Declaration

public static String escape(final String value) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static final Map<String, String> ESCAPES = new HashMap<String, String>();

    /**/*from  w  w w.  j a va2 s .  c o m*/
     * Escape a value when writing XML.
     *
     * Replaces each character in the ESCAPES map with its escaped value.
     *
     * This method should only be used when generating xml manually, since most
     * xml writers escape automatically.
     *
     * @param value
     *            the value to escape
     * @return the escaped value.
     */
    public static String escape(final String value) {
        String escapedValue = value;

        // replace each escapeable character
        Iterator<String> iter = ESCAPES.keySet().iterator();
        while (iter.hasNext()) {
            String raw = iter.next();
            String escaped = ESCAPES.get(raw);
            escapedValue = escapedValue.replace(raw, escaped);
        }

        return escapedValue;
    }
}

Related

  1. addEscapeSequence(String inputStr)
  2. addEscapesToCode(String code)
  3. addEscChar(String str)
  4. escape(final @Nonnull String string)
  5. escape(final String s)
  6. escape(String original)
  7. escape(String s)
  8. escape(String s)
  9. escape(String str)