[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[xml] libxslt patch: str:encode-uri and str:decode-uri



Hi!

I just hacked up two EXSLT functions. These are str:encode-uri and 
str:decode-uri, latest version, as described on exslt.org. It should be 
conformant to the specs, but I didn't test it further than "it works for me".
It only supports UTF-8 as encoding, which is okay by the spec, but might be a 
bit annoying. I didn't need it, so I didn't implement charset conversion.
I hope I didn't make any mistakes, this is my first work with libxml/libxslt.
Diff against 1.0.23 attached.

-- 
CU
  Joerg

PGP Public Key at http://ich.bin.kein.hoschi.de/~trouble/public_key.asc
PGP Key fingerprint = D34F 57C4 99D8 8F16 E16E  7779 CDDC 41A4 4C48 6F94
diff -rubB -X /usr/lib/portage/lib/diff.ignore work.old/libxslt-1.0.23/libexslt/strings.c work/libxslt-1.0.23/libexslt/strings.c
--- work.old/libxslt-1.0.23/libexslt/strings.c	Mon Mar 18 20:41:33 2002
+++ work/libxslt-1.0.23/libexslt/strings.c	Thu Jan  2 22:36:28 2003
@@ -12,6 +12,7 @@
 #include <libxml/xpathInternals.h>
 #include <libxml/parser.h>
 #include <libxml/encoding.h>
+#include <libxml/uri.h>
 
 #include <libxslt/xsltconfig.h>
 #include <libxslt/xsltutils.h>
@@ -108,6 +109,107 @@
 }
 
 /**
+ * exsltStrEncodeUriFunction:
+ * @ctxt: an XPath parser context
+ * @nargs: the number of arguments
+ *
+ * URI-Escapes a string
+ */
+static void
+exsltStrEncodeUriFunction (xmlXPathParserContextPtr ctxt, int nargs) {
+    int escape_all = 1, str_len = 0;
+    xmlChar *str = NULL, *ret = NULL, *tmp;
+
+    if ((nargs < 2) || (nargs > 3)) {
+	xmlXPathSetArityError(ctxt);
+	return;
+    }
+
+    if (nargs >= 3) {
+        /* check for UTF-8 if encoding was explicitly given;
+           we don't support anything else yet */
+        tmp = xmlXPathPopString(ctxt);
+        if (xmlUTF8Strlen(tmp) != 5 || xmlStrcmp((const xmlChar *)"UTF-8",tmp)) {
+	    xmlXPathReturnEmptyString(ctxt);
+	    xmlFree(tmp);
+	    return;
+	}
+	xmlFree(tmp);
+    }
+
+    escape_all = xmlXPathPopBoolean(ctxt);
+
+    str = xmlXPathPopString(ctxt);
+    str_len = xmlUTF8Strlen(str);
+
+    if (str_len == 0) {
+	xmlXPathReturnEmptyString(ctxt);
+	xmlFree(str);
+	return;
+    }
+
+    ret = xmlURIEscapeStr(str,(const xmlChar *)(escape_all?"-_.!~*'()":"-_.!~*'();/?:@&=+$,[]"));
+    xmlXPathReturnString(ctxt, ret);
+
+    if (str != NULL)
+	xmlFree(str);
+}
+
+/**
+ * exsltStrDecodeUriFunction:
+ * @ctxt: an XPath parser context
+ * @nargs: the number of arguments
+ *
+ * reverses URI-Escaping of a string
+ */
+static void
+exsltStrDecodeUriFunction (xmlXPathParserContextPtr ctxt, int nargs) {
+    int str_len = 0;
+    xmlChar *str = NULL, *ret = NULL, *tmp;
+
+    if ((nargs < 1) || (nargs > 2)) {
+	xmlXPathSetArityError(ctxt);
+	return;
+    }
+
+    if (nargs >= 2) {
+        /* check for UTF-8 if encoding was explicitly given;
+           we don't support anything else yet */
+        tmp = xmlXPathPopString(ctxt);
+        if (xmlUTF8Strlen(tmp) != 5 || xmlStrcmp((const xmlChar *)"UTF-8",tmp)) {
+	    xmlXPathReturnEmptyString(ctxt);
+	    xmlFree(tmp);
+	    return;
+	}
+	xmlFree(tmp);
+    }
+
+    str = xmlXPathPopString(ctxt);
+    str_len = xmlUTF8Strlen(str);
+
+    if (str_len == 0) {
+	xmlXPathReturnEmptyString(ctxt);
+	xmlFree(str);
+	return;
+    }
+
+    ret = xmlURIUnescapeString(str,0,NULL);
+    if (!xmlCheckUTF8(ret)) {
+	/* FIXME: instead of throwing away the whole URI, we should
+        only discard the invalid sequence(s). How to do that? */
+	xmlXPathReturnEmptyString(ctxt);
+	xmlFree(str);
+	xmlFree(ret);
+	return;
+    }
+    
+    xmlXPathReturnString(ctxt, ret);
+
+    if (str != NULL)
+	xmlFree(str);
+}
+
+/**
  * exsltStrPaddingFunction:
  * @ctxt: an XPath parser context
  * @nargs: the number of arguments
@@ -280,6 +382,12 @@
     xsltRegisterExtModuleFunction ((const xmlChar *) "tokenize",
 				   EXSLT_STRINGS_NAMESPACE,
 				   exsltStrTokenizeFunction);
+    xsltRegisterExtModuleFunction ((const xmlChar *) "encode-uri",
+				   EXSLT_STRINGS_NAMESPACE,
+				   exsltStrEncodeUriFunction);
+    xsltRegisterExtModuleFunction ((const xmlChar *) "decode-uri",
+				   EXSLT_STRINGS_NAMESPACE,
+				   exsltStrDecodeUriFunction);
     xsltRegisterExtModuleFunction ((const xmlChar *) "padding",
 				   EXSLT_STRINGS_NAMESPACE,
 				   exsltStrPaddingFunction);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]