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 XmlSocketProcessor::process(Match *m, PElement e, Responder *r, const string &id) { port = atoi(Kernel::process(m, e->getChild("port"), r, id).c_str()); server = new ServerSocket(port); server->setServerListener(this); server->init(); cout << "Starting up an XML Socket Server on port " << port << endl; SocketHandler::runLoop(); return ""; }
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; }
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 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; }
// Produces a formatted string of the AIML string AimlWriter::prettyAiml(PElement element) { if (element == NULL) return ""; string s = "<aiml>\n\n"; if (element->getTagname() != "aiml") { cerr << "Error: Document root is not 'aiml'" << endl; return ""; } velement array; velement_it ix; // Process <topic> first element->getChildren("topic", &array); // All the <topic> nodes... for (ix = array.begin(); ix != array.end(); ++ix) { velement categories; (*ix)->getChildren("category", &categories); // All of the <category> nodes per <topic> string topic = (*ix)->getAttribute("name"); s += "<topic name=\"" + topic + "\">\n\n"; for (velement_it iy = categories.begin(); iy != categories.end(); ++iy) { // Get the <that> <pattern> and <template> string Pattern = recurse((*iy)->getChild("pattern")); string That = recurse((*iy)->getChild("that")); string Template = recurse((*iy)->getChild("template")); s += "<category>\n"; if (!That.empty()) s += "<that>" + That + "</that>\n"; s += "<pattern>" + Pattern + "</pattern>\n"; s += "<template>\n" + Template + "\n</template>\n"; s += "</category>\n\n"; } s += "</topic>\n\n"; } array.clear(); element->getChildren("category", &array); // All the <category> nodes without a <topic> for (ix = array.begin(); ix != array.end(); ++ix) { // Get the <that> <pattern> and <template> string Pattern = recurse((*ix)->getChild("pattern")); string That = recurse((*ix)->getChild("that")); string Template = recurse((*ix)->getChild("template")); s += "<category>\n"; if (!That.empty()) s += "<that>" + That + "</that>\n"; s += "<pattern>" + Pattern + "</pattern>\n"; s += "<template>\n" + Template + "\n</template>\n"; s += "</category>\n\n"; } s += "</aiml>\n"; return s; }