HTML Encode : Document HTML « Development Class « 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 » Development Class » Document HTML 




HTML Encode
   
/*
 * HTMLEncode.java
 *
 * Created on 2007-9-15, 21:24:34
 *
Copyright (C) 2006,2007  Yong Li. All rights reserved.
 
This library 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 2.1 of the License, or (at your option) any later version.
 
This library 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 this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

//package com.teesoft.util;

/**
 *
 @author wind
 */

import java.util.Hashtable;

public class HTMLEncode {

  private HTMLEncode() {
  }

  protected static synchronized void buildEntityTables() {
    entityTableEncode = new Hashtable(ENTITIES.length);
    for (int i = 0; i < ENTITIES.length; i += 2) {
      if (!entityTableEncode.containsKey(ENTITIES[i])) {
        entityTableEncode.put(ENTITIES[i], ENTITIES[i + 1]);
      }
    }
  }

  public static final String encode(String s) {
    return encode(s, "\n<br>");
  }

  public static final String encode(String s, String cr) {
    if (entityTableEncode == null) {
      buildEntityTables();
    }
    if (s == null) {
      return "";
    }
    StringBuffer sb = new StringBuffer(s.length() 2);
    for (int i = 0; i < s.length(); i++) {
      char ch = s.charAt(i);
      if (ch >= '?' && ch <= 'Z' || ch >= 'a' && ch <= 'z' || ch == ' ') {
        sb.append(ch);
        continue;
      }
      if (ch == '\n') {
        sb.append(cr);
        continue;
      }
      String chEnc = encodeSingleChar(String.valueOf(ch));
      if (chEnc != null) {
        sb.append(chEnc);
      else {
        sb.append("&#");
        sb.append((new Integer(ch)).toString());
        sb.append(';');
      }
    }

    return sb.toString();
  }

  public static final String escape(String s) {
    if (entityTableEncode == null) {
      buildEntityTables();
    }
    if (s == null) {
      return "";
    }
    StringBuffer sb = new StringBuffer(s.length() 2);
    for (int i = 0; i < s.length(); i++) {
      char ch = s.charAt(i);
      if (ch >= '?' && ch <= 'Z' || ch >= 'a' && ch <= 'z' || ch == ' ') {
        sb.append(ch);
        continue;
      }
      String chEnc = encodeSingleChar(String.valueOf(ch));
      if (chEnc != null) {
        sb.append(chEnc);
      else {
        sb.append(ch);
      }
    }

    return sb.toString();
  }

  private static String encodeSingleChar(String ch) {
    return (StringentityTableEncode.get(ch);
  }

  private static final String[] ENTITIES = ">""&gt;""<""&lt;""[",
      "&#91;<b>""]""</b>&#93;""&""&amp;""\"""&quot;""'",
      "&#039;""\\""&#092;""\251""&copy;""\256""&reg;" };
  private static Hashtable entityTableEncode = null;

}

   
    
    
  














Related examples in the same category
1.HTMLDocument: Element Iterator Example
2.HTMLEditorKit DemoHTMLEditorKit Demo
3.SimpleAttributeSet ExampleSimpleAttributeSet Example
4.Text Tab SampleText Tab Sample
5.Styled DocumentStyled Document
6.Html utils for working with tag's names and attributes.
7.Escape HTML
8.Escape HTML
9.Encode HTML
10.Replace all the occurences of HTML escape strings with the respective characters.
11.HTML Rewriter
12.XMLWriter is a generic class that provides common behavior to writers of a tagged language such as XML, WordML and HTML.
13.Escape html entities.
14.Html Encoder
15.Remove Comment
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.