Filter an XML string and escape the angle brackets in unmatched tags. - Java XML

Java examples for XML:XML String Escape

Description

Filter an XML string and escape the angle brackets in unmatched tags.

Demo Code

/*---------------------------------------------------------------
 *  Copyright 2005 by the Radiological Society of North America
 *
 *  This source software is released under the terms of the
 *  RSNA Public License (http://mirc.rsna.org/rsnapubliclicense)
 *----------------------------------------------------------------*/

import java.io.StringWriter;

public class Main{

    /**//from  w  ww  . jav  a2  s  .com
     * Filter an XML string and escape the angle brackets in unmatched tags.
     * This method is used to defend against a user inserting tags that make
     * a block of text not well-formed.
     * <p>
     * This method is not bullet-proof, but it protects against most
     * common mistakes. The rest have to be caught by the parser.
     * @param theString the string in which to escape the special characters.
     * @return the modified string.
     */
    public static String makeFilteredString(String theString) {
        String t = theString;
        t = t.replaceAll("<br[\\s]*>", "<br />").replaceAll("</br[\\s]*>",
                "");
        t = t.replaceAll("<hr[\\s]*>", "<hr />").replaceAll("</hr[\\s]*>",
                "");
        t = t.replace("&", "&amp;");
        String s = "";
        int left;
        int right;
        while (t.length() > 0) {
            left = t.indexOf("<");
            if (left == -1)
                return s + t.replace(">", "&gt;");
            right = t.indexOf(">");
            if (right == -1)
                return s + t.replace("<", "&lt;");
            if (right < left) {
                s += t.substring(0, right) + "&gt;";
                t = t.substring(right + 1, t.length());
            } else {
                if (isItATag(t.substring(left, right + 1))) {
                    s += t.substring(0, right + 1);
                    t = t.substring(right + 1, t.length());
                } else {
                    s += t.substring(0, left) + "&lt;";
                    t = t.substring(left + 1, t.length());
                }
            }
        }
        return s;
    }
    private static boolean isItATag(String s) {
        if (s.charAt(0) != '<')
            return false;
        boolean endTag = false;
        int i = StringUtil.skipWhitespace(s, 1);
        if (s.charAt(i) == '/') {
            endTag = true;
            i = StringUtil.skipWhitespace(s, i + 1);
        }
        if ((i = StringUtil.skipWord(s, i)) < 0)
            return false;
        i = StringUtil.skipWhitespace(s, i);
        if (s.charAt(i) == '>')
            return true;
        if (endTag)
            return false;
        while (i < s.length()) {
            if (s.charAt(i) == '/') {
                i = StringUtil.skipWhitespace(s, i + 1);
                if (s.charAt(i) == '>')
                    return true;
                return false;
            }
            if (s.charAt(i) == '>')
                return true;
            if ((i = skipAttribute(s, i + 1)) < 0)
                return false;
        }
        return false;
    }
    /**
     * Skip over an attribute in an XML string.
     * @param xmlString the XML string.
     * @param i the index of the start of the attribute.
     * @return the index of the next non-whitespace
     * character after the attribute.
     */
    private static int skipAttribute(String xmlString, int i) {
        if ((i = StringUtil.skipWord(xmlString, i)) < 0)
            return -1;
        i = StringUtil.skipWhitespace(xmlString, i);
        if (xmlString.charAt(i) != '=')
            return -1;
        i = StringUtil.skipWhitespace(xmlString, i + 1);
        if (xmlString.charAt(i) != '\"')
            return -1;
        i = xmlString.indexOf('\"', i + 1);
        if (i < 0)
            return -1;
        i = StringUtil.skipWhitespace(xmlString, i + 1);
        return i;
    }
}

Related Tutorials