XMLTreeWalker.h Source File documentation

XMLTreeWalker.h
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/*
3 * This file is part of the libe-book project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10#ifndef XMLTREEWALKER_H_INCLUDED
11#define XMLTREEWALKER_H_INCLUDED
12
13#include <iterator>
14#include <string>
15
16#include <boost/intrusive_ptr.hpp>
17
18#include <librevenge-stream/librevenge-stream.h>
19
20#include "libebook_libcss.h"
21#include "XMLStylesheet.h"
22#include "XMLTreeNode.h"
23
24namespace libebook
25{
26
27 class XMLTreeWalker
28{
29public:
30 class Attribute;
31 class AttributeIterator;
32 class Iterator;
33
34 typedef int (*TokenizerFun_t)(const char *, std::size_t);
35
36public:
37 explicit XMLTreeWalker(const XMLTreeNodePtr_t &p, TokenizerFun_t tok = 0);
38
39 bool empty() const;
40
41 Iterator begin() const;
42 const Iterator end() const;
43
44 bool hasParent() const;
45 const XMLTreeWalker getParent() const;
46
47 bool isDocument() const;
48 bool isElement() const;
49 bool isText() const;
50
51 int getId() const;
52
53 const std::string getName() const;
54 const std::string getNamespace() const;
55
56 const std::string &getText() const;
57
58 bool hasAttributes() const;
59
60 AttributeIterator beginAttributes() const;
61 const AttributeIterator endAttributes() const;
62
63 CSSSelectResultsPtr_t getStyle(const XMLStylesheets_t &sheets, bool quirks = false,
64 const CSSStylesheetPtr_t &inlineSheet = CSSStylesheetPtr_t()) const;
65
66private:
67 XMLTreeNodePtr_t m_impl;
68 TokenizerFun_t m_tok;
69};
70
71 class XMLTreeWalker::Attribute
72{
73 // need access to ctor
74 friend class AttributeIterator;
75 friend class XMLTreeWalker;
76
77public:
78 int getId() const;
79 int getValueId() const;
80
81 const std::string getName() const;
82 const std::string getNamespace() const;
83 const std::string getValue() const;
84
85 bool operator==(const XMLTreeWalker::Attribute &rhs) const;
86
87private:
88 Attribute(const XMLTreeNodePtr_t &node, std::size_t index, TokenizerFun_t tok);
89
90private:
91 XMLTreeNodePtr_t m_node;
92 std::size_t m_index;
93 TokenizerFun_t m_tok;
94};
95
96bool operator!=(const XMLTreeWalker::Attribute &lhs, const XMLTreeWalker::Attribute &rhs);
97
98 class XMLTreeWalker::AttributeIterator
99{
100 friend class XMLTreeWalker; // needs access to ctor
101
102public:
103 AttributeIterator &operator++();
104 const AttributeIterator operator++(int);
105
106 const Attribute &operator*() const;
107 const Attribute *operator->() const;
108
109 bool operator==(const XMLTreeWalker::AttributeIterator &rhs) const;
110
111private:
112 explicit AttributeIterator(const XMLTreeNodePtr_t &node, TokenizerFun_t tok, bool end = false);
113
114private:
115 const XMLTreeNodePtr_t m_node;
116 std::size_t m_index;
117 Attribute m_current;
118 TokenizerFun_t m_tok;
119};
120
121bool operator!=(const XMLTreeWalker::AttributeIterator &lhs, const XMLTreeWalker::AttributeIterator &rhs);
122
123 class XMLTreeWalker::Iterator
124{
125 friend class XMLTreeWalker; // needs access to ctor
126
127public:
128 Iterator &operator++();
129 const Iterator operator++(int);
130
131 const XMLTreeWalker &operator*() const;
132 const XMLTreeWalker *operator->() const;
133
134 bool operator==(const XMLTreeWalker::Iterator &rhs) const;
135
136private:
137 Iterator(const XMLTreeNodePtr_t &node, TokenizerFun_t tok);
138
139private:
140 XMLTreeNodePtr_t m_node;
141 XMLTreeWalker m_current;
142 TokenizerFun_t m_tok;
143};
144
145bool operator!=(const XMLTreeWalker::Iterator &lhs, const XMLTreeWalker::Iterator &rhs);
146
147}
148
149 namespace std
150{
151
152// NOTE: I use specialization of std::iterator_traits because -Weffc++
153// is too eager and does not let me derive from std::iterator .
154
155template<>
156 struct iterator_traits<libebook::XMLTreeWalker::AttributeIterator>
157{
158 typedef libebook::XMLTreeWalker::Attribute value_type;
159 typedef std::ptrdiff_t difference_type;
160 typedef std::forward_iterator_tag iterator_category;
161 typedef libebook::XMLTreeWalker::Attribute *pointer_type;
162 typedef libebook::XMLTreeWalker::Attribute &reference_type;
163};
164
165template<>
166 struct iterator_traits<libebook::XMLTreeWalker::Iterator>
167{
168 typedef libebook::XMLTreeWalker value_type;
169 typedef std::ptrdiff_t difference_type;
170 typedef std::forward_iterator_tag iterator_category;
171 typedef libebook::XMLTreeWalker *pointer_type;
172 typedef libebook::XMLTreeWalker &reference_type;
173};
174
175}
176
177#endif // XMLTREEWALKER_H_INCLUDED
178
179/* vim:set shiftwidth=2 softtabstop=2 expandtab: */
XMLStylesheet.h
XMLTreeNode.h
libebook::XMLTreeWalker::AttributeIterator
Definition: XMLTreeWalker.h:99
libebook::XMLTreeWalker::AttributeIterator::operator*
const Attribute & operator*() const
Definition: XMLTreeWalker.cpp:1441
libebook::XMLTreeWalker::AttributeIterator::m_node
const XMLTreeNodePtr_t m_node
Definition: XMLTreeWalker.h:115
libebook::XMLTreeWalker::AttributeIterator::operator->
const Attribute * operator->() const
Definition: XMLTreeWalker.cpp:1448
libebook::XMLTreeWalker::AttributeIterator::m_tok
TokenizerFun_t m_tok
Definition: XMLTreeWalker.h:118
libebook::XMLTreeWalker::AttributeIterator::m_current
Attribute m_current
Definition: XMLTreeWalker.h:117
libebook::XMLTreeWalker::AttributeIterator::AttributeIterator
AttributeIterator(const XMLTreeNodePtr_t &node, TokenizerFun_t tok, bool end=false)
Definition: XMLTreeWalker.cpp:1460
libebook::XMLTreeWalker::AttributeIterator::operator++
AttributeIterator & operator++()
Definition: XMLTreeWalker.cpp:1423
libebook::XMLTreeWalker::AttributeIterator::m_index
std::size_t m_index
Definition: XMLTreeWalker.h:116
libebook::XMLTreeWalker::AttributeIterator::operator==
bool operator==(const XMLTreeWalker::AttributeIterator &rhs) const
Definition: XMLTreeWalker.cpp:1455
libebook::XMLTreeWalker::Attribute
Definition: XMLTreeWalker.h:72
libebook::XMLTreeWalker::Attribute::getValue
const std::string getValue() const
Definition: XMLTreeWalker.cpp:1401
libebook::XMLTreeWalker::Attribute::getName
const std::string getName() const
Definition: XMLTreeWalker.cpp:1391
libebook::XMLTreeWalker::Attribute::operator==
bool operator==(const XMLTreeWalker::Attribute &rhs) const
Definition: XMLTreeWalker.cpp:1406
libebook::XMLTreeWalker::Attribute::m_tok
TokenizerFun_t m_tok
Definition: XMLTreeWalker.h:93
libebook::XMLTreeWalker::Attribute::m_index
std::size_t m_index
Definition: XMLTreeWalker.h:92
libebook::XMLTreeWalker::Attribute::Attribute
Attribute(const XMLTreeNodePtr_t &node, std::size_t index, TokenizerFun_t tok)
Definition: XMLTreeWalker.cpp:1411
libebook::XMLTreeWalker::Attribute::getValueId
int getValueId() const
Definition: XMLTreeWalker.cpp:1382
libebook::XMLTreeWalker::Attribute::getNamespace
const std::string getNamespace() const
Definition: XMLTreeWalker.cpp:1396
libebook::XMLTreeWalker::Attribute::getId
int getId() const
Definition: XMLTreeWalker.cpp:1360
libebook::XMLTreeWalker::Attribute::m_node
XMLTreeNodePtr_t m_node
Definition: XMLTreeWalker.h:91
libebook::XMLTreeWalker::Iterator
Definition: XMLTreeWalker.h:124
libebook::XMLTreeWalker::Iterator::operator++
Iterator & operator++()
Definition: XMLTreeWalker.cpp:1474
libebook::XMLTreeWalker::Iterator::m_node
XMLTreeNodePtr_t m_node
Definition: XMLTreeWalker.h:140
libebook::XMLTreeWalker::Iterator::m_tok
TokenizerFun_t m_tok
Definition: XMLTreeWalker.h:142
libebook::XMLTreeWalker::Iterator::operator==
bool operator==(const XMLTreeWalker::Iterator &rhs) const
Definition: XMLTreeWalker.cpp:1506
libebook::XMLTreeWalker::Iterator::operator->
const XMLTreeWalker * operator->() const
Definition: XMLTreeWalker.cpp:1499
libebook::XMLTreeWalker::Iterator::operator*
const XMLTreeWalker & operator*() const
Definition: XMLTreeWalker.cpp:1492
libebook::XMLTreeWalker::Iterator::m_current
XMLTreeWalker m_current
Definition: XMLTreeWalker.h:141
libebook::XMLTreeWalker::Iterator::Iterator
Iterator(const XMLTreeNodePtr_t &node, TokenizerFun_t tok)
Definition: XMLTreeWalker.cpp:1511
libebook::XMLTreeWalker
Definition: XMLTreeWalker.h:28
libebook::XMLTreeWalker::getId
int getId() const
Definition: XMLTreeWalker.cpp:1283
libebook::XMLTreeWalker::isText
bool isText() const
Definition: XMLTreeWalker.cpp:1276
libebook::XMLTreeWalker::TokenizerFun_t
int(* TokenizerFun_t)(const char *, std::size_t)
Definition: XMLTreeWalker.h:34
libebook::XMLTreeWalker::hasAttributes
bool hasAttributes() const
Definition: XMLTreeWalker.cpp:1330
libebook::XMLTreeWalker::getParent
const XMLTreeWalker getParent() const
Definition: XMLTreeWalker.cpp:1255
libebook::XMLTreeWalker::XMLTreeWalker
XMLTreeWalker(const XMLTreeNodePtr_t &p, TokenizerFun_t tok=0)
Definition: XMLTreeWalker.cpp:1354
libebook::XMLTreeWalker::getName
const std::string getName() const
Definition: XMLTreeWalker.cpp:1306
libebook::XMLTreeWalker::begin
Iterator begin() const
Definition: XMLTreeWalker.cpp:1232
libebook::XMLTreeWalker::beginAttributes
AttributeIterator beginAttributes() const
Definition: XMLTreeWalker.cpp:1338
libebook::XMLTreeWalker::m_impl
XMLTreeNodePtr_t m_impl
Definition: XMLTreeWalker.h:67
libebook::XMLTreeWalker::getText
const std::string & getText() const
Definition: XMLTreeWalker.cpp:1322
libebook::XMLTreeWalker::end
const Iterator end() const
Definition: XMLTreeWalker.cpp:1240
libebook::XMLTreeWalker::empty
bool empty() const
Definition: XMLTreeWalker.cpp:1224
libebook::XMLTreeWalker::getStyle
CSSSelectResultsPtr_t getStyle(const XMLStylesheets_t &sheets, bool quirks=false, const CSSStylesheetPtr_t &inlineSheet=CSSStylesheetPtr_t()) const
Definition: XMLTreeWalker.cpp:1524
libebook::XMLTreeWalker::hasParent
bool hasParent() const
Definition: XMLTreeWalker.cpp:1248
libebook::XMLTreeWalker::endAttributes
const AttributeIterator endAttributes() const
Definition: XMLTreeWalker.cpp:1346
libebook::XMLTreeWalker::isElement
bool isElement() const
Definition: XMLTreeWalker.cpp:1269
libebook::XMLTreeWalker::isDocument
bool isDocument() const
Definition: XMLTreeWalker.cpp:1262
libebook::XMLTreeWalker::getNamespace
const std::string getNamespace() const
Definition: XMLTreeWalker.cpp:1314
libebook::XMLTreeWalker::m_tok
TokenizerFun_t m_tok
Definition: XMLTreeWalker.h:68
libebook_libcss.h
libebook::EBOOKHTMLToken::p
@ p
Definition: EBOOKHTMLToken.h:116
libebook::EBOOKHTMLToken::lhs
@ lhs
Definition: EBOOKHTMLToken.h:290
libebook::EBOOKHTMLToken::rhs
@ rhs
Definition: EBOOKHTMLToken.h:309
libebook
Definition: BBeBCollector.cpp:19
libebook::XMLStylesheets_t
std::deque< XMLStylesheet > XMLStylesheets_t
Definition: XMLStylesheet.h:51
libebook::XMLTreeNodePtr_t
boost::intrusive_ptr< XMLTreeNode > XMLTreeNodePtr_t
Definition: XMLTreeNode.h:24
libebook::CSSSelectResultsPtr_t
std::shared_ptr< css_select_results > CSSSelectResultsPtr_t
Definition: libebook_libcss.h:25
libebook::CSSStylesheetPtr_t
std::shared_ptr< css_stylesheet > CSSStylesheetPtr_t
Definition: libebook_libcss.h:23
libebook::operator!=
bool operator!=(const XMLTreeNode::QName &lhs, const XMLTreeNode::QName &rhs)
Definition: XMLTreeNode.cpp:445
std
Definition: XMLTreeWalker.h:150
std::iterator_traits< libebook::XMLTreeWalker::AttributeIterator >::difference_type
std::ptrdiff_t difference_type
Definition: XMLTreeWalker.h:159
std::iterator_traits< libebook::XMLTreeWalker::AttributeIterator >::reference_type
libebook::XMLTreeWalker::Attribute & reference_type
Definition: XMLTreeWalker.h:162
std::iterator_traits< libebook::XMLTreeWalker::AttributeIterator >::iterator_category
std::forward_iterator_tag iterator_category
Definition: XMLTreeWalker.h:160
std::iterator_traits< libebook::XMLTreeWalker::AttributeIterator >::value_type
libebook::XMLTreeWalker::Attribute value_type
Definition: XMLTreeWalker.h:158
std::iterator_traits< libebook::XMLTreeWalker::AttributeIterator >::pointer_type
libebook::XMLTreeWalker::Attribute * pointer_type
Definition: XMLTreeWalker.h:161
std::iterator_traits< libebook::XMLTreeWalker::Iterator >::difference_type
std::ptrdiff_t difference_type
Definition: XMLTreeWalker.h:169
std::iterator_traits< libebook::XMLTreeWalker::Iterator >::iterator_category
std::forward_iterator_tag iterator_category
Definition: XMLTreeWalker.h:170
std::iterator_traits< libebook::XMLTreeWalker::Iterator >::pointer_type
libebook::XMLTreeWalker * pointer_type
Definition: XMLTreeWalker.h:171
std::iterator_traits< libebook::XMLTreeWalker::Iterator >::reference_type
libebook::XMLTreeWalker & reference_type
Definition: XMLTreeWalker.h:172
std::iterator_traits< libebook::XMLTreeWalker::Iterator >::value_type
libebook::XMLTreeWalker value_type
Definition: XMLTreeWalker.h:168