XMLTreeWalker.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /*
00003  * This file is part of the libe-book project.
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this
00007  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
00008  */
00009 
00010 #ifndef XMLTREEWALKER_H_INCLUDED
00011 #define XMLTREEWALKER_H_INCLUDED
00012 
00013 #include <iterator>
00014 #include <string>
00015 
00016 #include <boost/intrusive_ptr.hpp>
00017 
00018 #include <librevenge-stream/librevenge-stream.h>
00019 
00020 #include "libebook_libcss.h"
00021 #include "XMLStylesheet.h"
00022 #include "XMLTreeNode.h"
00023 
00024 namespace libebook
00025 {
00026 
00027 class XMLTreeWalker
00028 {
00029 public:
00030   class Attribute;
00031   class AttributeIterator;
00032   class Iterator;
00033 
00034   typedef int (*TokenizerFun_t)(const char *, std::size_t);
00035 
00036 public:
00037   explicit XMLTreeWalker(const XMLTreeNodePtr_t &p, TokenizerFun_t tok = 0);
00038 
00039   bool empty() const;
00040 
00041   Iterator begin() const;
00042   const Iterator end() const;
00043 
00044   bool hasParent() const;
00045   const XMLTreeWalker getParent() const;
00046 
00047   bool isDocument() const;
00048   bool isElement() const;
00049   bool isText() const;
00050 
00051   int getId() const;
00052 
00053   const std::string getName() const;
00054   const std::string getNamespace() const;
00055 
00056   const std::string &getText() const;
00057 
00058   bool hasAttributes() const;
00059 
00060   AttributeIterator beginAttributes() const;
00061   const AttributeIterator endAttributes() const;
00062 
00063   CSSSelectResultsPtr_t getStyle(const XMLStylesheets_t &sheets, bool quirks = false,
00064                                  const CSSStylesheetPtr_t &inlineSheet = CSSStylesheetPtr_t()) const;
00065 
00066 private:
00067   XMLTreeNodePtr_t m_impl;
00068   TokenizerFun_t m_tok;
00069 };
00070 
00071 class XMLTreeWalker::Attribute
00072 {
00073   // need access to ctor
00074   friend class AttributeIterator;
00075   friend class XMLTreeWalker;
00076 
00077 public:
00078   int getId() const;
00079   int getValueId() const;
00080 
00081   const std::string getName() const;
00082   const std::string getNamespace() const;
00083   const std::string getValue() const;
00084 
00085   bool operator==(const XMLTreeWalker::Attribute &rhs) const;
00086 
00087 private:
00088   Attribute(const XMLTreeNodePtr_t &node, std::size_t index, TokenizerFun_t tok);
00089 
00090 private:
00091   XMLTreeNodePtr_t m_node;
00092   std::size_t m_index;
00093   TokenizerFun_t m_tok;
00094 };
00095 
00096 bool operator!=(const XMLTreeWalker::Attribute &lhs, const XMLTreeWalker::Attribute &rhs);
00097 
00098 class XMLTreeWalker::AttributeIterator
00099 {
00100   friend class XMLTreeWalker; // needs access to ctor
00101 
00102 public:
00103   AttributeIterator &operator++();
00104   const AttributeIterator operator++(int);
00105 
00106   const Attribute &operator*() const;
00107   const Attribute *operator->() const;
00108 
00109   bool operator==(const XMLTreeWalker::AttributeIterator &rhs) const;
00110 
00111 private:
00112   explicit AttributeIterator(const XMLTreeNodePtr_t &node, TokenizerFun_t tok, bool end = false);
00113 
00114 private:
00115   const XMLTreeNodePtr_t m_node;
00116   std::size_t m_index;
00117   Attribute m_current;
00118   TokenizerFun_t m_tok;
00119 };
00120 
00121 bool operator!=(const XMLTreeWalker::AttributeIterator &lhs, const XMLTreeWalker::AttributeIterator &rhs);
00122 
00123 class XMLTreeWalker::Iterator
00124 {
00125   friend class XMLTreeWalker; // needs access to ctor
00126 
00127 public:
00128   Iterator &operator++();
00129   const Iterator operator++(int);
00130 
00131   const XMLTreeWalker &operator*() const;
00132   const XMLTreeWalker *operator->() const;
00133 
00134   bool operator==(const XMLTreeWalker::Iterator &rhs) const;
00135 
00136 private:
00137   Iterator(const XMLTreeNodePtr_t &node, TokenizerFun_t tok);
00138 
00139 private:
00140   XMLTreeNodePtr_t m_node;
00141   XMLTreeWalker m_current;
00142   TokenizerFun_t m_tok;
00143 };
00144 
00145 bool operator!=(const XMLTreeWalker::Iterator &lhs, const XMLTreeWalker::Iterator &rhs);
00146 
00147 void intrusive_ptr_add_ref(XMLTreeNode *p);
00148 void intrusive_ptr_release(XMLTreeNode *p);
00149 
00150 }
00151 
00152 namespace std
00153 {
00154 
00155 // NOTE: I use specialization of std::iterator_traits because -Weffc++
00156 // is too eager and does not let me derive from std::iterator .
00157 
00158 template<>
00159 struct iterator_traits<libebook::XMLTreeWalker::AttributeIterator>
00160 {
00161   typedef libebook::XMLTreeWalker::Attribute value_type;
00162   typedef std::ptrdiff_t difference_type;
00163   typedef std::forward_iterator_tag iterator_category;
00164   typedef libebook::XMLTreeWalker::Attribute *pointer_type;
00165   typedef libebook::XMLTreeWalker::Attribute &reference_type;
00166 };
00167 
00168 template<>
00169 struct iterator_traits<libebook::XMLTreeWalker::Iterator>
00170 {
00171   typedef libebook::XMLTreeWalker value_type;
00172   typedef std::ptrdiff_t difference_type;
00173   typedef std::forward_iterator_tag iterator_category;
00174   typedef libebook::XMLTreeWalker *pointer_type;
00175   typedef libebook::XMLTreeWalker &reference_type;
00176 };
00177 
00178 }
00179 
00180 #endif // XMLTREEWALKER_H_INCLUDED
00181 
00182 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */