diff --git a/include/Request.h b/include/Request.h index f85e45f704..1078fd8898 100644 --- a/include/Request.h +++ b/include/Request.h @@ -219,13 +219,6 @@ protected: * @return string for logging */ string allocate_error (PoolObjectSQL::ObjectType obj, const string& error); - - /** - * Logs allocate errors - * @param message with the allocate error details (parsing) - * @return string for logging - */ - string allocate_error (char *error); }; /* -------------------------------------------------------------------------- */ diff --git a/include/RequestManagerVirtualNetwork.h b/include/RequestManagerVirtualNetwork.h index 46759ef899..de11553656 100644 --- a/include/RequestManagerVirtualNetwork.h +++ b/include/RequestManagerVirtualNetwork.h @@ -52,7 +52,7 @@ protected: string& error_str) = 0; /* -------------------------------------------------------------------- */ - string leases_error (char * error); + string leases_error (const string& error); }; /* ------------------------------------------------------------------------- */ diff --git a/include/Template.h b/include/Template.h index 1540c1e444..ab4ab441fd 100644 --- a/include/Template.h +++ b/include/Template.h @@ -70,7 +70,7 @@ public: * Parse a string representing the template, each attribute is inserted * in the template class. * @param parse_str string with template attributes - * @param error_msg error string, must be freed by the calling funtion. + * @param error_msg error string, must be freed by the calling function. * This string is null if no error occurred. * @return 0 on success. */ @@ -79,12 +79,23 @@ public: /** * Parse a template file. * @param filename of the template file - * @param error_msg error string, must be freed by the calling funtion. + * @param error_msg error string, must be freed by the calling function. * This string is null if no error occurred. * @return 0 on success. */ int parse(const char * filename, char **error_msg); + /** + * Parse a string representing the template, automatically detecting if + * it is the default syntax, or an XML template. Each attribute is inserted + * in the template class. + * @param parse_str string with template attributes, or XML template + * @param error_msg error string, must be freed by the calling function. + * This string is null if no error occurred. + * @return 0 on success. + */ + int parse_str_or_xml(const string &parse_str, string& error_msg); + /** * Marshall a template. This function generates a single string with the * template attributes ("VAR=VAL..."). diff --git a/src/pool/PoolObjectSQL.cc b/src/pool/PoolObjectSQL.cc index 208687f42c..2a1706b171 100644 --- a/src/pool/PoolObjectSQL.cc +++ b/src/pool/PoolObjectSQL.cc @@ -169,7 +169,6 @@ void PoolObjectSQL::set_template_error_message(const string& message) int PoolObjectSQL::replace_template(const string& tmpl_str, string& error) { Template * new_tmpl = get_new_template(); - char * error_msg = 0; if ( new_tmpl == 0 ) { @@ -177,21 +176,8 @@ int PoolObjectSQL::replace_template(const string& tmpl_str, string& error) return -1; } - if ( new_tmpl->parse(tmpl_str, &error_msg) != 0 ) + if ( new_tmpl->parse_str_or_xml(tmpl_str, error) != 0 ) { - ostringstream oss; - - oss << "Parse error"; - - if (error_msg != 0) - { - oss << ": " << error_msg; - - free(error_msg); - } - - error = oss.str(); - return -1; } diff --git a/src/rm/Request.cc b/src/rm/Request.cc index 4e44e59d9d..4b8b929b96 100644 --- a/src/rm/Request.cc +++ b/src/rm/Request.cc @@ -293,22 +293,3 @@ string Request::allocate_error (const string& error) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ - -string Request::allocate_error (char *error) -{ - ostringstream oss; - - oss << "Parse error"; - - if ( error != 0 ) - { - oss << ": " << error; - free(error); - } - else - { - oss << "."; - } - - return allocate_error(oss.str()); -} diff --git a/src/rm/RequestManagerAllocate.cc b/src/rm/RequestManagerAllocate.cc index 35c8282c2b..051463f1dc 100644 --- a/src/rm/RequestManagerAllocate.cc +++ b/src/rm/RequestManagerAllocate.cc @@ -99,16 +99,15 @@ void RequestManagerAllocate::request_execute(xmlrpc_c::paramList const& params, if ( do_template == true ) { - char * error_msg = 0; string str_tmpl = xmlrpc_c::value_string(params.getString(1)); tmpl = get_object_template(); - rc = tmpl->parse(str_tmpl, &error_msg); + rc = tmpl->parse_str_or_xml(str_tmpl, error_str); if ( rc != 0 ) { - failure_response(INTERNAL, allocate_error(error_msg), att); + failure_response(INTERNAL, allocate_error(error_str), att); delete tmpl; return; diff --git a/src/rm/RequestManagerVirtualMachine.cc b/src/rm/RequestManagerVirtualMachine.cc index 175643446a..084b31c274 100644 --- a/src/rm/RequestManagerVirtualMachine.cc +++ b/src/rm/RequestManagerVirtualMachine.cc @@ -420,7 +420,6 @@ void VirtualMachineSaveDisk::request_execute(xmlrpc_c::paramList const& paramLis int rc; ostringstream oss; string error_str; - char * error_char; // ------------------ Template for the new image ------------------ @@ -437,7 +436,7 @@ void VirtualMachineSaveDisk::request_execute(xmlrpc_c::paramList const& paramLis itemplate = new ImageTemplate; - itemplate->parse(oss.str(), &error_char); + itemplate->parse_str_or_xml(oss.str(), error_str); // ------------------ Authorize the operation ------------------ diff --git a/src/rm/RequestManagerVirtualNetwork.cc b/src/rm/RequestManagerVirtualNetwork.cc index d6278ef4f7..33ce01e512 100644 --- a/src/rm/RequestManagerVirtualNetwork.cc +++ b/src/rm/RequestManagerVirtualNetwork.cc @@ -22,19 +22,9 @@ using namespace std; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -string RequestManagerVirtualNetwork::leases_error (char *error) +string RequestManagerVirtualNetwork::leases_error (const string& error) { - ostringstream oss; - - oss << "Parse error."; - - if ( error != 0 ) - { - oss << " " << error; - free(error); - } - - return request_error("Error modifying network leases",oss.str()); + return request_error("Error modifying network leases",error); } /* ------------------------------------------------------------------------- */ @@ -50,7 +40,6 @@ void RequestManagerVirtualNetwork:: VirtualNetworkTemplate tmpl; VirtualNetwork * vn; - char * error_msg = 0; string error_str; int rc; @@ -59,11 +48,11 @@ void RequestManagerVirtualNetwork:: return; } - rc = tmpl.parse(str_tmpl, &error_msg); + rc = tmpl.parse_str_or_xml(str_tmpl, error_str); if ( rc != 0 ) { - failure_response(INTERNAL, leases_error(error_msg), att); + failure_response(INTERNAL, leases_error(error_str), att); return; } diff --git a/src/template/Template.cc b/src/template/Template.cc index 15a9842f03..d714182659 100644 --- a/src/template/Template.cc +++ b/src/template/Template.cc @@ -135,6 +135,52 @@ error_yy: /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +int Template::parse_str_or_xml(const string &parse_str, string& error_msg) +{ + int rc; + + if ( parse_str[0] == '<' ) + { + rc = from_xml(parse_str); + + if ( rc != 0 ) + { + error_msg = "Parse error: XML Template malformed."; + } + } + else + { + char * error_char = 0; + + rc = parse(parse_str, &error_char); + + if ( rc != 0 ) + { + ostringstream oss; + + oss << "Parse error"; + + if (error_char != 0) + { + oss << ": " << error_char; + } + else + { + oss << "."; + } + + error_msg = oss.str(); + + free(error_char); + } + } + + return rc; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void Template::marshall(string &str, const char delim) { multimap::iterator it;