Statistics
| Branch: | Tag: | Revision:

one / src / common / Attribute.cc @ 2a508a11

History | View | Annotate | Download (5.06 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
#include <string>
18
#include <sstream>
19
#include <cstring>
20

    
21
#include "Attribute.h"
22

    
23

    
24
const char * VectorAttribute::magic_sep      = "@^_^@";
25
const int    VectorAttribute::magic_sep_size = 5;
26

    
27
string * VectorAttribute::marshall(const char * _sep) const
28
{
29
    ostringstream os;
30
    string *      rs;
31
    const char *  my_sep;
32

    
33
    map<string,string>::const_iterator it;
34

    
35
    if ( _sep == 0 )
36
    {
37
            my_sep = magic_sep;
38
    }
39
    else
40
    {
41
            my_sep = _sep;
42
    }
43

    
44
    if ( attribute_value.size() == 0 )
45
    {
46
        return 0;
47
    }
48

    
49
    it = attribute_value.begin();
50

    
51
    os << it->first << "=" << it->second;
52

    
53
    for (++it; it != attribute_value.end(); it++)
54
    {
55
        os << my_sep << it->first << "=" << it->second;
56
    }
57

    
58
    rs = new string;
59

    
60
    *rs = os.str();
61

    
62
    return rs;
63
}
64

    
65
/* -------------------------------------------------------------------------- */
66
/* -------------------------------------------------------------------------- */
67

    
68
string * VectorAttribute::to_xml() const
69
{
70
        map<string,string>::const_iterator    it;
71
        ostringstream                   oss;
72
        string *                                                 xml;
73

    
74
        oss << "<" << name() << ">";
75

    
76
        for (it=attribute_value.begin();it!=attribute_value.end();it++)
77
        {
78
        if ( it->first.empty() )
79
        {
80
            continue;
81
        }
82

    
83
        if ( it->second.empty() )
84
        {
85
            oss << "<" << it->first << "/>";
86
        }
87
        else
88
        {
89
                    oss << "<" << it->first << "><![CDATA[" << it->second
90
                            << "]]></"<< it->first << ">";
91
        }
92
        }
93

    
94
        oss << "</"<< name() << ">";
95

    
96
        xml = new string;
97

    
98
        *xml = oss.str();
99

    
100
        return xml;
101
}
102

    
103

    
104
/* -------------------------------------------------------------------------- */
105
/* -------------------------------------------------------------------------- */
106

    
107
void VectorAttribute::unmarshall(const string& sattr, const char * _sep)
108
{
109
    size_t  bpos=0,epos,mpos;
110
    string  tmp;
111
    bool    cont = true;
112

    
113
    const char *  my_sep;
114
    int           my_sep_size;
115

    
116
    if ( _sep == 0 )
117
    {
118
        my_sep      = magic_sep;
119
        my_sep_size = magic_sep_size;
120
    }
121
    else
122
    {
123
        my_sep      = _sep;
124
        my_sep_size = strlen(_sep);
125
    }
126

    
127
    while(cont)
128
    {
129
            epos=sattr.find(my_sep,bpos);
130

    
131
            if (epos == string::npos)
132
            {
133
                    tmp  = sattr.substr(bpos);
134
                    cont = false;
135
            }
136
            else
137
            {
138
                    tmp  = sattr.substr(bpos,epos-bpos);
139
                    bpos = epos + my_sep_size;
140
            }
141

    
142
            mpos = tmp.find('=');
143

    
144
            if (mpos == string::npos)
145
            {
146
                    continue;
147
            }
148
        
149
        if ( mpos + 1 == tmp.size() )
150
        {
151
            attribute_value.insert(make_pair(tmp.substr(0,mpos),""));
152
        }
153
        else
154
        {
155
            attribute_value.insert(make_pair(tmp.substr(0,mpos),
156
                                             tmp.substr(mpos+1)));
157
        } 
158
    }
159
}
160
/* -------------------------------------------------------------------------- */
161
/* -------------------------------------------------------------------------- */
162

    
163
void VectorAttribute::replace(const map<string,string>& attr)
164
{
165
        attribute_value = attr;
166
}
167

    
168
/* -------------------------------------------------------------------------- */
169
/* -------------------------------------------------------------------------- */
170

    
171
void VectorAttribute::replace(const string& name, const string& value)
172
{
173
    map<string,string>::iterator it;
174

    
175
    it = attribute_value.find(name);
176
    
177
    if ( it != attribute_value.end() )
178
    {
179
        attribute_value.erase(it);
180
    }
181
    
182
    attribute_value.insert(make_pair(name,value));
183
}
184

    
185
/* -------------------------------------------------------------------------- */
186
/* -------------------------------------------------------------------------- */
187

    
188
string VectorAttribute::vector_value(const char *name) const
189
{
190
    map<string,string>::const_iterator it;
191

    
192
    it = attribute_value.find(name);
193

    
194
    if ( it == attribute_value.end() )
195
    {
196
        return "";
197
    }
198
    else
199
    {
200
        return it->second;
201
    }
202
}
203