HTML Helper : HTML Output « Servlets « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Servlets » HTML Output 




HTML Helper
   


import java.util.HashMap;
import java.util.Map;

/* -----------------------------------------------------------
 * nntp//rss - a bridge between the RSS world and NNTP clients
 * Copyright (c) 2002, 2003 Jason Brome.  All Rights Reserved.
 *
 * email: [email protected]
 * mail:  Methodize Solutions
 *        PO Box 3865
 *        Grand Central Station
 *        New York NY 10163
 
 * This file is part of nntp//rss
 *
 * Entities list from:
 * http://www.w3.org/TR/html401/sgml/entities.html
 
 * Portions © International Organization for Standardization 1986:
 * Permission to copy in any form is granted for use with
 * conforming SGML systems and applications as defined in
 * ISO 8879, provided this notice is included in all copies.
 *
 * nntp//rss 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 program; if not, write to the 
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
 * Boston, MA  02111-1307  USA
 * ----------------------------------------------------- */

/**
 @author Jason Brome <[email protected]>
 @version $Id: HTMLHelper.java,v 1.1 2003/03/24 03:13:03 jasonbrome Exp $
 */

public class HTMLHelper {

  private static Map escapeMap = new HashMap();

  static {

    //    escapeMap.put("nbsp", new Character((char) 160));
    escapeMap.put("nbsp"new Character(' '));

    escapeMap.put("iexcl"new Character((char161));
    escapeMap.put("cent"new Character((char162));
    escapeMap.put("pound"new Character((char163));
    escapeMap.put("curren"new Character((char164));
    escapeMap.put("yen"new Character((char165));
    escapeMap.put("brvbar"new Character((char166));
    escapeMap.put("sect"new Character((char167));
    escapeMap.put("uml"new Character((char168));
    escapeMap.put("copy"new Character((char169));
    escapeMap.put("ordf"new Character((char170));
    escapeMap.put("laquo"new Character((char171));
    escapeMap.put("not"new Character((char172));
    escapeMap.put("shy"new Character((char173));
    escapeMap.put("reg"new Character((char174));
    escapeMap.put("macr"new Character((char175));
    escapeMap.put("deg"new Character((char176));
    escapeMap.put("plusmn"new Character((char177));
    escapeMap.put("sup2"new Character((char178));
    escapeMap.put("sup3"new Character((char179));
    escapeMap.put("acute"new Character((char180));
    escapeMap.put("micro"new Character((char181));
    escapeMap.put("para"new Character((char182));
    escapeMap.put("middot"new Character((char183));
    escapeMap.put("cedil"new Character((char184));
    escapeMap.put("sup1"new Character((char185));
    escapeMap.put("ordm"new Character((char186));
    escapeMap.put("raquo"new Character((char187));
    escapeMap.put("frac14"new Character((char188));
    escapeMap.put("frac12"new Character((char189));
    escapeMap.put("frac34"new Character((char190));
    escapeMap.put("iquest"new Character((char191));
    escapeMap.put("Agrave"new Character((char192));
    escapeMap.put("Aacute"new Character((char193));
    escapeMap.put("Acirc"new Character((char194));
    escapeMap.put("Atilde"new Character((char195));
    escapeMap.put("Auml"new Character((char196));
    escapeMap.put("Aring"new Character((char197));
    escapeMap.put("AElig"new Character((char198));
    escapeMap.put("Ccedil"new Character((char199));
    escapeMap.put("Egrave"new Character((char200));
    escapeMap.put("Eacute"new Character((char201));
    escapeMap.put("Ecirc"new Character((char202));
    escapeMap.put("Euml"new Character((char203));
    escapeMap.put("Igrave"new Character((char204));
    escapeMap.put("Iacute"new Character((char205));
    escapeMap.put("Icirc"new Character((char206));
    escapeMap.put("Iuml"new Character((char207));
    escapeMap.put("ETH "new Character((char208));
    escapeMap.put("Ntilde"new Character((char209));
    escapeMap.put("Ograve"new Character((char210));
    escapeMap.put("Oacute"new Character((char211));
    escapeMap.put("Ocirc"new Character((char212));
    escapeMap.put("Otilde"new Character((char213));
    escapeMap.put("Ouml"new Character((char214));
    escapeMap.put("times"new Character((char215));
    escapeMap.put("Oslash"new Character((char216));
    escapeMap.put("Ugrave"new Character((char217));
    escapeMap.put("Uacute"new Character((char218));
    escapeMap.put("Ucirc"new Character((char219));
    escapeMap.put("Uuml"new Character((char220));
    escapeMap.put("Yacute"new Character((char221));
    escapeMap.put("THORN"new Character((char222));
    escapeMap.put("szlig"new Character((char223));
    escapeMap.put("agrave"new Character((char224));
    escapeMap.put("aacute"new Character((char225));
    escapeMap.put("acirc"new Character((char226));
    escapeMap.put("atilde"new Character((char227));
    escapeMap.put("auml"new Character((char228));
    escapeMap.put("aring"new Character((char229));
    escapeMap.put("aelig"new Character((char230));
    escapeMap.put("ccedil"new Character((char231));
    escapeMap.put("egrave"new Character((char232));
    escapeMap.put("eacute"new Character((char233));
    escapeMap.put("ecirc"new Character((char234));
    escapeMap.put("euml"new Character((char235));
    escapeMap.put("igrave"new Character((char236));
    escapeMap.put("iacute"new Character((char237));
    escapeMap.put("icirc"new Character((char238));
    escapeMap.put("iuml"new Character((char239));
    escapeMap.put("eth "new Character((char240));
    escapeMap.put("ntilde"new Character((char241));
    escapeMap.put("ograve"new Character((char242));
    escapeMap.put("oacute"new Character((char243));
    escapeMap.put("ocirc"new Character((char244));
    escapeMap.put("otilde"new Character((char245));
    escapeMap.put("ouml"new Character((char246));
    escapeMap.put("divide"new Character((char247));
    escapeMap.put("oslash"new Character((char248));
    escapeMap.put("ugrave"new Character((char249));
    escapeMap.put("uacute"new Character((char250));
    escapeMap.put("ucirc"new Character((char251));
    escapeMap.put("uuml"new Character((char252));
    escapeMap.put("yacute"new Character((char253));
    escapeMap.put("thorn"new Character((char254));
    escapeMap.put("yuml"new Character((char255));

    // Mathematical, Greek and Symbolic characters for HTML
    // Latin Extended-B
    escapeMap.put("fnof"new Character((char402));

    // Greek
    escapeMap.put("Alpha"new Character((char913));
    escapeMap.put("Beta"new Character((char914));
    escapeMap.put("Gamma"new Character((char915));
    escapeMap.put("Delta"new Character((char916));
    escapeMap.put("Epsilon"new Character((char917));
    escapeMap.put("Zeta"new Character((char918));
    escapeMap.put("Eta"new Character((char919));
    escapeMap.put("Theta"new Character((char920));
    escapeMap.put("Iota"new Character((char921));
    escapeMap.put("Kappa"new Character((char922));
    escapeMap.put("Lambda"new Character((char923));
    escapeMap.put("Mu"new Character((char924));
    escapeMap.put("Nu"new Character((char925));
    escapeMap.put("Xi"new Character((char926));
    escapeMap.put("Omicron"new Character((char927));
    escapeMap.put("Pi"new Character((char928));
    escapeMap.put("Rho"new Character((char929));
    escapeMap.put("Sigma"new Character((char931));
    escapeMap.put("Tau"new Character((char932));
    escapeMap.put("Upsilon"new Character((char933));
    escapeMap.put("Phi"new Character((char934));
    escapeMap.put("Chi"new Character((char935));
    escapeMap.put("Psi"new Character((char936));
    escapeMap.put("Omega"new Character((char937));
    escapeMap.put("alpha"new Character((char945));
    escapeMap.put("beta"new Character((char946));
    escapeMap.put("gamma"new Character((char947));
    escapeMap.put("delta"new Character((char948));
    escapeMap.put("epsilon"new Character((char949));
    escapeMap.put("zeta"new Character((char950));
    escapeMap.put("eta"new Character((char951));
    escapeMap.put("theta"new Character((char952));
    escapeMap.put("iota"new Character((char953));
    escapeMap.put("kappa"new Character((char954));
    escapeMap.put("lambda"new Character((char955));
    escapeMap.put("mu"new Character((char956));
    escapeMap.put("nu"new Character((char957));
    escapeMap.put("xi"new Character((char958));
    escapeMap.put("omicron"new Character((char959));
    escapeMap.put("pi"new Character((char960));
    escapeMap.put("rho"new Character((char961));
    escapeMap.put("sigmaf"new Character((char962));
    escapeMap.put("sigma"new Character((char963));
    escapeMap.put("tau"new Character((char964));
    escapeMap.put("upsilon"new Character((char965));
    escapeMap.put("phi"new Character((char966));
    escapeMap.put("chi"new Character((char967));
    escapeMap.put("psi"new Character((char968));
    escapeMap.put("omega"new Character((char969));
    escapeMap.put("thetasym"new Character((char977));
    escapeMap.put("upsih"new Character((char978));
    escapeMap.put("piv"new Character((char982));

    // General Punctuation
    escapeMap.put("bull"new Character((char8226));
    escapeMap.put("hellip"new Character((char8230));
    escapeMap.put("prime"new Character((char8242));
    escapeMap.put("Prime"new Character((char8243));
    escapeMap.put("oline"new Character((char8254));
    escapeMap.put("frasl"new Character((char8260));

    // Letterlike Symbols
    escapeMap.put("weierp"new Character((char8472));
    escapeMap.put("image"new Character((char8465));
    escapeMap.put("real"new Character((char8476));
    escapeMap.put("trade"new Character((char8482));
    escapeMap.put("alefsym"new Character((char8501));

    // Arrows
    escapeMap.put("larr"new Character((char8592));
    escapeMap.put("uarr"new Character((char8593));
    escapeMap.put("rarr"new Character((char8594));
    escapeMap.put("darr"new Character((char8595));
    escapeMap.put("harr"new Character((char8596));
    escapeMap.put("crarr"new Character((char8629));
    escapeMap.put("lArr"new Character((char8656));
    escapeMap.put("uArr"new Character((char8657));
    escapeMap.put("rArr"new Character((char8658));
    escapeMap.put("dArr"new Character((char8659));
    escapeMap.put("hArr"new Character((char8660));

    // Mathematical Operators
    escapeMap.put("forall"new Character((char8704));
    escapeMap.put("part"new Character((char8706));
    escapeMap.put("exist"new Character((char8707));
    escapeMap.put("empty"new Character((char8709));
    escapeMap.put("nabla"new Character((char8711));
    escapeMap.put("isin"new Character((char8712));
    escapeMap.put("notin"new Character((char8713));
    escapeMap.put("ni"new Character((char8715));
    escapeMap.put("prod"new Character((char8719));
    escapeMap.put("sum"new Character((char8721));
    escapeMap.put("minus"new Character((char8722));
    escapeMap.put("lowast"new Character((char8727));
    escapeMap.put("radic"new Character((char8730));
    escapeMap.put("prop"new Character((char8733));
    escapeMap.put("infin"new Character((char8734));
    escapeMap.put("ang"new Character((char8736));
    escapeMap.put("and"new Character((char8743));
    escapeMap.put("or"new Character((char8744));
    escapeMap.put("cap"new Character((char8745));
    escapeMap.put("cup"new Character((char8746));
    escapeMap.put("int"new Character((char8747));
    escapeMap.put("there4"new Character((char8756));
    escapeMap.put("sim"new Character((char8764));
    escapeMap.put("cong"new Character((char8773));
    escapeMap.put("asymp"new Character((char8776));
    escapeMap.put("ne"new Character((char8800));
    escapeMap.put("equiv"new Character((char8801));
    escapeMap.put("le"new Character((char8804));
    escapeMap.put("ge"new Character((char8805));
    escapeMap.put("sub"new Character((char8834));
    escapeMap.put("sup"new Character((char8835));
    escapeMap.put("nsub"new Character((char8836));
    escapeMap.put("sube"new Character((char8838));
    escapeMap.put("supe"new Character((char8839));
    escapeMap.put("oplus"new Character((char8853));
    escapeMap.put("otimes"new Character((char8855));
    escapeMap.put("perp"new Character((char8869));
    escapeMap.put("sdot"new Character((char8901));

    // Miscellaneous Technical
    escapeMap.put("lceil"new Character((char8968));
    escapeMap.put("rceil"new Character((char8969));
    escapeMap.put("lfloor"new Character((char8970));
    escapeMap.put("rfloor"new Character((char8971));
    escapeMap.put("lang"new Character((char9001));
    escapeMap.put("rang"new Character((char9002));

    // Geometric Shapes 
    escapeMap.put("loz"new Character((char9674));

    // Miscellaneous Symbols 
    escapeMap.put("spades"new Character((char9824));
    escapeMap.put("clubs"new Character((char9827));
    escapeMap.put("hearts"new Character((char9829));
    escapeMap.put("diams"new Character((char9830));

    // Special characters for HTML
    // C0 Controls and Basic Latin 
    escapeMap.put("quot"new Character((char34));
    escapeMap.put("amp"new Character((char38));
    escapeMap.put("lt"new Character((char60));
    escapeMap.put("gt"new Character((char62));
    escapeMap.put("apos"new Character('\''));

    // Latin Extended-A
    escapeMap.put("OElig"new Character((char338));
    escapeMap.put("oelig"new Character((char339));
    escapeMap.put("Scaron"new Character((char352));
    escapeMap.put("scaron"new Character((char353));
    escapeMap.put("Yuml"new Character((char376));

    // Spacing Modifier Letters 
    escapeMap.put("circ"new Character((char710));
    escapeMap.put("tilde"new Character((char732));

    // General Punctuation 
    escapeMap.put("ensp"new Character((char8194));
    escapeMap.put("emsp"new Character((char8195));
    escapeMap.put("thinsp"new Character((char8201));
    escapeMap.put("zwnj"new Character((char8204));
    escapeMap.put("zwj"new Character((char8205));
    escapeMap.put("lrm"new Character((char8206));
    escapeMap.put("rlm"new Character((char8207));
    escapeMap.put("ndash"new Character((char8211));
    escapeMap.put("mdash"new Character((char8212));
    escapeMap.put("lsquo"new Character((char8216));
    escapeMap.put("rsquo"new Character((char8217));
    escapeMap.put("sbquo"new Character((char8218));
    escapeMap.put("ldquo"new Character((char8220));
    escapeMap.put("rdquo"new Character((char8221));
    escapeMap.put("bdquo"new Character((char8222));
    escapeMap.put("dagger"new Character((char8224));
    escapeMap.put("Dagger"new Character((char8225));
    escapeMap.put("permil"new Character((char8240));
    escapeMap.put("lsaquo"new Character((char8249));
    escapeMap.put("rsaquo"new Character((char8250));
    escapeMap.put("euro"new Character((char8364));

  }

  public static String unescapeString(String value) {
    StringBuffer unescapedString = new StringBuffer();
    StringBuffer charBuf = null;

    for (int pos = 0; pos < value.length(); pos++) {
      char c = value.charAt(pos);

      if (c == '&') { // Process reference...
        c = value.charAt(++pos);
        boolean numeric = false;
        if (c == '#') {
          numeric = true;
          c = value.charAt(++pos);
        }

        if (charBuf == null) {
          charBuf = new StringBuffer(32);
        else {
          charBuf.setLength(0);
        }

        while (c != ';' && pos < value.length() 1) {
          charBuf.append(c);
          c = value.charAt(++pos);
        }

        if (numeric) {
          try {
            c = (charInteger.parseInt(charBuf.toString()16);
          catch (NumberFormatException nfe) {
            // If we can't process it, just write out the text...
            unescapedString.append("&#").append(
              charBuf.toString()).append(
              ';');
          }
        else {
          Character unescapedVer =
            (CharacterescapeMap.get(charBuf.toString());
          if (unescapedVer != null) {
            unescapedString.append(unescapedVer);
          else {
            unescapedString.append("&").append(
              charBuf.toString()).append(
              ';');
          }
        }

      else {
        unescapedString.append(c);
      }

    }

    return unescapedString.toString();
  }

  public static String escapeString(String value) {
    StringBuffer escapedString = new StringBuffer();
    for (int charCount = 0; charCount < value.length(); charCount++) {
      char c = value.charAt(charCount);
      switch (c) {
        case '&' :
          escapedString.append("&amp;");
          break;
        case '<' :
          escapedString.append("&lt;");
          break;
        case '>' :
          escapedString.append("&gt;");
          break;
        case '\"' :
          escapedString.append("&quot;");
          break;
        case '\'' :
          escapedString.append("&#39;");
          break;
        default :
          escapedString.append(c);
      }
    }
    return escapedString.toString();
  }

  public static String stripCRLF(String value) {
    StringBuffer strippedString = new StringBuffer();
    for (int i = 0; i < value.length(); i++) {
      char c = value.charAt(i);
      if (!(c == '\n' || c == '\r')) {
        strippedString.append(c);
      }
    }
    return strippedString.toString();
  }
}

   
    
    
  














Related examples in the same category
1.Servlet Output HTML Demo
2.Servlet Display Static HTML
3.Prints a conversion table of miles per gallon to kilometers per liter
4.Servlet: Print Table
5.Html utilities
6.Html Parse Servlet
7.Escape and unescape string
8.Escapes newlines, tabs, backslashes, and quotes in the specified string
9.Web Calendar
10.Escape HTML
11.Convert HTML to text
12.Text To HTML
13.Unescape HTML
14.Java object representations of the HTML table structure
15.Entity Decoder
16.Format a color to HTML RGB color format (e.g. #FF0000 for Color.red)
17.Definitions of HTML character entities and conversions between unicode characters and HTML character entities
18.Encode special characters and do formatting for HTML output
19.HTML color names
20.Utility methods for dealing with HTML
21.Filter the specified message string for characters that are sensitive in HTML
22.A collection of all character entites defined in the HTML4 standard.
23.Decode an HTML color string like '#F567BA;' into a Color
24.Normalize Post Data
25.Get HTML Color String from Java Color object
26.HTML Decoder
27.HTML Parser
28.HTML color and Java Color
29.HTML form Utilites
30.Html Dimensions
31.break Lines with HTML
32.insert HTML block dynamically
33.Convert an integer to an HTML RGB value
34.Convert to HTML string
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.