Java String Escape escapeXML(String message)

Here you can find the source of escapeXML(String message)

Description

Formats the message to be XML compatible, with the XML escaping.

License

CDDL license

Parameter

Parameter Description
message The raw message

Return

a localized and formatted message

Declaration

public static String escapeXML(String message) 

Method Source Code

//package com.java2s;
/* The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the License). You may not use this file except in
 * compliance with the License.//from  w ww .  java  2 s .  co m
 *
 * You can obtain a copy of the License at
 * http://www.sun.com/cddl/cddl.html or
 * install_dir/legal/LICENSE
 * See the License for the specific language governing
 * permission and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * Header Notice in each file and include the License file
 * at install_dir/legal/LICENSE.
 * If applicable, add the following below the CDDL Header,
 * with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * $Id: Utilities.java,v 1.15.2.2 2009/09/21 04:59:50 akara Exp $
 *
 * Copyright 2005-2009 Sun Microsystems Inc. All Rights Reserved
 */

import java.util.*;

public class Main {
    private static HashSet<String> xmlEscapes;

    /**
     * Formats the message to be XML compatible, with the XML escaping.
     *
     * @param message The raw message
     * @return a localized and formatted message
     */
    public static String escapeXML(String message) {
        StringBuilder msgBuffer = new StringBuilder(message.length() * 2);
        escapeXML(message, msgBuffer);
        return msgBuffer.toString();
    }

    /**
     * Formats the message to be XML compatible, with the XML escaping.
     *
     * @param message The raw message
     * @param msgBuffer The buffer to write the escaped string
     */
    public static void escapeXML(String message, StringBuilder msgBuffer) {
        char[] msgChars = message.toCharArray();
        for (int i = 0; i < msgChars.length; i++) {
            switch (msgChars[i]) {
            case '<':
                msgBuffer.append("&lt;");
                break;
            case '>':
                msgBuffer.append("&gt;");
                break;
            case '"':
                msgBuffer.append("&quot;");
                break;
            case '\'':
                msgBuffer.append("&apos;");
                break;
            case '&':
                i = checkEscapedXML(message, msgChars, i, msgBuffer);
                break;
            default:
                msgBuffer.append(msgChars[i]);
            }
        }
    }

    private static int checkEscapedXML(String message, char[] msgChars,
            int ampIdx, StringBuilder msgBuffer) {
        // First search the message for any ';' char, as in &..; escape
        // sequences.
        int seqStart = ampIdx + 1;
        int semiIdx = message.indexOf(';', seqStart);
        if (semiIdx < ampIdx) { // not found
            msgBuffer.append("&amp;");
            return ampIdx;
        } else if (msgChars[seqStart] == '#') { // Indicating a number
            ++seqStart;
            if (msgChars[seqStart] == 'x' || msgChars[seqStart] == 'X') { // hex
                for (int i = ++seqStart; i < semiIdx; i++) {
                    if ((msgChars[i] >= '0' && msgChars[i] <= '9')
                            || (msgChars[i] >= 'a' && msgChars[i] <= 'f')
                            || (msgChars[i] >= 'A' && msgChars[i] <= 'F')) {
                        continue;
                    } else {
                        msgBuffer.append("&amp;");
                        return ampIdx;
                    }
                }
            } else {
                for (int i = seqStart; i < semiIdx; i++) {
                    if (msgChars[i] >= '0' && msgChars[i] <= '9') {
                        continue;
                    } else {
                        msgBuffer.append("&amp;");
                        return ampIdx;
                    }
                }
            }
            msgBuffer.append(msgChars, ampIdx, semiIdx - ampIdx + 1);
            return semiIdx;
        } else { // expecting a valid sequence.
            if (xmlEscapes == null)
                initXMLEscapes();
            String sequence = new String(msgChars, seqStart, semiIdx
                    - seqStart);
            if (xmlEscapes.contains(sequence)) {
                msgBuffer.append(msgChars, ampIdx, semiIdx - ampIdx + 1);
                return semiIdx;
            } else {
                msgBuffer.append("&amp;");
                return ampIdx;
            }
        }
    }

    private static void initXMLEscapes() {
        String[] escStrings = { "quot", "amp", "apos", "lt", "gt", "nbsp",
                "iexcl", "cent", "pound", "curren", "yen", "brvbar",
                "sect", "uml", "copy", "ordf", "laquo", "not", "shy",
                "reg", "macr", "deg", "plusmn", "sup2", "sup3", "acute",
                "micro", "para", "middot", "cedil", "sup1", "ordm",
                "raquo", "frac14", "frac12", "frac34", "iquest" };
        xmlEscapes = new HashSet<String>(escStrings.length);
        for (String escString : escStrings)
            xmlEscapes.add(escString);
    }
}

Related

  1. escapeToFileName(String name)
  2. escapeUnicode(String s)
  3. escapeUnicodeString(final String input, final boolean escapeAscii)
  4. escapeUnsafeCharacters(String anyURI, boolean escapePercent)
  5. escapeWiki(String s)
  6. escapeXml(String str)
  7. escapeXML(String str)
  8. percentEncode(String s)
  9. percentEncode(String s)