2008-06-17 20:27:32 +04:00
/* -------------------------------------------------------------------------- */
2016-05-04 13:33:23 +03:00
/* Copyright 2002-2016, OpenNebula Project, OpenNebula Systems */
2008-06-17 20:27:32 +04:00
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
# ifndef TEMPLATE_H_
# define TEMPLATE_H_
# include <iostream>
# include <map>
# include <vector>
2010-07-29 14:53:36 +04:00
# include <libxml/tree.h>
# include <libxml/parser.h>
2008-06-17 20:27:32 +04:00
# include "Attribute.h"
using namespace std ;
/**
2011-02-01 20:26:26 +03:00
* Base class for file templates . A template is a file ( or a string for the
2008-06-17 20:27:32 +04:00
* matter of fact ) containing a set of attribute definitions of the form :
* NAME = VALUE
* where NAME is a string representing the name of the attribute , and VALUE can
2009-02-15 01:32:21 +03:00
* be a single string or a vector value ( array of string pairs ) . The file can
2008-06-17 20:27:32 +04:00
* contain several attributes with the same name .
*/
class Template
{
public :
2009-02-15 01:32:21 +03:00
Template ( bool _replace_mode = false ,
const char _separator = ' = ' ,
const char * _xml_root = " TEMPLATE " ) :
replace_mode ( _replace_mode ) ,
separator ( _separator ) ,
xml_root ( _xml_root ) { } ;
2008-06-17 20:27:32 +04:00
2011-04-11 01:55:49 +04:00
Template ( const Template & t )
{
multimap < string , Attribute * > : : const_iterator it ;
replace_mode = t . replace_mode ;
separator = t . separator ;
xml_root = t . xml_root ;
2015-05-30 14:29:27 +03:00
attributes . clear ( ) ;
2011-04-11 01:55:49 +04:00
for ( it = t . attributes . begin ( ) ; it ! = t . attributes . end ( ) ; it + + )
{
attributes . insert ( make_pair ( it - > first , ( it - > second ) - > clone ( ) ) ) ;
}
}
2015-06-01 20:26:03 +03:00
Template & operator = ( const Template & t )
{
multimap < string , Attribute * > : : const_iterator it ;
if ( this ! = & t )
{
replace_mode = t . replace_mode ;
separator = t . separator ;
xml_root = t . xml_root ;
attributes . clear ( ) ;
for ( it = t . attributes . begin ( ) ; it ! = t . attributes . end ( ) ; it + + )
{
attributes . insert ( make_pair ( it - > first , ( it - > second ) - > clone ( ) ) ) ;
}
}
return * this ;
}
2008-06-17 20:27:32 +04:00
/**
* The class destructor frees all the attributes conforming the template
*/
virtual ~ Template ( ) ;
2016-02-04 15:10:42 +03:00
/* ---------------------------------------------------------------------- */
/* Functions to create a Template parsing a file, stream in txt or XML */
/* ---------------------------------------------------------------------- */
2008-06-17 20:27:32 +04:00
/**
* Parse a string representing the template , each attribute is inserted
* in the template class .
* @ param parse_str string with template attributes
2012-01-30 22:31:21 +04:00
* @ param error_msg error string , must be freed by the calling function .
2008-06-17 20:27:32 +04:00
* This string is null if no error occurred .
* @ return 0 on success .
*/
int parse ( const string & parse_str , char * * error_msg ) ;
/**
* Parse a template file .
* @ param filename of the template file
2012-01-30 22:31:21 +04:00
* @ param error_msg error string , must be freed by the calling function .
2008-06-17 20:27:32 +04:00
* This string is null if no error occurred .
* @ return 0 on success .
*/
int parse ( const char * filename , char * * error_msg ) ;
2012-01-30 22:31:21 +04:00
/**
* 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 ) ;
2016-02-04 15:10:42 +03:00
/**
* Rebuilds the template from a xml formatted string
* @ param xml_str The xml - formatted string
*
* @ return 0 on success , - 1 otherwise
*/
int from_xml ( const string & xml_str ) ;
/**
* Rebuilds the object from an xml node
* @ param node The xml node pointer
*
* @ return 0 on success , - 1 otherwise
*/
int from_xml_node ( const xmlNodePtr node ) ;
/**
* Writes the Template into a output stream in txt format
*/
friend ostream & operator < < ( ostream & os , const Template & t ) ;
/* ---------------------------------------------------------------------- */
/* Functions to render a Template in a str, or xml */
/* ---------------------------------------------------------------------- */
2008-06-17 20:27:32 +04:00
/**
2009-02-15 01:32:21 +03:00
* Marshall a template . This function generates a single string with the
2008-06-17 20:27:32 +04:00
* template attributes ( " VAR=VAL<delim>... " ) .
* @ param str_tempalte string that hold the template
* @ param delim to separate attributes
*/
void marshall ( string & str , const char delim = ' \n ' ) ;
2009-01-26 21:25:15 +03:00
/**
* Writes the template in a simple xml string :
* < template >
* < single > value < / single >
* < vector >
* < attr > value < / attr >
* . . .
* < / vector >
* . . .
* < / template >
2009-02-15 01:32:21 +03:00
* The name of the root element is set when the Template object is created
2009-01-26 21:25:15 +03:00
* @ param xml string that hold the xml template representation
2009-05-22 04:46:52 +04:00
* @ return a reference to the generated string
2015-07-01 22:15:40 +03:00
*/
2009-05-22 04:46:52 +04:00
string & to_xml ( string & xml ) const ;
/**
* Writes the template in a plain text string
* @ param str string that hold the template representation
* @ return a reference to the generated string
2009-01-26 21:25:15 +03:00
*/
2009-05-22 04:46:52 +04:00
string & to_str ( string & str ) const ;
2009-02-15 01:32:21 +03:00
2016-02-04 15:10:42 +03:00
/* ---------------------------------------------------------------------- */
/* Functions to add, remove and change attributes from a Template */
/* ---------------------------------------------------------------------- */
2015-05-30 14:29:27 +03:00
/**
* Clears all the attributes from the template
*/
void clear ( ) ;
2008-06-17 20:27:32 +04:00
/**
* Sets a new attribute , the attribute MUST BE ALLOCATED IN THE HEAP , and
2009-02-15 01:32:21 +03:00
* will be freed when the template destructor is called .
2008-06-17 20:27:32 +04:00
* @ param attr pointer to the attribute
*/
2009-03-30 01:27:55 +04:00
virtual void set ( Attribute * attr ) ;
2008-06-17 20:27:32 +04:00
2016-03-02 01:31:31 +03:00
virtual void set ( vector < SingleAttribute * > & values )
{
_set < SingleAttribute > ( values ) ;
}
virtual void set ( vector < VectorAttribute * > & values )
{
_set < VectorAttribute > ( values ) ;
}
2012-05-25 14:56:51 +04:00
/**
2016-02-04 15:10:42 +03:00
* Adds a new attribute to the template ( replacing it if already defined )
2012-05-25 14:56:51 +04:00
* @ param name of the new attribute
* @ param value of the new attribute
* @ return 0 on success
*/
2016-02-04 15:10:42 +03:00
template < typename T >
int replace ( const string & name , const T & value )
2012-06-12 20:59:23 +04:00
{
2016-02-04 15:10:42 +03:00
std : : ostringstream oss ;
2012-06-12 20:59:23 +04:00
oss < < value ;
2013-02-23 22:49:06 +04:00
return replace ( name , oss . str ( ) ) ;
2012-06-12 20:59:23 +04:00
}
2016-02-04 15:10:42 +03:00
int replace ( const string & name , const string & value ) ;
2013-10-17 14:35:19 +04:00
2016-04-18 18:44:47 +03:00
int replace ( const string & name , const bool & value ) ;
2016-02-04 15:10:42 +03:00
/**
2012-06-12 20:59:23 +04:00
* Adds a new single attribute to the template . It will replace an existing
* one if replace_mode was set to true
2012-05-30 14:53:51 +04:00
* @ param name of the attribute
* @ param value of the attribute
*/
2016-02-04 15:10:42 +03:00
template < typename T >
void add ( const string & name , const T & value )
{
std : : ostringstream oss ;
2012-06-02 04:58:46 +04:00
oss < < value ;
set ( new SingleAttribute ( name , oss . str ( ) ) ) ;
2016-02-04 15:10:42 +03:00
}
2013-02-23 22:49:06 +04:00
2016-02-04 15:10:42 +03:00
void add ( const string & name , const string & value )
{
set ( new SingleAttribute ( name , value ) ) ;
}
2013-02-23 22:49:06 +04:00
2009-03-06 15:10:15 +03:00
/**
* Removes an attribute from the template . The attributes are returned . The
* attributes MUST be freed by the calling funtion
* @ param name of the attribute
* @ param values a vector containing a pointer to the attributes
2011-06-02 21:17:22 +04:00
* @ return the number of attributes removed
2009-03-06 15:10:15 +03:00
*/
2016-02-04 15:10:42 +03:00
template < typename T >
int remove ( const string & name , vector < T * > & values )
{
pair < multimap < string , Attribute * > : : iterator ,
multimap < string , Attribute * > : : iterator > index ;
multimap < string , Attribute * > : : iterator i ;
int j ;
index = attributes . equal_range ( name ) ;
for ( i = index . first , j = 0 ; i ! = index . second ; i + + , j + + )
{
values . push_back ( static_cast < T * > ( i - > second ) ) ;
}
attributes . erase ( index . first , index . second ) ;
return j ;
}
2009-03-06 15:10:15 +03:00
2012-06-15 14:28:20 +04:00
/**
2013-02-23 22:49:06 +04:00
* Removes an attribute from the template , but it DOES NOT free the
2012-06-15 14:28:20 +04:00
* attribute .
* @ param att Attribute to remove . It will be deleted
2013-02-23 22:49:06 +04:00
* @ return pointer to the removed attribute or 0 if non attribute was
2012-06-15 14:28:20 +04:00
* removed
*/
virtual Attribute * remove ( Attribute * att ) ;
2010-05-31 19:39:27 +04:00
/**
* Removes an attribute from the template , and frees the attributes .
* @ param name of the attribute
2011-06-02 21:17:22 +04:00
* @ return the number of attributes removed
2010-05-31 19:39:27 +04:00
*/
virtual int erase ( const string & name ) ;
2016-02-04 15:10:42 +03:00
/* ---------------------------------------------------------------------- */
/* Functions get attributes from a template */
/* ---------------------------------------------------------------------- */
2008-06-17 20:27:32 +04:00
/**
2016-02-04 15:10:42 +03:00
* Gets the all the attributes of the given name and stores a reference
* to them in a vector . If the selected attribute does not match the
* requested type it will not be included
2008-06-17 20:27:32 +04:00
* @ param name the attribute name .
2016-02-04 15:10:42 +03:00
* @ param values vector with the values
*
2011-06-02 21:17:22 +04:00
* @ return the number of elements in the vector
2008-06-17 20:27:32 +04:00
*/
2016-02-04 15:10:42 +03:00
inline virtual int get ( const string & n , vector < const VectorAttribute * > & v ) const
{
return __get < VectorAttribute > ( n , v ) ;
}
2008-11-13 19:21:17 +03:00
2016-02-04 15:10:42 +03:00
inline virtual int get ( const string & n , vector < VectorAttribute * > & v )
{
return __get < VectorAttribute > ( n , v ) ;
}
2009-02-15 01:32:21 +03:00
2016-02-04 15:10:42 +03:00
inline virtual int get ( const string & n , vector < const SingleAttribute * > & s ) const
{
return __get < SingleAttribute > ( n , s ) ;
}
2016-01-27 13:27:26 +03:00
2016-02-04 15:10:42 +03:00
inline virtual int get ( const string & n , vector < SingleAttribute * > & s )
{
return __get < SingleAttribute > ( n , s ) ;
}
2016-01-28 18:06:18 +03:00
/**
2016-02-04 15:10:42 +03:00
* Gets the first Attribute of the specified type with the given name .
* Const and non - const versions of this method is provided
2016-01-28 18:06:18 +03:00
* @ param name the attribute name .
2016-02-04 15:10:42 +03:00
* @ return true first attribute or 0 if not found or wrong type
2016-01-28 18:06:18 +03:00
*/
2017-01-05 18:51:30 +03:00
inline const VectorAttribute * get ( const string & name ) const
{
return __get < VectorAttribute > ( name ) ;
}
2016-01-28 18:06:18 +03:00
2017-01-05 18:51:30 +03:00
inline VectorAttribute * get ( const string & name )
{
return __get < VectorAttribute > ( name ) ;
}
2009-02-15 01:32:21 +03:00
2008-06-17 20:27:32 +04:00
/**
2016-02-04 15:10:42 +03:00
* Gets the value of a SingleAttribute with the given name and converts
* it to the target value format
2008-06-17 20:27:32 +04:00
* @ param name the attribute name .
2016-02-04 15:10:42 +03:00
* @ param value the attribute value
2011-04-01 19:17:26 +04:00
*
2016-02-04 15:10:42 +03:00
* @ return true if a SingleAttribute was found and it stores a valid
* value , false otherwise .
2008-06-17 20:27:32 +04:00
*/
2016-02-04 15:10:42 +03:00
template < typename T >
bool get ( const string & name , T & value ) const
{
const SingleAttribute * s = __get < SingleAttribute > ( name ) ;
2009-02-15 01:32:21 +03:00
2016-02-04 15:10:42 +03:00
value = 0 ;
2013-10-17 14:35:19 +04:00
2016-02-04 15:10:42 +03:00
if ( s = = 0 )
{
return false ;
}
2012-07-02 18:46:46 +04:00
2016-02-04 15:10:42 +03:00
istringstream iss ( s - > value ( ) ) ;
2012-12-20 21:21:30 +04:00
2016-02-04 15:10:42 +03:00
iss > > value ;
if ( iss . fail ( ) | | ! iss . eof ( ) )
{
return false ;
}
return true ;
}
virtual bool get ( const string & name , bool & value ) const ;
virtual bool get ( const string & name , string & value ) const ;
2013-01-08 21:34:08 +04:00
/**
* Trims the trailing spaces in the attribute
* @ param name of the attribute
* @ return True if the attribute was found and trimmed
*/
virtual bool trim ( const string & name ) ;
2010-07-29 14:53:36 +04:00
/**
2016-02-04 15:10:42 +03:00
* Trims the trailing spaces in the NAME attribute
* @ return True if the attribute was found and trimmed
2011-02-24 20:12:26 +03:00
*/
2016-02-04 15:10:42 +03:00
inline virtual bool trim_name ( )
{
return trim ( " NAME " ) ;
} ;
2011-02-24 20:12:26 +03:00
2013-02-15 17:58:58 +04:00
/**
2013-02-15 19:07:15 +04:00
* Merges another Template , adding the new attributes and
* replacing the existing ones
*
2013-02-15 17:58:58 +04:00
* @ param from_tmpl the template to be merged
*/
2016-04-26 16:35:07 +03:00
void merge ( const Template * from_tmpl ) ;
2013-02-15 17:58:58 +04:00
2014-09-03 13:36:46 +04:00
/**
* Deletes all restricted attributes
*/
virtual void remove_restricted ( ) ;
/**
* Deletes all the attributes , except the restricted ones
*/
virtual void remove_all_except_restricted ( ) ;
2014-10-06 18:57:40 +04:00
/**
* @ return true if the template defines one or more restricted attributes
*/
virtual bool has_restricted ( ) ;
2016-12-15 23:12:33 +03:00
/**
* @ return true if template is empty
*/
bool empty ( )
{
return attributes . empty ( ) ;
}
2008-06-17 20:27:32 +04:00
protected :
/**
* The template attributes
*/
multimap < string , Attribute * > attributes ;
2010-07-29 14:53:36 +04:00
/**
* Builds a SingleAttribute from the given node
* @ param node The xml element to build the attribute from .
*
* @ return the attribute , or 0 if the node doesn ' t contain a single att .
*/
Attribute * single_xml_att ( const xmlNode * node ) ;
/**
* Builds a VectorAttribute from the given node
* @ param node The xml element to build the attribute from .
*
* @ return the attribute , or 0 if the node doesn ' t contain a vector att .
*/
Attribute * vector_xml_att ( const xmlNode * node ) ;
2012-01-24 15:00:57 +04:00
/**
* Stores the attributes as restricted , these attributes will be used in
* Template : : check
* @ param rattrs Attributes to restrict
* @ param restricted_attributes The attributes will be stored here
*/
static void set_restricted_attributes (
2016-02-04 15:10:42 +03:00
vector < const SingleAttribute * > & rattrs ,
2012-01-24 15:00:57 +04:00
vector < string > & restricted_attributes ) ;
2012-01-20 20:45:14 +04:00
/**
* Checks the template for RESTRICTED ATTRIBUTES
* @ param rs_attr the first restricted attribute found if any
* @ return true if a restricted attribute is found in the template
*/
bool check ( string & rs_attr , const vector < string > & restricted_attributes ) ;
2014-09-03 13:36:46 +04:00
/**
* Deletes all restricted attributes
*/
void remove_restricted ( const vector < string > & restricted_attributes ) ;
/**
* Deletes all the attributes , except the restricted ones
*/
void remove_all_except_restricted ( const vector < string > & restricted_attributes ) ;
2013-02-06 18:23:55 +04:00
/**
* Updates the xml root element name
*
* @ param _xml_root New name
*/
void set_xml_root ( const char * _xml_root )
{
xml_root = _xml_root ;
} ;
2008-06-17 20:27:32 +04:00
private :
bool replace_mode ;
/**
* Mutex to perform just one flex - bison parsing at a time
*/
2015-07-01 22:15:40 +03:00
static pthread_mutex_t mutex ;
2009-02-15 01:32:21 +03:00
2008-06-18 19:58:44 +04:00
/**
* Character to separate key from value when dump onto a string
* */
2015-07-01 22:15:40 +03:00
char separator ;
2009-02-15 01:32:21 +03:00
/**
* Name of the Root element for the XML document
*/
2015-07-01 22:15:40 +03:00
string xml_root ;
2011-02-25 01:30:39 +03:00
/**
* Builds the template attribute from the node
* @ param root_element The xml element to build the template from .
*/
void rebuild_attributes ( const xmlNode * root_element ) ;
2016-02-04 15:10:42 +03:00
/**
* Gets the all the attributes of the given name and stores a reference
* to them in a vector . If the selected attribute does not match the
* requested type it will not be included
* @ param name the attribute name .
* @ param values vector with the values
*
* @ return the number of elements in the vector
*/
template < typename T >
int __get ( const string & name , vector < const T * > & values ) const
{
pair < multimap < string , Attribute * > : : const_iterator ,
multimap < string , Attribute * > : : const_iterator > index ;
multimap < string , Attribute * > : : const_iterator i ;
2016-08-26 13:05:52 +03:00
int j = 0 ;
2016-02-04 15:10:42 +03:00
index = attributes . equal_range ( name ) ;
2016-08-26 13:05:52 +03:00
for ( i = index . first ; i ! = index . second ; i + + )
2016-02-04 15:10:42 +03:00
{
const T * vatt = dynamic_cast < const T * > ( i - > second ) ;
if ( vatt = = 0 )
{
continue ;
}
values . push_back ( vatt ) ;
2016-08-26 13:05:52 +03:00
j + + ;
2016-02-04 15:10:42 +03:00
}
return j ;
}
/* Non-const version of get for all attributes */
template < typename T >
int __get ( const string & name , vector < T * > & values )
{
pair < multimap < string , Attribute * > : : iterator ,
multimap < string , Attribute * > : : iterator > index ;
multimap < string , Attribute * > : : iterator i ;
2016-08-26 13:05:52 +03:00
int j = 0 ;
2016-02-04 15:10:42 +03:00
index = attributes . equal_range ( name ) ;
2016-08-26 13:05:52 +03:00
for ( i = index . first ; i ! = index . second ; i + + )
2016-02-04 15:10:42 +03:00
{
T * vatt = dynamic_cast < T * > ( i - > second ) ;
if ( vatt = = 0 )
{
continue ;
}
values . push_back ( vatt ) ;
2016-08-26 13:05:52 +03:00
j + + ;
2016-02-04 15:10:42 +03:00
}
return j ;
}
/**
* Gets the first Attribute of the specified type with the given name .
* Const and non - const versions of this method is provided
* @ param name the attribute name .
* @ return true first attribute or 0 if not found or wrong type
*/
template < typename T >
const T * __get ( const string & s ) const
{
vector < const T * > atts ;
if ( __get < T > ( s , atts ) < 1 )
{
return 0 ;
}
return atts [ 0 ] ;
}
template < typename T >
T * __get ( const string & s )
{
return const_cast < T * > (
static_cast < const Template & > ( * this ) . __get < T > ( s ) ) ;
}
2016-03-02 01:31:31 +03:00
template < typename T >
void _set ( vector < T * > & values )
{
typename vector < T * > : : iterator it ;
for ( it = values . begin ( ) ; it ! = values . end ( ) ; it + + )
{
set ( * it ) ;
}
}
2008-06-17 20:27:32 +04:00
} ;
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
# endif /*TEMPLATE_H_*/