2008-06-17 16:27:32 +00:00
/* -------------------------------------------------------------------------- */
2011-02-25 14:34:44 +01:00
/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) */
2008-06-17 16:27:32 +00: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 12:53:36 +02:00
# include <libxml/tree.h>
# include <libxml/parser.h>
2008-06-17 16:27:32 +00:00
# include "Attribute.h"
using namespace std ;
/**
2011-02-01 18:26:26 +01:00
* Base class for file templates . A template is a file ( or a string for the
2008-06-17 16:27:32 +00: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-14 22:32:21 +00:00
* be a single string or a vector value ( array of string pairs ) . The file can
2008-06-17 16:27:32 +00:00
* contain several attributes with the same name .
*/
class Template
{
public :
2009-02-14 22:32:21 +00: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 16:27:32 +00:00
2011-04-10 23:55:49 +02:00
Template ( const Template & t )
{
multimap < string , Attribute * > : : const_iterator it ;
replace_mode = t . replace_mode ;
separator = t . separator ;
xml_root = t . xml_root ;
for ( it = t . attributes . begin ( ) ; it ! = t . attributes . end ( ) ; it + + )
{
attributes . insert ( make_pair ( it - > first , ( it - > second ) - > clone ( ) ) ) ;
}
}
2008-06-17 16:27:32 +00:00
/**
* The class destructor frees all the attributes conforming the template
*/
virtual ~ Template ( ) ;
/**
* Parse a string representing the template , each attribute is inserted
* in the template class .
* @ param parse_str string with template attributes
2009-02-14 22:32:21 +00:00
* @ param error_msg error string , must be freed by the calling funtion .
2008-06-17 16:27:32 +00: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
2009-02-14 22:32:21 +00:00
* @ param error_msg error string , must be freed by the calling funtion .
2008-06-17 16:27:32 +00:00
* This string is null if no error occurred .
* @ return 0 on success .
*/
int parse ( const char * filename , char * * error_msg ) ;
/**
2009-02-14 22:32:21 +00:00
* Marshall a template . This function generates a single string with the
2008-06-17 16:27:32 +00: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 18:25:15 +00:00
/**
* Writes the template in a simple xml string :
* < template >
* < single > value < / single >
* < vector >
* < attr > value < / attr >
* . . .
* < / vector >
* . . .
* < / template >
2009-02-14 22:32:21 +00:00
* The name of the root element is set when the Template object is created
2009-01-26 18:25:15 +00:00
* @ param xml string that hold the xml template representation
2009-05-22 00:46:52 +00:00
* @ return a reference to the generated string
*/
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 18:25:15 +00:00
*/
2009-05-22 00:46:52 +00:00
string & to_str ( string & str ) const ;
2009-02-14 22:32:21 +00:00
2008-06-17 16:27:32 +00:00
/**
* Sets a new attribute , the attribute MUST BE ALLOCATED IN THE HEAP , and
2009-02-14 22:32:21 +00:00
* will be freed when the template destructor is called .
2008-06-17 16:27:32 +00:00
* @ param attr pointer to the attribute
*/
2009-03-29 21:27:55 +00:00
virtual void set ( Attribute * attr ) ;
2008-06-17 16:27:32 +00:00
2009-03-06 12:10:15 +00: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 19:17:22 +02:00
* @ return the number of attributes removed
2009-03-06 12:10:15 +00:00
*/
virtual int remove (
const string & name ,
vector < Attribute * > & values ) ;
2010-05-31 17:39:27 +02:00
/**
* Removes an attribute from the template , and frees the attributes .
* @ param name of the attribute
2011-06-02 19:17:22 +02:00
* @ return the number of attributes removed
2010-05-31 17:39:27 +02:00
*/
virtual int erase ( const string & name ) ;
2008-06-17 16:27:32 +00:00
/**
2009-02-14 22:32:21 +00:00
* Gets all the attributes with the given name .
2008-06-17 16:27:32 +00:00
* @ param name the attribute name .
2011-06-02 19:17:22 +02:00
* @ return the number of elements in the vector
2008-06-17 16:27:32 +00:00
*/
virtual int get (
2009-02-14 22:32:21 +00:00
const string & name ,
2008-06-17 16:27:32 +00:00
vector < const Attribute * > & values ) const ;
2008-11-13 16:21:17 +00:00
/**
* Gets all the attributes with the given name , non - const version
* @ param name the attribute name .
2011-06-02 19:17:22 +02:00
* @ return the number of elements in the vector
2008-11-13 16:21:17 +00:00
*/
virtual int get (
2009-02-14 22:32:21 +00:00
const string & name ,
2008-11-13 16:21:17 +00:00
vector < Attribute * > & values ) ;
2009-02-14 22:32:21 +00:00
2008-06-17 16:27:32 +00:00
/**
2009-02-14 22:32:21 +00:00
* Gets the value of a Single attributes ( string ) with the given name .
2008-06-17 16:27:32 +00:00
* @ param name the attribute name .
* @ param value the attribute value , a string , " " if the attribute is not
2009-02-14 22:32:21 +00:00
* defined or not Single
2008-06-17 16:27:32 +00:00
*/
virtual void get (
2011-10-11 12:47:30 +02:00
const string & name ,
2008-06-17 16:27:32 +00:00
string & value ) const ;
2009-02-14 22:32:21 +00:00
2008-06-17 16:27:32 +00:00
/**
* Gets the value of a Single attributes ( int ) with the given name .
* @ param name the attribute name .
* @ param value the attribute value , an int , 0 if the attribute is not
2009-02-14 22:32:21 +00:00
* defined or not Single
2011-04-01 17:17:26 +02:00
*
2011-06-02 19:17:22 +02:00
* @ return True if the Single attribute was found
2008-06-17 16:27:32 +00:00
*/
2011-04-01 17:17:26 +02:00
virtual bool get (
2011-10-11 12:47:30 +02:00
const string & name ,
2008-06-17 16:27:32 +00:00
int & value ) const ;
2009-02-14 22:32:21 +00:00
2009-05-22 00:46:52 +00:00
friend ostream & operator < < ( ostream & os , const Template & t ) ;
2008-06-17 16:27:32 +00:00
2010-07-29 12:53:36 +02: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 ) ;
2011-02-24 18:12:26 +01:00
/**
* 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 ) ;
2008-06-17 16:27:32 +00:00
protected :
/**
* The template attributes
*/
multimap < string , Attribute * > attributes ;
2010-07-29 12:53:36 +02: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 ) ;
2008-06-17 16:27:32 +00:00
private :
bool replace_mode ;
/**
* Mutex to perform just one flex - bison parsing at a time
*/
2009-02-14 22:32:21 +00:00
static pthread_mutex_t mutex ;
2008-06-18 15:58:44 +00:00
/**
* Character to separate key from value when dump onto a string
* */
2009-02-14 22:32:21 +00:00
char separator ;
/**
* Name of the Root element for the XML document
*/
string xml_root ;
2011-02-24 23:30:39 +01: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 ) ;
2008-06-17 16:27:32 +00:00
} ;
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
# endif /*TEMPLATE_H_*/