1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
/**
* xsltICUSort.c: module to provide a sort function replacement using ICU,
* it is not included in standard due to the size of the ICU
* library
*
* Requires libxslt 1.1.38
*/
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>
#include <unicode/ucnv.h>
#include <unicode/ucol.h>
/**
* xsltICUNewLocale:
* @lang: lang
*
* Create a new ICU collator.
*/
void *
xsltICUNewLocale(const xmlChar *lang, int lowerFirst) {
UCollator *coll;
UErrorCode status = U_ZERO_ERROR;
coll = ucol_open((const char *) lang, &status);
if (U_FAILURE(status)) {
status = U_ZERO_ERROR;
coll = ucol_open("en", &status);
if (U_FAILURE(status)) {
return NULL;
}
}
if (lowerFirst)
ucol_setAttribute(coll, UCOL_CASE_FIRST, UCOL_LOWER_FIRST, &status);
else
ucol_setAttribute(coll, UCOL_CASE_FIRST, UCOL_UPPER_FIRST, &status);
return (void *) coll;
}
/**
* xsltICUNewLocale:
* @locale: ICU collator
*
* Free the ICU collator.
*/
void
xsltICUFreeLocale(void *coll) {
ucol_close((UCollator *) coll);
}
/**
* xsltICUGenSortKey:
* @locale: ICU collator
* @str: source string
*
* Generate a localized sort key.
*/
xmlChar *
xsltICUGenSortKey(void *coll, const xmlChar *str) {
UConverter *conv;
UChar *ustr = NULL;
xmlChar *result = NULL;
UErrorCode status = U_ZERO_ERROR;
int32_t ustrLen, resultLen;
conv = ucnv_open("UTF8", &status);
if (U_FAILURE(status))
goto error;
ustrLen = ucnv_toUChars(conv, NULL, 0, (const char *) str, -1, &status);
if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR)
goto error;
status = U_ZERO_ERROR;
ustr = (UChar *) xmlMalloc((ustrLen + 1) * sizeof(UChar));
if (ustr == NULL)
goto error;
ustrLen = ucnv_toUChars(conv, ustr, ustrLen, (const char *) str, -1,
&status);
if (U_FAILURE(status))
goto error;
resultLen = ucol_getSortKey((UCollator *) coll, ustr, ustrLen, NULL, 0);
if (resultLen == 0)
goto error;
result = (xmlChar *) xmlMalloc(resultLen);
if (result == NULL)
goto error;
resultLen = ucol_getSortKey((UCollator *) coll, ustr, ustrLen, result,
resultLen);
if (resultLen == 0) {
xmlFree(result);
result = NULL;
goto error;
}
error:
xmlFree(ustr);
return result;
}
int main(void) {
xmlDocPtr sourceDoc = xmlReadDoc(
BAD_CAST
"<d>\n"
" <e>Berta</e>\n"
" <e>Ärger</e>\n"
"</d>\n",
NULL, NULL, 0
);
xmlDocPtr styleDoc = xmlReadDoc(
BAD_CAST
"<xsl:stylesheet"
" version='1.0'"
" xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>\n"
" <xsl:template match='d'>\n"
" <xsl:for-each select='*'>\n"
" <xsl:sort lang='de'/>\n"
" <xsl:copy-of select='.'/>\n"
" </xsl:for-each>\n"
" </xsl:template>\n"
"</xsl:stylesheet>\n",
NULL, NULL, 0
);
xsltStylesheetPtr style = xsltParseStylesheetDoc(styleDoc);
if (style == NULL)
xmlFreeDoc(styleDoc);
xsltTransformContextPtr tctxt = xsltNewTransformContext(style, sourceDoc);
xsltSetCtxtLocaleHandlers(tctxt, xsltICUNewLocale, xsltICUFreeLocale,
xsltICUGenSortKey);
xmlDocPtr resultDoc = xsltApplyStylesheetUser(style, sourceDoc, NULL, NULL,
NULL, tctxt);
xsltFreeTransformContext(tctxt);
xsltSaveResultToFile(stdout, resultDoc, style);
xmlFreeDoc(resultDoc);
xsltFreeStylesheet(style);
xmlFreeDoc(sourceDoc);
return 0;
}
|