2008-06-17 16:27:32 +00:00
/* -------------------------------------------------------------------------- */
2014-01-09 11:51:20 +01:00
/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */
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 ATTRIBUTE_H_
# define ATTRIBUTE_H_
# include <string>
# include <map>
# include <sstream>
2009-03-06 12:10:15 +00:00
# include <algorithm>
2008-06-17 16:27:32 +00:00
using namespace std ;
/**
* Attribute base class for name - value pairs . This class provides a generic
2009-03-06 12:10:15 +00:00
* interface to implement
2008-06-17 16:27:32 +00:00
*/
class Attribute
{
public :
2008-11-13 16:21:17 +00:00
Attribute ( const string & aname ) : attribute_name ( aname )
2008-06-17 16:27:32 +00:00
{
transform (
attribute_name . begin ( ) ,
attribute_name . end ( ) ,
attribute_name . begin ( ) ,
( int ( * ) ( int ) ) toupper ) ;
2011-05-08 02:13:37 +02:00
// FIX Attribute name if it does not conform XML element
// naming conventions
int size = attribute_name . size ( ) ;
if ( ( size > 0 & & ! isalpha ( aname [ 0 ] ) ) | |
( size > = 3 & & ( aname [ 0 ] = = ' X ' & & aname [ 1 ] = = ' M ' & & aname [ 2 ] = = ' L ' ) ) )
{
2012-12-24 02:41:17 +01:00
attribute_name . insert ( 0 , " ONE_ " ) ;
2011-05-08 02:13:37 +02:00
}
2008-06-17 16:27:32 +00:00
} ;
virtual ~ Attribute ( ) { } ;
enum AttributeType
{
SIMPLE = 0 ,
VECTOR = 1
} ;
2009-03-06 12:10:15 +00:00
2008-06-17 16:27:32 +00:00
/**
* Gets the name of the attribute .
* @ return the attribute name
*/
const string & name ( ) const
{
return attribute_name ;
} ;
/**
* Marshall the attribute in a single string . The string MUST be freed
* by the calling function .
* @ return a string ( allocated in the heap ) holding the attribute value .
*/
2009-03-06 12:10:15 +00:00
virtual string * marshall ( const char * _sep = 0 ) const = 0 ;
2009-01-26 18:25:15 +00:00
/**
* Write the attribute using a simple XML format . The string MUST be freed
* by the calling function .
* @ return a string ( allocated in the heap ) holding the attribute value .
*/
virtual string * to_xml ( ) const = 0 ;
2008-06-17 16:27:32 +00:00
/**
* Builds a new attribute from a string .
*/
2009-03-06 12:10:15 +00:00
virtual void unmarshall ( const string & sattr , const char * _sep = 0 ) = 0 ;
2008-06-17 16:27:32 +00:00
/**
* Returns the attribute type
*/
virtual AttributeType type ( ) = 0 ;
2011-04-10 23:55:49 +02:00
/**
* Clones the current attribute
*/
virtual Attribute * clone ( ) const = 0 ;
2008-06-17 16:27:32 +00:00
2011-04-10 23:55:49 +02:00
protected :
2008-06-17 16:27:32 +00:00
/**
* The attribute name .
*/
string attribute_name ;
} ;
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/**
2009-03-06 12:10:15 +00:00
* The SingleAttribute class represents a simple attribute in the form
* NAME = VALUE .
2008-06-17 16:27:32 +00:00
*/
class SingleAttribute : public Attribute
{
public :
2008-11-13 16:21:17 +00:00
SingleAttribute ( const string & name ) : Attribute ( name ) { } ;
2008-06-17 16:27:32 +00:00
2008-11-13 16:21:17 +00:00
SingleAttribute ( const string & name , const string & value ) :
2008-06-17 16:27:32 +00:00
Attribute ( name ) , attribute_value ( value ) { } ;
2009-03-06 12:10:15 +00:00
2011-04-10 23:55:49 +02:00
SingleAttribute ( const SingleAttribute & sa ) : Attribute ( sa . attribute_name )
{
attribute_value = sa . attribute_value ;
} ;
2008-06-17 16:27:32 +00:00
~ SingleAttribute ( ) { } ;
2009-03-06 12:10:15 +00:00
2008-06-17 16:27:32 +00:00
/**
* Returns the attribute value , a string .
*/
const string & value ( ) const
{
return attribute_value ;
} ;
2009-03-06 12:10:15 +00:00
2008-06-17 16:27:32 +00:00
/**
* Marshall the attribute in a single string . The string MUST be freed
* by the calling function .
* @ return a string ( allocated in the heap ) holding the attribute value .
2009-03-06 12:10:15 +00:00
*/
string * marshall ( const char * _sep = 0 ) const
2008-06-17 16:27:32 +00:00
{
string * rs = new string ;
2009-03-06 12:10:15 +00:00
2008-06-17 16:27:32 +00:00
* rs = attribute_value ;
2009-03-06 12:10:15 +00:00
return rs ;
2008-06-17 16:27:32 +00:00
} ;
2009-03-06 12:10:15 +00:00
2009-01-26 18:25:15 +00:00
/**
* Write the attribute using a simple XML format :
2009-03-06 12:10:15 +00:00
*
* < attribute_name > attribute_value < / attribute_name >
*
2009-01-26 18:25:15 +00:00
* The string MUST be freed by the calling function .
* @ return a string ( allocated in the heap ) holding the attribute value .
*/
string * to_xml ( ) const
{
string * xml = new string ;
2009-03-06 12:10:15 +00:00
2010-05-18 23:38:16 +02:00
* xml = " < " + name ( ) + " ><![CDATA[ " + attribute_value
+ " ]]></ " + name ( ) + " > " ;
2009-03-06 12:10:15 +00:00
2009-01-26 18:25:15 +00:00
return xml ;
}
2009-03-06 12:10:15 +00:00
2008-06-17 16:27:32 +00:00
/**
* Builds a new attribute from a string .
2009-03-06 12:10:15 +00:00
*/
void unmarshall ( const string & sattr , const char * _sep = 0 )
2008-06-17 16:27:32 +00:00
{
attribute_value = sattr ;
} ;
2008-11-13 16:21:17 +00:00
/**
* Replaces the attribute value from a string .
*/
void replace ( const string & sattr )
{
attribute_value = sattr ;
} ;
2008-06-17 16:27:32 +00:00
/**
* Returns the attribute type
2009-03-06 12:10:15 +00:00
*/
2008-06-17 16:27:32 +00:00
AttributeType type ( )
{
return SIMPLE ;
} ;
2009-03-06 12:10:15 +00:00
2011-04-10 23:55:49 +02:00
/**
* Clones the current attribute
*/
Attribute * clone ( ) const
{
2012-12-24 02:41:17 +01:00
return new SingleAttribute ( * this ) ;
2011-04-10 23:55:49 +02:00
} ;
2008-06-17 16:27:32 +00:00
private :
2009-03-06 12:10:15 +00:00
string attribute_value ;
2008-06-17 16:27:32 +00:00
} ;
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/**
2009-03-06 12:10:15 +00:00
* The VectorAttribute class represents an array attribute in the form
* NAME = [ VAL_NAME_1 = VAL_VALUE_1 , . . . , VAL_NAME_N = VAL_VALUE_N ] .
2008-06-17 16:27:32 +00:00
*/
class VectorAttribute : public Attribute
{
public :
2008-11-13 16:21:17 +00:00
VectorAttribute ( const string & name ) : Attribute ( name ) { } ;
2008-06-17 16:27:32 +00:00
2008-11-13 16:21:17 +00:00
VectorAttribute ( const string & name , const map < string , string > & value ) :
2008-06-17 16:27:32 +00:00
Attribute ( name ) , attribute_value ( value ) { } ;
2011-04-10 23:55:49 +02:00
VectorAttribute ( const VectorAttribute & va ) : Attribute ( va . attribute_name )
{
attribute_value = va . attribute_value ;
} ;
2008-06-17 16:27:32 +00:00
~ VectorAttribute ( ) { } ;
2009-03-06 12:10:15 +00:00
2008-06-17 16:27:32 +00:00
/**
* Returns the attribute value , a string .
*/
const map < string , string > & value ( ) const
{
return attribute_value ;
} ;
2009-03-06 12:10:15 +00:00
2008-06-17 16:27:32 +00:00
/**
2012-06-02 02:58:46 +02:00
* Returns the string value
* @ param name of the attribute
2008-06-17 16:27:32 +00:00
*
2012-06-02 02:58:46 +02:00
* @ return the value of the attribute if found , empty otherwise
2008-06-17 16:27:32 +00:00
*/
string vector_value ( const char * name ) const ;
2009-03-06 12:10:15 +00:00
2012-12-24 02:41:17 +01:00
/**
* Returns the boolean value
* @ param name of the attribute
* @ param value Bool value ( " YES " is true )
*
* @ return 0 on success , - 1 otherwise
*/
int vector_value ( const char * name , bool & value ) const ;
2012-03-06 18:44:22 +01:00
/**
* Returns the integer value
*
* @ param name Name of the attribute
* @ param value Integer value
*
* @ return 0 on success , - 1 otherwise
*/
2012-05-29 00:36:13 +02:00
int vector_value ( const char * name , int & value ) const ;
2014-05-02 14:11:36 +02:00
/**
* Returns the unsigned integer value
*
* @ param name Name of the attribute
* @ param value Integer value
*
* @ return 0 on success , - 1 otherwise
*/
int vector_value ( const char * name , unsigned int & value ) const ;
2013-10-17 12:35:19 +02:00
/**
* Returns the long long value
*
* @ param name Name of the attribute
* @ param value Long long value
*
* @ return 0 on success , - 1 otherwise
*/
int vector_value ( const char * name , long long & value ) const ;
2012-07-16 17:15:22 +02:00
/**
* Returns the float value
*
* @ param name Name of the attribute
* @ param value Float value
*
* @ return 0 on success , - 1 otherwise
*/
int vector_value ( const char * name , float & value ) const ;
2012-05-29 00:36:13 +02:00
/**
* Returns the integer value
*
* @ param name Name of the attribute
2012-07-16 17:15:22 +02:00
* @ param value Integer value , if an error occurred the string returned is
2012-05-29 00:36:13 +02:00
* empty and value set to - 1 ;
*
* @ return the value in string form on success , " " otherwise
*/
string vector_value_str ( const char * name , int & value ) const ;
2012-03-06 18:44:22 +01:00
2012-07-16 18:28:58 +02:00
/**
* Returns the float value
*
* @ param name Name of the attribute
* @ param value Float value , if an error occurred the string returned is
* empty and value set to - 1 ;
*
* @ return the value in string form on success , " " otherwise
*/
string vector_value_str ( const char * name , float & value ) const ;
2008-06-17 16:27:32 +00:00
/**
* Marshall the attribute in a single string . The string MUST be freed
2009-03-06 12:10:15 +00:00
* by the calling function . The string is in the form :
2008-06-17 16:27:32 +00:00
* " VAL_NAME_1=VAL_VALUE_1,...,VAL_NAME_N=VAL_VALUE_N " .
* @ return a string ( allocated in the heap ) holding the attribute value .
2009-03-06 12:10:15 +00:00
*/
string * marshall ( const char * _sep = 0 ) const ;
2009-01-26 18:25:15 +00:00
/**
* Write the attribute using a simple XML format :
2009-03-06 12:10:15 +00:00
*
2009-01-26 18:25:15 +00:00
* < attribute_name >
* < val_name_1 > val_value_1 < / val_name_1 >
* . . .
* < val_name_n > val_value_n < / val_name_n >
2009-03-06 12:10:15 +00:00
* < / attribute_name >
*
2009-01-26 18:25:15 +00:00
* The string MUST be freed by the calling function .
* @ return a string ( allocated in the heap ) holding the attribute value .
*/
string * to_xml ( ) const ;
2008-06-17 16:27:32 +00:00
2014-05-08 15:48:16 +02:00
/**
* Same as above but the attribute is written in an string stream ;
*/
void to_xml ( ostringstream & oss ) const ;
2008-06-17 16:27:32 +00:00
/**
* Builds a new attribute from a string of the form :
* " VAL_NAME_1=VAL_VALUE_1,...,VAL_NAME_N=VAL_VALUE_N " .
2009-03-06 12:10:15 +00:00
*/
void unmarshall ( const string & sattr , const char * _sep = 0 ) ;
2008-11-13 16:21:17 +00:00
/**
* Replace the value of the given attribute with the provided map
*/
void replace ( const map < string , string > & attr ) ;
2014-07-10 16:24:25 +02:00
/**
* The attributes from vattr will be copied to this vector
* @ param attr Vector attribute to merge
* @ param replace True to replace existing values , false to copy values
* only if they don ' t exist in this vector attribute
*/
void merge ( VectorAttribute * vattr , bool replace ) ;
2009-04-03 23:34:33 +00:00
/**
* Replace the value of the given vector attribute
*/
void replace ( const string & name , const string & value ) ;
2012-06-05 19:04:02 +02:00
/**
* Replace the value of the given vector attribute
*/
void replace ( const string & name , int value )
{
ostringstream oss ;
oss < < value ;
replace ( name , oss . str ( ) ) ;
2012-12-24 02:41:17 +01:00
}
2012-06-05 19:04:02 +02:00
2014-05-08 13:42:28 +02:00
/**
* Replace the value of the given vector attribute
*/
void replace ( const string & name , unsigned int value )
{
ostringstream oss ;
oss < < value ;
replace ( name , oss . str ( ) ) ;
}
2013-01-17 23:14:34 +01:00
/**
* Replace the value of the given vector attribute
2013-10-17 12:35:19 +02:00
*/
void replace ( const string & name , long long value )
{
ostringstream oss ;
oss < < value ;
replace ( name , oss . str ( ) ) ;
}
/**
* Replace the value of the given vector attribute
2013-01-17 23:14:34 +01:00
*/
void replace ( const string & name , const char * value )
{
string svalue ( value ) ;
replace ( name , svalue ) ;
}
/**
* Replace the value of the given vector attribute
*/
void replace ( const string & name , bool value )
{
string b_value ;
if ( value = = true )
{
b_value = " YES " ;
}
else
{
b_value = " NO " ;
}
replace ( name , b_value ) ;
}
2012-06-13 18:42:42 +02:00
/**
* Removes given the vector attribute
* @ param name of the vector attribute
*/
void remove ( const string & name ) ;
2008-06-17 16:27:32 +00:00
/**
* Returns the attribute type
2009-03-06 12:10:15 +00:00
*/
2008-06-17 16:27:32 +00:00
AttributeType type ( )
{
return VECTOR ;
} ;
2011-04-10 23:55:49 +02:00
/**
* Clones the current attribute
*/
2013-03-11 20:08:44 -04:00
VectorAttribute * clone ( ) const
2011-04-10 23:55:49 +02:00
{
2012-12-24 02:41:17 +01:00
return new VectorAttribute ( * this ) ;
2011-04-10 23:55:49 +02:00
} ;
2014-05-08 15:48:16 +02:00
/**
* Clear the vector attribute values
*/
void clear ( )
{
attribute_value . clear ( ) ;
}
2008-06-17 16:27:32 +00:00
private :
2009-03-06 12:10:15 +00:00
2008-09-30 11:55:22 +00:00
static const char * magic_sep ;
2009-03-06 12:10:15 +00:00
2008-09-30 11:55:22 +00:00
static const int magic_sep_size ;
2008-06-17 16:27:32 +00:00
2009-03-06 12:10:15 +00:00
map < string , string > attribute_value ;
2008-06-17 16:27:32 +00:00
} ;
# endif /*ATTRIBUTE_H_*/