2008-07-29 11:03:26 +00:00
|
|
|
#include "afeval.h"
|
|
|
|
#include "affunctions.h"
|
2008-07-31 16:28:24 +00:00
|
|
|
#include <libxml++/libxml++.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
string filter;
|
|
|
|
map<string,AFPData> vars;
|
|
|
|
|
|
|
|
bool loadRequest();
|
2008-07-29 11:03:26 +00:00
|
|
|
|
|
|
|
int main( int argc, char** argv ) {
|
|
|
|
FilterEvaluator e;
|
2008-07-31 16:28:24 +00:00
|
|
|
registerBuiltinFunctions();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
e.reset();
|
|
|
|
|
|
|
|
if (!loadRequest())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool result;
|
|
|
|
|
|
|
|
try {
|
|
|
|
e.setVars( vars );
|
|
|
|
result = e.evaluateFilter( filter );
|
|
|
|
} catch (AFPException excep) {
|
|
|
|
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:
|
|
|
|
<request>
|
|
|
|
<vars>
|
|
|
|
<var key="varname">value</var>
|
|
|
|
</vars>
|
|
|
|
<rule> RULE CONTENT </rule>
|
|
|
|
</request> */
|
|
|
|
|
|
|
|
bool loadRequest() {
|
|
|
|
// Parse the XML.
|
|
|
|
xmlpp::DomParser parser;
|
|
|
|
parser.set_substitute_entities();
|
2008-07-29 11:03:26 +00:00
|
|
|
|
2008-07-31 16:28:24 +00:00
|
|
|
stringbuf sb(ios::out | ios::in);
|
|
|
|
cin.get( sb, '\x04' );
|
|
|
|
cin.get();
|
2008-07-29 11:03:26 +00:00
|
|
|
|
2008-07-31 16:28:24 +00:00
|
|
|
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" );
|
2008-07-29 11:03:26 +00:00
|
|
|
|
2008-07-31 16:28:24 +00:00
|
|
|
if (varNodes.begin() == varNodes.end()) {
|
|
|
|
throw AFPException( "Request did not contain any vars" );
|
2008-07-29 11:03:26 +00:00
|
|
|
}
|
2008-07-31 16:28:24 +00:00
|
|
|
|
|
|
|
xmlpp::Node::Node* varNode = *(varNodes.begin()); // Get the <vars> element
|
|
|
|
varNodes = varNode->get_children( "var" ); // Iterate through <var> child nodes
|
|
|
|
for (xmlpp::Node::NodeList::const_iterator it = varNodes.begin(); it!=varNodes.end(); ++it) {
|
|
|
|
xmlpp::Element* n = dynamic_cast<xmlpp::Element*>(*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] = "";
|
|
|
|
}
|
2008-07-29 11:03:26 +00:00
|
|
|
}
|
|
|
|
|
2008-07-31 16:28:24 +00:00
|
|
|
//Get code.
|
|
|
|
xmlpp::Node::NodeList codeNodes = rootNode->get_children( "rule" );
|
|
|
|
|
|
|
|
if (codeNodes.begin() == codeNodes.end()) {
|
|
|
|
throw new AFPException( "Request did not contain any filter" );
|
2008-07-29 11:03:26 +00:00
|
|
|
}
|
2008-07-31 16:28:24 +00:00
|
|
|
|
|
|
|
xmlpp::Node* codeNode = *(codeNodes.begin());
|
|
|
|
xmlpp::Element* codeElement = dynamic_cast<xmlpp::Element*>(codeNode);
|
|
|
|
|
|
|
|
filter = codeElement->get_child_text()->get_content();
|
|
|
|
|
|
|
|
return true;
|
2008-07-29 11:03:26 +00:00
|
|
|
}
|