%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
com.ozacc.mail.fetch.impl.sk_jp.text.EntityRefEncoder |
|
|
1 | /* |
|
2 | * @(#) $Id: EntityRefEncoder.java,v 1.1.2.1 2005/01/18 07:20:43 otsuka Exp $ |
|
3 | * $Revision: 1.1.2.1 $ |
|
4 | * Copyright (c) 2000 Shin Kinoshita All Rights Reserved. |
|
5 | */ |
|
6 | ||
7 | package com.ozacc.mail.fetch.impl.sk_jp.text; |
|
8 | ||
9 | import java.io.IOException; |
|
10 | import java.io.Reader; |
|
11 | import java.io.Writer; |
|
12 | ||
13 | /** |
|
14 | * <>&"¤?&lt;&gt;&amp;&quot;¤Ë |
|
15 | * ÊÑ´¹¤¹¤?Translator¤Ç¤¹¡£ |
|
16 | * ¥?¥¨¥ó¥È¥é¥ó¥È¤Ê¤Î¤Ç¡¢Ä̾?¤ÏINSTANCE/CANONICAL_INSTANCE¤òÍѤ¤¤?¤Ð¤è¤¤¤Ç¤¹¡£ |
|
17 | * ¤Þ¤¿String¥ª¥Ö¥¸¥§¥¯¥È¤ËÂФ·¤Æ¤Ïencode()¥á¥½¥Ã¥É¤¬»ÈÍѤǤ¤Þ¤¹¡£ |
|
18 | * @version $Revision: 1.1.2.1 $ $Date: 2005/01/18 07:20:43 $ |
|
19 | * @author Shin |
|
20 | */ |
|
21 | 0 | public class EntityRefEncoder implements Translator { |
22 | 0 | public static final EntityRefEncoder CANONICAL_INSTANCE = |
23 | 0 | new EntityRefEncoder(true); |
24 | 0 | public static final EntityRefEncoder INSTANCE = |
25 | 0 | new EntityRefEncoder(false); |
26 | ||
27 | public EntityRefEncoder() { |
|
28 | 0 | this(false); |
29 | 0 | } |
30 | 0 | public EntityRefEncoder(boolean canonical) { |
31 | 0 | setCanonical(canonical); |
32 | 0 | } |
33 | ||
34 | private boolean canonicalStatus; |
|
35 | private void setCanonical(boolean canonical) { |
|
36 | // public void setCanonical(boolean canonical) { |
|
37 | 0 | this.canonicalStatus = canonical; |
38 | 0 | } |
39 | ||
40 | /** |
|
41 | * ʸ»ú¥¹¥È¥ê¡¼¥à¤«¤éÆ?ÎϤ·¤¿Ê¸»úÎó¤òǤ°Õ¤ÎÊÑ´¹¤? |
|
42 | * ¹Ô¤¤¤Ê¤¬¤é½ÐÎÏ¥¹¥È¥ê¡¼¥à¤Ë½ñ¤½Ð¤·¤Þ¤¹¡£ |
|
43 | * <p> |
|
44 | * </p> |
|
45 | */ |
|
46 | public void translate(Reader r, Writer w) throws IOException { |
|
47 | int c; |
|
48 | 0 | while ((c = r.read()) != -1) { |
49 | 0 | translate((char)c, w, canonicalStatus); |
50 | } |
|
51 | 0 | w.flush(); |
52 | 0 | } |
53 | ||
54 | public String translate(String source) { |
|
55 | 0 | return encode(source); |
56 | } |
|
57 | /* |
|
58 | public static String encode(String s) { |
|
59 | if (s == null) return ""; |
|
60 | ||
61 | StringWriter w = new StringWriter(); |
|
62 | try { |
|
63 | EntityRefEncoder.INSTANCE.translate(new StringReader(s), w); |
|
64 | } catch (IOException e) { |
|
65 | throw new RuntimeException(); |
|
66 | } |
|
67 | return w.toString(); |
|
68 | } |
|
69 | */ |
|
70 | ||
71 | /** |
|
72 | * ʸ»úÎó¤Î¼ÂÂλ²¾È²½¤ò¹Ô¤¤¤Þ¤¹¡£ |
|
73 | * @param s ÂоÝʸ»úÎ? |
|
74 | * @return ÊÑ´¹¸åʸ»úÎ? |
|
75 | */ |
|
76 | // »÷¤¿¤è¤¦¤Ê¥³¡¼¥É¤ò½ñ¤¤¿¤¯¤Ï̵¤¤¤¬¹â®²½¤Î°Ù¡Ä |
|
77 | public static String encode(String s) { |
|
78 | 0 | if (s == null) return ""; |
79 | ||
80 | 0 | int len = s.length(); |
81 | 0 | StringBuffer buf = new StringBuffer(len + 128); |
82 | char c; |
|
83 | ||
84 | 0 | for (int i = 0; i < len; i++) { |
85 | 0 | c = s.charAt(i); |
86 | 0 | switch (c) { |
87 | 0 | case '<': buf.append("<"); break; |
88 | 0 | case '>': buf.append(">"); break; |
89 | 0 | case '&': buf.append("&"); break; |
90 | 0 | case '"': buf.append("""); break; |
91 | default: |
|
92 | 0 | buf.append(c); |
93 | } |
|
94 | } |
|
95 | 0 | return new String(buf); |
96 | } |
|
97 | ||
98 | /** |
|
99 | * ÆÃÄê¤Îʸ»ú¤ò¼ÂÂλ²¾È¤ËÊÑ´¹¤·¤Æ½ñ¤½Ð¤·¤Þ¤¹¡£ |
|
100 | */ |
|
101 | public static void translate(char c, Writer w, boolean canonical) |
|
102 | throws IOException { |
|
103 | 0 | switch (c) { |
104 | 0 | case '<': w.write("<"); break; |
105 | 0 | case '>': w.write(">"); break; |
106 | 0 | case '&': w.write("&"); break; |
107 | 0 | case '"': w.write("""); break; |
108 | case '\r': |
|
109 | case '\n': |
|
110 | 0 | if (canonical) { |
111 | 0 | w.write("&#"); |
112 | 0 | w.write(Integer.toString(c)); |
113 | 0 | w.write(';'); |
114 | } else { |
|
115 | 0 | w.write(c); |
116 | } |
|
117 | 0 | break; |
118 | default: |
|
119 | 0 | w.write(c); |
120 | } |
|
121 | 0 | } |
122 | ||
123 | /** |
|
124 | * ¼ÂÂλ²¾ÈÊÑ´¹¤µ¤?¤Æ¤¤¤?ʸ»úÎó¤ò¤â¤È¤ËÌᤷ¤Þ¤¹¡£ |
|
125 | * @param s ÂоÝʸ»úÎ? |
|
126 | * @return ÊÑ´¹¸åʸ»úÎ? |
|
127 | */ |
|
128 | public static String decode(String s) { |
|
129 | 0 | if (s == null) return ""; |
130 | ||
131 | 0 | int len = s.length(); |
132 | 0 | StringBuffer buf = new StringBuffer(len); |
133 | char c; |
|
134 | ||
135 | 0 | for (int i = 0; i < len; i++) { |
136 | 0 | c = s.charAt(i); |
137 | 0 | if (c != '&' || i > len - 4) { |
138 | 0 | buf.append(c); |
139 | 0 | continue; |
140 | } |
|
141 | 0 | if ((s.charAt(i + 2) == 't' || s.charAt(i + 2) == 'T') && |
142 | 0 | s.charAt(i + 3) == ';') { |
143 | 0 | switch (s.charAt(i + 1)) { |
144 | case 'l': |
|
145 | case 'L': |
|
146 | 0 | buf.append('<'); |
147 | 0 | i += 3; |
148 | 0 | continue; |
149 | case 'g': |
|
150 | case 'G': |
|
151 | 0 | buf.append('>'); |
152 | 0 | i += 3; |
153 | 0 | continue; |
154 | } |
|
155 | 0 | } else if (i < len - 4 && |
156 | 0 | (s.charAt(i + 1) == 'a' || s.charAt(i + 1) == 'A') && |
157 | 0 | (s.charAt(i + 2) == 'm' || s.charAt(i + 2) == 'M') && |
158 | 0 | (s.charAt(i + 3) == 'p' || s.charAt(i + 3) == 'P') && |
159 | 0 | s.charAt(i + 4) == ';') { |
160 | 0 | buf.append('&'); |
161 | 0 | i += 4; |
162 | 0 | continue; |
163 | 0 | } else if (i < len - 5 && |
164 | 0 | (s.charAt(i + 1) == 'q' || s.charAt(i + 1) == 'Q') && |
165 | 0 | (s.charAt(i + 2) == 'u' || s.charAt(i + 2) == 'U') && |
166 | 0 | (s.charAt(i + 3) == 'o' || s.charAt(i + 3) == 'O') && |
167 | 0 | (s.charAt(i + 4) == 't' || s.charAt(i + 4) == 'T') && |
168 | 0 | s.charAt(i + 5) == ';') { |
169 | 0 | buf.append('"'); |
170 | 0 | i += 5; |
171 | 0 | continue; |
172 | } |
|
173 | 0 | buf.append(c); |
174 | } |
|
175 | 0 | return new String(buf); |
176 | } |
|
177 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |