string AimlWriter::recurse(PElement element) { if (element == NULL) return ""; string s = ""; PElement child = element->getChild(); while (child != NULL) { if (child->getTagname() == "#text") { s += child->getText(false); } else if (child->getTagname() == "#cdata") { s += child->getText(false); } else { string tag = "<"; if (child->hasNamespace()) tag += child->getNamespace() + ":"; tag += child->getTagname(); if (child->hasAttributes()) { map<string, string> attr = child->getAttributes(); map<string, string>::const_iterator itr = attr.begin(); while (itr != attr.end()) { tag += " " + (*itr).first + "=\"" + (*itr).second + "\""; itr++; } } if (child->hasChildren()) { tag += ">" + recurse(child) + "</"; if (child->hasNamespace()) tag += child->getNamespace() + ":"; tag += child->getTagname() + ">"; } else { tag += "/>"; } s += tag; } child = child->getNextSibling(); } return s; }
string templateString(PElement element) { if (element == NULL) { return ""; } string result; PElement child = element->getChild(); while (child != NULL) { if (child->getTagname() == "#text") { result += child->getText(); } else if (child->getTagname() == "#cdata") { result += child->getText(false); } else { string tag = "<"; if (child->hasNamespace()) { tag += child->getNamespace() + ":"; } tag += child->getTagname(); if (child->hasAttributes()) { map<string, string> attr = child->getAttributes(); map<string, string>::const_iterator itr = attr.begin(); while (itr != attr.end()) { tag += " " + (*itr).first + "=\"" + (*itr).second + "\""; itr++; } } if (child->hasChildren()) { tag += ">" + templateString(child); tag += "</"; if (child->hasNamespace()) { tag += child->getNamespace() + ":"; } tag += child->getTagname() + ">"; } else { tag += "/>"; } result += tag; } child = child->getNextSibling(); } return result; }
string TemplateProcessor::process(Match *m, PElement e, Responder *r, const string &id) { string buffer = ""; if (e == NULL) { return buffer; } try { PElement child = e->getChild(); while (child != NULL) { if (child->getTagname() == "#text") { buffer += child->getText(); } else if (child->getTagname() == "#cdata") { buffer += child->getText(false); } else { // child = checkCompatability(child); /////////////// BEGIN NIGHTMARE ///////////////////////////////// if (child->hasNamespace()) { // Then we need to use the responder if (r == NULL) { // This is temporary!!!!! } else { buffer += r->respond(m, child, id); } } else { /////////////// END NIGHTMARE CODE ////////////////////////////// /* string tagname = child->getTagname(); if (child->hasNamespace()) { tagname = child->getNamespace(); // Temporary hack until responders put in } */ AimlProcessor *p = TemplateProcessor::getProcessor(child->getTagname()); if (p == NULL) { // Then nothing to process this tag with buffer += process(m, child, r, id); // is that right? } else { buffer += p->process(m, child, r, id); } } } child = child->getNextSibling(); } } catch (int &ex) { } return buffer; }
string TemplateProcessor::process(Match *m, PElement e, Responder *r, const string &id) { string buffer = ""; if (e == NULL) { return buffer; } try { PElement child = e->getChild(); while (child != NULL) { if (child->getTagname() == "#text") { buffer += child->getText(); } else if (child->getTagname() == "#cdata") { buffer += child->getText(false); } else { if (child->hasNamespace()) { if (r == NULL) { ; } else { buffer += r->respond(m, child, id); } } else { child = checkCompatibility(child); AimlProcessor *p = TemplateProcessor::getProcessor(child->getTagname()); if (p == NULL) { // Then nothing to process this tag with buffer += process(m, child, r, id); } else { buffer += p->process(m, child, r, id); } } } child = child->getNextSibling(); } } catch (int &ex) { } return buffer; }