UniXML.h

См. документацию.
00001 /* This file is part of the UniSet project
00002  * Copyright (c) 2002-2010 Free Software Foundation, Inc.
00003  * Copyright (c) 2002, 2009, 2010 Vitaly Lipatov
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018  */
00019 // --------------------------------------------------------------------------
00026 // --------------------------------------------------------------------------
00027 
00028 // Класс для работы с данными в XML, выборки и перекодирования
00029 
00030 #ifndef UniXML_H_
00031 #define UniXML_H_
00032  
00033 #include <assert.h>
00034 #include <string>
00035 
00036 #include <libxml/parser.h>
00037 #include <libxml/tree.h>
00038 
00039 
00040 class UniXML_iterator : public std::iterator<std::bidirectional_iterator_tag, xmlNode, ptrdiff_t,                                               xmlNode*, xmlNode&>
00041 {
00042     public:
00043         UniXML_iterator(xmlNode* node) :
00044             curNode(node)
00045         {}
00046         UniXML_iterator() {}
00047 
00048         std::string getProp(const std::string name) const;
00049         std::string getPropUtf8(const std::string name) const;
00050         int getIntProp(const std::string name) const;
00052         int getPIntProp(const std::string name, int def) const;
00053         void setProp(const std::string name, const std::string text);
00054         
00055         bool findName(const std::string node, const std::string searchname); 
00056         bool find(const std::string searchnode);
00057     
00059         bool goNext();
00060 
00062         bool goThrowNext();
00063         
00065         bool goPrev();
00066         
00067         bool canPrev();
00068         bool canNext();
00069         
00070         // Перейти к следующему узлу
00071         UniXML_iterator operator ++(int);
00072         UniXML_iterator operator ++();
00073         
00074         // Перейти к предыдущему узлу
00075         UniXML_iterator operator --(int);
00076         UniXML_iterator operator --();
00077         
00081         bool goParent();
00082         
00086         bool goChildren();
00087         
00088         // Получить текущий узел
00089         xmlNode* getCurrent() const
00090         {
00091             return curNode;
00092         }
00093 
00094         // Получить название текущего узла
00095         const std::string getName() const
00096         {
00097             if( curNode )
00098                 return (char*) curNode->name;
00099             else
00100                 return "";
00101         }
00102 
00103         const std::string getContent() const;
00104 
00105         operator xmlNode*()
00106         {
00107             //unideb << "current\n";
00108             return curNode;
00109         }
00110 
00111         inline void goBegin()
00112         {
00113             while(canPrev()){goPrev();}
00114         }
00115 
00116         inline void goEnd()
00117         {
00118             while(canNext()){goNext();}
00119         }
00120 
00121     protected:
00122         xmlNode* curNode;
00123 };
00124 
00125 class UniXML
00126 {
00127 public:
00128     
00129     typedef UniXML_iterator                 iterator;
00130 
00131     inline xmlNode* getFirstNode()
00132     {
00133         return xmlDocGetRootElement(doc);
00134     }
00135 
00137     inline iterator begin()
00138     {
00139         return iterator(getFirstNode());
00140     }
00141 
00142     inline iterator end()
00143     {
00144         return  iterator(NULL);
00145     }
00146 
00147     // Загружает указанный файл
00148     void open(const std::string filename);
00149 
00150     void close();
00151     inline bool isOpen(){ return doc!=0; }
00152     UniXML(const std::string filename);
00153 
00154     UniXML();
00155 
00156     ~UniXML();
00157 
00158     xmlNode* cur;
00159     xmlDoc* doc;
00160     std::string filename;
00161     
00162     // Создать новый XML-документ
00163     void newDoc(const std::string& root_node, std::string xml_ver="1.0");
00164 
00165     // Получить свойство name указанного узла node
00166     static std::string getProp(const xmlNode* node, const std::string name);
00167     static std::string getPropUtf8(const xmlNode* node, const std::string name);
00168     static int getIntProp(const xmlNode* node, const std::string name);
00170     static int getPIntProp(const xmlNode* node, const std::string name, int def);
00171     
00172     // Установить свойство name указанного узла node
00173     static void setProp(xmlNode* node, const std::string name, const std::string text);
00174     
00175     // Добавить новый дочерний узел
00176     static xmlNode* createChild(xmlNode* node, const std::string title, const std::string text);
00177     
00178     // Добавить следующий узел
00179     static xmlNode* createNext(xmlNode* node, const std::string title, const std::string text);
00180     
00181     // Удалить указанный узел и все вложенные узлы
00182     static void removeNode(xmlNode* node);
00183     
00184     // копировать указанный узел и все вложенные узлы
00185     static xmlNode* copyNode(xmlNode* node, int recursive=1);
00186     
00187     // Сохранить в файл, если параметр не указан, сохраняет в тот файл
00188     // который был загружен последним.
00189     bool save(const std::string filename="", int level = 2);
00190 
00191     // Переместить указатель к следующему узлу
00192     static xmlNode* nextNode(xmlNode* node);
00193 
00194     // После проверки исправить рекурсивный алгоритм на обычный,
00195     // используя ->parent
00196     xmlNode* findNode(xmlNode* node, const std::string searchnode, const std::string name = "") const;
00197     xmlNode* findNodeUtf8(xmlNode* node, const std::string searchnode, const std::string name = "") const;
00198 
00199     xmlNode* extFindNode(xmlNode* node, int depth, int width, const std::string searchnode, const std::string name = "", bool top=true );
00200     xmlNode* extFindNodeUtf8(xmlNode* node, int depth, int width, const std::string searchnode, const std::string name = "", bool top=true );
00201 
00202 
00203 protected:
00204     // Преобразование текстовой строки из XML в строку нашего внутреннего представления
00205     static std::string xml2local(const std::string text);
00206 
00207     // Преобразование текстовой строки из нашего внутреннего представления в строку для XML
00208     // Возвращает указатель на временный буфер, который один на все вызовы функции.
00209     static const xmlChar* local2xml(std::string text);
00210     static std::string local2utf8(const std::string text);
00211 
00212     static int recur;
00213 
00214 };
00215 
00216 #endif

Документация по UniSet. Последние изменения: Mon Dec 26 11:26:59 2011. Создано системой  doxygen 1.5.9