string toString(PElement element) { if (element == NULL) { return "*"; } string result; PElement child = element->getChild(); while (child != NULL) { if (child->getTagname() == "#text") { // Then this child only contains text, no markup result += child->getText(); } else if (child->getTagname() == "#cdata") { ; } else { // Since only using on <that> and <pattern> // The tag can only be <bot_xxx/> or <bot name="xxx"/> or <name/> if (child->getTagname() == "name") { result += Kernel::respond("BOT NAME", "unknown"); } else if (child->getTagname() == "bot") { // Is a <bot name="xxx"/> predicate result += Kernel::respond("BOT " + toUpper(child->getAttribute("name")), "unknown"); } else { // It is old-skool <bot_xxx/> result += Kernel::respond("BOT " + toUpper(child->getTagname().substr(4)), "unknown"); } } child = child->getNextSibling(); } return trim(toUpper(result));//Substituter::normalize(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 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 IrcResponder::respond(Match *m, PElement element, const string &id) { if (element->getNamespace() != "irc") { return ""; } else { string tag = element->getTagname(); if (tag == "channel") { return mChannel; } else if (tag == "nick") { return mNick; } else if (tag == "command") { string cmd = Kernel::process(m, element, this, id); if (cmd[0] == '/') { processAlias(cmd); } else { q.push(cmd); } } else if (tag == "exit") { disconnect(); serverRunning = false; // Natural quit } else { cout << "<irc:" << tag << "> not supported" << endl; } } return ""; }
// 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; }
PElement TemplateProcessor::checkCompatibility(PElement e) { string elementName = e->getTagname(); if (elementName.find("_") != string::npos) { StringTokenizer st(elementName, "_"); string action = st.nextToken(); string property = st.nextToken(); e->setTagname(action); e->setAttribute("name", property); } return e; }
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 { 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; }
void XmlSocketResponder::recv(string &s) { strstream strs; strs << s << endl; PElement root = parser->parse(strs, "meh"); if (root == NULL) { return; } if (root->getTagname() != "message") { // send("<message>Error processing message</message>"); return; } if (root->getChild("#text") == NULL) { return; } string text = root->getChild("#text")->getText(); string reply = Kernel::respond(text, "xmlsocket"); send("<message>" + reply + "</message>"); }