1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| package org.apache.xindice.util; |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| public class XMLUtilities { |
28 |
| private static final String REPLACEMENT = "�"; |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
0
| public static String escape(char[] value, int offset, int length, boolean strict) {
|
45 |
0
| StringBuffer buf = new StringBuffer();
|
46 |
0
| int start = offset;
|
47 |
0
| int blockLength = 0;
|
48 |
| |
49 |
0
| for (int i = offset; i < length; i++) {
|
50 |
0
| String outval = escape(value[i], strict);
|
51 |
| |
52 |
0
| if (outval == null) {
|
53 |
0
| if (isLeadingSurrogate(value[i])) {
|
54 |
0
| if (i + 1 < length && isTrailingSurrogate(value[i + 1])) {
|
55 |
0
| outval = getSurrogateValue(value[i], value[i + 1]);
|
56 |
0
| i++;
|
57 |
| } else { |
58 |
0
| if (strict) {
|
59 |
0
| throw new XindiceRuntimeException("Leading surrogate &#" + Integer.toString(value[i]) + ";" +
|
60 |
| "must be followed by trailing surrogate"); |
61 |
| } else { |
62 |
0
| outval = REPLACEMENT;
|
63 |
| } |
64 |
| } |
65 |
| } else { |
66 |
0
| blockLength++;
|
67 |
| } |
68 |
| } |
69 |
| |
70 |
0
| if (outval != null) {
|
71 |
0
| if (blockLength > 0) {
|
72 |
0
| buf.append(value, start, blockLength);
|
73 |
| } |
74 |
0
| buf.append(outval);
|
75 |
0
| start = i + 1;
|
76 |
0
| blockLength = 0;
|
77 |
| } |
78 |
| } |
79 |
| |
80 |
0
| if (blockLength > 0 && start > offset) {
|
81 |
0
| buf.append(value, start, blockLength);
|
82 |
| } |
83 |
| |
84 |
0
| return buf.length() > 0 ? buf.toString() : new String(value, offset, length);
|
85 |
| } |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
0
| public static String escape(char[] value, int offset, int length) {
|
97 |
0
| return escape(value, offset, length, false);
|
98 |
| } |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
39449
| public static String escape(String text, boolean strict) {
|
113 |
39449
| StringBuffer buf = null;
|
114 |
39449
| int length = text.length();
|
115 |
| |
116 |
39449
| for (int i = 0; i < length; i++) {
|
117 |
455181
| char ch = text.charAt(i);
|
118 |
455181
| String outval = escape(ch, strict);
|
119 |
| |
120 |
455181
| if (outval == null) {
|
121 |
455113
| if (isLeadingSurrogate(ch)) {
|
122 |
0
| if (i + 1 < length && isTrailingSurrogate(text.charAt(i + 1))) {
|
123 |
0
| outval = getSurrogateValue(ch, text.charAt(i + 1));
|
124 |
| |
125 |
0
| if (buf == null) {
|
126 |
0
| buf = new StringBuffer(text.substring(0, i));
|
127 |
| } |
128 |
0
| i++;
|
129 |
| } else { |
130 |
0
| if (strict) {
|
131 |
0
| throw new XindiceRuntimeException("Leading surrogate &#" + Integer.toString(ch) + ";" +
|
132 |
| "must be followed by trailing surrogate"); |
133 |
| } else { |
134 |
0
| outval = REPLACEMENT;
|
135 |
| } |
136 |
| } |
137 |
| } |
138 |
| } |
139 |
| |
140 |
455181
| if (outval != null && buf == null) {
|
141 |
68
| buf = new StringBuffer(text.substring(0, i));
|
142 |
| } |
143 |
| |
144 |
455181
| if (outval != null) {
|
145 |
68
| buf.append(outval);
|
146 |
455113
| } else if (buf != null) {
|
147 |
148
| buf.append(ch);
|
148 |
| } |
149 |
| } |
150 |
| |
151 |
39449
| return buf != null ? buf.toString() : text;
|
152 |
| } |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
39449
| public static String escape(String text) {
|
162 |
39449
| return escape(text, false);
|
163 |
| } |
164 |
| |
165 |
455181
| private static String escape(char ch, boolean strict) {
|
166 |
455181
| String outval = null;
|
167 |
| |
168 |
455181
| switch (ch) {
|
169 |
6
| case '&':
|
170 |
6
| outval = "&";
|
171 |
6
| break;
|
172 |
38
| case '\'':
|
173 |
38
| outval = "'";
|
174 |
38
| break;
|
175 |
12
| case '\"':
|
176 |
12
| outval = """;
|
177 |
12
| break;
|
178 |
6
| case '<':
|
179 |
6
| outval = "<";
|
180 |
6
| break;
|
181 |
6
| case '>':
|
182 |
6
| outval = ">";
|
183 |
6
| break;
|
184 |
455113
| default:
|
185 |
455113
| if (isTrailingSurrogate(ch)) {
|
186 |
0
| if (strict) {
|
187 |
0
| throw new XindiceRuntimeException("Trailing surrogate &#" + Integer.toString(ch) +
|
188 |
| "; must follow leading surrogate"); |
189 |
| } else { |
190 |
0
| outval = REPLACEMENT;
|
191 |
| } |
192 |
455113
| } else if (!isLeadingSurrogate(ch) && !isLegal(ch)) {
|
193 |
0
| outval = "&#" + Integer.toString(ch) + ";";
|
194 |
| } |
195 |
455113
| break;
|
196 |
| } |
197 |
| |
198 |
455181
| return outval;
|
199 |
| } |
200 |
| |
201 |
455113
| private static boolean isLegal(char ch) {
|
202 |
455113
| return ch == 0x9 || ch == 0xA || ch == 0xD ||
|
203 |
| (ch >= 0x20 && ch <= 0xD7FF) || |
204 |
| (ch >= 0xE000 && ch <= 0xFFFD); |
205 |
| } |
206 |
| |
207 |
| |
208 |
| |
209 |
| |
210 |
| |
211 |
| |
212 |
| |
213 |
0
| private static String getSurrogateValue(char high, char low) {
|
214 |
0
| int val = (high & 0x3FF) << 10 | (low & 0x3FF) + 0x10000;
|
215 |
0
| return "&#" + Integer.toString(val) + ";";
|
216 |
| } |
217 |
| |
218 |
910226
| private static boolean isLeadingSurrogate(char ch) {
|
219 |
910226
| return ch >= 0xD800 && ch <= 0xDBFF;
|
220 |
| } |
221 |
| |
222 |
455113
| private static boolean isTrailingSurrogate(char ch) {
|
223 |
455113
| return ch >= 0xDC00 && ch <= 0xDFFF;
|
224 |
| } |
225 |
| } |