UniSet  2.0.0
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 #include <cstddef>
00036 
00037 #include <libxml/parser.h>
00038 #include <libxml/tree.h>
00039 // --------------------------------------------------------------------------
00040 class UniXML_iterator:
00041     public std::iterator<std::bidirectional_iterator_tag, xmlNode, ptrdiff_t,xmlNode*, xmlNode&>
00042 {
00043     public:
00044         UniXML_iterator(xmlNode* node) :
00045             curNode(node)
00046         {}
00047         UniXML_iterator() {}
00048 
00049         std::string getProp( const std::string& name );
00050         std::string getPropUtf8( const std::string& name );
00051         int getIntProp( const std::string& name );
00053         int getPIntProp( const std::string& name, int def );
00054         void setProp( const std::string& name, const std::string& text );
00055 
00056         bool findName( const std::string& node, const std::string& searchname );
00057         bool find( const std::string& searchnode);
00058 
00060         bool goNext();
00061 
00063         bool goThrowNext();
00064 
00066         bool goPrev();
00067 
00068         bool canPrev();
00069         bool canNext();
00070 #if 0
00071         friend inline bool operator==(const UniXML_iterator& lhs, const UniXML_iterator& rhs)
00072         {
00073             return ( lhs.curNode != 0 && rhs.curNode!=0 && lhs.curNode == rhs.curNode );
00074         }
00075         
00076         friend inline bool operator!=(const UniXML_iterator& lhs, const UniXML_iterator& rhs){return !(lhs == rhs);}
00077 
00078         inline bool operator==(int a)
00079         {
00080             if( a == 0 )
00081                 return (curNode == NULL);
00082 
00083             return false;
00084         }
00085 #endif
00086         // Перейти к следующему узлу
00087         UniXML_iterator operator+(int);
00088         UniXML_iterator operator++(int);
00089         UniXML_iterator operator+=(int);
00090         UniXML_iterator operator++();
00091 
00092         // Перейти к предыдущему узлу
00093         UniXML_iterator operator-(int);
00094         UniXML_iterator operator--(int);
00095         UniXML_iterator operator--();
00096         UniXML_iterator operator-=(int);
00097 
00101         bool goParent();
00102 
00106         bool goChildren();
00107 
00108         // Получить текущий узел
00109         xmlNode* getCurrent()
00110         {
00111             return curNode;
00112         }
00113 
00114         // Получить название текущего узла
00115         inline const std::string getName() const
00116         {
00117             if( curNode )
00118             {
00119                 if( !curNode->name )
00120                     return "";
00121 
00122                 return (char*) curNode->name;
00123             }
00124 
00125             return "";
00126         }
00127 
00128         const std::string getContent() const;
00129 
00130         inline operator xmlNode*() const
00131         {
00132             //ulog.< "current\n";
00133             return curNode;
00134         }
00135 
00136         inline void goBegin()
00137         {
00138             while(canPrev()){goPrev();}
00139         }
00140 
00141         inline void goEnd()
00142         {
00143             while(canNext()){goNext();}
00144         }
00145 
00146     protected:
00147         xmlNode* curNode;
00148 };
00149 // --------------------------------------------------------------------------
00150 class UniXML
00151 {
00152 public:
00153 
00154     typedef UniXML_iterator iterator;
00155 
00156     inline xmlNode* getFirstNode()
00157     {
00158         return xmlDocGetRootElement(doc);
00159     }
00160 
00161     inline xmlNode* getFirstNode() const
00162     {
00163         return xmlDocGetRootElement(doc);
00164     }
00165 
00166 
00168     inline iterator begin()
00169     {
00170         return iterator(getFirstNode());
00171     }
00172 
00173     inline iterator end()
00174     {
00175         return  iterator(NULL);
00176     }
00177 
00178     // Загружает указанный файл
00179     void open(const std::string& filename);
00180 
00181     void close();
00182     inline bool isOpen(){ return doc!=0; }
00183     UniXML( const std::string& filename );
00184 
00185     UniXML();
00186 
00187     ~UniXML();
00188 
00189     xmlNode* cur;
00190     xmlDoc* doc;
00191     std::string filename;
00192 
00193     // Создать новый XML-документ
00194     void newDoc(const std::string& root_node, std::string xml_ver="1.0");
00195 
00196     // Получить свойство name указанного узла node
00197     static std::string getProp(const xmlNode* node, const std::string& name);
00198     static std::string getPropUtf8(const xmlNode* node, const std::string& name);
00199     static int getIntProp(const xmlNode* node, const std::string& name);
00201     static int getPIntProp(const xmlNode* node, const std::string& name, int def);
00202 
00203     // Установить свойство name указанного узла node
00204     static void setProp(xmlNode* node, const std::string& name, const std::string& text);
00205 
00206     // Добавить новый дочерний узел
00207     static xmlNode* createChild(xmlNode* node, const std::string& title, const std::string& text);
00208 
00209     // Добавить следующий узел
00210     static xmlNode* createNext(xmlNode* node, const std::string& title, const std::string& text);
00211 
00212     // Удалить указанный узел и все вложенные узлы
00213     static void removeNode(xmlNode* node);
00214 
00215     // копировать указанный узел и все вложенные узлы
00216     static xmlNode* copyNode(xmlNode* node, int recursive=1);
00217 
00218     // Сохранить в файл, если параметр не указан, сохраняет в тот файл
00219     // который был загружен последним.
00220     bool save(const std::string& filename="", int level = 2);
00221 
00222     // Переместить указатель к следующему узлу
00223     static xmlNode* nextNode(xmlNode* node);
00224 
00225     // После проверки исправить рекурсивный алгоритм на обычный,
00226     // используя ->parent
00227     xmlNode* findNode( xmlNode* node, const std::string& searchnode, const std::string& name = "") const;
00228     xmlNode* findNodeUtf8( xmlNode* node, const std::string& searchnode, const std::string& name = "") const;
00229 
00230     // ??
00231     //width means number of nodes of the same level as node in 1-st parameter (width number includes first node)
00232     //depth means number of times we can go to the children, if 0 we can't go only to elements of the same level
00233     xmlNode* extFindNode( xmlNode* node, int depth, int width, const std::string& searchnode, const std::string& name = "", bool top=true ) const;
00234     xmlNode* extFindNodeUtf8( xmlNode* node, int depth, int width, const std::string& searchnode, const std::string& name = "", bool top=true ) const;
00235 
00236 
00237 protected:
00238     // Преобразование текстовой строки из XML в строку нашего внутреннего представления
00239     static std::string xml2local( const std::string& text );
00240 
00241     // Преобразование текстовой строки из нашего внутреннего представления в строку для XML
00242     // Возвращает указатель на временный буфер, который один на все вызовы функции.
00243     static const xmlChar* local2xml( const std::string& text );
00244     static std::string local2utf8( const std::string& text );
00245 
00246     static int recur;
00247 };
00248 // --------------------------------------------------------------------------
00249 #endif