Subversion Repositories HomeAutomation

Rev

Rev 969 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 969 Rev 997
1
/***************************************************************************
1
/***************************************************************************
2
 *   Copyright (C) November 30, 2008 by Mattias Runge                             *
2
 *   Copyright (C) November 30, 2008 by Mattias Runge                             *
3
 *   mattias@runge.se                                                      *
3
 *   mattias@runge.se                                                      *
4
 *   xmlnode.cpp                                            *
4
 *   xmlnode.cpp                                            *
5
 *                                                                         *
5
 *                                                                         *
6
 *   This program is free software; you can redistribute it and/or modify  *
6
 *   This program is free software; you can redistribute it and/or modify  *
7
 *   it under the terms of the GNU General Public License as published by  *
7
 *   it under the terms of the GNU General Public License as published by  *
8
 *   the Free Software Foundation; either version 2 of the License, or     *
8
 *   the Free Software Foundation; either version 2 of the License, or     *
9
 *   (at your option) any later version.                                   *
9
 *   (at your option) any later version.                                   *
10
 *                                                                         *
10
 *                                                                         *
11
 *   This program is distributed in the hope that it will be useful,       *
11
 *   This program is distributed in the hope that it will be useful,       *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU General Public License for more details.                          *
14
 *   GNU General Public License for more details.                          *
15
 *                                                                         *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU General Public License     *
16
 *   You should have received a copy of the GNU General Public License     *
17
 *   along with this program; if not, write to the                         *
17
 *   along with this program; if not, write to the                         *
18
 *   Free Software Foundation, Inc.,                                       *
18
 *   Free Software Foundation, Inc.,                                       *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20
 ***************************************************************************/
20
 ***************************************************************************/
21
 
21
 
22
#include "xmlnode.h"
22
#include "xmlnode.h"
23
 
23
 
24
void XmlNode::load(string filename)
24
void XmlNode::load(string filename)
25
{
25
{
26
    parse(file_get_contents(filename));
26
    parse(file_get_contents(filename));
27
}
27
}
28
 
28
 
29
void XmlNode::parse(string xmlData)
29
void XmlNode::parse(string xmlData)
30
{
30
{
31
    char c;
31
    char c;
32
    int position = 0;
32
    int position = 0;
33
    while (position < xmlData.length())
33
    while (position < xmlData.length())
34
    {
34
    {
35
        c = xmlData.at(position);
35
        c = xmlData.at(position);
36
 
36
 
37
        switch (c)
37
        switch (c)
38
        {
38
        {
39
            case '<':
39
            case '<':
40
                position = parseTag(xmlData, position);
40
                position = parseTag(xmlData, position);
41
                break;
41
                break;
42
        }
42
        }
43
        position++;
43
        position++;
44
    }
44
    }
45
}
45
}
46
 
46
 
47
int XmlNode::parseTag(string xmlData, int position)
47
int XmlNode::parseTag(string xmlData, int position)
48
{
48
{
49
    if (xmlData.at(position) != '<')
49
    if (xmlData.at(position) != '<')
50
        throw new XmlException("Did not find < as expected. Character " + itos(position));
50
        throw new XmlException("Did not find < as expected. Character " + itos(position));
51
 
51
 
52
    position++;
52
    position++;
53
 
53
 
54
    int endPosition = xmlData.find('>', position);
54
    int endPosition = xmlData.find('>', position);
55
 
55
 
56
    if (xmlData.at(position) == '?' || xmlData.at(position) == '!' || xmlData.at(position) == '/')
56
    if (xmlData.at(position) == '?' || xmlData.at(position) == '!' || xmlData.at(position) == '/')
57
    {
57
    {
58
        return endPosition;
58
        return endPosition;
59
    }
59
    }
60
 
60
 
-
 
61
    string tagString;
-
 
62
 
-
 
63
    try
-
 
64
    {
61
    string tagString = xmlData.substr(position, endPosition-position);
65
        tagString = xmlData.substr(position, endPosition-position);
-
 
66
    }
-
 
67
    catch (std::out_of_range& e)
-
 
68
    {
-
 
69
        cout << "DEBUG: parseTag exception: " << e.what() << "\n";
-
 
70
        cout << "DEBUG: A xmlData=" << xmlData << " :: position=" << position << " :: endPosition=" << endPosition << endl;
-
 
71
    }
62
 
72
 
63
    tagString = trim(tagString);
73
    tagString = trim(tagString);
64
    tagString = trim(tagString, '\n');
74
    tagString = trim(tagString, '\n');
65
    tagString = trim(tagString);
75
    tagString = trim(tagString);
66
    tagString += " ";
76
    tagString += " ";
67
 
77
 
68
    bool foundEnd = false;
78
    bool foundEnd = false;
69
    bool foundTag = false;
79
    bool foundTag = false;
70
    string buffer;
80
    string buffer;
71
    int pos = 0;
81
    int pos = 0;
72
    char c;
82
    char c;
73
    int s, e;
83
    int s, e;
74
 
84
 
75
    while (pos < tagString.length())
85
    while (pos < tagString.length())
76
    {
86
    {
77
        c = tagString.at(pos);
87
        c = tagString.at(pos);
78
 
88
 
79
        switch (c)
89
        switch (c)
80
        {
90
        {
81
            case ' ':
91
            case ' ':
82
                if (!foundTag)
92
                if (!foundTag)
83
                {
93
                {
84
                    myTagName = buffer;
94
                    myTagName = buffer;
85
                    buffer = "";
95
                    buffer = "";
86
                    //cout << "Found tag: " << myTagName << endl;
96
                    //cout << "Found tag: " << myTagName << endl;
87
                    foundTag = true;
97
                    foundTag = true;
88
                }
98
                }
89
                break;
99
                break;
90
 
100
 
91
            case '/':
101
            case '/':
92
                foundEnd = true;
102
                foundEnd = true;
93
                break;
103
                break;
94
 
104
 
95
            case '=':
105
            case '=':
96
                s = tagString.find('"', pos)+1;
106
                s = tagString.find('"', pos)+1;
97
                e = tagString.find('"', s);
107
                e = tagString.find('"', s);
98
 
108
 
-
 
109
                try
-
 
110
                {
99
                myAttributes[buffer] = tagString.substr(s, e-s);
111
                    myAttributes[buffer] = tagString.substr(s, e-s);
-
 
112
                }
-
 
113
                catch (std::out_of_range& ex)
-
 
114
                {
-
 
115
                    cout << "DEBUG: parseTag exception: " << ex.what() << "\n";
-
 
116
                    cout << "DEBUG: B tagString=" << tagString << " :: s=" << s << " :: e=" << e << endl;
-
 
117
                    continue;
-
 
118
                }
-
 
119
 
-
 
120
 
100
                //cout << "Found attribute: " << buffer << " = " << myAttributes[buffer] << endl;
121
                //cout << "Found attribute: " << buffer << " = " << myAttributes[buffer] << endl;
101
                pos = e;
122
                pos = e;
102
                buffer = "";
123
                buffer = "";
103
                break;
124
                break;
104
 
125
 
105
            default:
126
            default:
106
                buffer += c;
127
                buffer += c;
107
                break;
128
                break;
108
        }
129
        }
109
        pos++;
130
        pos++;
110
    }
131
    }
111
   
132
   
112
    position = endPosition;
133
    position = endPosition;
113
 
134
 
114
    if (!foundEnd)
135
    if (!foundEnd)
115
    {
136
    {
116
        while (position < xmlData.length())
137
        while (position < xmlData.length())
117
        {
138
        {
118
            c = xmlData.at(position);
139
            c = xmlData.at(position);
119
 
140
 
120
            switch (c)
141
            switch (c)
121
            {
142
            {
122
                case '<':
143
                case '<':
123
                    if (xmlData.at(position+1) == '/')
144
                    if (xmlData.at(position+1) == '/')
124
                    {
145
                    {
125
                        position += 2;
146
                        position += 2;
126
                        endPosition = xmlData.find('>', position);
147
                        endPosition = xmlData.find('>', position);
127
 
148
 
-
 
149
                        try
-
 
150
                        {
128
                        if (myTagName.compare(xmlData.substr(position, endPosition-position)) == 0)
151
                            if (myTagName.compare(xmlData.substr(position, endPosition-position)) == 0)
129
                        {
152
                            {
130
                            //cout << "Found end tag: " << myTagName << endl;
153
                                //cout << "Found end tag: " << myTagName << endl;
131
                            return endPosition;
154
                                return endPosition;
132
                        }
155
                            }
133
                        else
156
                            else
134
                        {
157
                            {
135
                            throw new XmlException("Invalid end tag found. " + xmlData.substr(position, endPosition-position) + "::" + myTagName);
158
                                throw new XmlException("Invalid end tag found. " + xmlData.substr(position, endPosition-position) + "::" + myTagName);
136
                        }
159
                            }
-
 
160
                        }
-
 
161
                        catch (std::out_of_range& e)
-
 
162
                        {
-
 
163
                            cout << "DEBUG: parseTag exception: " << e.what() << "\n";
-
 
164
                            cout << "DEBUG: C xmlData=" << xmlData << " :: position=" << position << " :: endPosition=" << endPosition << endl;
-
 
165
                            continue;
-
 
166
                        }
-
 
167
 
-
 
168
                       
137
                    }
169
                    }
138
 
170
 
139
                    XmlNode subNode;
171
                    XmlNode subNode;
140
                    position = subNode.parseTag(xmlData, position);
172
                    position = subNode.parseTag(xmlData, position);
141
                    myChildren.push_back(subNode);
173
                    myChildren.push_back(subNode);
142
                    break;
174
                    break;
143
            }
175
            }
144
 
176
 
145
            position++;
177
            position++;
146
        }
178
        }
147
    }
179
    }
148
 
180
 
149
    return position;
181
    return position;
150
}
182
}
151
 
183
 
152
XmlNode XmlNode::findChild(string tagName)
184
XmlNode XmlNode::findChild(string tagName)
153
{
185
{
154
    for (int n = 0; n < myChildren.size(); n++)
186
    for (int n = 0; n < myChildren.size(); n++)
155
    {
187
    {
156
        if (myChildren[n].getTagName().compare(tagName) == 0)
188
        if (myChildren[n].getTagName().compare(tagName) == 0)
157
            return myChildren[n];
189
            return myChildren[n];
158
    }
190
    }
159
 
191
 
160
    XmlNode defaultNode;
192
    XmlNode defaultNode;
161
 
193
 
162
    return defaultNode;
194
    return defaultNode;
163
}
195
}
164
 
196
 
165
string XmlNode::getAttributeValue(string attributeName)
197
string XmlNode::getAttributeValue(string attributeName)
166
{
198
{
167
    map<string, string>::const_iterator iterator = myAttributes.find(attributeName);
199
    map<string, string>::const_iterator iterator = myAttributes.find(attributeName);
168
 
200
 
169
    if (iterator == myAttributes.end())
201
    if (iterator == myAttributes.end())
170
        return NULL;
202
        return NULL;
171
 
203
 
172
    return iterator->second;
204
    return iterator->second;
173
}
205
}
174
 
206