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