42 #include "CmdTranslation.h" 43 #include "BESTokenizer.h" 44 #include "BESSyntaxUserError.h" 46 #define MY_ENCODING "ISO-8859-1" 48 map<string, CmdTranslation::p_cmd_translator> CmdTranslation::_translations;
49 bool CmdTranslation::_is_show =
false;
51 int CmdTranslation::initialize(
int,
char**)
53 _translations[
"show"] = CmdTranslation::translate_show;
54 _translations[
"show.catalog"] = CmdTranslation::translate_catalog;
55 _translations[
"show.info"] = CmdTranslation::translate_catalog;
56 _translations[
"show.error"] = CmdTranslation::translate_show_error;
57 _translations[
"set"] = CmdTranslation::translate_set;
58 _translations[
"set.context"] = CmdTranslation::translate_context;
59 _translations[
"set.container"] = CmdTranslation::translate_container;
60 _translations[
"define"] = CmdTranslation::translate_define;
61 _translations[
"delete"] = CmdTranslation::translate_delete;
62 _translations[
"get"] = CmdTranslation::translate_get;
66 int CmdTranslation::terminate(
void)
71 void CmdTranslation::add_translation(
const string &name, p_cmd_translator func)
73 CmdTranslation::_translations[name] = func;
76 void CmdTranslation::remove_translation(
const string &name)
78 map<string, p_cmd_translator>::iterator i = CmdTranslation::_translations.find(name);
79 if (i != CmdTranslation::_translations.end()) {
80 CmdTranslation::_translations.erase(i);
84 string CmdTranslation::translate(
const string &commands)
96 cerr <<
"failed to build tokenizer for translation" << endl;
104 xmlTextWriterPtr writer = 0;
105 xmlBufferPtr buf = 0;
110 buf = xmlBufferCreate();
112 cerr <<
"testXmlwriterMemory: Error creating the xml buffer" << endl;
118 writer = xmlNewTextWriterMemory(buf, 0);
119 if (writer == NULL) {
120 cerr <<
"testXmlwriterMemory: Error creating the xml writer" << endl;
127 rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
129 cerr <<
"testXmlwriterMemory: Error at xmlTextWriterStartDocument" << endl;
130 xmlFreeTextWriter(writer);
136 rc = xmlTextWriterStartElement(writer, BAD_CAST
"request");
138 cerr <<
"testXmlwriterMemory: Error at xmlTextWriterStartElement" << endl;
139 xmlFreeTextWriter(writer);
144 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"reqID",
145 BAD_CAST
"some_unique_value");
147 cerr <<
"failed to add the request id attribute" << endl;
151 bool status = do_translate(t, writer);
153 xmlFreeTextWriter(writer);
158 rc = xmlTextWriterEndElement(writer);
160 cerr <<
"failed to close request element" << endl;
161 xmlFreeTextWriter(writer);
165 rc = xmlTextWriterEndDocument(writer);
167 cerr <<
"failed to end the document" << endl;
171 xmlFreeTextWriter(writer);
176 cerr <<
"failed to retrieve document as string" << endl;
179 doc = (
char *) buf->content;
189 bool CmdTranslation::do_translate(
BESTokenizer &t, xmlTextWriterPtr writer)
192 CmdTranslation::p_cmd_translator p = _translations[token];
194 cerr << endl <<
"Invalid command " << token << endl << endl;
199 bool status = p(t, writer);
225 return do_translate(t, writer);
228 bool CmdTranslation::translate_show(
BESTokenizer &t, xmlTextWriterPtr writer)
230 CmdTranslation::set_show(
true);
233 if (show_what.empty()) {
234 t.
parse_error(
"show command must be followed by target");
237 string new_cmd =
"show." + show_what;
238 CmdTranslation::p_cmd_translator p = _translations[new_cmd];
245 string err = (string)
"show " + show_what +
" commands must end with a semicolon";
248 show_what[0] = toupper(show_what[0]);
249 string tag =
"show" + show_what;
252 int rc = xmlTextWriterStartElement(writer, BAD_CAST tag.c_str());
254 cerr <<
"failed to start " << tag <<
" element" << endl;
259 rc = xmlTextWriterEndElement(writer);
261 cerr <<
"failed to close " << tag <<
" element" << endl;
268 bool CmdTranslation::translate_show_error(
BESTokenizer &t, xmlTextWriterPtr writer)
271 if (show_what.empty() || show_what !=
"error") {
277 string err = (string)
"show " + show_what +
" command must include the error type to show";
283 string err = (string)
"show " + show_what +
" commands must end with a semicolon";
286 show_what[0] = toupper(show_what[0]);
287 string tag =
"show" + show_what;
290 int rc = xmlTextWriterStartElement(writer, BAD_CAST tag.c_str());
292 cerr <<
"failed to start " << tag <<
" element" << endl;
297 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"type",
298 BAD_CAST etype.c_str());
300 cerr <<
"failed to add the get type attribute" << endl;
305 rc = xmlTextWriterEndElement(writer);
307 cerr <<
"failed to close " << tag <<
" element" << endl;
314 bool CmdTranslation::translate_catalog(
BESTokenizer &t, xmlTextWriterPtr writer)
319 if (show_what.empty() || (show_what !=
"info" && show_what !=
"catalog")) {
320 t.
parse_error(
"show command must be info or catalog");
323 show_what[0] = toupper(show_what[0]);
324 string tag =
"show" + show_what;
328 if (token ==
"for") {
331 t.
parse_error(
"show catalog command expecting node");
337 t.
parse_error(
"show command must be terminated by a semicolon");
341 int rc = xmlTextWriterStartElement(writer, BAD_CAST tag.c_str());
343 cerr <<
"failed to start " << tag <<
" element" << endl;
349 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"node",
350 BAD_CAST node.c_str());
352 cerr <<
"failed to add the catalog node attribute" << endl;
358 rc = xmlTextWriterEndElement(writer);
360 cerr <<
"failed to close " << tag <<
" element" << endl;
367 bool CmdTranslation::translate_set(
BESTokenizer &t, xmlTextWriterPtr writer)
370 if (set_what.empty()) {
371 t.
parse_error(
"set command must be followed by target");
374 string new_cmd =
"set." + set_what;
375 CmdTranslation::p_cmd_translator p = _translations[new_cmd];
377 cerr <<
"no such command: set " << set_what << endl;
384 bool CmdTranslation::translate_context(
BESTokenizer &t, xmlTextWriterPtr writer)
394 t.
parse_error(
"missing word \"to\" in set context");
402 t.
parse_error(
"set context command must end with semicolon");
406 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"setContext");
408 cerr <<
"failed to start setContext element" << endl;
413 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"name",
414 BAD_CAST name.c_str());
416 cerr <<
"failed to add the context name attribute" << endl;
421 rc = xmlTextWriterWriteString(writer, BAD_CAST value.c_str());
423 cerr <<
"failed to write the value of the context" << endl;
428 rc = xmlTextWriterEndElement(writer);
430 cerr <<
"failed to close setContext element" << endl;
437 bool CmdTranslation::translate_container(
BESTokenizer &t, xmlTextWriterPtr writer)
445 if (space ==
"values" || space ==
";") {
446 t.
parse_error(
"expecting name of container storage");
450 if (token !=
"values") {
455 if (name ==
";" || name ==
",") {
461 t.
parse_error(
"missing comma in set container after name");
465 if (value ==
"," || value ==
";") {
466 t.
parse_error(
"expecting location of the container");
480 t.
parse_error(
"set container command must end with semicolon");
484 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"setContainer");
486 cerr <<
"failed to start setContext element" << endl;
491 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"name",
492 BAD_CAST name.c_str());
494 cerr <<
"failed to add the context name attribute" << endl;
498 if (!space.empty()) {
500 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"space",
501 BAD_CAST space.c_str());
503 cerr <<
"failed to add the container space attribute" << endl;
510 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"type",
511 BAD_CAST type.c_str());
513 cerr <<
"failed to add the container type attribute" << endl;
519 rc = xmlTextWriterWriteString(writer, BAD_CAST value.c_str());
521 cerr <<
"failed to write the location of the container" << endl;
526 rc = xmlTextWriterEndElement(writer);
528 cerr <<
"failed to close setContext element" << endl;
535 bool CmdTranslation::translate_define(
BESTokenizer &t, xmlTextWriterPtr writer)
555 t.
parse_error(
"Looking for keyword as in define command");
558 list<string> containers;
559 map<string, string> clist;
563 containers.push_back(token);
564 clist[token] = token;
572 map<string, string> constraints;
573 string default_constraint;
574 map<string, string> attrs;
575 if (token ==
"with") {
578 while (token !=
"aggregate" && token !=
";") {
580 if (token ==
"constraint") {
586 t.
parse_error(
"constraint container does not exist");
592 else if (type == 2) {
609 if (token ==
"aggregate") {
614 if (token !=
"using") {
615 t.
parse_error(
"aggregation expecting keyword \"using\"");
619 else if (token ==
"using") {
623 t.
parse_error(
"aggregation expecting keyword \"by\"");
628 t.
parse_error(
"aggregation expecting keyword \"by\" or \"using\"");
635 t.
parse_error(
"define command must end with semicolon");
639 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"define");
641 cerr <<
"failed to start setContext element" << endl;
646 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"name",
647 BAD_CAST name.c_str());
649 cerr <<
"failed to add the context name attribute" << endl;
653 if (!space.empty()) {
655 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"space",
656 BAD_CAST space.c_str());
658 cerr <<
"failed to add the container space attribute" << endl;
664 if (!default_constraint.empty()) {
666 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"constraint");
668 cerr <<
"failed to start container constraint element" << endl;
673 rc = xmlTextWriterWriteString(writer, BAD_CAST default_constraint.c_str());
675 cerr <<
"failed to write constraint for container" << endl;
680 rc = xmlTextWriterEndElement(writer);
682 cerr <<
"failed to close constraint element" << endl;
687 list<string>::iterator i = containers.begin();
688 list<string>::iterator e = containers.end();
689 for (; i != e; i++) {
691 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"container");
693 cerr <<
"failed to start container element" << endl;
698 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"name",
699 BAD_CAST (*i).c_str());
701 cerr <<
"failed to add the context name attribute" << endl;
706 string constraint = constraints[(*i)];
707 if (!constraint.empty()) {
709 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"constraint");
711 cerr <<
"failed to start container constraint element" << endl;
716 rc = xmlTextWriterWriteString(writer, BAD_CAST constraint.c_str());
718 cerr <<
"failed to write constraint for container" << endl;
723 rc = xmlTextWriterEndElement(writer);
725 cerr <<
"failed to close constraint element" << endl;
730 string attr = attrs[(*i)];
733 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"attributes");
735 cerr <<
"failed to start container attributes element" << endl;
740 rc = xmlTextWriterWriteString(writer, BAD_CAST attr.c_str());
742 cerr <<
"failed to write attributes for container" << endl;
747 rc = xmlTextWriterEndElement(writer);
749 cerr <<
"failed to close attributes element" << endl;
755 rc = xmlTextWriterEndElement(writer);
757 cerr <<
"failed to close setContext element" << endl;
762 if (!agg_cmd.empty()) {
764 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"aggregate");
766 cerr <<
"failed to start aggregate element" << endl;
770 if (!agg_handler.empty()) {
772 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"handler",
773 BAD_CAST agg_handler.c_str());
775 cerr <<
"failed to add the context name attribute" << endl;
781 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"cmd",
782 BAD_CAST agg_cmd.c_str());
784 cerr <<
"failed to add the context name attribute" << endl;
789 rc = xmlTextWriterEndElement(writer);
791 cerr <<
"failed to close setContext element" << endl;
797 rc = xmlTextWriterEndElement(writer);
799 cerr <<
"failed to close setContext element" << endl;
806 bool CmdTranslation::translate_delete(
BESTokenizer &t, xmlTextWriterPtr writer)
819 string new_cmd =
"delete." + del_what;
821 CmdTranslation::p_cmd_translator p = _translations[new_cmd];
827 if (del_what ==
"container" || del_what ==
"definition") {
830 else if (del_what ==
"containers" || del_what ==
"definitions") {
837 del_what[0] = toupper(del_what[0]);
838 string tag =
"delete" + del_what;
847 if (token ==
"from") {
853 t.
parse_error(
"delete command expected to end with semicolon");
857 int rc = xmlTextWriterStartElement(writer, BAD_CAST tag.c_str());
859 cerr <<
"failed to start aggregate element" << endl;
865 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"name",
866 BAD_CAST name.c_str());
868 cerr <<
"failed to add the context name attribute" << endl;
873 if (!space.empty()) {
875 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"space",
876 BAD_CAST space.c_str());
878 cerr <<
"failed to add the context name attribute" << endl;
884 rc = xmlTextWriterEndElement(writer);
886 cerr <<
"failed to close setContext element" << endl;
893 bool CmdTranslation::translate_get(
BESTokenizer &t, xmlTextWriterPtr writer)
901 if (token !=
"for") {
902 t.
parse_error(
"get command expecting keyword \"for\"");
913 if (token ==
"return") {
916 t.
parse_error(
"get command expecting keyword \"as\" for return");
921 else if (token ==
"using") {
925 else if (token ==
"contentStartId") {
929 else if (token ==
"mimeBoundary") {
933 else if (token ==
";") {
942 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"get");
944 cerr <<
"failed to start aggregate element" << endl;
949 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"type",
950 BAD_CAST get_what.c_str());
952 cerr <<
"failed to add the get type attribute" << endl;
957 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"definition",
958 BAD_CAST def_name.c_str());
960 cerr <<
"failed to add the get definition attribute" << endl;
966 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"url",
967 BAD_CAST url.c_str());
969 cerr <<
"failed to add the url attribute" << endl;
974 if (!returnAs.empty()) {
976 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"returnAs",
977 BAD_CAST returnAs.c_str());
979 cerr <<
"failed to add the returnAs attribute" << endl;
984 if (!starting.empty()) {
986 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"contentStartId");
988 cerr <<
"failed to start contentStartId element" << endl;
993 rc = xmlTextWriterWriteString(writer, BAD_CAST starting.c_str());
995 cerr <<
"failed to write contentStartId for get request" << endl;
1000 rc = xmlTextWriterEndElement(writer);
1002 cerr <<
"failed to close constraint element" << endl;
1007 if (!bounding.empty()) {
1009 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"mimeBoundary");
1011 cerr <<
"failed to start mimeBoundary element" << endl;
1016 rc = xmlTextWriterWriteString(writer, BAD_CAST bounding.c_str());
1018 cerr <<
"failed to write mimeBoundary for get request" << endl;
1023 rc = xmlTextWriterEndElement(writer);
1025 cerr <<
"failed to close mimeBoundary element" << endl;
1031 rc = xmlTextWriterEndElement(writer);
1033 cerr <<
"failed to close get element" << endl;
1040 void CmdTranslation::dump(ostream &strm)
1042 strm << BESIndent::LMarg <<
"CmdTranslation::dump" << endl;
1043 BESIndent::Indent();
1044 if (_translations.empty()) {
1045 strm << BESIndent::LMarg <<
"NO translations registered" << endl;
1048 strm << BESIndent::LMarg <<
"translations registered" << endl;
1049 BESIndent::Indent();
1050 map<string, p_cmd_translator>::iterator i = _translations.begin();
1051 map<string, p_cmd_translator>::iterator e = _translations.end();
1052 for (; i != e; i++) {
1053 strm << BESIndent::LMarg << (*i).first << endl;
1055 BESIndent::UnIndent();
1057 BESIndent::UnIndent();
virtual std::string get_message()
get the error message for this exception
tokenizer for the BES request command string
string & get_next_token()
returns the next token from the token list
error thrown if there is a user syntax error in the request or any other user error
string & get_first_token()
returns the first token from the token list
string remove_quotes(const string &s)
removes quotes from a quoted token
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
void tokenize(const char *p)
tokenize the BES request/command string
string & get_current_token()
returns the current token from the token list