From 8335fdb6b7e7afb57d096e0f3a453b662f7a23c0 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Sat, 24 Jul 2004 19:06:51 +0000 Subject: - Post processing code cleanup. --- src/domhelpers.h | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) (limited to 'src/domhelpers.h') diff --git a/src/domhelpers.h b/src/domhelpers.h index 043ffd4..d125f80 100644 --- a/src/domhelpers.h +++ b/src/domhelpers.h @@ -40,6 +40,9 @@ #define __DOMHELPERS_H__ #include "sablo.h" +#include +#include +#include /* * DOMHelpers @@ -68,6 +71,88 @@ public: // Get previous element (in XML flow) of a given name static DOM::Element getPriorElement(const DOM::Node& node, const string& name); + + // Get first child element of a given name + static DOM::Element getChildElement(const DOM::Node& parent, const string& name); + + // Check if a given element is anothers ancestor + static bool hasAncestor(const DOM::Node& ancestor, const DOM::Node& node); +}; + +/* + * ElementTable + * + * A table of elements matched to their ids for quick access while applying + * things like fonts, styles, lists from their definitions. + */ +class ElementTable : + public std::map +{ +public: + void load(const DOM::Node& parent, const string& name); + + DOM::Element get(const wstring& id) const; + + bool has(const wstring& id) const + { return find(id) != end(); } + + void removeIds(); +}; + +// Some other handy types +typedef std::set StringSet; +typedef std::stack NodeStack; + +/* + * ElementIterator + * + * For iterating through the elements in a document. + */ +class ElementIterator + : public std::iterator +{ +public: + ElementIterator() + { m_current = NULL; } + ElementIterator(const DOM::Element& top) + { m_top = top; m_current = top; next(); } + ElementIterator(const ElementIterator& x) + { m_top = x.m_top; m_current = x.m_current; } + + const DOM::Element& operator*() const + { return m_current; } + const DOM::Element* operator->() const + { return (&**this); } + const ElementIterator& operator++() + { next(); return (*this); } + const ElementIterator& operator--() + { prev(); return (*this); } + + // Friend comparision functions + friend bool operator==(const ElementIterator& x, const ElementIterator& y); + friend bool operator!=(const ElementIterator& x, const ElementIterator& y); + +// Implementation +protected: + + void next(); + DOM::Element nextel(DOM::Node node); + + void prev(); + DOM::Element prevel(DOM::Node node); + +// Data +protected: + + DOM::Element m_top; + DOM::Element m_current; + bool m_done; }; +// friend functions +inline bool operator==(const ElementIterator& x, const ElementIterator& y) + { return y.m_current == x.m_current && y.m_top == x.m_top; } +inline bool operator!=(const ElementIterator& x, const ElementIterator& y) + { return (!(x == y)); } + #endif // __DOMHELPERS_H__ -- cgit v1.2.3