Java Char Array to Entity charsToEntities(String str, boolean xml11)

Here you can find the source of charsToEntities(String str, boolean xml11)

Description

Converts <, >, & in the string to their HTML entity equivalents.

License

Open Source License

Parameter

Parameter Description
str The string
xml11 Whether to allow XML 1.1 constructs.

Declaration

public static String charsToEntities(String str, boolean xml11) 

Method Source Code

//package com.java2s;
/*//from  ww w .j a  v a  2  s  .  co m
 * StandardUtilities.java - Miscelaneous XML utility functions.
 * :tabSize=4:indentSize=4:noTabs=false:
 * :folding=explicit:collapseFolds=1:
 *
 * Copyright (C) 1999, 2006 Marcelo Vanzin, Slava Pestov
 * Portions copyright (C) 2000 Richard S. Hall
 * Portions copyright (C) 2001 Dirk Moebius
 *
 * 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 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.
 */

public class Main {
    /**
     * Converts &lt;, &gt;, &amp; in the string to their HTML entity
     * equivalents.
     *
     * <p>If <code>xml11</code> is true, then character entities
     * are used to convert illegal XML characters (mainly ASCII
     * control characters).</p>
     *
     * @param str The string
     * @param xml11 Whether to allow XML 1.1 constructs.
     */
    public static String charsToEntities(String str, boolean xml11) {
        StringBuilder buf = new StringBuilder(str.length());
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);

            // control characters, excluding \t, \r and \n
            // See: http://www.w3.org/International/questions/qa-controls
            if (((0x00 <= ch && ch <= 0x1F) || (0x7F <= ch && ch <= 0x9F))
                    && ch != '\r' && ch != '\n' && ch != '\t') {
                if (xml11 && ch != 0x00) {
                    buf.append("&#").append((int) ch).append(';');
                } else {
                    // The character is illegal.
                    // But put a PI instead, to make it
                    // recoverable in certain apps.
                    buf.append("<?illegal-xml-character ").append((int) ch)
                            .append("?>");
                }
                continue;
            }

            switch (ch) {
            case '<':
                buf.append("&lt;");
                break;
            case '>':
                buf.append("&gt;");
                break;
            case '&':
                buf.append("&amp;");
                break;
            default:
                buf.append(ch);
                break;
            }
        }
        return buf.toString();
    }
}

Related

  1. charsToEntities(String str)