Java HTML Encode htmlEncode(String text)

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

Description

html Encode

License

Open Source License

Declaration

public static String htmlEncode(String text) 

Method Source Code

//package com.java2s;
/*/*from  ww  w.j  av a  2  s. c om*/
 * JasperReports - Free Java Reporting Library.
 * Copyright (C) 2001 - 2014 TIBCO Software Inc. All rights reserved.
 * http://www.jaspersoft.com
 *
 * Unless you have purchased a commercial license agreement from Jaspersoft,
 * the following license terms apply:
 *
 * This program is part of JasperReports.
 *
 * JasperReports is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JasperReports 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     *
     */
    public static String htmlEncode(String text) {
        if (text == null || text.length() == 0) {
            return text;
        }

        int length = text.length();
        StringBuffer ret = new StringBuffer(length * 12 / 10);

        boolean isEncodeSpace = true;
        int last = 0;
        for (int i = 0; i < length; i++) {
            char c = text.charAt(i);
            switch (c) {
            case ' ':
                if (isEncodeSpace) {
                    if (last < i) {
                        ret.append(text.substring(last, i));
                    }
                    last = i + 1;

                    ret.append("&nbsp;");
                    isEncodeSpace = false;
                } else {
                    isEncodeSpace = true;
                }
                break;
            case '&':
                if (last < i) {
                    ret.append(text.substring(last, i));
                }
                last = i + 1;

                ret.append("&amp;");
                isEncodeSpace = false;
                break;
            case '>':
                if (last < i) {
                    ret.append(text.substring(last, i));
                }
                last = i + 1;

                ret.append("&gt;");
                isEncodeSpace = false;
                break;
            case '<':
                if (last < i) {
                    ret.append(text.substring(last, i));
                }
                last = i + 1;

                ret.append("&lt;");
                isEncodeSpace = false;
                break;
            case '\"':
                if (last < i) {
                    ret.append(text.substring(last, i));
                }
                last = i + 1;

                ret.append("&quot;");
                isEncodeSpace = false;
                break;
            // it does not work in IE
            //               case '\'' :
            //                  if (last < i)
            //                  {
            //                     ret.append(text.substring(last, i));
            //                  }
            //                  last = i + 1;
            //                  
            //                  ret.append("&apos;");
            //                  isEncodeSpace = false;
            //                  break;
            case '\n':
                if (last < i) {
                    ret.append(text.substring(last, i));
                }
                last = i + 1;

                ret.append("<br/>");
                isEncodeSpace = false;
                break;

            default:
                isEncodeSpace = false;
                break;
            }
        }

        if (last < length) {
            ret.append(text.substring(last));
        }

        return ret.toString();
    }
}

Related

  1. htmlEncode(String string)
  2. HTMLEncode(String string)
  3. HtmlEncode(String strInput)
  4. htmlEncode(String strSrc)
  5. htmlEncode(String strSrc)
  6. htmlEncode(String txt)
  7. htmlEncode(String txt)
  8. htmlEncoded(String text)
  9. htmlEncoder(String content)