Statistics
| Branch: | Tag: | Revision:

one / include / Attribute.h @ dd32809e

History | View | Annotate | Download (7.88 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             */
3
/*                                                                            */
4
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
5
/* not use this file except in compliance with the License. You may obtain    */
6
/* a copy of the License at                                                   */
7
/*                                                                            */
8
/* http://www.apache.org/licenses/LICENSE-2.0                                 */
9
/*                                                                            */
10
/* Unless required by applicable law or agreed to in writing, software        */
11
/* distributed under the License is distributed on an "AS IS" BASIS,          */
12
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
13
/* See the License for the specific language governing permissions and        */
14
/* limitations under the License.                                             */
15
/* -------------------------------------------------------------------------- */
16

    
17
#ifndef ATTRIBUTE_H_
18
#define ATTRIBUTE_H_
19

    
20
#include <string>
21
#include <map>
22
#include <sstream>
23
#include <algorithm>
24

    
25
using namespace std;
26

    
27
/**
28
 *  Attribute base class for name-value pairs. This class provides a generic
29
 *  interface to implement
30
 */
31
class Attribute
32
{
33
public:
34

    
35
    Attribute(const string& aname):attribute_name(aname)
36
    {
37
        transform (
38
            attribute_name.begin(),
39
            attribute_name.end(),
40
            attribute_name.begin(),
41
            (int(*)(int))toupper);
42

    
43
        // FIX Attribute name if it does not conform XML element
44
        // naming conventions
45

    
46
        int size = attribute_name.size();
47

    
48
        if  ((size >0 && !isalpha(aname[0]))||
49
             (size >=3 && (aname[0]=='X' && aname[1]=='M' && aname[2]=='L')))
50
        {
51
            attribute_name.insert(0,"ONE_");    
52
        }
53
    };
54

    
55
    virtual ~Attribute(){};
56

    
57
    enum AttributeType
58
    {
59
        SIMPLE = 0,
60
        VECTOR = 1
61
    };
62

    
63
    /**
64
     *  Gets the name of the attribute.
65
     *    @return the attribute name
66
     */
67
    const string& name() const
68
    {
69
        return attribute_name;
70
    };
71

    
72
    /**
73
     *  Marshall the attribute in a single string. The string MUST be freed
74
     *  by the calling function.
75
     *    @return a string (allocated in the heap) holding the attribute value.
76
     */
77
    virtual string * marshall(const char * _sep = 0) const = 0;
78

    
79
    /**
80
     *  Write the attribute using a simple XML format. The string MUST be freed
81
     *  by the calling function.
82
     *    @return a string (allocated in the heap) holding the attribute value.
83
     */
84
    virtual string * to_xml() const = 0;
85

    
86
    /**
87
     *  Builds a new attribute from a string.
88
     */
89
    virtual void unmarshall(const string& sattr, const char * _sep = 0) = 0;
90

    
91
    /**
92
     *  Returns the attribute type
93
     */
94
    virtual AttributeType type() = 0;
95

    
96
    /**
97
     *  Clones the current attribute
98
     */
99
    virtual Attribute* clone() const = 0;
100

    
101
protected:
102
    /**
103
     *  The attribute name.
104
     */
105
    string attribute_name;
106
};
107

    
108
/* -------------------------------------------------------------------------- */
109
/* -------------------------------------------------------------------------- */
110

    
111
/**
112
 *  The SingleAttribute class represents a simple attribute in the form
113
 *  NAME = VALUE.
114
 */
115

    
116
class SingleAttribute : public Attribute
117
{
118
public:
119

    
120
    SingleAttribute(const string& name):Attribute(name){};
121

    
122
    SingleAttribute(const string& name, const string& value):
123
        Attribute(name),attribute_value(value){};
124

    
125
    SingleAttribute(const SingleAttribute& sa):Attribute(sa.attribute_name)
126
    {
127
        attribute_value = sa.attribute_value;
128
    };
129

    
130
    ~SingleAttribute(){};
131

    
132
    /**
133
     *  Returns the attribute value, a string.
134
     */
135
    const string& value() const
136
    {
137
        return attribute_value;
138
    };
139

    
140
    /**
141
     *  Marshall the attribute in a single string. The string MUST be freed
142
     *  by the calling function.
143
     *    @return a string (allocated in the heap) holding the attribute value.
144
     */
145
    string * marshall(const char * _sep = 0) const
146
    {
147
        string * rs = new string;
148

    
149
        *rs = attribute_value;
150

    
151
        return rs;
152
    };
153

    
154
    /**
155
     *  Write the attribute using a simple XML format:
156
     *
157
     *  <attribute_name>attribute_value</attribute_name>
158
     *
159
     *  The string MUST be freed by the calling function.
160
     *    @return a string (allocated in the heap) holding the attribute value.
161
     */
162
    string * to_xml() const
163
    {
164
            string * xml = new string;
165

    
166
            *xml = "<" + name() + "><![CDATA[" + attribute_value
167
                     + "]]></"+ name() + ">";
168

    
169
            return xml;
170
    }
171

    
172
    /**
173
     *  Builds a new attribute from a string.
174
     */
175
    void unmarshall(const string& sattr, const char * _sep = 0)
176
    {
177
        attribute_value = sattr;
178
    };
179

    
180
    /**
181
     *  Replaces the attribute value from a string.
182
     */
183
    void replace(const string& sattr)
184
    {
185
        attribute_value = sattr;
186
    };
187

    
188
    /**
189
     *  Returns the attribute type
190
     */
191
    AttributeType type()
192
    {
193
        return SIMPLE;
194
    };
195

    
196
    /**
197
     *  Clones the current attribute
198
     */
199
    Attribute* clone() const
200
    {
201
        return new SingleAttribute(*this);   
202
    };
203

    
204
private:
205

    
206
    string attribute_value;
207
};
208

    
209
/* -------------------------------------------------------------------------- */
210
/* -------------------------------------------------------------------------- */
211

    
212
/**
213
 *  The VectorAttribute class represents an array attribute in the form
214
 *  NAME = [ VAL_NAME_1=VAL_VALUE_1,...,VAL_NAME_N=VAL_VALUE_N].
215
 */
216

    
217
class VectorAttribute : public Attribute
218
{
219
public:
220

    
221
    VectorAttribute(const string& name):Attribute(name){};
222

    
223
    VectorAttribute(const string& name,const  map<string,string>& value):
224
            Attribute(name),attribute_value(value){};
225

    
226
    VectorAttribute(const VectorAttribute& va):Attribute(va.attribute_name)
227
    {
228
        attribute_value = va.attribute_value;
229
    };
230

    
231
    ~VectorAttribute(){};
232

    
233
    /**
234
     *  Returns the attribute value, a string.
235
     */
236
    const map<string,string>& value() const
237
    {
238
        return attribute_value;
239
    };
240

    
241
    /**
242
     *
243
     */
244
    string vector_value(const char *name) const;
245

    
246
    /**
247
     *  Marshall the attribute in a single string. The string MUST be freed
248
     *  by the calling function. The string is in the form:
249
     *  "VAL_NAME_1=VAL_VALUE_1,...,VAL_NAME_N=VAL_VALUE_N".
250
     *    @return a string (allocated in the heap) holding the attribute value.
251
     */
252
    string * marshall(const char * _sep = 0) const;
253

    
254
    /**
255
     *  Write the attribute using a simple XML format:
256
     *
257
     *  <attribute_name>
258
     *    <val_name_1>val_value_1</val_name_1>
259
     *    ...
260
     *    <val_name_n>val_value_n</val_name_n>
261
     *  </attribute_name>
262
     *
263
     *  The string MUST be freed by the calling function.
264
     *    @return a string (allocated in the heap) holding the attribute value.
265
     */
266
    string * to_xml() const;
267

    
268
    /**
269
     *  Builds a new attribute from a string of the form:
270
     *  "VAL_NAME_1=VAL_VALUE_1,...,VAL_NAME_N=VAL_VALUE_N".
271
     */
272
    void unmarshall(const string& sattr, const char * _sep = 0);
273

    
274
    /**
275
     *  Replace the value of the given attribute with the provided map
276
     */
277
    void replace(const map<string,string>& attr);
278

    
279
    /**
280
     *  Replace the value of the given vector attribute
281
     */
282
    void replace(const string& name, const string& value);
283
    
284
    /**
285
     *  Returns the attribute type
286
     */
287
    AttributeType type()
288
    {
289
        return VECTOR;
290
    };
291

    
292
    /**
293
     *  Clones the current attribute
294
     */
295
    Attribute* clone() const
296
    {
297
        return new VectorAttribute(*this);   
298
    };
299

    
300
private:
301

    
302
        static const char * magic_sep;
303

    
304
        static const int        magic_sep_size;
305

    
306
    map<string,string> attribute_value;
307
};
308

    
309
#endif /*ATTRIBUTE_H_*/