Java tutorial
/* * 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(" "); } else if (c == '<') { sb.append("<"); } else if (c == '>') { sb.append(">"); } else if (amp && c == '&') { sb.append("&"); } else if (c == '"') { sb.append("""); } else if (quote && c == '\'') { sb.append("'"); } else if ((int) c == '\u00A5') { sb.append("¥"); } 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(" "); } else if (c == '<') { sb.append("<"); } else if (c == '>') { sb.append(">"); } else if (amp && c == '&') { sb.append("&"); } else if (c == '"') { sb.append("""); } else if (quote && c == '\'') { sb.append("'"); } else if ((int) c == '\u00A5' || (int) c == '\u005C\') { sb.append("¥"); } 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(" ", HtmlEscapeUtil.escape(" ", false, false)); assertEquals("&nbsp;", HtmlEscapeUtil.escape(" ", true, true)); } public void testescape_quote() throws Exception { assertEquals("'hoge'", HtmlEscapeUtil.escape("'hoge'", false, false)); assertEquals("'hoge'", HtmlEscapeUtil.escape("'hoge'", true, true)); } public void testescape_lt() throws Exception { assertEquals("<<", HtmlEscapeUtil.escape("<<", false, false)); assertEquals("<<", HtmlEscapeUtil.escape("<<", true, true)); } public void testescape_gt() throws Exception { assertEquals(">>", HtmlEscapeUtil.escape(">>", false, false)); assertEquals(">>", HtmlEscapeUtil.escape(">>", true, true)); } public void testescape_00A5_withDefaultStrategy() throws Exception { char c = '\u00a5'; assertEquals("¥", HtmlEscapeUtil.escape(Character.toString(c), false, false)); assertEquals("¥", HtmlEscapeUtil.escape(Character.toString(c), true, true)); c = '\u005c\'; assertNotSame("¥", HtmlEscapeUtil.escape(Character.toString(c), false, false)); assertNotSame("¥", 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("¥¥", HtmlEscapeUtil.escape("\\\\", false, false)); assertEquals("¥¥", HtmlEscapeUtil.escape("\\\\", true, true)); HtmlEscapeUtil.setHtmlEscapeStrategy(orgStrategy); } }