#include "afeval.h" #include "affunctions.h" #include #include #include #include #include string filter; map vars; bool loadRequest(); int main( int argc, char** argv ) { FilterEvaluator e; registerBuiltinFunctions(); while (true) { bool result; try { e.reset(); if (!loadRequest()) continue; e.setVars( vars ); result = e.evaluateFilter( filter ); } catch (AFPException excep) { cout << "EXCEPTION: " << excep.what() << endl; cerr << "EXCEPTION: " << excep.what() << endl; } cout << ( result ? "MATCH\n" : "NOMATCH\n" ); // exit(result ? 1 : 0); // Exit 0 means OK, exit 1 means match } } /* REQUEST FORMAT: value RULE CONTENT */ bool loadRequest() { // Parse the XML. xmlpp::DomParser parser; parser.set_substitute_entities(); stringbuf sb(ios::out | ios::in); cin.get( sb, '\x04' ); cin.get(); string text = sb.str(); // Remove the NULL for( string::iterator it = text.begin(); it!=text.end(); ++it ) { if (*it == '\x04') { text.erase(it); } } if (text.size() < 2) { return false; } istringstream ss(text); parser.parse_stream( ss ); // parser.parse_file( "xml.test" ); xmlpp::Node* rootNode = parser.get_document()->get_root_node(); // Get vars xmlpp::Node::NodeList varNodes = rootNode->get_children( "vars" ); if (varNodes.begin() == varNodes.end()) { throw AFPException( "Request did not contain any vars" ); } xmlpp::Node::Node* varNode = *(varNodes.begin()); // Get the element varNodes = varNode->get_children( "var" ); // Iterate through child nodes for (xmlpp::Node::NodeList::const_iterator it = varNodes.begin(); it!=varNodes.end(); ++it) { xmlpp::Element* n = dynamic_cast(*it); string attName = n->get_attribute( "key" )->get_value(); if (n->has_child_text()) { string attValue = n->get_child_text()->get_content(); vars[attName] = AFPData(attValue); } else { vars[attName] = ""; } } //Get code. xmlpp::Node::NodeList codeNodes = rootNode->get_children( "rule" ); if (codeNodes.begin() == codeNodes.end()) { throw new AFPException( "Request did not contain any filter" ); } xmlpp::Node* codeNode = *(codeNodes.begin()); xmlpp::Element* codeElement = dynamic_cast(codeNode); filter = codeElement->get_child_text()->get_content(); return true; }