45#include "CmdTranslation.h"
46#include "BESTokenizer.h"
47#include "BESSyntaxUserError.h"
49#define MY_ENCODING "ISO-8859-1"
51map<string, CmdTranslation::p_cmd_translator> CmdTranslation::_translations;
52bool CmdTranslation::_is_show =
false;
54int CmdTranslation::initialize(
int,
char**)
56 _translations[
"show"] = CmdTranslation::translate_show;
57 _translations[
"show.catalog"] = CmdTranslation::translate_catalog;
58 _translations[
"show.info"] = CmdTranslation::translate_catalog;
59 _translations[
"show.error"] = CmdTranslation::translate_show_error;
60 _translations[
"set"] = CmdTranslation::translate_set;
61 _translations[
"set.context"] = CmdTranslation::translate_context;
62 _translations[
"set.container"] = CmdTranslation::translate_container;
63 _translations[
"define"] = CmdTranslation::translate_define;
64 _translations[
"delete"] = CmdTranslation::translate_delete;
65 _translations[
"get"] = CmdTranslation::translate_get;
69int CmdTranslation::terminate(
void)
74void CmdTranslation::add_translation(
const string &name, p_cmd_translator func)
76 CmdTranslation::_translations[name] = func;
79void CmdTranslation::remove_translation(
const string &name)
81 map<string, p_cmd_translator>::iterator i = CmdTranslation::_translations.find(name);
82 if (i != CmdTranslation::_translations.end()) {
83 CmdTranslation::_translations.erase(i);
87string CmdTranslation::translate(
const string &commands)
99 cerr <<
"failed to build tokenizer for translation" << endl;
107 xmlTextWriterPtr writer = 0;
108 xmlBufferPtr buf = 0;
113 buf = xmlBufferCreate();
115 cerr <<
"testXmlwriterMemory: Error creating the xml buffer" << endl;
121 writer = xmlNewTextWriterMemory(buf, 0);
122 if (writer == NULL) {
123 cerr <<
"testXmlwriterMemory: Error creating the xml writer" << endl;
130 rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
132 cerr <<
"testXmlwriterMemory: Error at xmlTextWriterStartDocument" << endl;
133 xmlFreeTextWriter(writer);
139 rc = xmlTextWriterStartElement(writer, BAD_CAST
"request");
141 cerr <<
"testXmlwriterMemory: Error at xmlTextWriterStartElement" << endl;
142 xmlFreeTextWriter(writer);
147 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"reqID",
148 BAD_CAST
"some_unique_value");
150 cerr <<
"failed to add the request id attribute" << endl;
154 bool status = do_translate(t, writer);
156 xmlFreeTextWriter(writer);
161 rc = xmlTextWriterEndElement(writer);
163 cerr <<
"failed to close request element" << endl;
164 xmlFreeTextWriter(writer);
168 rc = xmlTextWriterEndDocument(writer);
170 cerr <<
"failed to end the document" << endl;
174 xmlFreeTextWriter(writer);
179 cerr <<
"failed to retrieve document as string" << endl;
182 doc = (
char *) buf->content;
192bool CmdTranslation::do_translate(
BESTokenizer &t, xmlTextWriterPtr writer)
195 CmdTranslation::p_cmd_translator p = _translations[token];
197 cerr << endl <<
"Invalid command " << token << endl << endl;
202 bool status = p(t, writer);
228 return do_translate(t, writer);
231bool CmdTranslation::translate_show(
BESTokenizer &t, xmlTextWriterPtr writer)
233 CmdTranslation::set_show(
true);
236 if (show_what.empty()) {
237 t.
parse_error(
"show command must be followed by target");
240 string new_cmd =
"show." + show_what;
241 CmdTranslation::p_cmd_translator p = _translations[new_cmd];
248 string err = (string)
"show " + show_what +
" commands must end with a semicolon";
251 show_what[0] = toupper(show_what[0]);
252 string tag =
"show" + show_what;
255 int rc = xmlTextWriterStartElement(writer, BAD_CAST tag.c_str());
257 cerr <<
"failed to start " << tag <<
" element" << endl;
262 rc = xmlTextWriterEndElement(writer);
264 cerr <<
"failed to close " << tag <<
" element" << endl;
271bool CmdTranslation::translate_show_error(
BESTokenizer &t, xmlTextWriterPtr writer)
274 if (show_what.empty() || show_what !=
"error") {
280 string err = (string)
"show " + show_what +
" command must include the error type to show";
286 string err = (string)
"show " + show_what +
" commands must end with a semicolon";
289 show_what[0] = toupper(show_what[0]);
290 string tag =
"show" + show_what;
293 int rc = xmlTextWriterStartElement(writer, BAD_CAST tag.c_str());
295 cerr <<
"failed to start " << tag <<
" element" << endl;
300 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"type",
301 BAD_CAST etype.c_str());
303 cerr <<
"failed to add the get type attribute" << endl;
308 rc = xmlTextWriterEndElement(writer);
310 cerr <<
"failed to close " << tag <<
" element" << endl;
317bool CmdTranslation::translate_catalog(
BESTokenizer &t, xmlTextWriterPtr writer)
322 if (show_what.empty() || (show_what !=
"info" && show_what !=
"catalog")) {
323 t.
parse_error(
"show command must be info or catalog");
326 show_what[0] = toupper(show_what[0]);
327 string tag =
"show" + show_what;
331 if (token ==
"for") {
334 t.
parse_error(
"show catalog command expecting node");
340 t.
parse_error(
"show command must be terminated by a semicolon");
344 int rc = xmlTextWriterStartElement(writer, BAD_CAST tag.c_str());
346 cerr <<
"failed to start " << tag <<
" element" << endl;
352 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"node",
353 BAD_CAST node.c_str());
355 cerr <<
"failed to add the catalog node attribute" << endl;
361 rc = xmlTextWriterEndElement(writer);
363 cerr <<
"failed to close " << tag <<
" element" << endl;
370bool CmdTranslation::translate_set(
BESTokenizer &t, xmlTextWriterPtr writer)
373 if (set_what.empty()) {
374 t.
parse_error(
"set command must be followed by target");
377 string new_cmd =
"set." + set_what;
378 CmdTranslation::p_cmd_translator p = _translations[new_cmd];
380 cerr <<
"no such command: set " << set_what << endl;
387bool CmdTranslation::translate_context(
BESTokenizer &t, xmlTextWriterPtr writer)
397 t.
parse_error(
"missing word \"to\" in set context");
405 t.
parse_error(
"set context command must end with semicolon");
409 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"setContext");
411 cerr <<
"failed to start setContext element" << endl;
416 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"name",
417 BAD_CAST name.c_str());
419 cerr <<
"failed to add the context name attribute" << endl;
424 rc = xmlTextWriterWriteString(writer, BAD_CAST value.c_str());
426 cerr <<
"failed to write the value of the context" << endl;
431 rc = xmlTextWriterEndElement(writer);
433 cerr <<
"failed to close setContext element" << endl;
440bool CmdTranslation::translate_container(
BESTokenizer &t, xmlTextWriterPtr writer)
448 if (space ==
"values" || space ==
";") {
449 t.
parse_error(
"expecting name of container storage");
453 if (token !=
"values") {
458 if (name ==
";" || name ==
",") {
464 t.
parse_error(
"missing comma in set container after name");
468 if (value ==
"," || value ==
";") {
469 t.
parse_error(
"expecting location of the container");
483 t.
parse_error(
"set container command must end with semicolon");
487 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"setContainer");
489 cerr <<
"failed to start setContext element" << endl;
494 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"name",
495 BAD_CAST name.c_str());
497 cerr <<
"failed to add the context name attribute" << endl;
501 if (!space.empty()) {
503 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"space",
504 BAD_CAST space.c_str());
506 cerr <<
"failed to add the container space attribute" << endl;
513 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"type",
514 BAD_CAST type.c_str());
516 cerr <<
"failed to add the container type attribute" << endl;
522 rc = xmlTextWriterWriteString(writer, BAD_CAST value.c_str());
524 cerr <<
"failed to write the location of the container" << endl;
529 rc = xmlTextWriterEndElement(writer);
531 cerr <<
"failed to close setContext element" << endl;
538bool CmdTranslation::translate_define(
BESTokenizer &t, xmlTextWriterPtr writer)
558 t.
parse_error(
"Looking for keyword as in define command");
561 list<string> containers;
562 map<string, string> clist;
566 containers.push_back(token);
567 clist[token] = token;
575 map<string, string> constraints;
576 string default_constraint;
577 map<string, string> attrs;
578 if (token ==
"with") {
581 while (token !=
"aggregate" && token !=
";") {
583 if (token ==
"constraint") {
589 t.
parse_error(
"constraint container does not exist");
595 else if (type == 2) {
612 if (token ==
"aggregate") {
617 if (token !=
"using") {
618 t.
parse_error(
"aggregation expecting keyword \"using\"");
622 else if (token ==
"using") {
626 t.
parse_error(
"aggregation expecting keyword \"by\"");
631 t.
parse_error(
"aggregation expecting keyword \"by\" or \"using\"");
638 t.
parse_error(
"define command must end with semicolon");
642 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"define");
644 cerr <<
"failed to start setContext element" << endl;
649 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"name",
650 BAD_CAST name.c_str());
652 cerr <<
"failed to add the context name attribute" << endl;
656 if (!space.empty()) {
658 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"space",
659 BAD_CAST space.c_str());
661 cerr <<
"failed to add the container space attribute" << endl;
667 if (!default_constraint.empty()) {
669 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"constraint");
671 cerr <<
"failed to start container constraint element" << endl;
676 rc = xmlTextWriterWriteString(writer, BAD_CAST default_constraint.c_str());
678 cerr <<
"failed to write constraint for container" << endl;
683 rc = xmlTextWriterEndElement(writer);
685 cerr <<
"failed to close constraint element" << endl;
690 list<string>::iterator i = containers.begin();
691 list<string>::iterator e = containers.end();
692 for (; i != e; i++) {
694 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"container");
696 cerr <<
"failed to start container element" << endl;
701 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"name",
702 BAD_CAST (*i).c_str());
704 cerr <<
"failed to add the context name attribute" << endl;
709 string constraint = constraints[(*i)];
710 if (!constraint.empty()) {
712 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"constraint");
714 cerr <<
"failed to start container constraint element" << endl;
719 rc = xmlTextWriterWriteString(writer, BAD_CAST constraint.c_str());
721 cerr <<
"failed to write constraint for container" << endl;
726 rc = xmlTextWriterEndElement(writer);
728 cerr <<
"failed to close constraint element" << endl;
733 string attr = attrs[(*i)];
736 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"attributes");
738 cerr <<
"failed to start container attributes element" << endl;
743 rc = xmlTextWriterWriteString(writer, BAD_CAST attr.c_str());
745 cerr <<
"failed to write attributes for container" << endl;
750 rc = xmlTextWriterEndElement(writer);
752 cerr <<
"failed to close attributes element" << endl;
758 rc = xmlTextWriterEndElement(writer);
760 cerr <<
"failed to close setContext element" << endl;
765 if (!agg_cmd.empty()) {
767 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"aggregate");
769 cerr <<
"failed to start aggregate element" << endl;
773 if (!agg_handler.empty()) {
775 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"handler",
776 BAD_CAST agg_handler.c_str());
778 cerr <<
"failed to add the context name attribute" << endl;
784 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"cmd",
785 BAD_CAST agg_cmd.c_str());
787 cerr <<
"failed to add the context name attribute" << endl;
792 rc = xmlTextWriterEndElement(writer);
794 cerr <<
"failed to close setContext element" << endl;
800 rc = xmlTextWriterEndElement(writer);
802 cerr <<
"failed to close setContext element" << endl;
809bool CmdTranslation::translate_delete(
BESTokenizer &t, xmlTextWriterPtr writer)
822 string new_cmd =
"delete." + del_what;
824 CmdTranslation::p_cmd_translator p = _translations[new_cmd];
830 if (del_what ==
"container" || del_what ==
"definition") {
833 else if (del_what ==
"containers" || del_what ==
"definitions") {
840 del_what[0] = toupper(del_what[0]);
841 string tag =
"delete" + del_what;
850 if (token ==
"from") {
856 t.
parse_error(
"delete command expected to end with semicolon");
860 int rc = xmlTextWriterStartElement(writer, BAD_CAST tag.c_str());
862 cerr <<
"failed to start aggregate element" << endl;
868 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"name",
869 BAD_CAST name.c_str());
871 cerr <<
"failed to add the context name attribute" << endl;
876 if (!space.empty()) {
878 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"space",
879 BAD_CAST space.c_str());
881 cerr <<
"failed to add the context name attribute" << endl;
887 rc = xmlTextWriterEndElement(writer);
889 cerr <<
"failed to close setContext element" << endl;
896bool CmdTranslation::translate_get(
BESTokenizer &t, xmlTextWriterPtr writer)
904 if (token !=
"for") {
905 t.
parse_error(
"get command expecting keyword \"for\"");
916 if (token ==
"return") {
919 t.
parse_error(
"get command expecting keyword \"as\" for return");
924 else if (token ==
"using") {
928 else if (token ==
"contentStartId") {
932 else if (token ==
"mimeBoundary") {
936 else if (token ==
";") {
945 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"get");
947 cerr <<
"failed to start aggregate element" << endl;
952 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"type",
953 BAD_CAST get_what.c_str());
955 cerr <<
"failed to add the get type attribute" << endl;
960 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"definition",
961 BAD_CAST def_name.c_str());
963 cerr <<
"failed to add the get definition attribute" << endl;
969 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"url",
970 BAD_CAST url.c_str());
972 cerr <<
"failed to add the url attribute" << endl;
977 if (!returnAs.empty()) {
979 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST
"returnAs",
980 BAD_CAST returnAs.c_str());
982 cerr <<
"failed to add the returnAs attribute" << endl;
987 if (!starting.empty()) {
989 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"contentStartId");
991 cerr <<
"failed to start contentStartId element" << endl;
996 rc = xmlTextWriterWriteString(writer, BAD_CAST starting.c_str());
998 cerr <<
"failed to write contentStartId for get request" << endl;
1003 rc = xmlTextWriterEndElement(writer);
1005 cerr <<
"failed to close constraint element" << endl;
1010 if (!bounding.empty()) {
1012 int rc = xmlTextWriterStartElement(writer, BAD_CAST
"mimeBoundary");
1014 cerr <<
"failed to start mimeBoundary element" << endl;
1019 rc = xmlTextWriterWriteString(writer, BAD_CAST bounding.c_str());
1021 cerr <<
"failed to write mimeBoundary for get request" << endl;
1026 rc = xmlTextWriterEndElement(writer);
1028 cerr <<
"failed to close mimeBoundary element" << endl;
1034 rc = xmlTextWriterEndElement(writer);
1036 cerr <<
"failed to close get element" << endl;
1043void CmdTranslation::dump(ostream &strm)
1045 strm << BESIndent::LMarg <<
"CmdTranslation::dump" << endl;
1046 BESIndent::Indent();
1047 if (_translations.empty()) {
1048 strm << BESIndent::LMarg <<
"NO translations registered" << endl;
1051 strm << BESIndent::LMarg <<
"translations registered" << endl;
1052 BESIndent::Indent();
1053 map<string, p_cmd_translator>::iterator i = _translations.begin();
1054 map<string, p_cmd_translator>::iterator e = _translations.end();
1055 for (; i != e; i++) {
1056 strm << BESIndent::LMarg << (*i).first << endl;
1058 BESIndent::UnIndent();
1060 BESIndent::UnIndent();
std::string get_message() const
get the error message for this exception
error thrown if there is a user syntax error in the request or any other user error
tokenizer for the BES request command string
void parse_error(const std::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
std::string remove_quotes(const std::string &s)
removes quotes from a quoted token
std::string parse_container_name(const std::string &s, unsigned int &type)
parses a container name for constraint and attributes
std::string & get_current_token()
returns the current token from the token list
std::string & get_first_token()
returns the first token from the token list
std::string & get_next_token()
returns the next token from the token list