yast2-storage

XmlFile.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010 Novell, Inc.
00003  *
00004  * All Rights Reserved.
00005  *
00006  * This program is free software; you can redistribute it and/or modify it
00007  * under the terms of version 2 of the GNU General Public License as published
00008  * by the Free Software Foundation.
00009  *
00010  * This program is distributed in the hope that it will be useful, but WITHOUT
00011  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00013  * more details.
00014  *
00015  * You should have received a copy of the GNU General Public License along
00016  * with this program; if not, contact Novell, Inc.
00017  *
00018  * To contact Novell about this file by physical or electronic mail, you may
00019  * find current contact information at www.novell.com.
00020  */
00021 
00022 
00023 #ifndef XML_FILE_H
00024 #define XML_FILE_H
00025 
00026 
00027 #include <libxml/tree.h>
00028 #include <string>
00029 #include <list>
00030 #include <boost/noncopyable.hpp>
00031 
00032 #include "storage/AppUtil.h"
00033 
00034 
00035 namespace storage
00036 {
00037     using namespace std;
00038 
00039 
00040     class XmlFile : boost::noncopyable
00041     {
00042 
00043     public:
00044 
00045         XmlFile();
00046         XmlFile(const string& filename);
00047 
00048         ~XmlFile();
00049 
00050         bool save(const string& filename)
00051             { return xmlSaveFormatFile(filename.c_str(), doc, 1); }
00052 
00053         void setRootElement(xmlNode* node)
00054             { xmlDocSetRootElement(doc, node); }
00055 
00056         const xmlNode* getRootElement() const
00057             { return xmlDocGetRootElement(doc); }
00058 
00059     private:
00060 
00061         xmlDoc* doc;
00062 
00063     };
00064 
00065 
00066     xmlNode* xmlNewNode(const char* name);
00067     xmlNode* xmlNewChild(xmlNode* node, const char* name);
00068 
00069 
00070     const xmlNode* getChildNode(const xmlNode* node, const char* name);
00071     list<const xmlNode*> getChildNodes(const xmlNode* node, const char* name);
00072 
00073 
00074     bool getChildValue(const xmlNode* node, const char* name, string& value);
00075     bool getChildValue(const xmlNode* node, const char* name, bool& value);
00076 
00077     template<typename Type>
00078     bool getChildValue(const xmlNode* node, const char* name, Type& value)
00079     {
00080         string tmp;
00081         if (!getChildValue(node, name, tmp))
00082             return false;
00083 
00084         std::istringstream istr(tmp);
00085         classic(istr);
00086         istr >> value;
00087         return true;
00088     }
00089 
00090 
00091     void setChildValue(xmlNode* node, const char* name, const char* value);
00092     void setChildValue(xmlNode* node, const char* name, const string& value);
00093     void setChildValue(xmlNode* node, const char* name, bool value);
00094 
00095     template<typename Type>
00096     void setChildValue(xmlNode* node, const char* name, const Type& value)
00097     {
00098         std::ostringstream ostr;
00099         classic(ostr);
00100         ostr << value;
00101         setChildValue(node, name, ostr.str());
00102     }
00103 
00104     template<typename Type>
00105     void setChildValue(xmlNode* node, const char* name, const list<Type>& values)
00106     {
00107         for (typename list<Type>::const_iterator it = values.begin(); it != values.end(); ++it)
00108             setChildValue(node, name, *it);
00109     }
00110 
00111 }
00112 
00113 
00114 #endif