00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "xml_document.h"
00023 #include <libxml++/libxml++.h>
00024 #include <iostream>
00025 #include "string_tools.h"
00026 #include "file_tools.h"
00027
00028 bool XmlReader::Load(const std::string &filename)
00029 {
00030 if( !IsFileExist(filename) )
00031 return false;
00032
00033
00034
00035
00036
00037 parser.parse_file(filename);
00038 return IsOk();
00039 }
00040
00041 xmlpp::Element* XmlReader::GetMarker(const xmlpp::Node *x,
00042 const std::string &name)
00043 {
00044 xmlpp::Node::NodeList nodes = x -> get_children(name);
00045 if (nodes.size() != 1) return NULL;
00046
00047 xmlpp::Element *elem = dynamic_cast<xmlpp::Element*> (nodes.front());
00048 assert (elem != NULL);
00049 return elem;
00050 }
00051
00052 xmlpp::Element* XmlReader::Access(const xmlpp::Node *x,
00053 const std::string &name,
00054 const std::string &attr_name)
00055 {
00056 xmlpp::Node::NodeList nodes = x -> get_children(name);
00057 xmlpp::Node::NodeList::iterator
00058 it = nodes.begin(),
00059 end = nodes.end();
00060 for (; it != end; ++it) {
00061
00062 xmlpp::Element *elem = dynamic_cast<xmlpp::Element*>(*it);
00063 assert (elem != NULL);
00064 const xmlpp::Attribute *attr = elem->get_attribute("name");
00065 if (attr != NULL) {
00066 if (attr -> get_value() == attr_name) return elem;
00067 }
00068 }
00069 return NULL;
00070 }
00071
00072 bool XmlReader::ReadString(const xmlpp::Node *x,
00073 const std::string &name,
00074 std::string &output)
00075 {
00076 xmlpp::Element *elem = GetMarker(x, name);
00077 if (elem == NULL) return false;
00078 return ReadMarkerValue(elem, output);
00079 }
00080
00081 bool XmlReader::ReadDouble(const xmlpp::Node *x,
00082 const std::string &name,
00083 double &output)
00084 {
00085 std::string val;
00086 if (!ReadString(x, name, val)) return false;
00087 return str2double (val, output);
00088 }
00089
00090 bool XmlReader::ReadInt(const xmlpp::Node *x,
00091 const std::string &name,
00092 int &output)
00093 {
00094 std::string val;
00095 if (!ReadString(x, name, val)) return false;
00096 return str2int (val, output);
00097 }
00098
00099 bool XmlReader::ReadUint(const xmlpp::Node *x,
00100 const std::string &name,
00101 uint &output)
00102 {
00103 int val;
00104 if (!ReadInt(x, name, val)) return false;
00105 if (0 <= val) {
00106 output = static_cast<unsigned int>(val);
00107 return true;
00108 } else {
00109 return false;
00110 }
00111 }
00112
00113 bool XmlReader::ReadBool (const xmlpp::Node *x,
00114 const std::string &name,
00115 bool &output)
00116 {
00117 std::string val;
00118 if (!ReadString(x, name, val)) return false;
00119 return str2bool (val, output);
00120 }
00121
00122 bool XmlReader::ReadMarkerValue(const xmlpp::Node *marker,
00123 std::string &output)
00124 {
00125 if(marker->get_children().size() == 0)
00126 {
00127 output = "";
00128 return true;
00129 }
00130
00131
00132 assert(marker->get_children().size() == 1);
00133 assert(marker->get_children().front()->get_name() == "text");
00134 const xmlpp::TextNode *text = dynamic_cast<const xmlpp::TextNode*>(marker->get_children().front());
00135 assert(text != NULL);
00136 output = text->get_content();
00137 return true;
00138 }
00139
00140 bool XmlReader::ReadStringList(const xmlpp::Node *x,
00141 const std::string &name,
00142 std::list<std::string> &output)
00143 {
00144 xmlpp::Node::NodeList nodes = x -> get_children(name);
00145 xmlpp::Node::NodeList::iterator
00146 it=nodes.begin(),
00147 end=nodes.end();
00148
00149 output.clear();
00150 for (; it != end; ++it)
00151 {
00152 std::string txt;
00153
00154 xmlpp::Element *elem = dynamic_cast<xmlpp::Element*> (*it);
00155 assert (elem != NULL);
00156 if (!ReadMarkerValue(elem, txt))
00157 {
00158 output.clear();
00159 return false;
00160 }
00161 output.push_back (txt);
00162 }
00163 return true;
00164 }
00165
00166 bool XmlReader::ReadStringAttr(const xmlpp::Element *x,
00167 const std::string &name,
00168 std::string &output)
00169 {
00170 assert (x != NULL);
00171 const xmlpp::Attribute *attr = x -> get_attribute(name);
00172 if (attr == NULL) return false;
00173 output = attr->get_value();
00174 return true;
00175 }
00176
00177 bool XmlReader::ReadIntAttr(const xmlpp::Element *x,
00178 const std::string &name,
00179 int &output)
00180 {
00181 std::string val;
00182 if (!ReadStringAttr(x, name, val)) return false;
00183 return str2int (val, output);
00184 }
00185
00186 bool XmlReader::ReadUintAttr(const xmlpp::Element *x,
00187 const std::string &name,
00188 unsigned int &output)
00189 {
00190 int val;
00191 if (!ReadIntAttr(x, name, val)) return false;
00192 if (0 <= val) {
00193 output = static_cast<unsigned int> (val);
00194 return true;
00195 } else {
00196 return false;
00197 }
00198 }
00199
00200 bool XmlReader::ReadBoolAttr(const xmlpp::Element *x,
00201 const std::string &name,
00202 bool &output)
00203 {
00204 std::string val;
00205 if (!ReadStringAttr(x, name, val)) return false;
00206 return str2bool(val, output);
00207 }
00208
00209
00210 bool XmlReader::ReadDoubleAttr(const xmlpp::Element *x,
00211 const std::string &name,
00212 double &output)
00213 {
00214 std::string val;
00215 if (!ReadStringAttr(x, name, val)) return false;
00216 return str2double(val, output);
00217 }
00218
00219 bool XmlReader::IsOk() const
00220 {
00221 return parser;
00222 }
00223
00224 xmlpp::Element* XmlReader::GetRoot() const
00225 {
00226 assert(IsOk());
00227 xmlpp::Element *root = parser.get_document()->get_root_node();
00228 assert(root != NULL);
00229 return root;
00230 }
00231
00232
00233
00234 XmlWriter::XmlWriter()
00235 {
00236 m_doc = NULL;
00237 m_root = NULL;
00238 m_save = false;
00239 }
00240
00241 XmlWriter::~XmlWriter()
00242 {
00243 Save();
00244 delete m_doc;
00245 }
00246
00247 bool XmlWriter::IsOk() const
00248 {
00249 return (m_doc != NULL) && (m_root != NULL);
00250 }
00251
00252 void XmlWriter::WriteElement(xmlpp::Element *x,
00253 const std::string &name,
00254 const std::string &value)
00255 {
00256 xmlpp::Element *elem = x->add_child(name);
00257 elem->add_child_text(value);
00258 m_save = false;
00259 }
00260
00261 bool XmlWriter::Create(const std::string &filename,const std::string &root,
00262 const std::string &version,const std::string &encoding)
00263 {
00264 delete m_doc;
00265 delete m_root;
00266 m_save = false;
00267 m_filename = filename;
00268 m_encoding = encoding;
00269 m_doc = new xmlpp::Document(version);
00270 m_root = m_doc->create_root_node(root);
00271 assert(m_root != NULL);
00272 return true;
00273 }
00274
00275 xmlpp::Element* XmlWriter::GetRoot()
00276 {
00277 assert (m_root != NULL);
00278 return m_root;
00279 }
00280
00281 bool XmlWriter::Save()
00282 {
00283 if (m_save) return true;
00284 m_save = true;
00285 try {
00286 m_doc->write_to_file_formatted(m_filename, m_encoding);
00287 } catch (const xmlpp::exception &err) {
00288 return false;
00289 }
00290 return true;
00291 }