1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.component.html.ext;
20
21 import javax.faces.FacesException;
22 import java.lang.StringBuffer;
23
24 class _SubIdConverter
25 {
26 private static final String HEX_CHARSET = "0123456789ABCDEF";
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public static String encode(final String string)
46 {
47 StringBuffer sb = null;
48 String app;
49 char c;
50 boolean endLoop = false;
51 for (int i = 0; i < string.length (); ++i)
52 {
53 app = null;
54 c = string.charAt(i);
55
56 if (( c >= '0' && c <='9') || (c >='A' && c <='Z') || (c >='a' && c <='z')
57 || c == ':' || c == '.' )
58 {
59
60 }
61 else
62 {
63
64 app = "_" + HEX_CHARSET.charAt( ((c >> 0x0C) % 0x10)) + HEX_CHARSET.charAt( ((c >> 0x8) % 0x10)) + HEX_CHARSET.charAt( ((c >> 0x4) % 0x10)) +HEX_CHARSET.charAt(c % 0x10);
65 }
66
67 if (app != null)
68 {
69 if (sb == null)
70 {
71 sb = new StringBuffer(string.substring(0, i));
72 }
73 sb.append(app);
74 } else {
75 if (sb != null)
76 {
77 sb.append(c);
78 }
79 }
80 if (endLoop)
81 {
82 break;
83 }
84 }
85 if (sb == null)
86 {
87 return string;
88 }
89 else
90 {
91 return sb.toString();
92 }
93 }
94
95 public static String decode(final String string)
96 {
97 StringBuffer sb = null;
98 String app;
99 char c;
100 boolean endLoop = false;
101 for (int i = 0; i < string.length (); ++i)
102 {
103 app = null;
104 c = string.charAt(i);
105
106 if (c == '_')
107 {
108 int value = (toDigit(string.charAt(i+1)) << 0x0C) + (toDigit(string.charAt(i+2)) << 0x08) + (toDigit(string.charAt(i+3)) << 0x04) + toDigit(string.charAt(i+4));
109
110 if (sb == null)
111 {
112 sb = new StringBuffer(string.substring(0, i));
113 }
114 i += 4;
115 app = ""+((char)value);
116 }
117 else
118 {
119
120 }
121
122 if (app != null)
123 {
124 if (sb == null)
125 {
126 sb = new StringBuffer(string.substring(0, i));
127 }
128 sb.append(app);
129 } else {
130 if (sb != null)
131 {
132 sb.append(c);
133 }
134 }
135 if (endLoop)
136 {
137 break;
138 }
139 }
140 if (sb == null)
141 {
142 return string;
143 }
144 else
145 {
146 return sb.toString();
147 }
148 }
149
150
151
152
153
154
155
156
157
158
159
160
161 protected static int toDigit(char ch)
162 {
163 int digit = Character.digit(ch, 16);
164 if (digit == -1)
165 {
166 throw new FacesException("Illegal hexadecimal charcter ");
167 }
168 return digit;
169 }
170 }