#include <expat.h>
#include <exception>
#include <vector>
+#include <memory>
/*DOC:
==xml module==
ptr.pop_back ();
}
};
+
+ virtual void newNode (const XML_Char* name, const XML_Char** atts) = 0;
+ virtual void addText (const ustring& s) = 0;
+ virtual void endNode () = 0;
+};
+
+class xmlReaderData_rg: public xmlReaderData {
+public:
+ xmlReaderData_rg () {};
+ virtual ~xmlReaderData_rg () {};
+
virtual void newNode (const XML_Char* name, const XML_Char** atts) {
MNodeList a;
a.append (newMNode_sym (new ustring (fixUTF8 (ustring (name)))));
a.append (NULL);
for (; *atts; ) {
-#if 0
- a.append (newMNode_sym (new ustring (fixUTF8 (ustring (*atts)))));
+ MNode* c = new MNode;
+ c->set_car (newMNode_sym (new ustring (fixUTF8 (ustring (*atts)))));
atts ++;
- a.append (newMNode_str (new ustring (fixUTF8 (ustring (*atts)))));
+ c->set_cdr (newMNode_str (new ustring (fixUTF8 (ustring (*atts)))));
atts ++;
-#endif
+ a.append (c);
+ }
+ ptr.push_back (a.release ());
+ mode.push_back (M_START);
+ text.resize (0);
+ };
+ virtual void addText (const ustring& s) {
+ mode_t m = mode.back ();
+
+ if (m == M_START || m == M_TEXT) {
+ text.append (s);
+ mode.back () = M_TEXT;
+ }
+ };
+ virtual void endNode () {
+ MNode* a = ptr.back ();
+ mode_t m = mode.back ();
+
+ ptr.pop_back ();
+ mode.pop_back ();
+
+ switch (m) {
+ case M_START:
+ break;
+ case M_TEXT:
+ a->cdr ()->set_car (newMNode_str (new ustring (text)));
+ break;
+ case M_CHILD:
+ break;
+ default:;
+ }
+ if (ptr.size () == 0) {
+ ans = a;
+ } else {
+ if (! ptr.back ()->cdr ()->car ()) {
+ MNode* c = new MNode;
+ c->set_car (a);
+ ptr.back ()->cdr ()->set_car (c);
+ } else if (ptr.back ()->cdr ()->car ()->isCons ()) {
+ MNode* c = ptr.back ()->cdr ()->car ();
+ MNode* d = new MNode;
+
+ while (c->cdr ()) {
+ c = c->cdr ();
+ }
+ c->set_cdr (d);
+ d->set_car (a);
+ } else {
+ assert (0);
+ }
+ mode.back () = M_CHILD;
+ }
+ };
+};
+
+class xmlReaderData_sxml: public xmlReaderData {
+public:
+ xmlReaderData_sxml () {};
+ virtual ~xmlReaderData_sxml () {};
+
+ // ******
+ virtual void newNode (const XML_Char* name, const XML_Char** atts) {
+ MNodeList a;
+ a.append (newMNode_sym (new ustring (fixUTF8 (ustring (name)))));
+ a.append (NULL);
+ for (; *atts; ) {
MNode* c = new MNode;
c->set_car (newMNode_sym (new ustring (fixUTF8 (ustring (*atts)))));
atts ++;
/*DOC:
===xml-read===
- (xml-read TEXT) -> LIST
+ (xml-read TEXT [#sxml | :sxml BOOL]) -> LIST
*/
//#AFUNC xml-read ml_xml_read
MNode* ml_xml_read (MNode* cell, MlEnv* mlenv) {
MNode* arg = cell->cdr ();
ustring text;
- xmlReaderData data;
+ bool fsxml = false;
+// xmlReaderData_rg data;
+ std::auto_ptr<xmlReaderData> data;
expat exp (false);
int rc;
+ std::vector<MNode*> params;
+ std::vector<MNode*> keywords;
+ static paramList kwlist[] = {
+ {CharConst ("sxml"), true}, // 0
+ {NULL, 0, 0}
+ };
+#if 0
if (! arg)
throw (uErrorWrongNumber);
text = eval_str (arg->car (), mlenv);
nextNode (arg);
if (arg)
throw (uErrorWrongNumber);
-
+#endif
+ setParams (arg, 1, ¶ms, kwlist, &keywords, NULL);
+ text = eval_str (params[0], mlenv);
+ if (keywords[0])
+ fsxml = eval_bool (keywords[0], mlenv);
+
+ if (fsxml)
+ data.reset (new xmlReaderData_sxml);
+ else
+ data.reset (new xmlReaderData_rg);
exp.setElementHandler (xmlstart, xmlend, xmltext);
- exp.setUserData (&data);
+ exp.setUserData (data.get ());
rc = exp.parse (text.data (), text.length (), true);
#ifdef DEBUG
if (! rc) {
#endif /* DEBUG */
#ifdef DEBUG2
- std::cerr << data.ans ()->dump_string () << "\n";
+ std::cerr << data->ans ()->dump_string () << "\n";
#endif /* DEBUG */
- return data.ans.release ();
+ return data->ans.release ();
}
/*DOC:
===xml-read-file===
- (xml-read-file FILENAME) -> LIST
+ (xml-read-file FILENAME [#sxml | :sxml BOOL]) -> LIST
Element ::= (list TagName ElementBody Attribute* )
Attribute ::= (cons AttributeName AttributeValue )
MNode* ml_xml_read_file (MNode* cell, MlEnv* mlenv) {
MNode* arg = cell->cdr ();
ustring filename;
- xmlReaderData data;
+ bool fsxml = false;
+// xmlReaderData_rg data;
+ std::auto_ptr<xmlReaderData> data;
expat exp;
FileMacro fp;
size_t s;
+ std::vector<MNode*> params;
+ std::vector<MNode*> keywords;
+ static paramList kwlist[] = {
+ {CharConst ("sxml"), true}, // 0
+ {NULL, 0, 0}
+ };
+#if 0
if (! arg)
throw (uErrorWrongNumber);
filename = eval_str (arg->car (), mlenv);
nextNode (arg);
if (arg)
throw (uErrorWrongNumber);
-
+#endif
+ setParams (arg, 1, ¶ms, kwlist, &keywords, NULL);
+ filename = eval_str (params[0], mlenv);
+ if (keywords[0])
+ fsxml = eval_bool (keywords[0], mlenv);
+
+ if (fsxml)
+ data.reset (new xmlReaderData_sxml);
+ else
+ data.reset (new xmlReaderData_rg);
if (mlenv->env->storedir.empty ())
throw (uErrorNoStore);
filename = mlenv->env->path_store_file (filename);
if (fp.openRead (filename.c_str ())) {
exp.setElementHandler (xmlstart, xmlend, xmltext);
- exp.setUserData (&data);
+ exp.setUserData (data.get ());
while (1) {
s = fp.read (exp.buff, exp.buffsize);
exp.parse (s, (s == 0));
}
#ifdef DEBUG2
- std::cerr << data.ans ()->dump_string () << "\n";
+ std::cerr << data->ans ()->dump_string () << "\n";
#endif /* DEBUG */
- return data.ans.release ();
+ return data->ans.release ();
}
static void xmlWrite (MotorOutput* out, MNode* list) {
c = list->car ();
d = list->cdr ();
out->out_raw (CharConst ("<"))->out_toHTML_noCtrl (*name);
-#if 0
- while (d && d->isCons ()
- && d->cdr () && d->cdr ()->isCons ()) {
- out->out_raw (uSPC)
- ->out_toHTML (to_string (d->car ()))
- ->out_raw (CharConst ("=\""))
- ->out_toHTML (to_string (d->cdr ()->car ()))
- ->out_raw (uQ2);
- d = d->cdr ()->cdr ();
- }
-#endif
while (d && d->isCons ()) {
if (d->car () && d->car ()->isCons ()) {
out->out_raw (uSPC)
/*DOC:
===xml-output===
- (xml-output [#continue | :continue BOOL] LIST) -> NIL
+ (xml-output [#continue | :continue BOOL] [#sxml | :sxml BOOL] LIST) -> NIL
*/
//#AFUNC xml-output ml_xml_output
MNode* arg = cell->cdr ();
MNodePtr xml;
bool cflag = false;
+ bool fsxml = false;
std::vector<MNode*> params;
std::vector<MNode*> keywords;
static paramList kwlist[] = {
{CharConst ("continue"), true},
+ {CharConst ("sxml"), true},
{NULL, 0, 0}
};
setParams (arg, 1, ¶ms, kwlist, &keywords, NULL);
xml = eval (params[0], mlenv);
- if (keywords[0] && eval_bool (keywords[0], mlenv))
- cflag = true;
+ if (keywords[0])
+ cflag = eval_bool (keywords[0], mlenv);
+ if (keywords[1])
+ fsxml = eval_bool (keywords[1], mlenv);
if (! mlenv->env->responseDone)
mlenv->env->standardResponse (ustring (CharConst (kMIME_XML)), ustring (uUTF8), uEmpty, false);
/*DOC:
===xml-output-string===
- (xml-output LIST) -> STRING
+ (xml-output LIST [#sxml | :sxml BOOL]) -> STRING
*/
//#AFUNC xml-output-string ml_xml_output_string
MNode* ml_xml_output_string (MNode* cell, MlEnv* mlenv) {
MNode* arg = cell->cdr ();
MNodePtr xml;
+ bool fsxml = false;
std::vector<MNode*> params;
+ std::vector<MNode*> keywords;
+ static paramList kwlist[] = {
+ {CharConst ("sxml"), true}, // 0
+ {NULL, 0, 0}
+ };
- setParams (arg, 1, ¶ms, NULL, NULL, NULL);
+ setParams (arg, 1, ¶ms, kwlist, &keywords, NULL);
xml = eval (params[0], mlenv);
+ if (keywords[0])
+ fsxml = eval_bool (keywords[0], mlenv);
if (! mlenv->env->responseDone)
mlenv->env->standardResponse (ustring (CharConst (kMIME_XML)), ustring (uUTF8), uEmpty, false);
/*DOC:
===input-xml===
- (input-xml) -> LIST
+ (input-xml [#sxml | :sxml BOOL]) -> LIST
*/
//#AFUNC input-xml ml_input_xml
MNode* ml_input_xml (MNode* cell, MlEnv* mlenv) {
MNode* arg = cell->cdr ();
- xmlReaderData data;
+ bool fsxml = false;
+// xmlReaderData_rg data;
+ std::auto_ptr<xmlReaderData> data;
expat exp (false);
+ std::vector<MNode*> keywords;
+ static paramList kwlist[] = {
+ {CharConst ("sxml"), true}, // 0
+ {NULL, 0, 0}
+ };
- if (arg)
- throw (uErrorWrongNumber);
+// if (arg)
+// throw (uErrorWrongNumber);
+ setParams (arg, 0, NULL, kwlist, &keywords, NULL);
+ if (keywords[0])
+ fsxml = eval_bool (keywords[0], mlenv);
+
+ if (fsxml)
+ data.reset (new xmlReaderData_sxml);
+ else
+ data.reset (new xmlReaderData_rg);
exp.setElementHandler (xmlstart, xmlend, xmltext);
- exp.setUserData (&data);
- exp.parse (mlenv->env->form->postData.data (), mlenv->env->form->postData.length (), true);
+ exp.setUserData (data.get ());
+ exp.parse (mlenv->env->form->queryString.data (), mlenv->env->form->queryString.length (), true);
#ifdef DEBUG2
- std::cerr << data.ans ()->dump_string () << "\n";
+ std::cerr << data->ans ()->dump_string () << "\n";
#endif /* DEBUG */
- return data.ans.release ();
+ return data->ans.release ();
}