Java HTML Encode htmlEncoded(String text)

Here you can find the source of htmlEncoded(String text)

Description

Substitutes conflicting characters in text with html masked expressions.

License

Open Source License

Parameter

Parameter Description
text a parameter

Declaration

public static String htmlEncoded(String text) 

Method Source Code

//package com.java2s;
/*//w ww .ja v  a  2 s  .c o m
 *  Util in org.jpws.front.util
 *  file: Util.java
 * 
 *  Project Jpws-Front
 *  @author Wolfgang Keller
 *  Created 28.09.2004
 *  Version
 * 
 *  Copyright (c) 2005 by Wolfgang Keller, Munich, Germany
 * 
 This program is not freeware software but copyright protected to the author(s)
 stated above. However, you can use, redistribute and/or modify it under the terms 
 of the GNU General Public License as published by the Free Software Foundation, 
 version 2 of the License.
    
 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, or go to
 http://www.gnu.org/copyleft/gpl.html.
 */

public class Main {
    /** Substitutes conflicting characters in <code>text</code> with html
     *  masked expressions. The returned string is displayable in html interpreting
     *  components.
     * 
     * @param text
     * @return
     */
    public static String htmlEncoded(String text) {
        StringBuffer b;
        int i, len;
        char c;

        len = text.length();
        b = new StringBuffer(len * 3);
        for (i = 0; i < len; i++) {
            c = text.charAt(i);
            switch (c) {
            case '<':
                b.append("&lt;");
                break;
            case '>':
                b.append("&gt;");
                break;
            case '&':
                b.append("&amp;");
                break;
            case '"':
                b.append("&quot;");
                break;
            default:
                b.append(c);
            }
        }
        return b.toString();
    }
}

Related

  1. htmlEncode(String strSrc)
  2. htmlEncode(String strSrc)
  3. htmlEncode(String text)
  4. htmlEncode(String txt)
  5. htmlEncode(String txt)
  6. htmlEncoder(String content)
  7. htmlEncoding(String str)
  8. htmlEntityEncode(String s)
  9. htmlize(CharSequence q)