Coverage Report - org.seasar.teeda.core.util.HTMLEncodeUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
HTMLEncodeUtil
75%
6/8
N/A
2.5
HTMLEncodeUtil$AbstractHtmlEncodeStrategy
100%
7/7
100%
2/2
2.5
HTMLEncodeUtil$DefaultHtmlEncodeStrategy
94%
16/17
94%
17/18
2.5
HTMLEncodeUtil$HtmlEncodeStrategy
N/A
N/A
2.5
HTMLEncodeUtil$JapaneseHtmlEncodeStrategy
59%
10/17
60%
12/20
2.5
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.teeda.core.util;
 17  
 
 18  
 /**
 19  
  * @author yone
 20  
  * @author shot
 21  
  */
 22  
 public final class HTMLEncodeUtil {
 23  
 
 24  0
     private HTMLEncodeUtil() {
 25  0
     }
 26  
 
 27  1
     private static HtmlEncodeStrategy encodeStrategy = new DefaultHtmlEncodeStrategy();
 28  
 
 29  
     public static HtmlEncodeStrategy getHtmlEncodeStrategy() {
 30  1
         return encodeStrategy;
 31  
     }
 32  
 
 33  
     public static void setHtmlEncodeStrategy(
 34  
             HtmlEncodeStrategy htmlEncodeStrategy) {
 35  2
         encodeStrategy = htmlEncodeStrategy;
 36  2
     }
 37  
 
 38  
     public static String encodeAll(final String s) {
 39  62
         return encode(s, true, true);
 40  
     }
 41  
 
 42  
     public static String encode(final String s, final boolean quote,
 43  
             final boolean amp) {
 44  1846
         return encodeStrategy.encode(s, quote, amp);
 45  
     }
 46  
 
 47  
     public static interface HtmlEncodeStrategy {
 48  
 
 49  
         public String encode(final String s, final boolean quote,
 50  
                 final boolean amp);
 51  
 
 52  
     }
 53  
 
 54  2
     public static abstract class AbstractHtmlEncodeStrategy implements
 55  
             HtmlEncodeStrategy {
 56  
 
 57  
         public String encode(final String s, final boolean quote,
 58  
                 final boolean amp) {
 59  1846
             char[] chars = s.toCharArray();
 60  1846
             StringBuffer sb = new StringBuffer(s.length() + 64);
 61  10157
             for (int i = 0; i < chars.length; i++) {
 62  8311
                 char c = chars[i];
 63  8311
                 encodeEach(sb, c, quote, amp);
 64  
             }
 65  1846
             return new String(sb);
 66  
         }
 67  
 
 68  
         protected abstract void encodeEach(final StringBuffer buf,
 69  
                 final char c, final boolean quote, final boolean amp);
 70  
     }
 71  
 
 72  1
     public static class DefaultHtmlEncodeStrategy extends
 73  
             AbstractHtmlEncodeStrategy {
 74  
 
 75  
         protected void encodeEach(final StringBuffer sb, final char c,
 76  
                 final boolean quote, final boolean amp) {
 77  8307
             if ((int) c == '\u00A0') {
 78  0
                 sb.append("&nbsp;");
 79  8307
             } else if (c == '<') {
 80  14
                 sb.append("&lt;");
 81  8293
             } else if (c == '>') {
 82  14
                 sb.append("&gt;");
 83  8279
             } else if (amp && c == '&') {
 84  4
                 sb.append("&amp;");
 85  8275
             } else if (c == '"') {
 86  1
                 sb.append("&quot;");
 87  8274
             } else if (quote && c == '\'') {
 88  8
                 sb.append("&#39;");
 89  8266
             } else if ((int) c == '\u00A5') {
 90  2
                 sb.append("&yen;");
 91  
             } else {
 92  8264
                 sb.append(c);
 93  
             }
 94  8307
         }
 95  
 
 96  
     }
 97  
 
 98  1
     public static class JapaneseHtmlEncodeStrategy extends
 99  
             AbstractHtmlEncodeStrategy {
 100  
 
 101  
         protected void encodeEach(final StringBuffer sb, final char c,
 102  
                 final boolean quote, final boolean amp) {
 103  4
             if ((int) c == '\u00A0') {
 104  0
                 sb.append("&nbsp;");
 105  4
             } else if (c == '<') {
 106  0
                 sb.append("&lt;");
 107  4
             } else if (c == '>') {
 108  0
                 sb.append("&gt;");
 109  4
             } else if (amp && c == '&') {
 110  0
                 sb.append("&amp;");
 111  4
             } else if (c == '"') {
 112  0
                 sb.append("&quot;");
 113  4
             } else if (quote && c == '\'') {
 114  0
                 sb.append("&#39;");
 115  4
             } else if ((int) c == '\u00A5' || (int) c == '\u005C\') {
 116  4
                 sb.append("&yen;");
 117  
             } else {
 118  0
                 sb.append(c);
 119  
             }
 120  4
         }
 121  
 
 122  
     }
 123  
 }