Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
XMLIO.hh
1 #ifndef __BIAS_XMLIO_hh__
2 #define __BIAS_XMLIO_hh__
3 
4 /*
5 This file is part of the BIAS library (Basic ImageAlgorithmS).
6 
7 Copyright (C) 2003-2009 (see file CONTACT for details)
8  Multimediale Systeme der Informationsverarbeitung
9  Institut fuer Informatik
10  Christian-Albrechts-Universitaet Kiel
11 
12 
13 BIAS is free software; you can redistribute it and/or modify
14 it under the terms of the GNU Lesser General Public License as published by
15 the Free Software Foundation; either version 2.1 of the License, or
16 (at your option) any later version.
17 
18 BIAS is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU Lesser General Public License for more details.
22 
23 You should have received a copy of the GNU Lesser General Public License
24 along with BIAS; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27 
28 #include <bias_config.h>
29 
30 // for dependant code:
31 #ifndef BIAS_HAVE_XML2
32 # error BIAS_HAVE_XML2 required to use XMLIO.hh. Please recompile BIAS with USE_XML2 or dont include this file.
33 #endif
34 
35 #ifdef BIAS_HAVE_XML2
36 
37 //forward declarations for avoiding xml-headers
38 typedef struct _xmlAttr xmlAttr;
39 typedef xmlAttr *xmlAttrPtr;
40 typedef struct _xmlNode xmlNode;
41 typedef xmlNode* xmlNodePtr;
42 typedef struct _xmlDoc xmlDoc;
43 typedef xmlDoc* xmlDocPtr;
44 
45 #include <vector>
46 #include <Base/Debug/Error.hh>
47 #include <Base/Debug/Debug.hh>
48 
49 namespace BIAS {
50 
51  /** @class XMLIO
52  @ingroup g_utils
53  @brief Wrapper class for reading and writing XML files based on the
54  XML library libxml2
55 
56  @author streckel 05/2004
57 
58  Implements a tree-oriented reader and writer for XML files
59 
60  the naming convention for this class is:
61  <rootNode>
62  <childNodeName AttributeName="AttributeValue">
63  Content of childNodeName
64  </childNodeName>
65  </rootNode>
66 
67  The class first creates a tree in memory, this tree is written to
68  a file in one flush using write().
69  A new tree is created with create().
70  A tree is read from file and parsed with read().
71  */
72  class BIASCommon_EXPORT XMLIO : public Debug {
73 
74  public:
75  XMLIO();
76  ~XMLIO();
77 
78  void clear();
79 
80  // ****************************************************************
81  // * Static conversion methods between character encoding formats
82  // ****************************************************************
83 
84  /** @brief Convert character string from UTF-8 format to ISO 8895-1.
85  @returns 0 in case of success, -1 if conversion failed
86  @author esquivel
87  @date 08/2008 */
88  static int IsoLatin1ToUtf8(const std::string& isoLatin1, std::string& utf8);
89 
90  /** @brief Convert character string from ISO 8895-1 format to UTF-8.
91  @returns 0 in case of success, -1 if conversion failed
92  @author esquivel
93  @date 08/2008 */
94  static int Utf8ToIsoLatin1(const std::string& utf8, std::string& isoLatin1);
95 
96  // ****************************************************************
97  // * Write methods
98  // ****************************************************************
99 
100  /** @brief Create the base of a new XML-Tree in memory, already with a
101  one and only root node.
102  @author streckel 05/2004 */
103  xmlNodePtr create(const std::string& RootNodeName);
104 
105  /** @brief Add a child node to an incoming node with the given name.
106  @author streckel 05/2004 */
107  xmlNodePtr addChildNode(const xmlNodePtr ParentNode,
108  const std::string& NewNodeName);
109 
110  /** @brief Add an attribute to a node.
111  @author apetersen 07/2005 */
112  void addAttribute(const xmlNodePtr Node,
113  const std::string& AttributeName,
114  bool AttributeValue);
115  /** @brief Add an attribute to a node.
116  @author streckel 05/2004 */
117  void addAttribute(const xmlNodePtr Node,
118  const std::string& AttributeName,
119  const std::string& AttributeValue);
120  /** @brief Add an attribute to a node.
121  */
122  void addAttribute(const xmlNodePtr Node,
123  const std::string& AttributeName,
124  const char* AttributeValue);
125 
126  /** @brief Add an attribute to a node.
127  @author streckel 05/2004 */
128  void addAttribute(const xmlNodePtr Node,
129  const std::string& AttributeName,
130  int AttributeValue);
131  /** @brief Add an attribute to a node.
132  @author streckel 05/2004 */
133  void addAttribute(const xmlNodePtr Node,
134  const std::string& AttributeName,
135  double AttributeValue);
136  /** @brief Add an attribute to a node.
137  @author apetersen 07/2005 */
138  void addAttribute(const xmlNodePtr Node,
139  const std::string& AttributeName,
140  const std::vector<double> &AttributeValue);
141  /** @brief Add an attribute to a node.
142  @author apetersen 07/2005 */
143  void addAttribute(const xmlNodePtr Node,
144  const std::string& AttributeName,
145  const std::vector<int> &AttributeValue);
146  /** @brief Add content to a node.
147  @author streckel 05/2004 */
148  void addContent(const xmlNodePtr Node, const std::string& Content);
149  void addContent(const xmlNodePtr Node, const int Content);
150  void addContent(const xmlNodePtr Node, const double Content);
151  void addContent(const xmlNodePtr Node, const std::vector<int> Content);
152 
153  /** @brief Add comment to a node.
154  @author streckel 05/2004 */
155  void addComment(const xmlNodePtr Node, const std::string& Comment);
156 
157  /** @brief Set content of a node.
158  Method will replace existent content.
159  @author bartczak 08/2004 */
160  void setContent(const xmlNodePtr Node, const std::string& Content);
161  void setContent(const xmlNodePtr Node, const int Content);
162  void setContent(const xmlNodePtr Node, const double Content);
163  void setContent(const xmlNodePtr Node, const std::vector<int> Content);
164 
165  /** @brief Write the whole tree that was constructed in memory to disk.
166  @param AutoAddCompressionSuffix relevant only if compression is active
167  true means .z is attached, false means filename is used as is
168  @author streckel 05/2004 */
169  int write(const std::string& Filename,
170  bool AutoAddCompressionSuffix = true) const;
171 
172  // ****************************************************************
173  // * Read & analysis methods
174  // ****************************************************************
175 
176  /** @brief Read and parse an XML file from disk, DtD validation is not
177  yet implemented. Returns pointer to the RootNode or NULL if parse
178  errors occurred.
179  @author streckel 05/2004 */
180  xmlNodePtr read(const std::string& Filename);
181 
182  /** @brief Get the first child of a given parent, or NULL for no childs.
183  @author streckel 05/2004 */
184  xmlNodePtr getFirstChild(const xmlNodePtr ParentNode);
185 
186  /** @brief Get the next child of the parent specified in the last
187  getFirstChild() call, the class remembers the last returned child.
188  NULL is returned the last child was reached.
189  @author streckel 05/2004 */
190  xmlNodePtr getNextChild();
191 
192  /** @brief Get the next sibling of the given node. Do not give the
193  parent node but the previous sibling node.
194  @author streckel 05/2004 */
195  xmlNodePtr getNextChild(const xmlNodePtr SiblingNode);
196 
197  /** @brief Get a child of a Parent node by specifying the childs name,
198  NULL is returned if the ParentNode has no child of the given name.
199  @author streckel 05/2004 */
200  xmlNodePtr getChild(const xmlNodePtr ParentNode,
201  const std::string& ChildName);
202 
203  /** Convenience wrapper: returns a vector of pointors to every (direct) child with the specified name.
204  & \author bartczak 03/2009
205  **/
206  void GetChildren(const xmlNodePtr ParentNode,
207  const std::string& ChildName,
208  std::vector<xmlNodePtr>& childrenWithSameName);
209 
210  /** Finds the 'first' child with name ChildName and an attribute named AttributeName that
211  * has the string value AttributeValue, return NULL if unsuccessful.
212  * \author bartczak 03/2009
213  */
214  xmlNodePtr GetChildWithAttributeValueString(const xmlNodePtr ParentNode,
215  const std::string& ChildName,
216  const std::string& AttributeName,
217  const std::string& AttributeValue);
218 
219  /** @brief Get the name of a given Node.
220  @author streckel 05/2004 */
221  std::string getNodeName(const xmlNodePtr Node) const;
222 
223  /** @brief accesor functions for recursive usage
224  @author woelk 02/2006 */
225  void GetChildren(const xmlNodePtr ParentNode,
226  std::vector<xmlNodePtr>& children);
227 
228  /** @brief Get the content of a given Node.
229  @author streckel 05/2004 */
230  std::string getNodeContentString(const xmlNodePtr Node) const;
231  int getNodeContentInt(const xmlNodePtr Node) const;
232  double getNodeContentDouble(const xmlNodePtr Node) const;
233  std::vector<int> getNodeContentVectorInt(const xmlNodePtr Node) const;
234  std::vector<double> getNodeContentVectorDouble(const xmlNodePtr Node) const;
235 
236  /** @brief Get the first attribute of a given parent, or NULL for no
237  attributes.
238  @author streckel 05/2004 */
239  xmlAttrPtr getFirstAttribute(const xmlNodePtr Node);
240 
241  /** @brief Get the next attribute of the parent specified in the last
242  getFirstAttribute() call, the class remembers the last returned
243  attribute. NULL iis returned if the last attribute was reached.
244  @author streckel 05/2004 */
245  xmlAttrPtr getNextAttribute();
246 
247  /** @brief accesor functions for recursive usage
248  @author woelk 02/2006 */
249  void GetAttributes(const xmlNodePtr Node,
250  std::vector<xmlAttrPtr>& attributes);
251 
252  /** @brief Get the name of a given Attribute.
253  @author streckel 05/2004 */
254  std::string getAttributeName(const xmlAttrPtr Attribute) const;
255 
256  /** @brief search for a specific attribute
257  @return NULL if not found
258  @author woelk 02/2006 */
259  xmlAttrPtr getAttributeByName(const xmlNodePtr Node,
260  const std::string& attribute_name);
261 
262  /** @brief Get the value of a given Attribute, with type-cast overloads
263  for different attribute types. Use getAttributeValueString() for
264  unknown attribute types.
265  @author streckel 05/2004 & apetersen 07/2005 */
266  bool getAttributeValueBool(const xmlAttrPtr Attribute) const;
267  std::string getAttributeValueString(const xmlAttrPtr Attribute) const;
268  int getAttributeValueInt(const xmlAttrPtr Attribute) const;
269  double getAttributeValueDouble(const xmlAttrPtr Attribute) const;
270  std::vector<double> getAttributeValueVecDbl(const xmlAttrPtr Attribute) const;
271  std::vector<int> getAttributeValueVecInt(const xmlAttrPtr Attribute) const;
272 
273  /** @brief Get the value of an Attribute stecified by the attributes
274  name, with type-cast overloads for different attribute types.
275  Use getAttributeValueString() for unknown attribute types.
276  @author streckel 05/2004 & apetersen 07/2005 */
277  bool getAttributeValueBool(const xmlNodePtr Node,
278  const std::string& AttributeName) const;
279  std::string getAttributeValueString(const xmlNodePtr Node,
280  const std::string& AttributeName) const;
281  int getAttributeValueInt(const xmlNodePtr Node,
282  const std::string& AttributeName) const;
283  double getAttributeValueDouble(const xmlNodePtr Node,
284  const std::string& AttributeName) const;
285  std::vector<double> getAttributeValueVecDbl(const xmlNodePtr Node,
286  const std::string& AttributeName) const;
287  std::vector<int> getAttributeValueVecInt(const xmlNodePtr Node,
288  const std::string& AttributeName) const;
289 
290  /** @brief set level of compression, needs zlib
291  @param level 0=compression off, ... , 9 means highest
292  @return previous compression level
293  @author koeser 01/2005 */
294  int SetCompression(int level=9);
295 
296  /** serialize tree to string
297  @author evers
298  */
299  int WriteToString(std::string &str);
300 
301  /** construct tree from string
302  @author evers
303  */
304  xmlNodePtr ReadFromString(const std::string &str);
305 
306  xmlNodePtr getRootNode() { return RootNode_;}
307 
308  void SetEncoding(const std::string &encoding) {
309  Encoding_ = encoding;}
310 
311  std::string GetEncoding(){return Encoding_;}
312 
313  protected:
314 
315  xmlDocPtr xmlDoc_;
316 
317  xmlNodePtr RootNode_;
318  xmlNodePtr CurrNode_;
319 
320  xmlAttrPtr CurrAttr_;
321 
323  std::string Encoding_;
324 
325  };
326 
327 
328 } // namespace
329 
330 #endif // BIAS_HAVE_XML2
331 
332 #endif // __BIAS_XMLIO_hh__
bool isInitialized_
Definition: XMLIO.hh:322
xmlNodePtr RootNode_
Definition: XMLIO.hh:317
std::string Encoding_
Definition: XMLIO.hh:323
Wrapper class for reading and writing XML files based on the XML library libxml2. ...
Definition: XMLIO.hh:72
void SetEncoding(const std::string &encoding)
Definition: XMLIO.hh:308
xmlDocPtr xmlDoc_
Definition: XMLIO.hh:315
std::string IsoLatin1ToUtf8(const std::string &s)
Definition: StringConv.hh:93
xmlAttrPtr CurrAttr_
Definition: XMLIO.hh:320
xmlNodePtr CurrNode_
Definition: XMLIO.hh:318
xmlNodePtr getRootNode()
Definition: XMLIO.hh:306
std::string GetEncoding()
Definition: XMLIO.hh:311