OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 18 matching lines...) Expand all Loading... |
29 #include "config.h" | 29 #include "config.h" |
30 #include "core/xml/XSLTUnicodeSort.h" | 30 #include "core/xml/XSLTUnicodeSort.h" |
31 | 31 |
32 #include "wtf/text/WTFString.h" | 32 #include "wtf/text/WTFString.h" |
33 #include "wtf/unicode/Collator.h" | 33 #include "wtf/unicode/Collator.h" |
34 #include <libxslt/templates.h> | 34 #include <libxslt/templates.h> |
35 #include <libxslt/xsltutils.h> | 35 #include <libxslt/xsltutils.h> |
36 | 36 |
37 namespace WebCore { | 37 namespace WebCore { |
38 | 38 |
39 // Based on default implementation from libxslt 1.1.22 and xsltICUSort.c example
. | 39 inline const xmlChar* toXMLChar(const char* string) |
| 40 { |
| 41 return reinterpret_cast<const xmlChar*>(string); |
| 42 } |
| 43 |
| 44 // Based on default implementation from libxslt 1.1.22 and xsltICUSort.c |
| 45 // example. |
40 void xsltUnicodeSortFunction(xsltTransformContextPtr ctxt, xmlNodePtr *sorts, in
t nbsorts) | 46 void xsltUnicodeSortFunction(xsltTransformContextPtr ctxt, xmlNodePtr *sorts, in
t nbsorts) |
41 { | 47 { |
42 #ifdef XSLT_REFACTORED | 48 #ifdef XSLT_REFACTORED |
43 xsltStyleItemSortPtr comp; | 49 xsltStyleItemSortPtr comp; |
44 #else | 50 #else |
45 xsltStylePreCompPtr comp; | 51 xsltStylePreCompPtr comp; |
46 #endif | 52 #endif |
47 xmlXPathObjectPtr *resultsTab[XSLT_MAX_SORT]; | 53 xmlXPathObjectPtr* resultsTab[XSLT_MAX_SORT]; |
48 xmlXPathObjectPtr *results = NULL, *res; | 54 xmlXPathObjectPtr* results = 0; |
49 xmlNodeSetPtr list = NULL; | 55 xmlNodeSetPtr list = 0; |
50 int descending, number, desc, numb; | |
51 int len = 0; | |
52 int i, j, incr; | |
53 int tst; | |
54 int depth; | 56 int depth; |
55 xmlNodePtr node; | 57 xmlNodePtr node; |
56 xmlXPathObjectPtr tmp; | |
57 int tempstype[XSLT_MAX_SORT], temporder[XSLT_MAX_SORT]; | 58 int tempstype[XSLT_MAX_SORT], temporder[XSLT_MAX_SORT]; |
58 | 59 |
59 if ((ctxt == NULL) || (sorts == NULL) || (nbsorts <= 0) || | 60 if (!ctxt || !sorts || nbsorts <= 0 || nbsorts >= XSLT_MAX_SORT) |
60 (nbsorts >= XSLT_MAX_SORT)) | |
61 return; | 61 return; |
62 if (sorts[0] == NULL) | 62 if (!sorts[0]) |
63 return; | 63 return; |
64 comp = static_cast<xsltStylePreComp*>(sorts[0]->psvi); | 64 comp = static_cast<xsltStylePreComp*>(sorts[0]->psvi); |
65 if (comp == NULL) | 65 if (!comp) |
66 return; | 66 return; |
67 | 67 |
68 list = ctxt->nodeList; | 68 list = ctxt->nodeList; |
69 if ((list == NULL) || (list->nodeNr <= 1)) | 69 if (!list || list->nodeNr <= 1) |
70 return; /* nothing to do */ | 70 return; // Nothing to do. |
71 | 71 |
72 for (j = 0; j < nbsorts; j++) { | 72 for (int j = 0; j < nbsorts; ++j) { |
73 comp = static_cast<xsltStylePreComp*>(sorts[j]->psvi); | 73 comp = static_cast<xsltStylePreComp*>(sorts[j]->psvi); |
74 tempstype[j] = 0; | 74 tempstype[j] = 0; |
75 if ((comp->stype == NULL) && (comp->has_stype != 0)) { | 75 if (!comp->stype && comp->has_stype) { |
76 comp->stype = | 76 comp->stype = xsltEvalAttrValueTemplate(ctxt, sorts[j], toXMLChar("d
ata-type"), XSLT_NAMESPACE); |
77 xsltEvalAttrValueTemplate(ctxt, sorts[j], | 77 if (comp->stype) { |
78 (const xmlChar *) "data-type", | |
79 XSLT_NAMESPACE); | |
80 if (comp->stype != NULL) { | |
81 tempstype[j] = 1; | 78 tempstype[j] = 1; |
82 if (xmlStrEqual(comp->stype, (const xmlChar *) "text")) | 79 if (xmlStrEqual(comp->stype, toXMLChar("text"))) { |
83 comp->number = 0; | 80 comp->number = 0; |
84 else if (xmlStrEqual(comp->stype, (const xmlChar *) "number")) | 81 } else if (xmlStrEqual(comp->stype, toXMLChar("number"))) { |
85 comp->number = 1; | 82 comp->number = 1; |
86 else { | 83 } else { |
87 xsltTransformError(ctxt, NULL, sorts[j], | 84 xsltTransformError(ctxt, 0, sorts[j], "xsltDoSortFunction: n
o support for data-type = %s\n", comp->stype); |
88 "xsltDoSortFunction: no support for data-type = %s\n", | 85 comp->number = 0; // Use default. |
89 comp->stype); | |
90 comp->number = 0; /* use default */ | |
91 } | 86 } |
92 } | 87 } |
93 } | 88 } |
94 temporder[j] = 0; | 89 temporder[j] = 0; |
95 if ((comp->order == NULL) && (comp->has_order != 0)) { | 90 if (!comp->order && comp->has_order) { |
96 comp->order = xsltEvalAttrValueTemplate(ctxt, sorts[j], | 91 comp->order = xsltEvalAttrValueTemplate(ctxt, sorts[j], toXMLChar("o
rder"), XSLT_NAMESPACE); |
97 (const xmlChar *) "order", | 92 if (comp->order) { |
98 XSLT_NAMESPACE); | |
99 if (comp->order != NULL) { | |
100 temporder[j] = 1; | 93 temporder[j] = 1; |
101 if (xmlStrEqual(comp->order, (const xmlChar *) "ascending")) | 94 if (xmlStrEqual(comp->order, toXMLChar("ascending"))) { |
102 comp->descending = 0; | 95 comp->descending = 0; |
103 else if (xmlStrEqual(comp->order, | 96 } else if (xmlStrEqual(comp->order, toXMLChar("descending"))) { |
104 (const xmlChar *) "descending")) | |
105 comp->descending = 1; | 97 comp->descending = 1; |
106 else { | 98 } else { |
107 xsltTransformError(ctxt, NULL, sorts[j], | 99 xsltTransformError(ctxt, 0, sorts[j], "xsltDoSortFunction: i
nvalid value %s for order\n", comp->order); |
108 "xsltDoSortFunction: invalid value %s for order\n", | 100 comp->descending = 0; // Use default. |
109 comp->order); | |
110 comp->descending = 0; /* use default */ | |
111 } | 101 } |
112 } | 102 } |
113 } | 103 } |
114 } | 104 } |
115 | 105 |
116 len = list->nodeNr; | 106 int len = list->nodeNr; |
117 | 107 |
118 resultsTab[0] = xsltComputeSortResult(ctxt, sorts[0]); | 108 resultsTab[0] = xsltComputeSortResult(ctxt, sorts[0]); |
119 for (i = 1;i < XSLT_MAX_SORT;i++) | 109 for (int i = 1; i < XSLT_MAX_SORT; ++i) |
120 resultsTab[i] = NULL; | 110 resultsTab[i] = 0; |
121 | 111 |
122 results = resultsTab[0]; | 112 results = resultsTab[0]; |
123 | 113 |
124 comp = static_cast<xsltStylePreComp*>(sorts[0]->psvi); | 114 comp = static_cast<xsltStylePreComp*>(sorts[0]->psvi); |
125 descending = comp->descending; | 115 int descending = comp->descending; |
126 number = comp->number; | 116 int number = comp->number; |
127 if (results == NULL) | 117 if (!results) |
128 return; | 118 return; |
129 | 119 |
130 // We are passing a language identifier to a function that expects a locale
identifier. | 120 // We are passing a language identifier to a function that expects a locale |
131 // The implementation of Collator should be lenient, and accept both "en-US"
and "en_US", for example. | 121 // identifier. The implementation of Collator should be lenient, and accept |
132 // This lets an author to really specify sorting rules, e.g. "de_DE@collatio
n=phonebook", which isn't | 122 // both "en-US" and "en_US", for example. This lets an author to really |
| 123 // specify sorting rules, e.g. "de_DE@collation=phonebook", which isn't |
133 // possible with language alone. | 124 // possible with language alone. |
134 Collator collator(comp->has_lang ? (const char*)comp->lang : "en"); | 125 Collator collator(comp->has_lang ? reinterpret_cast<const char*>(comp->lang)
: "en"); |
135 collator.setOrderLowerFirst(comp->lower_first); | 126 collator.setOrderLowerFirst(comp->lower_first); |
136 | 127 |
137 /* Shell's sort of node-set */ | 128 // Shell's sort of node-set. |
138 for (incr = len / 2; incr > 0; incr /= 2) { | 129 for (int incr = len / 2; incr > 0; incr /= 2) { |
139 for (i = incr; i < len; i++) { | 130 for (int i = incr; i < len; ++i) { |
140 j = i - incr; | 131 int j = i - incr; |
141 if (results[i] == NULL) | 132 if (!results[i]) |
142 continue; | 133 continue; |
143 | 134 |
144 while (j >= 0) { | 135 while (j >= 0) { |
145 if (results[j] == NULL) | 136 int tst; |
| 137 if (!results[j]) { |
146 tst = 1; | 138 tst = 1; |
147 else { | 139 } else { |
148 if (number) { | 140 if (number) { |
149 /* We make NaN smaller than number in accordance | 141 // We make NaN smaller than number in accordance with |
150 with XSLT spec */ | 142 // XSLT spec. |
151 if (xmlXPathIsNaN(results[j]->floatval)) { | 143 if (xmlXPathIsNaN(results[j]->floatval)) { |
152 if (xmlXPathIsNaN(results[j + incr]->floatval)) | 144 if (xmlXPathIsNaN(results[j + incr]->floatval)) |
153 tst = 0; | 145 tst = 0; |
154 else | 146 else |
155 tst = -1; | 147 tst = -1; |
156 } else if (xmlXPathIsNaN(results[j + incr]->floatval)) | 148 } else if (xmlXPathIsNaN(results[j + incr]->floatval)) { |
157 tst = 1; | 149 tst = 1; |
158 else if (results[j]->floatval == | 150 } else if (results[j]->floatval == results[j + incr]->fl
oatval) { |
159 results[j + incr]->floatval) | |
160 tst = 0; | 151 tst = 0; |
161 else if (results[j]->floatval > | 152 } else if (results[j]->floatval > results[j + incr]->flo
atval) { |
162 results[j + incr]->floatval) | |
163 tst = 1; | 153 tst = 1; |
164 else tst = -1; | 154 } else { |
| 155 tst = -1; |
| 156 } |
165 } else { | 157 } else { |
166 Vector<UChar> string1; | 158 Vector<UChar> string1; |
167 Vector<UChar> string2; | 159 Vector<UChar> string2; |
168 String::fromUTF8(reinterpret_cast<const char*>(results[j
]->stringval)).appendTo(string1); | 160 String::fromUTF8(reinterpret_cast<const char*>(results[j
]->stringval)).appendTo(string1); |
169 String::fromUTF8(reinterpret_cast<const char*>(results[j
+ incr]->stringval)).appendTo(string2); | 161 String::fromUTF8(reinterpret_cast<const char*>(results[j
+ incr]->stringval)).appendTo(string2); |
170 tst = collator.collate(string1.data(), string1.size(), s
tring2.data(), string2.size()); | 162 tst = collator.collate(string1.data(), string1.size(), s
tring2.data(), string2.size()); |
171 } | 163 } |
172 if (descending) | 164 if (descending) |
173 tst = -tst; | 165 tst = -tst; |
174 } | 166 } |
175 if (tst == 0) { | 167 if (tst == 0) { |
176 /* | 168 // Okay we need to use multi level sorts. |
177 * Okay we need to use multi level sorts | |
178 */ | |
179 depth = 1; | 169 depth = 1; |
180 while (depth < nbsorts) { | 170 while (depth < nbsorts) { |
181 if (sorts[depth] == NULL) | 171 if (!sorts[depth]) |
182 break; | 172 break; |
183 comp = static_cast<xsltStylePreComp*>(sorts[depth]->psvi
); | 173 comp = static_cast<xsltStylePreComp*>(sorts[depth]->psvi
); |
184 if (comp == NULL) | 174 if (!comp) |
185 break; | 175 break; |
186 desc = comp->descending; | 176 int desc = comp->descending; |
187 numb = comp->number; | 177 int numb = comp->number; |
188 | 178 |
189 /* | 179 // Compute the result of the next level for the full |
190 * Compute the result of the next level for the | 180 // set, this might be optimized ... or not |
191 * full set, this might be optimized ... or not | 181 if (!resultsTab[depth]) |
192 */ | 182 resultsTab[depth] = xsltComputeSortResult(ctxt, sort
s[depth]); |
193 if (resultsTab[depth] == NULL) | 183 xmlXPathObjectPtr* res = resultsTab[depth]; |
194 resultsTab[depth] = xsltComputeSortResult(ctxt, | 184 if (!res) |
195 sorts[depth]); | |
196 res = resultsTab[depth]; | |
197 if (res == NULL) | |
198 break; | 185 break; |
199 if (res[j] == NULL) { | 186 if (!res[j]) { |
200 if (res[j+incr] != NULL) | 187 if (res[j + incr]) |
201 tst = 1; | 188 tst = 1; |
202 } else { | 189 } else { |
203 if (numb) { | 190 if (numb) { |
204 /* We make NaN smaller than number in | 191 // We make NaN smaller than number in accordance |
205 accordance with XSLT spec */ | 192 // with XSLT spec. |
206 if (xmlXPathIsNaN(res[j]->floatval)) { | 193 if (xmlXPathIsNaN(res[j]->floatval)) { |
207 if (xmlXPathIsNaN(res[j + | 194 if (xmlXPathIsNaN(res[j + incr]->floatval)) |
208 incr]->floatval)) | |
209 tst = 0; | 195 tst = 0; |
210 else | 196 else |
211 tst = -1; | 197 tst = -1; |
212 } else if (xmlXPathIsNaN(res[j + incr]-> | 198 } else if (xmlXPathIsNaN(res[j + incr]->floatval
)) { |
213 floatval)) | |
214 tst = 1; | 199 tst = 1; |
215 else if (res[j]->floatval == res[j + incr]-> | 200 } else if (res[j]->floatval == res[j + incr]->fl
oatval) { |
216 floatval) | |
217 tst = 0; | 201 tst = 0; |
218 else if (res[j]->floatval > | 202 } else if (res[j]->floatval > res[j + incr]->flo
atval) { |
219 res[j + incr]->floatval) | |
220 tst = 1; | 203 tst = 1; |
221 else tst = -1; | 204 } else { |
| 205 tst = -1; |
| 206 } |
222 } else { | 207 } else { |
223 Vector<UChar> string1; | 208 Vector<UChar> string1; |
224 Vector<UChar> string2; | 209 Vector<UChar> string2; |
225 String::fromUTF8(reinterpret_cast<const char*>(r
es[j]->stringval)).appendTo(string1); | 210 String::fromUTF8(reinterpret_cast<const char*>(r
es[j]->stringval)).appendTo(string1); |
226 String::fromUTF8(reinterpret_cast<const char*>(r
es[j + incr]->stringval)).appendTo(string2); | 211 String::fromUTF8(reinterpret_cast<const char*>(r
es[j + incr]->stringval)).appendTo(string2); |
227 tst = collator.collate(string1.data(), string1.s
ize(), string2.data(), string2.size()); | 212 tst = collator.collate(string1.data(), string1.s
ize(), string2.data(), string2.size()); |
228 } | 213 } |
229 if (desc) | 214 if (desc) |
230 tst = -tst; | 215 tst = -tst; |
231 } | 216 } |
232 | 217 |
233 /* | 218 // if we still can't differenciate at this level try one |
234 * if we still can't differenciate at this level | 219 // level deeper. |
235 * try one level deeper. | |
236 */ | |
237 if (tst != 0) | 220 if (tst != 0) |
238 break; | 221 break; |
239 depth++; | 222 depth++; |
240 } | 223 } |
241 } | 224 } |
242 if (tst == 0) { | 225 if (tst == 0) { |
243 tst = results[j]->index > results[j + incr]->index; | 226 tst = results[j]->index > results[j + incr]->index; |
244 } | 227 } |
245 if (tst > 0) { | 228 if (tst > 0) { |
246 tmp = results[j]; | 229 xmlXPathObjectPtr tmp = results[j]; |
247 results[j] = results[j + incr]; | 230 results[j] = results[j + incr]; |
248 results[j + incr] = tmp; | 231 results[j + incr] = tmp; |
249 node = list->nodeTab[j]; | 232 node = list->nodeTab[j]; |
250 list->nodeTab[j] = list->nodeTab[j + incr]; | 233 list->nodeTab[j] = list->nodeTab[j + incr]; |
251 list->nodeTab[j + incr] = node; | 234 list->nodeTab[j + incr] = node; |
252 depth = 1; | 235 depth = 1; |
253 while (depth < nbsorts) { | 236 while (depth < nbsorts) { |
254 if (sorts[depth] == NULL) | 237 if (!sorts[depth]) |
255 break; | 238 break; |
256 if (resultsTab[depth] == NULL) | 239 if (!resultsTab[depth]) |
257 break; | 240 break; |
258 res = resultsTab[depth]; | 241 xmlXPathObjectPtr* res = resultsTab[depth]; |
259 tmp = res[j]; | 242 tmp = res[j]; |
260 res[j] = res[j + incr]; | 243 res[j] = res[j + incr]; |
261 res[j + incr] = tmp; | 244 res[j + incr] = tmp; |
262 depth++; | 245 depth++; |
263 } | 246 } |
264 j -= incr; | 247 j -= incr; |
265 } else | 248 } else { |
266 break; | 249 break; |
| 250 } |
267 } | 251 } |
268 } | 252 } |
269 } | 253 } |
270 | 254 |
271 for (j = 0; j < nbsorts; j++) { | 255 for (int j = 0; j < nbsorts; ++j) { |
272 comp = static_cast<xsltStylePreComp*>(sorts[j]->psvi); | 256 comp = static_cast<xsltStylePreComp*>(sorts[j]->psvi); |
273 if (tempstype[j] == 1) { | 257 if (tempstype[j] == 1) { |
274 /* The data-type needs to be recomputed each time */ | 258 // The data-type needs to be recomputed each time. |
275 xmlFree((void *)(comp->stype)); | 259 xmlFree(const_cast<xmlChar*>(comp->stype)); |
276 comp->stype = NULL; | 260 comp->stype = 0; |
277 } | 261 } |
278 if (temporder[j] == 1) { | 262 if (temporder[j] == 1) { |
279 /* The order needs to be recomputed each time */ | 263 // The order needs to be recomputed each time. |
280 xmlFree((void *)(comp->order)); | 264 xmlFree(const_cast<xmlChar*>(comp->order)); |
281 comp->order = NULL; | 265 comp->order = 0; |
282 } | 266 } |
283 if (resultsTab[j] != NULL) { | 267 if (resultsTab[j]) { |
284 for (i = 0;i < len;i++) | 268 for (int i = 0; i < len; ++i) |
285 xmlXPathFreeObject(resultsTab[j][i]); | 269 xmlXPathFreeObject(resultsTab[j][i]); |
286 xmlFree(resultsTab[j]); | 270 xmlFree(resultsTab[j]); |
287 } | 271 } |
288 } | 272 } |
289 } | 273 } |
290 | 274 |
291 } // namespace WebCore | 275 } // namespace WebCore |
OLD | NEW |