|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 #include <string> |
|
18 #include <iostream> |
|
19 #include <map> |
|
20 #include <vector> |
|
21 #include <algorithm> |
|
22 using namespace std; |
|
23 |
|
24 #ifndef XMLNODE |
|
25 #define XMLNODE |
|
26 |
|
27 class XMLNode |
|
28 { |
|
29 public: |
|
30 typedef map<string, string> Attributes; |
|
31 |
|
32 XMLNode(); |
|
33 XMLNode(const XMLNode&); |
|
34 XMLNode(const string&); |
|
35 XMLNode(const string&, const string&); |
|
36 virtual ~XMLNode(); |
|
37 |
|
38 // Operator overloads |
|
39 bool operator==(const XMLNode&); |
|
40 bool operator==(const string&); |
|
41 bool operator!=(const XMLNode&); |
|
42 bool operator!=(const string&); |
|
43 bool operator<(const XMLNode&) const; |
|
44 bool operator<(const string&) const; |
|
45 bool operator>(const XMLNode&) const; |
|
46 bool operator>(const string&) const; |
|
47 XMLNode& operator=(const XMLNode&); |
|
48 |
|
49 // Accessors and Mutators |
|
50 const string& value() const { return value_; } |
|
51 void value(const string& v) { value_ = v; } |
|
52 |
|
53 const string& name() const { return name_; } |
|
54 void name(const string& n) { name_ = n; } |
|
55 |
|
56 void addChild(XMLNode&); |
|
57 void child(XMLNode&); |
|
58 XMLNode child(const string&); |
|
59 XMLNode child(const string& elementname, int& index); |
|
60 |
|
61 bool hasChild(const string&); |
|
62 size_t childCount() { return children_.size(); } |
|
63 bool children() { return (!children_.empty()); } |
|
64 |
|
65 public: |
|
66 vector<XMLNode> children_; |
|
67 |
|
68 private: |
|
69 string value_; |
|
70 string name_; |
|
71 Attributes attributes_; |
|
72 |
|
73 }; |
|
74 |
|
75 XMLNode::XMLNode() |
|
76 { |
|
77 } |
|
78 |
|
79 XMLNode::XMLNode(const std::string& n, const std::string& v) |
|
80 { |
|
81 name_ = n; |
|
82 value_ = v; |
|
83 } |
|
84 |
|
85 XMLNode::XMLNode(const std::string& n) |
|
86 { |
|
87 name_ = n; |
|
88 } |
|
89 |
|
90 XMLNode::XMLNode(const XMLNode& rhs) |
|
91 { |
|
92 if(&rhs == this) |
|
93 return; |
|
94 |
|
95 this->operator=(rhs); |
|
96 } |
|
97 |
|
98 XMLNode::~XMLNode() |
|
99 { |
|
100 } |
|
101 |
|
102 XMLNode& XMLNode::operator=(const XMLNode& rhs) |
|
103 { |
|
104 if(&rhs == this) |
|
105 return *this; |
|
106 |
|
107 value_ = rhs.value_; |
|
108 name_ = rhs.name_; |
|
109 children_ = rhs.children_; |
|
110 return *this; |
|
111 } |
|
112 |
|
113 bool XMLNode::operator==(const XMLNode& n) |
|
114 { |
|
115 return ((n.name_ == name_) && (n.value_ == value_)); |
|
116 } |
|
117 |
|
118 bool XMLNode::operator==(const std::string& n) |
|
119 { |
|
120 return (n == name_); |
|
121 } |
|
122 |
|
123 bool XMLNode::operator!=(const XMLNode& n) |
|
124 { |
|
125 return ((n.name_ != name_) && (n.value_ != value_)); |
|
126 } |
|
127 |
|
128 bool XMLNode::operator!=(const std::string& n) |
|
129 { |
|
130 return (n != name_); |
|
131 } |
|
132 |
|
133 bool XMLNode::operator<(const XMLNode& n) const |
|
134 { |
|
135 return ((n.name_ < name_) && (n.value_ < value_)); |
|
136 } |
|
137 |
|
138 bool XMLNode::operator<(const std::string& n) const |
|
139 { |
|
140 return (strcmp(name_.c_str(), n.c_str()) < 0); |
|
141 } |
|
142 |
|
143 bool XMLNode::operator>(const XMLNode& n) const |
|
144 { |
|
145 return ((n.name_ > name_) && (n.value_ > value_)); |
|
146 } |
|
147 |
|
148 bool XMLNode::operator>(const std::string& n) const |
|
149 { |
|
150 return (strcmp(name_.c_str(), n.c_str()) > 0); |
|
151 } |
|
152 |
|
153 void XMLNode::child(XMLNode& c) |
|
154 { |
|
155 children_.push_back(c); |
|
156 } |
|
157 |
|
158 bool XMLNode::hasChild(const std::string& name) |
|
159 { |
|
160 vector<XMLNode>::iterator iter = find(children_.begin(), children_.end(), name); |
|
161 return (iter != children_.end()); |
|
162 } |
|
163 |
|
164 XMLNode XMLNode::child(const std::string& name) |
|
165 { |
|
166 vector<XMLNode>::iterator iter = find(children_.begin(), children_.end(), name); |
|
167 if(iter != children_.end()) |
|
168 return *iter; |
|
169 |
|
170 return XMLNode(); |
|
171 } |
|
172 XMLNode XMLNode::child(const std::string& elementname, int& index) |
|
173 { |
|
174 vector<XMLNode>::iterator iter = find(children_.begin(), children_.end(), elementname); |
|
175 if(iter != children_.end()) |
|
176 { |
|
177 index = (int)(iter - children_.begin()); |
|
178 return *iter; |
|
179 } |
|
180 |
|
181 return XMLNode(); |
|
182 } |
|
183 |
|
184 |
|
185 |
|
186 ostream& operator<<(ostream& o, XMLNode& node) |
|
187 { |
|
188 o << "Writing Node name to stream : " << node.name() << endl; |
|
189 return o; |
|
190 } |
|
191 |
|
192 ostream& operator<<(XMLNode& node, ostream& o) |
|
193 { |
|
194 o << "Writing Node name to stream : " << node.name() << endl; |
|
195 return o; |
|
196 } |
|
197 |
|
198 void XMLNode::addChild(XMLNode& node) |
|
199 { |
|
200 children_.push_back(node); |
|
201 } |
|
202 |
|
203 #endif // XMLNODE |