Escape html entities. : Document HTML « Development Class « Java






Escape html entities.

   
/*
 * Copyright 2008-2010 the T2 Project ant the Others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package org.t2framework.commons.util;


/**
 * <#if locale="en">
 * <p>
 * Escape html entities. You can plug in HtmlEscapeStrategy and change behavior.
 * 
 * </p>
 * <#else>
 * <p>
 * 
 * </p>
 * </#if>
 * 
 * @author shot
 */
public class HtmlEscapeUtil {
  private static HtmlEscapeStrategy escapeStrategy = new DefaultHtmlEscapeStrategy();

  public static HtmlEscapeStrategy getHtmlEscapeStrategy() {
    return escapeStrategy;
  }

  public static void setHtmlEscapeStrategy(
      HtmlEscapeStrategy htmlEscapeStrategy) {
    escapeStrategy = htmlEscapeStrategy;
  }

  public static String escape(final String s) {
    return escape(s, true, true);
  }

  public static String escape(final String s, final boolean quote,
      final boolean amp) {
    return escapeStrategy.escape(s, quote, amp);
  }

  public static interface HtmlEscapeStrategy {

    public String escape(final String s, final boolean quote,
        final boolean amp);

  }

  public static abstract class AbstractHtmlEscapeStrategy implements
      HtmlEscapeStrategy {

    public String escape(final String s, final boolean quote,
        final boolean amp) {
      char[] chars = s.toCharArray();
      StringBuffer sb = new StringBuffer(s.length() + 64);
      for (int i = 0; i < chars.length; i++) {
        char c = chars[i];
        escapeEach(sb, c, quote, amp);
      }
      return new String(sb);
    }

    protected abstract void escapeEach(final StringBuffer buf,
        final char c, final boolean quote, final boolean amp);
  }

  public static class DefaultHtmlEscapeStrategy extends
      AbstractHtmlEscapeStrategy {

    protected void escapeEach(final StringBuffer sb, final char c,
        final boolean quote, final boolean amp) {
      if ((int) c == '\u00A0') {
        sb.append("&nbsp;");
      } else if (c == '<') {
        sb.append("&lt;");
      } else if (c == '>') {
        sb.append("&gt;");
      } else if (amp && c == '&') {
        sb.append("&amp;");
      } else if (c == '"') {
        sb.append("&quot;");
      } else if (quote && c == '\'') {
        sb.append("&#39;");
      } else if ((int) c == '\u00A5') {
        sb.append("&yen;");
      } else {
        sb.append(c);
      }
    }

  }

  public static class JapaneseHtmlEscapeStrategy extends
      AbstractHtmlEscapeStrategy {

    protected void escapeEach(final StringBuffer sb, final char c,
        final boolean quote, final boolean amp) {
      if ((int) c == '\u00A0') {
        sb.append("&nbsp;");
      } else if (c == '<') {
        sb.append("&lt;");
      } else if (c == '>') {
        sb.append("&gt;");
      } else if (amp && c == '&') {
        sb.append("&amp;");
      } else if (c == '"') {
        sb.append("&quot;");
      } else if (quote && c == '\'') {
        sb.append("&#39;");
      } else if ((int) c == '\u00A5' || (int) c == '\u005C\') {
        sb.append("&yen;");
      } else {
        sb.append(c);
      }
    }

  }
}
---------------------
/*
 * Copyright 2008-2009 the T2 Project ant the Others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.t2framework.commons.util;

import org.t2framework.commons.util.HtmlEscapeUtil;
import org.t2framework.commons.util.HtmlEscapeUtil.HtmlEscapeStrategy;

import junit.framework.TestCase;

public class HtmlEscapeUtilTest extends TestCase {

  public void testescape_amp() throws Exception {
    assertEquals("&nbsp;", HtmlEscapeUtil.escape("&nbsp;", false, false));
    assertEquals("&amp;nbsp;", HtmlEscapeUtil.escape("&nbsp;", true, true));
  }

  public void testescape_quote() throws Exception {
    assertEquals("'hoge'", HtmlEscapeUtil.escape("'hoge'", false, false));
    assertEquals("&#39;hoge&#39;", HtmlEscapeUtil.escape("'hoge'", true,
        true));
  }

  public void testescape_lt() throws Exception {
    assertEquals("&lt;&lt;", HtmlEscapeUtil.escape("<<", false, false));
    assertEquals("&lt;&lt;", HtmlEscapeUtil.escape("<<", true, true));
  }

  public void testescape_gt() throws Exception {
    assertEquals("&gt;&gt;", HtmlEscapeUtil.escape(">>", false, false));
    assertEquals("&gt;&gt;", HtmlEscapeUtil.escape(">>", true, true));
  }

  public void testescape_00A5_withDefaultStrategy() throws Exception {
    char c = '\u00a5';
    assertEquals("&yen;", HtmlEscapeUtil.escape(Character.toString(c),
        false, false));
    assertEquals("&yen;", HtmlEscapeUtil.escape(Character.toString(c),
        true, true));
    c = '\u005c\';
    assertNotSame("&yen;", HtmlEscapeUtil.escape(Character.toString(c),
        false, false));
    assertNotSame("&yen;", HtmlEscapeUtil.escape(Character.toString(c),
        true, true));
    assertEquals("\\", HtmlEscapeUtil.escape(Character.toString(c), false,
        false));
    assertEquals("\\", HtmlEscapeUtil.escape(Character.toString(c), true,
        true));
  }

  public void testescape_yen() throws Exception {
    HtmlEscapeStrategy orgStrategy = HtmlEscapeUtil.getHtmlEscapeStrategy();
    HtmlEscapeUtil
        .setHtmlEscapeStrategy(new HtmlEscapeUtil.JapaneseHtmlEscapeStrategy());
    assertEquals("&yen;&yen;", HtmlEscapeUtil.escape("\\\\", false, false));
    assertEquals("&yen;&yen;", HtmlEscapeUtil.escape("\\\\", true, true));
    HtmlEscapeUtil.setHtmlEscapeStrategy(orgStrategy);
  }

}

   
    
    
  








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.HTML Encode
13.XMLWriter is a generic class that provides common behavior to writers of a tagged language such as XML, WordML and HTML.
14.Html Encoder
15.Remove Comment