Update of /cvsroot/monetdb/MonetDB5/src/modules/atoms
In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv22749/src/modules/atoms
Modified Files:
Tag: GDK-2
xml.mx
Log Message:
propagated changes of Tuesday Aug 28 2007 - Monday Sep 03 2007
from the development trunk to the GDK-2 branch
Index: xml.mx
===================================================================
RCS file: /cvsroot/monetdb/MonetDB5/src/modules/atoms/xml.mx,v
retrieving revision 1.4.2.4
retrieving revision 1.4.2.5
diff -u -d -r1.4.2.4 -r1.4.2.5
--- xml.mx 28 Aug 2007 13:15:14 -0000 1.4.2.4
+++ xml.mx 3 Sep 2007 13:08:17 -0000 1.4.2.5
@@ -21,7 +21,14 @@
This module contains the primitives needed in the SQL
front-end to support SQL/XML.
@mal
-atom xml:str; #use a subtype for XML
+atom xml :str;
+
+command fromstr():xml
+address XMLfromString
+ comment "Convert a string to an xml. ";
+command tostr():str
+address XMLtoString
+ comment "Convert xml to string equivalent";
command xml(src:str):xml
address XMLstr2xml
@@ -49,8 +56,7 @@
command content(val:str):xml
address XMLcontent
-comment "Check the value for compliance as content, i.e.
-it may contain multiple roots and character data.";
+comment "Check the value for compliance as content, i.e. it may contain
multiple roots and character data.";
command root(val:xml, version:str, standalone:str):xml
address XMLroot
@@ -60,27 +66,36 @@
address XMLattribute
comment "Construct an attribute value pair";
-command element(name:str, ns:str, attr:str, s:xml) :xml
+command element(name:str, ns:str, attr:xml, s:xml) :xml
address XMLelement
-comment "The basic building block for XML elements are namespaces, attributes
and a sequence
-of xml elements. The name space and the attributes may be left
unspecified(=nil:bat).";
+comment "The basic building block for XML elements are namespaces, attributes
and a sequence of xml elements. The name space and the attributes may be left
unspecified(=nil:bat).";
command element(name:str, s:xml) :xml
address XMLelementSmall
-comment "The basic building block for XML elements are namespaces, attributes
and a sequence
-of xml elements. The name space and the attributes may be left
unspecified(=nil:bat).";
+comment "The basic building block for XML elements are namespaces, attributes
and a sequence of xml elements. The name space and the attributes may be left
unspecified(=nil:bat).";
-pattern concat(val:xml... ):xml
+command concat(val1:xml, val2:xml):xml
address XMLconcat
comment "Concatenate the xml values";
pattern forest(val:xml...):xml
address XMLforest
-comment "Construct an element list");
+comment "Construct an element list";
command isdocument(val:str):bit
address XMLisdocument
-comment "Validate the string as a document"
+comment "Validate the string as a document";
+
+command prelude() :void
+address XMLprelude;
+
+xml.prelude();
+
+module calc;
+command xml(src:str):xml address XMLstr2xml;
+command xml(src:xml):xml address XMLxml2xml;
+
+
@{
@- Implementation
The implementation of the XML atomary type is based
@@ -107,8 +122,14 @@
#define xml_export extern
#endif
+xml_export int TYPE_xml;
+
+xml_export int XMLfromString(str src, int *len, xml *x);
+xml_export int XMLtoString(str *s, int *len, str src);
+
xml_export str XMLxml2str(str *s, xml *x);
xml_export str XMLstr2xml(xml *x, str *s);
+xml_export str XMLxml2xml(xml *x, xml *s);
xml_export str XMLdocument(xml *x, str *s);
xml_export str XMLcontent(xml *x, str *s);
xml_export str XMLisdcoument(bit *x, str *s);
@@ -122,6 +143,8 @@
xml_export str XMLconcat(xml *ret, xml *left, xml *right);
xml_export str XMLforest(MalBlkPtr mb, MalStkPtr stk, InstrPtr p);
+xml_export str XMLprelude(void);
+
#endif /* XML_H */
@c
#include "xml.h"
@@ -136,34 +159,37 @@
str
XMLstr2xml(xml *x, str *val){
str t= *val;
- str buf= alloca(6*sizeof(*t)), s=buf;
+ str buf= alloca(6*strlen(t)), s=buf;
- for(; *t; t++)
- if (*t == '&'){
- strcat(s,"&");
- while(*s) s++;
+ for(*s = 0; *t; t++)
+ if (*t == '&') {
+ *s++ = '&'; *s++ = 'a'; *s++ = 'm'; *s++ = 'p'; *s++ = ';';
} else if (*t == '<'){
- strcat(s,"<");
- while(*s) s++;
+ *s++ = '&'; *s++ = 'l'; *s++ = 't'; *s++ = ';';
} else if (*t == '>'){
- strcat(s,">");
- while(*s) s++;
+ *s++ = '&'; *s++ = 'g'; *s++ = 't'; *s++ = ';';
}else if (*t == '"'){
- strcat(s,""");
- while(*s) s++;
+ *s++ = '&'; *s++ = 'q'; *s++ = 'u'; *s++ = 'o'; *s++ = 't';
+ *s++ = ';';
} else if (*t == '\''){
- strcat(s,"'");
- while(*s) s++;
+ *s++ = '&'; *s++ = 'a'; *s++ = 'p'; *s++ = 'o'; *s++ = 's';
+ *s++ = ';';
} else if ((*t & 0xFF) < 0x20){
- sprintf(s, "&#%d;", *t & 0xFF);
- while(*s) s++;
- } else
+ s += sprintf(s, "&#%d;", *t & 0xFF);
+ } else {
*s++= *t;
+ }
*s=0;
*x= GDKstrdup(buf);
return MAL_SUCCEED;
}
+str
+XMLxml2xml(xml *s, xml *x){
+ *s= GDKstrdup(*x);
+ return MAL_SUCCEED;
+}
+
str
XMLdocument(xml *x, str *val){
/* call the libxml2 library to perform the test */
@@ -236,22 +262,20 @@
XMLelement(xml *ret, str *name, str *nspace, xml *attr, xml *val)
{
int len;
- str buf= (str) GDKmalloc(len=strlen(*name) +
+ str buf= (str) GDKmalloc(len=2*strlen(*name) +
strlen(*nspace) + strlen(*attr)+
strlen("<></> ")+strlen(*val)+1);
if( strNil(*nspace) && strNil(*attr))
- snprintf(buf,len,"<%s>%s</%s>",
+ snprintf(buf, len,"<%s>%s</%s>",
*name, *val, *name);
- else
- if( strNil(*nspace))
- snprintf(buf,len,"<%s %s>%s</%s>",
- *name,*nspace, *val, *name);
- else
- if( strNil(*attr))
- snprintf(buf,len,"<%s %s>%s</%s>",
+ else if( strNil(*nspace))
+ snprintf(buf, len,"<%s %s>%s</%s>",
*name, *attr, *val, *name);
+ else if( strNil(*attr))
+ snprintf(buf, len,"<%s %s>%s</%s>",
+ *name, *nspace, *val, *name);
else
- snprintf(buf,len,"<%s %s %s>%s</%s>",
+ snprintf(buf, len,"<%s %s %s>%s</%s>",
*name, *nspace, *attr, *val, *name);
*ret= buf;
@@ -278,6 +302,7 @@
*ret= buf;
return MAL_SUCCEED;
}
+
str
XMLforest(MalBlkPtr mb, MalStkPtr stk, InstrPtr p)
{
@@ -297,6 +322,43 @@
return MAL_SUCCEED;
}
+int TYPE_xml;
+
+str
+XMLprelude()
+{
+ TYPE_xml = ATOMindex("xml");
+ return MAL_SUCCEED;
+}
+
+int XMLfromString(str src, int *len, xml *x)
+{
+ if (*x)
+ GDKfree(*x);
+ XMLstr2xml(x, &src);
+ *len = strlen(*x);
+ return *len;
+}
+
+int XMLtoString(str *s, int *len, str src)
+{
+ int l;
+
+ if (GDK_STRNIL(src)) {
+ *s = GDKstrdup("nil");
+ return 0;
+ }
+ l = src == 0 ? 3 : strlen(src) + 1;
+ if (l >= *len) {
+ GDKfree(*s);
+ *s = (str) GDKmalloc(l);
+ }
+ snprintf(*s, l, "%s", src);
+ *len = l - 1;
+ return *len;
+}
+
+
@sql
CREATE TYPE xml EXTERNAL NAME xml;
@@ -307,7 +369,7 @@
CREATE FUNCTION pi (nme STRING, val STRING) RETURNS xml external name xml.pi;
CREATE FUNCTION root (val STRING, version STRING, standalone STRING) RETURNS
xml external name xml.root;
CREATE FUNCTION attribute (nme STRING, val STRING) RETURNS xml external name
xml.attribute;
-CREATE FUNCTION element (nme STRING, ns STRING, attr xml, s xml) RETURNS xml
external name xml.element;
+CREATE FUNCTION "element" (nme STRING, ns STRING, attr xml, s xml) RETURNS xml
external name xml.element;
CREATE FUNCTION concat (val1 xml, val2 xml) RETURNS xml external name
xml.concat;
CREATE FUNCTION forest (val1 xml, val2 xml) RETURNS xml external name
xml.forest;
CREATE FUNCTION isdocument (val STRING) RETURNS xml external name
xml.isdocument;
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Monetdb-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/monetdb-checkins