XMLTreeNode.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 XMLTREENODE_H_INCLUDED
00011 #define XMLTREENODE_H_INCLUDED
00012 
00013 #include <deque>
00014 #include <string>
00015 #include <utility>
00016 
00017 #include <boost/intrusive_ptr.hpp>
00018 
00019 #include "libebook_libcss.h"
00020 
00021 namespace libebook
00022 {
00023 
00024 struct XMLTreeNode;
00025 typedef boost::intrusive_ptr<XMLTreeNode> XMLTreeNodePtr_t;
00026 
00027 struct XMLTreeNode
00028 {
00029   enum NodeType
00030   {
00031     NODE_TYPE_DOCUMENT = 1 << 1,
00032     NODE_TYPE_ELEMENT = 1 << 2,
00033     NODE_TYPE_TEXT = 1 << 3,
00034     NODE_TYPE_COMMENT = 1 << 4,
00035     NODE_TYPE_DOCTYPE = 1 << 5
00036   };
00037 
00038   struct QName
00039   {
00040     LWCStringPtr_t name;
00041     LWCStringPtr_t ns;
00042 
00043     QName();
00044     explicit QName(const LWCStringPtr_t &name, const LWCStringPtr_t &ns = LWCStringPtr_t());
00045   };
00046 
00047   typedef std::pair<QName, QName> LinkId_t;
00048 
00049   struct Configuration
00050   {
00051     bool caseless;
00052 
00053     std::deque<QName> classes; //< A list of attributes representing a "class".
00054     std::deque<QName> ids; //< a list of attributes representing an "ID".
00055     std::deque<QName> langs; //< a list of attributes representing a "lang".
00056 
00062     std::deque<LinkId_t> links;
00063 
00064     explicit Configuration(bool caseless_ = false);
00065   };
00066 
00067   typedef boost::shared_ptr<Configuration> ConfigurationPtr_t;
00068 
00069   struct ElementData
00070   {
00071     QName qname;
00072     mutable int id;
00073     mutable bool idSet;
00074 
00075     ElementData();
00076     explicit ElementData(const LWCStringPtr_t &name, const LWCStringPtr_t &ns = LWCStringPtr_t());
00077   };
00078 
00079   struct AttributeData
00080   {
00081     QName qname;
00082     LWCStringPtr_t value;
00083     mutable int id;
00084     mutable bool idSet;
00085 
00086     AttributeData();
00087     AttributeData(const LWCStringPtr_t &name, const LWCStringPtr_t &ns, const LWCStringPtr_t &val);
00088     AttributeData(const LWCStringPtr_t &name, const LWCStringPtr_t &val);
00089   };
00090 
00091 public:
00092   explicit XMLTreeNode(NodeType nodeType, const ConfigurationPtr_t &config = ConfigurationPtr_t());
00093   ~XMLTreeNode();
00094 
00095   NodeType type;
00096   ConfigurationPtr_t configuration;
00097 
00098   ElementData self;
00099   std::deque<AttributeData> attributes;
00100   std::string text;
00101 
00102   XMLTreeNodePtr_t parent;
00103   // siblings
00104   XMLTreeNodePtr_t prev;
00105   XMLTreeNodePtr_t next;
00106   // children
00107   XMLTreeNodePtr_t first;
00108   XMLTreeNodePtr_t last;
00109 
00110   size_t elements;
00111 
00112   mutable int refcount;
00113 
00114   XMLTreeNodePtr_t clone(bool deep = false) const;
00115 
00116   XMLTreeNodePtr_t appendChild(const XMLTreeNodePtr_t &child);
00117   XMLTreeNodePtr_t insertChildBefore(const XMLTreeNodePtr_t &child, const XMLTreeNodePtr_t &selected);
00118   void removeChild(const XMLTreeNodePtr_t &child);
00119   void transferChildren(const XMLTreeNodePtr_t &newParent);
00120 
00121   void addAttributes(const std::deque<AttributeData> &attrs);
00122 
00123   bool hasChildren(bool ignoreText = false) const;
00124 
00125 private:
00126   // disable copying
00127   XMLTreeNode(const XMLTreeNode &);
00128   XMLTreeNode &operator=(const XMLTreeNode &);
00129 };
00130 
00131 bool operator==(const XMLTreeNode::QName &lhs, const XMLTreeNode::QName &rhs);
00132 bool operator!=(const XMLTreeNode::QName &lhs, const XMLTreeNode::QName &rhs);
00133 bool caselessEqual(const XMLTreeNode::QName &lhs, const XMLTreeNode::QName &rhs);
00134 
00135 bool operator==(const XMLTreeNode::AttributeData &lhs, const XMLTreeNode::AttributeData &rhs);
00136 bool operator!=(const XMLTreeNode::AttributeData &lhs, const XMLTreeNode::AttributeData &rhs);
00137 
00138 bool operator==(const XMLTreeNode::ElementData &lhs, const XMLTreeNode::ElementData &rhs);
00139 bool operator!=(const XMLTreeNode::ElementData &lhs, const XMLTreeNode::ElementData &rhs);
00140 
00141 void intrusive_ptr_add_ref(XMLTreeNode *p);
00142 void intrusive_ptr_release(XMLTreeNode *p);
00143 
00144 }
00145 
00146 #endif // XMLTREENODE_H_INCLUDED
00147 
00148 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */