46 #define MY_ENCODING "ISO-8859-1" 48 map< string, CmdTranslation::p_cmd_translator > CmdTranslation::_translations ;
49 bool CmdTranslation::_is_show = false ;
54 _translations[
"show"] = CmdTranslation::translate_show ;
55 _translations[
"show.catalog"] = CmdTranslation::translate_catalog ;
56 _translations[
"show.info"] = CmdTranslation::translate_catalog ;
57 _translations[
"show.error"] = CmdTranslation::translate_show_error ;
58 _translations[
"set"] = CmdTranslation::translate_set ;
59 _translations[
"set.context"] = CmdTranslation::translate_context ;
60 _translations[
"set.container"] = CmdTranslation::translate_container ;
61 _translations[
"define"] = CmdTranslation::translate_define ;
62 _translations[
"delete"] = CmdTranslation::translate_delete ;
63 _translations[
"get"] = CmdTranslation::translate_get ;
76 CmdTranslation::_translations[name] = func ;
82 map<string,p_cmd_translator>::iterator i =
83 CmdTranslation::_translations.find( name ) ;
84 if( i != CmdTranslation::_translations.end() )
86 CmdTranslation::_translations.erase( i ) ;
106 cerr <<
"failed to build tokenizer for translation" << endl ;
114 xmlTextWriterPtr writer = 0 ;
115 xmlBufferPtr buf = 0 ;
120 buf = xmlBufferCreate() ;
123 cerr <<
"testXmlwriterMemory: Error creating the xml buffer" << endl ;
129 writer = xmlNewTextWriterMemory( buf, 0 ) ;
132 cerr <<
"testXmlwriterMemory: Error creating the xml writer" << endl ;
139 rc = xmlTextWriterStartDocument( writer, NULL,
MY_ENCODING, NULL ) ;
142 cerr <<
"testXmlwriterMemory: Error at xmlTextWriterStartDocument" 144 xmlFreeTextWriter( writer ) ;
150 rc = xmlTextWriterStartElement( writer, BAD_CAST
"request" ) ;
153 cerr <<
"testXmlwriterMemory: Error at xmlTextWriterStartElement" 155 xmlFreeTextWriter( writer ) ;
160 rc = xmlTextWriterWriteAttribute( writer, BAD_CAST
"reqID",
161 BAD_CAST
"some_unique_value" ) ;
164 cerr <<
"failed to add the request id attribute" << endl ;
168 bool status = do_translate( t, writer ) ;
171 xmlFreeTextWriter( writer ) ;
176 rc = xmlTextWriterEndElement( writer ) ;
179 cerr <<
"failed to close request element" << endl ;
180 xmlFreeTextWriter( writer ) ;
184 rc = xmlTextWriterEndDocument( writer ) ;
187 cerr <<
"failed to end the document" << endl ;
191 xmlFreeTextWriter(writer);
197 cerr <<
"failed to retrieve document as string" << endl ;
201 doc = (
char *)buf->content ;
204 xmlBufferFree( buf ) ;
212 CmdTranslation::do_translate(
BESTokenizer &t, xmlTextWriterPtr writer )
215 CmdTranslation::p_cmd_translator p = _translations[token] ;
218 cerr << endl <<
"Invalid command " << token << endl << endl ;
224 bool status = p( t, writer ) ;
255 return do_translate( t, writer ) ;
259 CmdTranslation::translate_show(
BESTokenizer &t, xmlTextWriterPtr writer )
264 if( show_what.empty() )
266 t.
parse_error(
"show command must be followed by target" ) ;
269 string new_cmd =
"show." + show_what ;
270 CmdTranslation::p_cmd_translator p = _translations[new_cmd] ;
273 return p( t, writer ) ;
279 string err = (string)
"show " + show_what
280 +
" commands must end with a semicolon" ;
283 show_what[0] = toupper( show_what[0] ) ;
284 string tag =
"show" + show_what ;
287 int rc = xmlTextWriterStartElement( writer, BAD_CAST tag.c_str() ) ;
290 cerr <<
"failed to start " << tag <<
" element" << endl ;
295 rc = xmlTextWriterEndElement( writer ) ;
298 cerr <<
"failed to close " << tag <<
" element" << endl ;
306 CmdTranslation::translate_show_error(
BESTokenizer &t, xmlTextWriterPtr writer)
309 if( show_what.empty() || show_what !=
"error" )
317 string err = (string)
"show " + show_what
318 +
" command must inlude the error type to show" ;
325 string err = (string)
"show " + show_what
326 +
" commands must end with a semicolon" ;
329 show_what[0] = toupper( show_what[0] ) ;
330 string tag =
"show" + show_what ;
333 int rc = xmlTextWriterStartElement( writer, BAD_CAST tag.c_str() ) ;
336 cerr <<
"failed to start " << tag <<
" element" << endl ;
341 rc = xmlTextWriterWriteAttribute( writer, BAD_CAST
"type",
342 BAD_CAST etype.c_str() ) ;
345 cerr <<
"failed to add the get type attribute" << endl ;
350 rc = xmlTextWriterEndElement( writer ) ;
353 cerr <<
"failed to close " << tag <<
" element" << endl ;
361 CmdTranslation::translate_catalog(
BESTokenizer &t, xmlTextWriterPtr writer )
366 if( show_what.empty() || ( show_what !=
"info" && show_what !=
"catalog" ) )
368 t.
parse_error(
"show command must be info or catalog" ) ;
371 show_what[0] = toupper( show_what[0] ) ;
372 string tag =
"show" + show_what ;
381 t.
parse_error(
"show catalog command expecting node" ) ;
388 t.
parse_error(
"show command must be terminated by a semicolon" ) ;
392 int rc = xmlTextWriterStartElement( writer, BAD_CAST tag.c_str() ) ;
395 cerr <<
"failed to start " << tag <<
" element" << endl ;
402 rc = xmlTextWriterWriteAttribute( writer, BAD_CAST
"node",
403 BAD_CAST node.c_str() ) ;
406 cerr <<
"failed to add the catalog node attribute" << endl ;
412 rc = xmlTextWriterEndElement( writer ) ;
415 cerr <<
"failed to close " << tag <<
" element" << endl ;
424 xmlTextWriterPtr writer )
427 if( set_what.empty() )
429 t.
parse_error(
"set command must be followed by target" ) ;
432 string new_cmd =
"set." + set_what ;
433 CmdTranslation::p_cmd_translator p = _translations[new_cmd] ;
436 cerr <<
"no such command: set " << set_what << endl ;
440 return p( t, writer ) ;
445 xmlTextWriterPtr writer )
457 t.
parse_error(
"missing word \"to\" in set context" ) ;
467 t.
parse_error(
"set context command must end with semicolon" ) ;
471 int rc = xmlTextWriterStartElement( writer, BAD_CAST
"setContext" ) ;
474 cerr <<
"failed to start setContext element" << endl ;
479 rc = xmlTextWriterWriteAttribute( writer, BAD_CAST
"name",
480 BAD_CAST name.c_str() ) ;
483 cerr <<
"failed to add the context name attribute" << endl ;
488 rc = xmlTextWriterWriteString( writer, BAD_CAST value.c_str() ) ;
491 cerr <<
"failed to write the value of the context" << endl ;
496 rc = xmlTextWriterEndElement( writer ) ;
499 cerr <<
"failed to close setContext element" << endl ;
508 xmlTextWriterPtr writer )
517 if( space ==
"values" || space ==
";" )
519 t.
parse_error(
"expecting name of container storage" ) ;
523 if( token !=
"values" )
525 t.
parse_error(
"missing values for set container" ) ;
529 if( name ==
";" || name ==
"," )
531 t.
parse_error(
"expecting name of the container" ) ;
537 t.
parse_error(
"missing comma in set container after name" ) ;
541 if( value ==
"," || value ==
";" )
543 t.
parse_error(
"expecting location of the container" ) ;
560 t.
parse_error(
"set container command must end with semicolon" ) ;
564 int rc = xmlTextWriterStartElement( writer, BAD_CAST
"setContainer" ) ;
567 cerr <<
"failed to start setContext element" << endl ;
572 rc = xmlTextWriterWriteAttribute( writer, BAD_CAST
"name",
573 BAD_CAST name.c_str() ) ;
576 cerr <<
"failed to add the context name attribute" << endl ;
583 rc = xmlTextWriterWriteAttribute( writer, BAD_CAST
"space",
584 BAD_CAST space.c_str() ) ;
587 cerr <<
"failed to add the container space attribute" << endl ;
595 rc = xmlTextWriterWriteAttribute( writer, BAD_CAST
"type",
596 BAD_CAST type.c_str() ) ;
599 cerr <<
"failed to add the container type attribute" << endl ;
605 rc = xmlTextWriterWriteString( writer, BAD_CAST value.c_str() ) ;
608 cerr <<
"failed to write the location of the container" << endl ;
613 rc = xmlTextWriterEndElement( writer ) ;
616 cerr <<
"failed to close setContext element" << endl ;
625 xmlTextWriterPtr writer )
647 t.
parse_error(
"Looking for keyword as in define command" ) ;
650 list<string> containers ;
651 map<string,string> clist ;
656 containers.push_back( token ) ;
657 clist[token] = token ;
666 map<string,string> constraints ;
667 string default_constraint ;
668 map<string,string> attrs ;
669 if( token ==
"with" )
673 while( token !=
"aggregate" && token !=
";" )
676 if( token ==
"constraint" )
685 t.
parse_error(
"contstraint container does not exist" ) ;
712 if( token ==
"aggregate" )
719 if( token !=
"using" )
721 t.
parse_error(
"aggregation expecting keyword \"using\"");
725 else if( token ==
"using" )
731 t.
parse_error(
"aggregation expecting keyword \"by\"");
737 t.
parse_error(
"aggregation expecting keyword \"by\" or \"using\"");
745 t.
parse_error(
"define command must end with semicolon" ) ;
749 int rc = xmlTextWriterStartElement( writer, BAD_CAST
"define" ) ;
752 cerr <<
"failed to start setContext element" << endl ;
757 rc = xmlTextWriterWriteAttribute( writer, BAD_CAST
"name",
758 BAD_CAST name.c_str() ) ;
761 cerr <<
"failed to add the context name attribute" << endl ;
768 rc = xmlTextWriterWriteAttribute( writer, BAD_CAST
"space",
769 BAD_CAST space.c_str() ) ;
772 cerr <<
"failed to add the container space attribute" << endl ;
778 if( !default_constraint.empty() )
781 int rc = xmlTextWriterStartElement( writer, BAD_CAST
"constraint" );
784 cerr <<
"failed to start container constraint element" << endl ;
789 rc = xmlTextWriterWriteString( writer, BAD_CAST default_constraint.c_str());
792 cerr <<
"failed to write constraint for container" << endl ;
797 rc = xmlTextWriterEndElement( writer ) ;
800 cerr <<
"failed to close constraint element" << endl ;
805 list<string>::iterator i = containers.begin() ;
806 list<string>::iterator e = containers.end() ;
810 int rc = xmlTextWriterStartElement( writer, BAD_CAST
"container" ) ;
813 cerr <<
"failed to start container element" << endl ;
818 rc = xmlTextWriterWriteAttribute( writer, BAD_CAST
"name",
819 BAD_CAST (*i).c_str() ) ;
822 cerr <<
"failed to add the context name attribute" << endl ;
827 string constraint = constraints[(*i)] ;
828 if( !constraint.empty() )
831 int rc = xmlTextWriterStartElement( writer, BAD_CAST
"constraint" );
834 cerr <<
"failed to start container constraint element" << endl ;
839 rc = xmlTextWriterWriteString( writer, BAD_CAST constraint.c_str());
842 cerr <<
"failed to write constraint for container" << endl ;
847 rc = xmlTextWriterEndElement( writer ) ;
850 cerr <<
"failed to close constraint element" << endl ;
855 string attr = attrs[(*i)] ;
859 int rc = xmlTextWriterStartElement( writer, BAD_CAST
"attributes" );
862 cerr <<
"failed to start container attributes element" << endl ;
867 rc = xmlTextWriterWriteString( writer, BAD_CAST attr.c_str());
870 cerr <<
"failed to write attributes for container" << endl ;
875 rc = xmlTextWriterEndElement( writer ) ;
878 cerr <<
"failed to close attributes element" << endl ;
884 rc = xmlTextWriterEndElement( writer ) ;
887 cerr <<
"failed to close setContext element" << endl ;
892 if( !agg_cmd.empty() )
895 int rc = xmlTextWriterStartElement( writer, BAD_CAST
"aggregate" ) ;
898 cerr <<
"failed to start aggregate element" << endl ;
902 if( !agg_handler.empty() )
905 rc = xmlTextWriterWriteAttribute( writer, BAD_CAST
"handler",
906 BAD_CAST agg_handler.c_str() ) ;
909 cerr <<
"failed to add the context name attribute" << endl ;
915 rc = xmlTextWriterWriteAttribute( writer, BAD_CAST
"cmd",
916 BAD_CAST agg_cmd.c_str() ) ;
919 cerr <<
"failed to add the context name attribute" << endl ;
924 rc = xmlTextWriterEndElement( writer ) ;
927 cerr <<
"failed to close setContext element" << endl ;
933 rc = xmlTextWriterEndElement( writer ) ;
936 cerr <<
"failed to close setContext element" << endl ;
945 xmlTextWriterPtr writer )
958 string new_cmd =
"delete." + del_what ;
960 CmdTranslation::p_cmd_translator p = _translations[new_cmd] ;
963 return p( t, writer ) ;
967 if( del_what ==
"container" || del_what ==
"definition" )
971 else if( del_what ==
"containers" || del_what ==
"definitions" )
980 del_what[0] = toupper( del_what[0] ) ;
981 string tag =
"delete" + del_what ;
991 if( token ==
"from" )
999 t.
parse_error(
"delete command expected to end with semicolon" ) ;
1003 int rc = xmlTextWriterStartElement( writer, BAD_CAST tag.c_str() ) ;
1006 cerr <<
"failed to start aggregate element" << endl ;
1013 rc = xmlTextWriterWriteAttribute( writer, BAD_CAST
"name",
1014 BAD_CAST name.c_str() ) ;
1017 cerr <<
"failed to add the context name attribute" << endl ;
1022 if( !space.empty() )
1025 rc = xmlTextWriterWriteAttribute( writer, BAD_CAST
"space",
1026 BAD_CAST space.c_str() ) ;
1029 cerr <<
"failed to add the context name attribute" << endl ;
1035 rc = xmlTextWriterEndElement( writer ) ;
1038 cerr <<
"failed to close setContext element" << endl ;
1047 xmlTextWriterPtr writer )
1055 if( token !=
"for" )
1057 t.
parse_error(
"get command expecting keyword \"for\"" ) ;
1069 if( token ==
"return" )
1074 t.
parse_error(
"get command expecting keyword \"as\" for return" ) ;
1079 else if( token ==
"using" )
1084 else if( token ==
"contentStartId" )
1089 else if( token ==
"mimeBoundary" )
1094 else if( token ==
";" )
1100 t.
parse_error(
"unexpected token in get command" ) ;
1105 int rc = xmlTextWriterStartElement( writer, BAD_CAST
"get" ) ;
1108 cerr <<
"failed to start aggregate element" << endl ;
1113 rc = xmlTextWriterWriteAttribute( writer, BAD_CAST
"type",
1114 BAD_CAST get_what.c_str() ) ;
1117 cerr <<
"failed to add the get type attribute" << endl ;
1122 rc = xmlTextWriterWriteAttribute( writer, BAD_CAST
"definition",
1123 BAD_CAST def_name.c_str() ) ;
1126 cerr <<
"failed to add the get definition attribute" << endl ;
1133 rc = xmlTextWriterWriteAttribute( writer, BAD_CAST
"url",
1134 BAD_CAST url.c_str() ) ;
1137 cerr <<
"failed to add the url attribute" << endl ;
1142 if( !returnAs.empty() )
1145 rc = xmlTextWriterWriteAttribute( writer, BAD_CAST
"returnAs",
1146 BAD_CAST returnAs.c_str() ) ;
1149 cerr <<
"failed to add the returnAs attribute" << endl ;
1154 if( !starting.empty() )
1157 int rc = xmlTextWriterStartElement( writer, BAD_CAST
"contentStartId" );
1160 cerr <<
"failed to start contentStartId element" << endl ;
1165 rc = xmlTextWriterWriteString( writer, BAD_CAST starting.c_str());
1168 cerr <<
"failed to write contentStartId for get request" << endl ;
1173 rc = xmlTextWriterEndElement( writer ) ;
1176 cerr <<
"failed to close constraint element" << endl ;
1181 if( !bounding.empty() )
1184 int rc = xmlTextWriterStartElement( writer, BAD_CAST
"mimeBoundary" );
1187 cerr <<
"failed to start mimeBoundary element" << endl ;
1192 rc = xmlTextWriterWriteString( writer, BAD_CAST bounding.c_str());
1195 cerr <<
"failed to write mimeBoundary for get request" << endl ;
1200 rc = xmlTextWriterEndElement( writer ) ;
1203 cerr <<
"failed to close mimeBoundary element" << endl ;
1209 rc = xmlTextWriterEndElement( writer ) ;
1212 cerr <<
"failed to close get element" << endl ;
1224 if( _translations.empty() )
1232 map<string,p_cmd_translator>::iterator i = _translations.begin() ;
1233 map<string,p_cmd_translator>::iterator e = _translations.end() ;
1234 for( ; i != e; i++ )
static void add_translation(const string &name, p_cmd_translator func)
tokenizer for the BES request command string
string & get_next_token()
returns the next token from the token list
static void set_show(bool val)
static int terminate(void)
error thrown if there is a user syntax error in the request or any other user error ...
virtual string get_message()
get the error message for this exception
static string translate(const string &commands)
string & get_first_token()
returns the first token from the token list
string remove_quotes(const string &s)
removes quotes from a quoted token
static ostream & LMarg(ostream &strm)
static void remove_translation(const string &name)
static void dump(ostream &strm)
string parse_container_name(const string &s, unsigned int &type)
parses a container name for constraint and attributes
void parse_error(const string &s="")
throws an exception giving the tokens up to the point of the problem
static int initialize(int argc, char **argv)
void tokenize(const char *p)
tokenize the BES request/command string
string & get_current_token()
returns the current token from the token list