Android XML String Escape appendPlainText(StringBuilder dest, String src)

Here you can find the source of appendPlainText(StringBuilder dest, String src)

Description

append Plain Text

License

Open Source License

Declaration

public final static void appendPlainText(StringBuilder dest, String src) 

Method Source Code

//package com.java2s;
/*/*  ww w.ja  v a2s.co  m*/
 * XMLUtils.java
 *
 * Created on 23.12.2011
 *
 * Copyright (c) 2005-2011, Eugene Stahov (evgs@bombus-im.org), 
 * http://bombus-im.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 software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

public class Main {
    public final static void appendPlainText(StringBuilder dest, String src) {
        if (src == null)
            return;
        int len = src.length();
        for (int i = 0; i < len; i++) {
            char ch = src.charAt(i);
            switch (ch) {
            case '&':
                dest.append("&amp;");
                continue;
            case '"':
                dest.append("&quot;");
                continue;
            case '<':
                dest.append("&lt;");
                continue;
            case '>':
                dest.append("&gt;");
                continue;
            case '\'':
                dest.append("&apos;");
                continue;
            case 0:
                continue; //cutout any zeroes
            default:
                dest.append(ch);
                continue;
                /* UTF-8 encoding was moved out
                //UTF-8 encoding
                if (ch<=0x7f) { 
                   dest.append(ch); 
                   continue; 
                }
                        
                 if (((ch >= 0x80) && (ch <= 0x7ff))  ) {
                     dest.append((char)(0xc0 | (0x1f & (ch >> 6))))
                         .append((char)(0x80 | (0x3f & ch)));
                 }
                 if ((ch >= 0x800) && (ch <= 0xffff)) {
                     dest.append( (char)(0xe0 | (0x0f & (ch >> 12))) )
                         .append( (char)(0x80 | (0x3f & (ch >>  6))) )
                         .append( (char)(0x80 | (0x3f & (ch)      )) );
                 }
                 */
            }
        }
    }
}

Related

  1. encodeEntities(String content)
  2. encodeEntities(String content)
  3. encodeXML(final String str)
  4. XmlEscape(String value)
  5. xmlEscape(String source)
  6. buildBuiltinXMLEntityMap()