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.
9 Copyright (C) 2010-2012, 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.
30 #include <libxml++/libxml++.h>
38 XmlNodeMapping
*m_mapping
;
40 Glib::ustring m_summary
;
44 void ProcessListKey(std::ostream
&os
, const Glib::ustring
&name
);
45 void ProcessKey(std::ostream
&os
, const Glib::ustring
&key
);
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
);
66 typedef std::vector
<Glib::ustring
> compare_list
;
67 typedef std::vector
<XmlNodeSummary
> summary_list
;
70 Glib::ustring m_node_key_name
; // "/contact/Address" excluding any
71 // [1] and [2] offsets
72 // This is is from the friendly map
75 Glib::ustring m_format
;
76 compare_list m_compare_node_names
;
77 summary_list m_summaries
;
80 static void SplitCompareName(const Glib::ustring
&compare_name
,
81 Glib::ustring
&name
, Glib::ustring
&type
);
84 XmlNodeMapping(const Glib::ustring
&node_name
, int priority
,
85 const Glib::ustring
&format
);
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; }
96 void AddCompareName(const Glib::ustring
&node_name
);
97 void AddNode(xmlpp::Node
*node
);
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;
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
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
128 explicit deref_iterator(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
;
141 void LoadMap(const std::string
&type_name
,
142 const std::string
&map_filename
);
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);
152 XmlNodeMapping
* Find(const Glib::ustring
&node_name
);
154 void SortBySummary();
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);
166 XmlNodeMap
& operator=(const XmlNodeMap
&other
);