Bumped copyright dates for 2013
[barry.git] / desktop / src / xmlmap.h
blob065c4ea8adacdadfa632a28fc7c8a69902469c49
1 ///
2 /// \file xmlmap.h
3 /// Map an XML node tree according to an XML map file.
4 /// The map is not fully nested, and provides mostly a
5 /// flat view, based on the map.
6 ///
8 /*
9 Copyright (C) 2010-2013, Chris Frey <cdfrey@foursquare.net>
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 See the GNU General Public License in the COPYING file at the
21 root directory of this project for more details.
24 #ifndef __XMLMAP_H__
25 #define __XMLMAP_H__
27 #include <string>
28 #include <vector>
29 #include <iosfwd>
30 #include <libxml++/libxml++.h>
31 #include <tr1/memory>
33 class XmlNodeMapping;
35 class XmlNodeSummary
37 xmlpp::Node *m_node;
38 XmlNodeMapping *m_mapping;
39 Glib::ustring m_name;
40 Glib::ustring m_summary;
42 protected:
43 void CreateSummary();
44 void ProcessListKey(std::ostream &os, const Glib::ustring &name);
45 void ProcessKey(std::ostream &os, const Glib::ustring &key);
47 public:
48 XmlNodeSummary(xmlpp::Node *node, XmlNodeMapping *mapping);
50 Glib::ustring Path() const { return m_node ? m_node->get_path(): Glib::ustring(); }
51 const Glib::ustring& Name() const { return m_name; }
52 const Glib::ustring& Summary() const { return m_summary; }
54 Glib::ustring GetContent(const Glib::ustring &child_name) const;
55 Glib::ustring GetContent(xmlpp::Node *node) const;
57 static bool SummaryCompare(const XmlNodeSummary &a,
58 const XmlNodeSummary &b);
59 static bool PathCompare(const XmlNodeSummary &a,
60 const XmlNodeSummary &b);
63 class XmlNodeMapping
65 public:
66 typedef std::vector<Glib::ustring> compare_list;
67 typedef std::vector<XmlNodeSummary> summary_list;
69 private:
70 Glib::ustring m_node_key_name; // "/contact/Address" excluding any
71 // [1] and [2] offsets
72 // This is is from the friendly map
73 // definition file.
74 int m_priority;
75 Glib::ustring m_format;
76 compare_list m_compare_node_names;
77 summary_list m_summaries;
79 protected:
80 static void SplitCompareName(const Glib::ustring &compare_name,
81 Glib::ustring &name, Glib::ustring &type);
83 public:
84 XmlNodeMapping(const Glib::ustring &node_name, int priority,
85 const Glib::ustring &format);
87 // data access
88 const Glib::ustring& KeyName() const { return m_node_key_name; }
89 Glib::ustring ShortName() const
90 { return size() ? m_summaries[0].Name() : Glib::ustring(); }
91 int Priority() const { return m_priority; }
92 const Glib::ustring& Format() const { return m_format; }
93 bool IsEmpty() const { return size() == 0; }
95 // operations
96 void AddCompareName(const Glib::ustring &node_name);
97 void AddNode(xmlpp::Node *node);
98 void SortBySummary();
99 void SortByPath();
101 const XmlNodeSummary& operator[] (int index) const;
102 size_t size() const { return m_summaries.size(); }
103 void clear() { m_summaries.clear(); }
105 bool operator== (const XmlNodeMapping &other) const;
106 bool operator!= (const XmlNodeMapping &other) const
107 { return !operator==(other); }
109 void Dump(std::ostream &os) const;
110 void DumpSummaries(std::ostream &os) const;
113 class XmlNodeMap
114 : public std::vector<std::tr1::shared_ptr<XmlNodeMapping> >
116 // we use a vector of shared_ptr's here because the Summary
117 // objects store pointers to the Mappings, and must remain stable
118 public:
119 typedef std::tr1::shared_ptr<XmlNodeMapping> ptr_type;
120 typedef std::vector<ptr_type> base_type;
122 // helps to hide the shared_ptr complexity when the
123 // user iterates through the vector
124 template <class BaseT, class TargetT>
125 class deref_iterator : public BaseT
127 public:
128 explicit deref_iterator(BaseT it)
129 : BaseT(it)
131 TargetT& operator*() const
132 { return *BaseT::operator*(); }
133 TargetT* operator->() const
134 { return &(*BaseT::operator*()); }
137 typedef deref_iterator<base_type::iterator, XmlNodeMapping> iterator;
138 typedef deref_iterator<base_type::const_iterator, XmlNodeMapping> const_iterator;
140 private:
141 void LoadMap(const std::string &type_name,
142 const std::string &map_filename);
144 public:
145 XmlNodeMap(const std::string &type_name,
146 const std::string &map_filename);
147 XmlNodeMap(const std::string &map_filename);
148 XmlNodeMap(const XmlNodeMap &other);
150 void ImportNodes(xmlpp::Node *node, bool purge = false);
151 void PurgeEmpties();
152 XmlNodeMapping* Find(const Glib::ustring &node_name);
154 void SortBySummary();
155 void SortByPath();
157 void Dump(std::ostream &os, int stop_priority = 1) const;
159 iterator begin() { return iterator(base_type::begin()); }
160 iterator end() { return iterator(base_type::end()); }
161 const_iterator begin() const { return const_iterator(base_type::begin()); }
162 const_iterator end() const { return const_iterator(base_type::end()); }
163 iterator priority_end(int stop_priority = 1);
164 void clear();
166 XmlNodeMap& operator=(const XmlNodeMap &other);
169 #endif