Java XML Encode xmlEncodeAttr(String src)

Here you can find the source of xmlEncodeAttr(String src)

Description

xml Encode Attr

License

Open Source License

Declaration

public static String xmlEncodeAttr(String src) 

Method Source Code

//package com.java2s;
/*/* w  w  w .  j  a  va  2s .  c  o m*/
 * ====================================================================
 * Copyright (c) 2004-2012 TMate Software Ltd.  All rights reserved.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at http://svnkit.com/license.html
 * If newer versions of this license are posted there, you may use a
 * newer version instead, at your option.
 * ====================================================================
 */

public class Main {
    public static String xmlEncodeAttr(String src) {
        StringBuffer sb = new StringBuffer(src.length());
        for (int i = 0; i < src.length(); i++) {
            char ch = src.charAt(i);
            switch (ch) {
            case '&':
                if (sb == null) {
                    sb = createStringBuffer(src, i);
                }
                sb.append("&amp;");
                break;
            case '<':
                if (sb == null) {
                    sb = createStringBuffer(src, i);
                }
                sb.append("&lt;");
                break;
            case '>':
                if (sb == null) {
                    sb = createStringBuffer(src, i);
                }
                sb.append("&gt;");
                break;
            case '\'':
                if (sb == null) {
                    sb = createStringBuffer(src, i);
                }
                sb.append("&apos;");
                break;
            case '\"':
                if (sb == null) {
                    sb = createStringBuffer(src, i);
                }
                sb.append("&quot;");
                break;
            case '\r':
                if (sb == null) {
                    sb = createStringBuffer(src, i);
                }
                sb.append("&#13;");
                break;
            case '\n':
                if (sb == null) {
                    sb = createStringBuffer(src, i);
                }
                sb.append("&#10;");
                break;
            case '\t':
                if (sb == null) {
                    sb = createStringBuffer(src, i);
                }
                sb.append("&#9;");
                break;
            default:
                if (sb != null) {
                    sb.append(ch);
                }
            }
        }
        return sb != null ? sb.toString() : src;
    }

    private static StringBuffer createStringBuffer(String src, int length) {
        StringBuffer sb = new StringBuffer(src.length());
        sb.append(src.toCharArray(), 0, length);
        return sb;
    }
}

Related

  1. xmlEncode(String text)
  2. xmlEncode(String text)
  3. xmlEncode(String text)
  4. xmlEncode(String text, String invalidCharReplacement)
  5. XMLEncode(String value)
  6. xmlEncodeCDATA(String src)