2008-06-17 20:27:32 +04:00
/* -------------------------------------------------------------------------- */
2013-01-24 19:18:30 +04:00
/* Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs */
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 ATTRIBUTE_H_
# define ATTRIBUTE_H_
# include <string>
# include <map>
# include <sstream>
2009-03-06 15:10:15 +03:00
# include <algorithm>
2008-06-17 20:27:32 +04:00
using namespace std ;
/**
* Attribute base class for name - value pairs . This class provides a generic
2009-03-06 15:10:15 +03:00
* interface to implement
2008-06-17 20:27:32 +04:00
*/
class Attribute
{
public :
2008-11-13 19:21:17 +03:00
Attribute ( const string & aname ) : attribute_name ( aname )
2008-06-17 20:27:32 +04:00
{
transform (
attribute_name . begin ( ) ,
attribute_name . end ( ) ,
attribute_name . begin ( ) ,
( int ( * ) ( int ) ) toupper ) ;
2011-05-08 04:13:37 +04: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 05:41:17 +04:00
attribute_name . insert ( 0 , " ONE_ " ) ;
2011-05-08 04:13:37 +04:00
}
2008-06-17 20:27:32 +04:00
} ;
virtual ~ Attribute ( ) { } ;
enum AttributeType
{
SIMPLE = 0 ,
VECTOR = 1
} ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04: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 15:10:15 +03:00
virtual string * marshall ( const char * _sep = 0 ) const = 0 ;
2009-01-26 21:25:15 +03: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 20:27:32 +04:00
/**
* Builds a new attribute from a string .
*/
2009-03-06 15:10:15 +03:00
virtual void unmarshall ( const string & sattr , const char * _sep = 0 ) = 0 ;
2008-06-17 20:27:32 +04:00
/**
* Returns the attribute type
*/
virtual AttributeType type ( ) = 0 ;
2011-04-11 01:55:49 +04:00
/**
* Clones the current attribute
*/
virtual Attribute * clone ( ) const = 0 ;
2008-06-17 20:27:32 +04:00
2011-04-11 01:55:49 +04:00
protected :
2008-06-17 20:27:32 +04:00
/**
* The attribute name .
*/
string attribute_name ;
} ;
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/**
2009-03-06 15:10:15 +03:00
* The SingleAttribute class represents a simple attribute in the form
* NAME = VALUE .
2008-06-17 20:27:32 +04:00
*/
class SingleAttribute : public Attribute
{
public :
2008-11-13 19:21:17 +03:00
SingleAttribute ( const string & name ) : Attribute ( name ) { } ;
2008-06-17 20:27:32 +04:00
2008-11-13 19:21:17 +03:00
SingleAttribute ( const string & name , const string & value ) :
2008-06-17 20:27:32 +04:00
Attribute ( name ) , attribute_value ( value ) { } ;
2009-03-06 15:10:15 +03:00
2011-04-11 01:55:49 +04:00
SingleAttribute ( const SingleAttribute & sa ) : Attribute ( sa . attribute_name )
{
attribute_value = sa . attribute_value ;
} ;
2008-06-17 20:27:32 +04:00
~ SingleAttribute ( ) { } ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
* Returns the attribute value , a string .
*/
const string & value ( ) const
{
return attribute_value ;
} ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04: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 15:10:15 +03:00
*/
string * marshall ( const char * _sep = 0 ) const
2008-06-17 20:27:32 +04:00
{
string * rs = new string ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
* rs = attribute_value ;
2009-03-06 15:10:15 +03:00
return rs ;
2008-06-17 20:27:32 +04:00
} ;
2009-03-06 15:10:15 +03:00
2009-01-26 21:25:15 +03:00
/**
* Write the attribute using a simple XML format :
2009-03-06 15:10:15 +03:00
*
* < attribute_name > attribute_value < / attribute_name >
*
2009-01-26 21:25:15 +03: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 15:10:15 +03:00
2010-05-19 01:38:16 +04:00
* xml = " < " + name ( ) + " ><![CDATA[ " + attribute_value
+ " ]]></ " + name ( ) + " > " ;
2009-03-06 15:10:15 +03:00
2009-01-26 21:25:15 +03:00
return xml ;
}
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
* Builds a new attribute from a string .
2009-03-06 15:10:15 +03:00
*/
void unmarshall ( const string & sattr , const char * _sep = 0 )
2008-06-17 20:27:32 +04:00
{
attribute_value = sattr ;
} ;
2008-11-13 19:21:17 +03:00
/**
* Replaces the attribute value from a string .
*/
void replace ( const string & sattr )
{
attribute_value = sattr ;
} ;
2008-06-17 20:27:32 +04:00
/**
* Returns the attribute type
2009-03-06 15:10:15 +03:00
*/
2008-06-17 20:27:32 +04:00
AttributeType type ( )
{
return SIMPLE ;
} ;
2009-03-06 15:10:15 +03:00
2011-04-11 01:55:49 +04:00
/**
* Clones the current attribute
*/
Attribute * clone ( ) const
{
2012-12-24 05:41:17 +04:00
return new SingleAttribute ( * this ) ;
2011-04-11 01:55:49 +04:00
} ;
2008-06-17 20:27:32 +04:00
private :
2009-03-06 15:10:15 +03:00
string attribute_value ;
2008-06-17 20:27:32 +04:00
} ;
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/**
2009-03-06 15:10:15 +03: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 20:27:32 +04:00
*/
class VectorAttribute : public Attribute
{
public :
2008-11-13 19:21:17 +03:00
VectorAttribute ( const string & name ) : Attribute ( name ) { } ;
2008-06-17 20:27:32 +04:00
2008-11-13 19:21:17 +03:00
VectorAttribute ( const string & name , const map < string , string > & value ) :
2008-06-17 20:27:32 +04:00
Attribute ( name ) , attribute_value ( value ) { } ;
2011-04-11 01:55:49 +04:00
VectorAttribute ( const VectorAttribute & va ) : Attribute ( va . attribute_name )
{
attribute_value = va . attribute_value ;
} ;
2008-06-17 20:27:32 +04:00
~ VectorAttribute ( ) { } ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
* Returns the attribute value , a string .
*/
const map < string , string > & value ( ) const
{
return attribute_value ;
} ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
2012-06-02 04:58:46 +04:00
* Returns the string value
* @ param name of the attribute
2008-06-17 20:27:32 +04:00
*
2012-06-02 04:58:46 +04:00
* @ return the value of the attribute if found , empty otherwise
2008-06-17 20:27:32 +04:00
*/
string vector_value ( const char * name ) const ;
2009-03-06 15:10:15 +03:00
2012-12-24 05:41:17 +04: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 21:44:22 +04:00
/**
* Returns the integer value
*
* @ param name Name of the attribute
* @ param value Integer value
*
* @ return 0 on success , - 1 otherwise
*/
2012-05-29 02:36:13 +04:00
int vector_value ( const char * name , int & value ) const ;
2012-07-16 19:15:22 +04: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 02:36:13 +04:00
/**
* Returns the integer value
*
* @ param name Name of the attribute
2012-07-16 19:15:22 +04:00
* @ param value Integer value , if an error occurred the string returned is
2012-05-29 02:36:13 +04: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 21:44:22 +04:00
2012-07-16 20:28:58 +04: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 20:27:32 +04:00
/**
* Marshall the attribute in a single string . The string MUST be freed
2009-03-06 15:10:15 +03:00
* by the calling function . The string is in the form :
2008-06-17 20:27:32 +04: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 15:10:15 +03:00
*/
string * marshall ( const char * _sep = 0 ) const ;
2009-01-26 21:25:15 +03:00
/**
* Write the attribute using a simple XML format :
2009-03-06 15:10:15 +03:00
*
2009-01-26 21:25:15 +03:00
* < attribute_name >
* < val_name_1 > val_value_1 < / val_name_1 >
* . . .
* < val_name_n > val_value_n < / val_name_n >
2009-03-06 15:10:15 +03:00
* < / attribute_name >
*
2009-01-26 21:25:15 +03: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 20:27:32 +04: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 15:10:15 +03:00
*/
void unmarshall ( const string & sattr , const char * _sep = 0 ) ;
2008-11-13 19:21:17 +03:00
/**
* Replace the value of the given attribute with the provided map
*/
void replace ( const map < string , string > & attr ) ;
2009-04-04 03:34:33 +04:00
/**
* Replace the value of the given vector attribute
*/
void replace ( const string & name , const string & value ) ;
2012-06-05 21:04:02 +04: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 05:41:17 +04:00
}
2012-06-05 21:04:02 +04:00
2013-01-18 02:14:34 +04:00
/**
* Replace the value of the given vector attribute
*/
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 20:42:42 +04:00
/**
* Removes given the vector attribute
* @ param name of the vector attribute
*/
void remove ( const string & name ) ;
2008-06-17 20:27:32 +04:00
/**
* Returns the attribute type
2009-03-06 15:10:15 +03:00
*/
2008-06-17 20:27:32 +04:00
AttributeType type ( )
{
return VECTOR ;
} ;
2011-04-11 01:55:49 +04:00
/**
* Clones the current attribute
*/
2013-03-12 04:08:44 +04:00
VectorAttribute * clone ( ) const
2011-04-11 01:55:49 +04:00
{
2012-12-24 05:41:17 +04:00
return new VectorAttribute ( * this ) ;
2011-04-11 01:55:49 +04:00
} ;
2008-06-17 20:27:32 +04:00
private :
2009-03-06 15:10:15 +03:00
2008-09-30 15:55:22 +04:00
static const char * magic_sep ;
2009-03-06 15:10:15 +03:00
2008-09-30 15:55:22 +04:00
static const int magic_sep_size ;
2008-06-17 20:27:32 +04:00
2009-03-06 15:10:15 +03:00
map < string , string > attribute_value ;
2008-06-17 20:27:32 +04:00
} ;
# endif /*ATTRIBUTE_H_*/