2008-06-17 20:27:32 +04:00
/* -------------------------------------------------------------------------- */
2011-02-25 16:34:44 +03:00
/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) */
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 ' ) ) )
{
attribute_name . insert ( 0 , " ONE_ " ) ;
}
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
{
return new SingleAttribute ( * this ) ;
} ;
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
/**
*
*/
string vector_value ( const char * name ) const ;
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
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 ) ;
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
*/
Attribute * clone ( ) const
{
return new VectorAttribute ( * this ) ;
} ;
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_*/