appkit  1.5.1
RoadNarrows Robotics Application Kit
Xml.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Link: https://github.com/roadnarrows-robotics/rnr-sdk
6 //
7 // Library: librnr_appkit
8 //
9 // File: Xml.h
10 //
11 /*! \file
12  *
13  * $LastChangedDate: 2014-04-01 15:50:36 -0600 (Tue, 01 Apr 2014) $
14  * $Rev: 3627 $
15  *
16  * \brief XML base class.
17  *
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  * \author Daniel Packard (daniel@roadnarrows.com)
20  *
21  * \par Copyright
22  * \h_copy 2012-2017. RoadNarrows LLC.\n
23  * http://www.roadnarrows.com\n
24  * All Rights Reserved
25  */
26 /*
27  * @EulaBegin@
28  *
29  * Permission is hereby granted, without written agreement and without
30  * license or royalty fees, to use, copy, modify, and distribute this
31  * software and its documentation for any purpose, provided that
32  * (1) The above copyright notice and the following two paragraphs
33  * appear in all copies of the source code and (2) redistributions
34  * including binaries reproduces these notices in the supporting
35  * documentation. Substantial modifications to this software may be
36  * copyrighted by their authors and need not follow the licensing terms
37  * described here, provided that the new terms are clearly indicated in
38  * all files where they apply.
39  *
40  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
41  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
42  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
43  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
44  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
45  * THE POSSIBILITY OF SUCH DAMAGE.
46  *
47  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
48  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
49  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
50  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
51  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
52  *
53  * @EulaEnd@
54  */
55 ////////////////////////////////////////////////////////////////////////////////
56 
57 #ifndef _RNR_XML_H
58 #define _RNR_XML_H
59 
60 #include <string>
61 
62 #include "rnr/tinyxml/tinyxml.h"
63 
64 #include "rnr/rnrconfig.h"
65 
66 
67 /*!
68  * \brief RoadNarrows Robotics
69  */
70 namespace rnr
71 {
72  /*! XML declaration fixed string */
73  const char* const XmlDecl = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
74 
75  /*! XML schema instance fixed substring */
76  const char* const XmlNsXsi =
77  "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
78 
79  const char* const XslDecl =
80  "<xsl:stylesheet version=\"1.0\" "
81  "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">";
82 
83  //----------------------------------------------------------------------------
84  // Xml Base Class
85  //----------------------------------------------------------------------------
86 
87  /*!
88  * \brief XML base class.
89  */
90  class Xml
91  {
92  public:
93  /*!
94  * \brief Initialization constructor.
95  *
96  * \param strRootElem XML document top-level root element.
97  * \param strXsiUrl XML schema instance URL. Examples:\n
98  * "http://www.roadnarrows.com/xml/<em>pkg</em>/1.0/<em>name</em>.xsd"\n
99  * "file://<em>prefix</em>/share/<em>pkg</em>/<em>name</em>.xsd"\n
100  * "<em>name</em>.xsd"\n
101  * Default: No schema.
102  * \param strXslUrl XML extended stylesheet language transformation
103  * URL. Examples\n
104  * "http://www.roadnarrows.com/xml/<em>pkg</em>/1.0/<em>name</em>.xsl"\n
105  * "file://<em>prefix</em>/share/<em>pkg</em>/<em>name</em>.xsl"\n
106  * "<em>name</em>.xsl"\n
107  * Default: No stylesheet.
108  */
109  Xml(const std::string &strRootElem,
110  const std::string &strXsiUrl="",
111  const std::string &strXslUrl="");
112 
113  /*!
114  * \brief Destructor.
115  */
116  virtual ~Xml();
117 
118 
119  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
120  // I/O Methods
121  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
122 
123  /*!
124  * \brief Load XML file into DOM.
125  *
126  * \param strXmlFileName XML file path name.
127  *
128  * \copydoc doc_return_std
129  */
130  virtual int loadFile(const std::string &strXmlFileName);
131 
132  /*!
133  * \brief Save DOM to XML file.
134  *
135  * \param strXmlFileName XML file path name.
136  *
137  * \copydoc doc_return_std
138  */
139  virtual int saveFile(const std::string &strXmlFileName);
140 
141  /*!
142  * \brief Create XML file with the given major element name under the
143  * root element.
144  *
145  * Any current DOM is not accessed nor altered.
146  *
147  * \param strXmlFileName XML file path name.
148  * \param strMajorElemName XML major element name. A major element is an
149  * XML direct child of the root element.
150  *
151  * \copydoc doc_return_std
152  */
153  virtual int createTemplateFile(const std::string &strXmlFileName,
154  const std::string &strMajorElemName);
155 
156  /*!
157  * \brief Check that the XML file exists and has read/write access.
158  *
159  * \param strXmlFileName XML file path name.
160  * \param bRequired File is [not] required to exist.
161  *
162  * \return Returns true if access permitted, false otherwise.
163  */
164  bool fileExists(const std::string &strXmlFileName, bool bRequired=false);
165 
166 
167  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
168  // Class Attribute Methods
169  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
170 
171  /*!
172  * \brief Set XML document schema instance.
173  *
174  * \param strXsiUrl URL of schema.
175  */
176  void setSchemaInstance(const std::string &strXsiUrl)
177  {
178  m_strXsiUrl = strXsiUrl;
179  }
180 
181  /*!
182  * \brief Set XML Extended Style Sheet.
183  *
184  * \param strXslUrl URL of stylesheet.
185  */
186  void setStylesheet(const std::string &strXslUrl)
187  {
188  m_strXslUrl = strXslUrl;
189  }
190 
191  /*!
192  * \brief Get last load/save/create XML file name.
193  *
194  * \return File path name.
195  */
196  std::string getFileName()
197  {
198  return m_strXmlFileName;
199  }
200 
201  /*!
202  * \brief Check if DOM has been modified since last save.
203  *
204  * \return Returns true or false.
205  */
206  bool isModified()
207  {
208  return m_bModified;
209  }
210 
211  /*!
212  * \brief Set DOM modified state.
213  *
214  * \param bModified Modified state: true or false.
215  */
216  void setModifiedState(bool bModified)
217  {
218  m_bModified = bModified;
219  }
220 
221  /*!
222  * \brief Get last error message.
223  *
224  * \return String.
225  */
226  std::string getErrorMsg()
227  {
228  return m_bufErrMsg;
229  }
230 
231 
232  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
233  // DOM Methods
234  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
235 
236  /*!
237  * \brief Get the major element in the DOM.
238  *
239  * \param strMajorElemName XML major element name. A major element is an
240  * XML direct child of the root element.
241  *
242  * \return On success, returns pointer to elemen.\n
243  * On failure, NULL is returned.
244  */
245  TiXmlElement *getMajorElem(const std::string &strMajorElemName);
246 
247  /*!
248  * \brief Get element's text.
249  *
250  * Leading and trailing white space is stripped.
251  *
252  * \param pElem Pointer to DOM element object.
253  *
254  * <elem>
255  * text...
256  * </elem>
257  *
258  * \return Text string.
259  */
260  std::string elemText(TiXmlElement *pElem);
261 
262  /*!
263  * \brief Get element's attribute value.
264  *
265  * \param pElem Pointer to DOM element object.
266  *
267  * \return Attribute value string.
268  */
269  std::string elemAttr(TiXmlElement *pElem, const std::string &strAttrName);
270 
271 
272  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
273  // String Methods
274  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
275 
276  /*!
277  * \brief Convert string to integer.
278  *
279  * \param [in] str String in hex, decimal, or octal format.
280  * \param [out] val Converted integer value.
281  *
282  * \copydoc doc_return_std
283  */
284  int strToInt(const std::string &str, int &val);
285 
286  /*!
287  * \brief Convert integer to string.
288  *
289  * \param [in] val Integer value.
290  * \param [out] str Converted string in hex, decimal, or octal format.
291  * \param base One of: 16 10 8.
292  *
293  * \copydoc doc_return_std
294  */
295  int intToStr(const int val, std::string &str, int base=10);
296 
297  /*!
298  * \brief Convert string to double-precision floating-point number.
299  *
300  * \param [in] str String in hex, decimal, or octal format.
301  * \param [out] val Converted double value.
302  *
303  * \copydoc doc_return_std
304  */
305  int strToDouble(const std::string &str, double &val);
306 
307  /*!
308  * \brief Convert double to string.
309  *
310  * \param [in] val Double value.
311  * \param [out] str Converted string in hex, decimal, or octal format.
312  *
313  * \copydoc doc_return_std
314  */
315  int doubleToStr(const double val, std::string &str);
316 
317  protected:
318  std::string m_strRootElemName; ///< xml top-level root element name
319  std::string m_strXsiUrl; ///< xml schema instance
320  std::string m_strXslUrl; ///< xml style sheet
321 
322  std::string m_strXmlFileName; ///< xml file path name
323 
324  std::string m_strXmlHead; ///< xml document head
325  std::string m_strXmlTail; ///< xml document tail
326 
327  TiXmlDocument m_xmlDoc; ///< parsed xml DOM
328  TiXmlElement *m_pElemRoot; ///< top-level root element
329  bool m_bModified; ///< xml [not] modified
330  char m_bufErrMsg[256]; ///< error message buffer
331 
332  /*!
333  * \brief Make XML document head string.
334  *
335  * The XML head string contains XML declarations plus the root element
336  * opening statement.
337  */
338  virtual void makeXmlHead();
339 
340  /*!
341  * \brief Make XML document tail string.
342  *
343  * The XML tail string contains the root element closing statement.
344  */
345  virtual void makeXmlTail();
346 
347  /*!
348  * \brief Set XML error message.
349  *
350  * \param sFmt Format string.
351  * \param ... Format variable arguments.
352  */
353  void setErrorMsg(const char *sFmt, ...);
354  };
355 
356 } // rnr namespace
357 
358 #endif // _RNR_XML_H
const char *const XmlNsXsi
Definition: Xml.h:76
bool isModified()
Check if DOM has been modified since last save.
Definition: Xml.h:206
std::string getFileName()
Get last load/save/create XML file name.
Definition: Xml.h:196
virtual int loadFile(const std::string &strXmlFileName)
Load XML file into DOM.
Definition: Xml.cxx:95
XML base class.
Definition: Xml.h:90
virtual void makeXmlHead()
Make XML document head string.
Definition: Xml.cxx:366
std::string getErrorMsg()
Get last error message.
Definition: Xml.h:226
int intToStr(const int val, std::string &str, int base=10)
Convert integer to string.
Definition: Xml.cxx:313
std::string m_strXmlTail
xml document tail
Definition: Xml.h:325
std::string m_strXsiUrl
xml schema instance
Definition: Xml.h:319
virtual int saveFile(const std::string &strXmlFileName)
Save DOM to XML file.
Definition: Xml.cxx:143
char m_bufErrMsg[256]
error message buffer
Definition: Xml.h:330
void setStylesheet(const std::string &strXslUrl)
Set XML Extended Style Sheet.
Definition: Xml.h:186
TiXmlDocument m_xmlDoc
parsed xml DOM
Definition: Xml.h:327
std::string m_strXmlFileName
xml file path name
Definition: Xml.h:322
std::string elemText(TiXmlElement *pElem)
Get element&#39;s text.
Definition: Xml.cxx:270
virtual int createTemplateFile(const std::string &strXmlFileName, const std::string &strMajorElemName)
Create XML file with the given major element name under the root element.
Definition: Xml.cxx:171
std::string m_strRootElemName
xml top-level root element name
Definition: Xml.h:318
std::string elemAttr(TiXmlElement *pElem, const std::string &strAttrName)
Get element&#39;s attribute value.
Definition: Xml.cxx:283
int strToDouble(const std::string &str, double &val)
Convert string to double-precision floating-point number.
Definition: Xml.cxx:340
void setSchemaInstance(const std::string &strXsiUrl)
Set XML document schema instance.
Definition: Xml.h:176
std::string m_strXslUrl
xml style sheet
Definition: Xml.h:320
void setErrorMsg(const char *sFmt,...)
Set XML error message.
Definition: Xml.cxx:411
Xml(const std::string &strRootElem, const std::string &strXsiUrl="", const std::string &strXslUrl="")
Initialization constructor.
Definition: Xml.cxx:79
virtual ~Xml()
Destructor.
Definition: Xml.cxx:91
bool fileExists(const std::string &strXmlFileName, bool bRequired=false)
Check that the XML file exists and has read/write access.
Definition: Xml.cxx:210
virtual void makeXmlTail()
Make XML document tail string.
Definition: Xml.cxx:406
bool m_bModified
xml [not] modified
Definition: Xml.h:329
void setModifiedState(bool bModified)
Set DOM modified state.
Definition: Xml.h:216
std::string m_strXmlHead
xml document head
Definition: Xml.h:324
TiXmlElement * m_pElemRoot
top-level root element
Definition: Xml.h:328
int doubleToStr(const double val, std::string &str)
Convert double to string.
Definition: Xml.cxx:353
TiXmlElement * getMajorElem(const std::string &strMajorElemName)
Get the major element in the DOM.
Definition: Xml.cxx:235
RoadNarrows Robotics.
Definition: Camera.h:74
int strToInt(const std::string &str, int &val)
Convert string to integer.
Definition: Xml.cxx:298
const char *const XmlDecl
Definition: Xml.h:73