MIRA
XMLDomPreprocessor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_XMLDOMPREPROCESSOR_H_
48 #define _MIRA_XMLDOMPREPROCESSOR_H_
49 
50 #include <list>
51 #include <map>
52 #include <memory>
53 #ifndef Q_MOC_RUN
54 #include <boost/function.hpp>
55 #endif
56 #include <xml/XMLDom.h>
57 
58 namespace mira {
59 
61 
63 
65 {
66  XMLVariableValue(const std::string& v = "",
67  const std::string& a = "") : value(v), annotation(a) {}
68 
69  template<typename Reflector>
70  void reflect(Reflector& r) {
71  r.property("value", value, "Variable value");
72  r.property("annotation", annotation, "Source annotation");
73  }
74 
75  XMLVariableValue& operator=(const char* s) { value = s; annotation = ""; return *this;}
76 
77  bool operator==(const std::string& s) const { return value == s; }
78  bool operator!=(const std::string& s) const { return value != s; }
79  bool operator==(const char* s) const { return value == s; }
80  bool operator!=(const char* s) const { return value != s; }
81 
82  operator std::string() const { return value; }
83 
84  friend std::ostream& operator<<(std::ostream& s, const XMLVariableValue& x);
85 
86  std::string value;
87  std::string annotation;
88 };
89 
90 std::ostream& operator<<(std::ostream& s, const XMLVariableValue& x);
91 
92 typedef std::map<std::string, XMLVariableValue> XMLVariablesMap;
93 
95 
96 class XMLMacroProcessor;
97 
110 {
111 public:
112  typedef boost::function<std::string (const std::string&)> Callback;
113  typedef std::list<Path> IncludePathStack;
114 
115  XMLDomPreprocessor(bool expandOnly = false);
116  XMLDomPreprocessor(const XMLVariablesMap& iVariables, bool expandOnly = false);
117 
118  void preprocessXML(XMLDom& ioXml);
119  void registerXMLVariableCallback(const std::string& pattern, Callback&& callback);
120 
122 
124 
125  bool expandMacrosOnly() const { return mExpandMacrosOnly; }
126 
128 
129 protected:
130 
131  std::string resolveXMLVariables(const std::string& pattern, const std::string& var);
132  std::string resolveContent(std::string content);
133 
135 
137 
138  bool parseCondition(XMLDom::iterator& node);
141  bool parseExists(XMLDom::iterator& node);
145 
146  std::map<std::string, Callback> mCallbacks;
147 
149 
150  std::unique_ptr<XMLMacroProcessor> mMacroProcessor;
151 
153 
155 
156 protected:
157 
158  friend class XMLMacroProcessor;
159 
160  void preprocessAll(XMLDom::iterator& iNode);
161 };
162 
163 
168 void preprocessXML(XMLDom& ioXml, XMLVariablesMap& ioVariables);
169 
171 
172 } // namespace
173 
174 #endif
A STL conform DOM reader/writer for XML.
boost::function< std::string(const std::string &)> Callback
Definition: XMLDomPreprocessor.h:112
bool parseCondition(XMLDom::iterator &node)
A STL conform wrapper for libxml2 to read XML files as DOM.
Definition: XMLDom.h:74
std::string annotation
Definition: XMLDomPreprocessor.h:87
bool parseExistsRequireAll(XMLDom::iterator &node)
void processSpecialVariables(XMLDom::iterator &node)
bool operator!=(const std::string &s) const
Definition: XMLDomPreprocessor.h:78
XMLDomPreprocessor(bool expandOnly=false)
std::string value
Definition: XMLDomPreprocessor.h:86
IncludePathStack mIncludePathStack
Definition: XMLDomPreprocessor.h:152
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
std::unique_ptr< XMLMacroProcessor > mMacroProcessor
Definition: XMLDomPreprocessor.h:150
bool parseConditionsRequireAll(XMLDom::iterator &node)
bool parseExistsRequireAny(XMLDom::iterator &node)
void preprocessAllLowLevel(XMLDom::iterator &iNode)
bool expandMacrosOnly() const
Definition: XMLDomPreprocessor.h:125
std::ostream & operator<<(std::ostream &s, const LibraryVersion &version)
XMLDom::iterator parseInclude(XMLDom::iterator &ioNode)
bool operator==(const char *s) const
Definition: XMLDomPreprocessor.h:79
std::list< Path > IncludePathStack
Definition: XMLDomPreprocessor.h:113
void preprocessXML(XMLDom &ioXml, XMLVariablesMap &ioVariables)
Shortcut for processing a XML document without creating a XMLDomPreprocessor yourself.
std::string resolveContent(std::string content)
std::map< std::string, XMLVariableValue > XMLVariablesMap
Definition: XMLDomPreprocessor.h:92
friend std::ostream & operator<<(std::ostream &s, const XMLVariableValue &x)
XMLVariablesMap variables
Definition: XMLDomPreprocessor.h:121
Iterator for iterating over xml nodes that have the same parent (sibligs)
Definition: XMLDom.h:850
bool operator==(const std::string &s) const
Definition: XMLDomPreprocessor.h:77
bool parseExists(XMLDom::iterator &node)
void reflect(Reflector &r)
Definition: XMLDomPreprocessor.h:70
void registerXMLVariableCallback(const std::string &pattern, Callback &&callback)
std::string resolveXMLVariables(const std::string &pattern, const std::string &var)
bool operator!=(const char *s) const
Definition: XMLDomPreprocessor.h:80
bool mExpandMacrosOnly
Definition: XMLDomPreprocessor.h:154
Preprocesses XML documents and resolves all special tags like , <if>, <warning> and so on...
Definition: XMLDomPreprocessor.h:109
Variables defined in xml documents.
Definition: XMLDomPreprocessor.h:64
Definition: XMLMacroProcessor.h:62
bool parseConditionsRequireAny(XMLDom::iterator &node)
XMLMacroProcessor & getMacroProcessor()
Definition: XMLDomPreprocessor.h:123
std::map< std::string, Callback > mCallbacks
Definition: XMLDomPreprocessor.h:146
void preprocessAll(XMLDom::iterator &iNode)
void preprocessXML(XMLDom &ioXml)
bool mAddedInfoToException
Definition: XMLDomPreprocessor.h:148
XMLVariableValue(const std::string &v="", const std::string &a="")
Definition: XMLDomPreprocessor.h:66
XMLVariableValue & operator=(const char *s)
Definition: XMLDomPreprocessor.h:75