appkit  1.5.1
RoadNarrows Robotics Application Kit
rnr::Xml Class Reference

XML base class. More...

#include <Xml.h>

Public Member Functions

 Xml (const std::string &strRootElem, const std::string &strXsiUrl="", const std::string &strXslUrl="")
 Initialization constructor. More...
 
virtual ~Xml ()
 Destructor.
 
virtual int loadFile (const std::string &strXmlFileName)
 Load XML file into DOM. More...
 
virtual int saveFile (const std::string &strXmlFileName)
 Save DOM to XML file. More...
 
virtual int createTemplateFile (const std::string &strXmlFileName, const std::string &strMajorElemName)
 Create XML file with the given major element name under the root element. More...
 
bool fileExists (const std::string &strXmlFileName, bool bRequired=false)
 Check that the XML file exists and has read/write access. More...
 
void setSchemaInstance (const std::string &strXsiUrl)
 Set XML document schema instance. More...
 
void setStylesheet (const std::string &strXslUrl)
 Set XML Extended Style Sheet. More...
 
std::string getFileName ()
 Get last load/save/create XML file name. More...
 
bool isModified ()
 Check if DOM has been modified since last save. More...
 
void setModifiedState (bool bModified)
 Set DOM modified state. More...
 
std::string getErrorMsg ()
 Get last error message. More...
 
TiXmlElement * getMajorElem (const std::string &strMajorElemName)
 Get the major element in the DOM. More...
 
std::string elemText (TiXmlElement *pElem)
 Get element's text. More...
 
std::string elemAttr (TiXmlElement *pElem, const std::string &strAttrName)
 Get element's attribute value. More...
 
int strToInt (const std::string &str, int &val)
 Convert string to integer. More...
 
int intToStr (const int val, std::string &str, int base=10)
 Convert integer to string. More...
 
int strToDouble (const std::string &str, double &val)
 Convert string to double-precision floating-point number. More...
 
int doubleToStr (const double val, std::string &str)
 Convert double to string. More...
 

Protected Member Functions

virtual void makeXmlHead ()
 Make XML document head string. More...
 
virtual void makeXmlTail ()
 Make XML document tail string. More...
 
void setErrorMsg (const char *sFmt,...)
 Set XML error message. More...
 

Protected Attributes

std::string m_strRootElemName
 xml top-level root element name
 
std::string m_strXsiUrl
 xml schema instance
 
std::string m_strXslUrl
 xml style sheet
 
std::string m_strXmlFileName
 xml file path name
 
std::string m_strXmlHead
 xml document head
 
std::string m_strXmlTail
 xml document tail
 
TiXmlDocument m_xmlDoc
 parsed xml DOM
 
TiXmlElement * m_pElemRoot
 top-level root element
 
bool m_bModified
 xml [not] modified
 
char m_bufErrMsg [256]
 error message buffer
 

Detailed Description

XML base class.

Definition at line 90 of file Xml.h.

Constructor & Destructor Documentation

Xml::Xml ( const std::string &  strRootElem,
const std::string &  strXsiUrl = "",
const std::string &  strXslUrl = "" 
)

Initialization constructor.

Parameters
strRootElemXML document top-level root element.
strXsiUrlXML schema instance URL. Examples:
"http://www.roadnarrows.com/xml/<em>pkg</em>/1.0/<em>name</em>.xsd"
"file://<em>prefix</em>/share/<em>pkg</em>/<em>name</em>.xsd"
"<em>name</em>.xsd"
Default: No schema.
strXslUrlXML extended stylesheet language transformation URL. Examples
"http://www.roadnarrows.com/xml/<em>pkg</em>/1.0/<em>name</em>.xsl"
"file://<em>prefix</em>/share/<em>pkg</em>/<em>name</em>.xsl"
"<em>name</em>.xsl"
Default: No stylesheet.

Definition at line 79 of file Xml.cxx.

References m_bModified, m_bufErrMsg, and m_pElemRoot.

81  :
82  m_strRootElemName(strRootElem),
83  m_strXsiUrl(strXsiUrl),
84  m_strXslUrl(strXslUrl)
85 {
86  m_pElemRoot = NULL;
87  m_bModified = false;
88  m_bufErrMsg[0] = 0;
89 }
std::string m_strXsiUrl
xml schema instance
Definition: Xml.h:319
char m_bufErrMsg[256]
error message buffer
Definition: Xml.h:330
std::string m_strRootElemName
xml top-level root element name
Definition: Xml.h:318
std::string m_strXslUrl
xml style sheet
Definition: Xml.h:320
bool m_bModified
xml [not] modified
Definition: Xml.h:329
TiXmlElement * m_pElemRoot
top-level root element
Definition: Xml.h:328

Member Function Documentation

int Xml::createTemplateFile ( const std::string &  strXmlFileName,
const std::string &  strMajorElemName 
)
virtual

Create XML file with the given major element name under the root element.

Any current DOM is not accessed nor altered.

Parameters
strXmlFileNameXML file path name.
strMajorElemNameXML major element name. A major element is an XML direct child of the root element.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 171 of file Xml.cxx.

References m_bufErrMsg, m_strXmlFileName, m_strXmlHead, m_strXmlTail, makeXmlHead(), makeXmlTail(), and setErrorMsg().

173 {
174  FILE *fp;
175 
176  if( strXmlFileName.empty() )
177  {
178  setErrorMsg("No file name.");
179  LOGERROR("%s", m_bufErrMsg);
180  return RC_ERROR;
181  }
182 
183  m_strXmlFileName = strXmlFileName;
184 
185  // open file
186  if( (fp = fopen(m_strXmlFileName.c_str(), "w+")) == NULL )
187  {
188  setErrorMsg("%s: %s(errno=%d).",
189  m_strXmlFileName.c_str(), strerror(errno), errno);
190  LOGERROR("%s", m_bufErrMsg);
191  return RC_ERROR;
192  }
193 
194  makeXmlHead();
195  makeXmlTail();
196 
197  // create template of document
198  fprintf(fp, "%s", m_strXmlHead.c_str());
199  fprintf(fp, "<%s>\n</%s>\n",
200  strMajorElemName.c_str(), strMajorElemName.c_str());
201  fprintf(fp, "%s", m_strXmlTail.c_str());
202 
203  fclose(fp);
204 
205  LOGDIAG3("Created file %s.", m_strXmlFileName.c_str());
206 
207  return OK;
208 }
virtual void makeXmlHead()
Make XML document head string.
Definition: Xml.cxx:366
std::string m_strXmlTail
xml document tail
Definition: Xml.h:325
char m_bufErrMsg[256]
error message buffer
Definition: Xml.h:330
std::string m_strXmlFileName
xml file path name
Definition: Xml.h:322
void setErrorMsg(const char *sFmt,...)
Set XML error message.
Definition: Xml.cxx:411
virtual void makeXmlTail()
Make XML document tail string.
Definition: Xml.cxx:406
std::string m_strXmlHead
xml document head
Definition: Xml.h:324
int Xml::doubleToStr ( const double  val,
std::string &  str 
)

Convert double to string.

Parameters
[in]valDouble value.
[out]strConverted string in hex, decimal, or octal format.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 353 of file Xml.cxx.

Referenced by getErrorMsg().

354 {
355  char buf[64];
356 
357  snprintf(buf, sizeof(buf), "%lf", val);
358 
359  buf[sizeof(buf)-1] = 0;
360 
361  str = buf;
362 
363  return OK;
364 }
string Xml::elemAttr ( TiXmlElement *  pElem,
const std::string &  strAttrName 
)

Get element's attribute value.

Parameters
pElemPointer to DOM element object.
Returns
Attribute value string.

Definition at line 283 of file Xml.cxx.

Referenced by getErrorMsg().

284 {
285  std::string str;
286  const char *s;
287 
288  if( (pElem != NULL) &&
289  !strAttrName.empty() &&
290  ((s = pElem->Attribute(strAttrName.c_str())) != NULL) )
291  {
292  str = s;
293  }
294 
295  return str;
296 }
string Xml::elemText ( TiXmlElement *  pElem)

Get element's text.

Leading and trailing white space is stripped.

Parameters
pElemPointer to DOM element object.

<elem> text... </elem>

Returns
Text string.

Definition at line 270 of file Xml.cxx.

Referenced by getErrorMsg().

271 {
272  std::string str;
273  const char *s;
274 
275  if( (pElem != NULL) && ((s = pElem->GetText()) != NULL) )
276  {
277  str = s;
278  }
279 
280  return str;
281 }
bool Xml::fileExists ( const std::string &  strXmlFileName,
bool  bRequired = false 
)

Check that the XML file exists and has read/write access.

Parameters
strXmlFileNameXML file path name.
bRequiredFile is [not] required to exist.
Returns
Returns true if access permitted, false otherwise.

Definition at line 210 of file Xml.cxx.

References m_bufErrMsg, and setErrorMsg().

211 {
212  bool bAccess;
213 
214  if( strXmlFileName.empty() )
215  {
216  setErrorMsg("No file name.");
217  LOGERROR("%s", m_bufErrMsg);
218  bAccess = false;
219  }
220  else
221  {
222  bAccess = access(strXmlFileName.c_str(), F_OK|R_OK|W_OK) == 0;
223  }
224 
225  if( !bAccess && bRequired )
226  {
227  setErrorMsg("%s: %s(errno=%d).",
228  strXmlFileName.c_str(), strerror(errno), errno);
229  LOGERROR("%s", m_bufErrMsg);
230  }
231 
232  return bAccess;
233 }
char m_bufErrMsg[256]
error message buffer
Definition: Xml.h:330
void setErrorMsg(const char *sFmt,...)
Set XML error message.
Definition: Xml.cxx:411
std::string rnr::Xml::getErrorMsg ( )
inline

Get last error message.

Returns
String.

Definition at line 226 of file Xml.h.

References doubleToStr(), elemAttr(), elemText(), getMajorElem(), intToStr(), m_bufErrMsg, strToDouble(), and strToInt().

227  {
228  return m_bufErrMsg;
229  }
char m_bufErrMsg[256]
error message buffer
Definition: Xml.h:330
std::string rnr::Xml::getFileName ( )
inline

Get last load/save/create XML file name.

Returns
File path name.

Definition at line 196 of file Xml.h.

References m_strXmlFileName.

197  {
198  return m_strXmlFileName;
199  }
std::string m_strXmlFileName
xml file path name
Definition: Xml.h:322
TiXmlElement * Xml::getMajorElem ( const std::string &  strMajorElemName)

Get the major element in the DOM.

Parameters
strMajorElemNameXML major element name. A major element is an XML direct child of the root element.
Returns
On success, returns pointer to elemen.
On failure, NULL is returned.

Definition at line 235 of file Xml.cxx.

References m_pElemRoot.

Referenced by getErrorMsg().

236 {
237  TiXmlElement *pElem;
238  const char *sValue;
239 
240  if( strMajorElemName.empty() )
241  {
242  return NULL;
243  }
244 
245  if( m_pElemRoot == NULL )
246  {
247  return NULL;
248  }
249 
250  // major secion elements
251  for(pElem = m_pElemRoot->FirstChildElement();
252  pElem != NULL;
253  pElem = pElem->NextSiblingElement())
254  {
255  sValue = pElem->Value();
256 
257  if( sValue == NULL )
258  {
259  continue;
260  }
261  else if( !strcasecmp(sValue, strMajorElemName.c_str()) )
262  {
263  return pElem;
264  }
265  }
266 
267  return NULL;
268 }
TiXmlElement * m_pElemRoot
top-level root element
Definition: Xml.h:328
int Xml::intToStr ( const int  val,
std::string &  str,
int  base = 10 
)

Convert integer to string.

Parameters
[in]valInteger value.
[out]strConverted string in hex, decimal, or octal format.
baseOne of: 16 10 8.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 313 of file Xml.cxx.

Referenced by getErrorMsg().

314 {
315  char buf[64];
316 
317  switch( base )
318  {
319  case 16:
320  snprintf(buf, sizeof(buf), "0x%x", val);
321  break;
322  case 10:
323  snprintf(buf, sizeof(buf), "%d", val);
324  break;
325  case 8:
326  snprintf(buf, sizeof(buf), "0%o", val);
327  break;
328  default:
329  LOGERROR("%d: Unsupported base conversion.", base);
330  return RC_ERROR;
331  }
332 
333  buf[sizeof(buf)-1] = 0;
334 
335  str = buf;
336 
337  return OK;
338 }
bool rnr::Xml::isModified ( )
inline

Check if DOM has been modified since last save.

Returns
Returns true or false.

Definition at line 206 of file Xml.h.

References m_bModified.

207  {
208  return m_bModified;
209  }
bool m_bModified
xml [not] modified
Definition: Xml.h:329
int Xml::loadFile ( const std::string &  strXmlFileName)
virtual

Load XML file into DOM.

Parameters
strXmlFileNameXML file path name.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 95 of file Xml.cxx.

References m_bufErrMsg, m_pElemRoot, m_strRootElemName, m_strXmlFileName, m_xmlDoc, setErrorMsg(), and setModifiedState().

96 {
97  if( strXmlFileName.empty() )
98  {
99  setErrorMsg("No file name.");
100  LOGERROR("%s", m_bufErrMsg);
101  return RC_ERROR;
102  }
103 
104  m_strXmlFileName = strXmlFileName;
105 
106  // load and parse xml file
107  if( !m_xmlDoc.LoadFile(m_strXmlFileName.c_str()) )
108  {
109  setErrorMsg("%s[r%d,c%d]: %s.",
110  m_strXmlFileName.c_str(), m_xmlDoc.ErrorRow(),
111  m_xmlDoc.ErrorCol(), m_xmlDoc.ErrorDesc());
112  LOGERROR("%s", m_bufErrMsg);
113  return RC_ERROR;
114  }
115 
116  // <rootelem> ... </rootelem>
117  m_pElemRoot = m_xmlDoc.RootElement();
118 
119  if( m_pElemRoot == NULL )
120  {
121  setErrorMsg("Not a valid %s XML document: No root <%s> element found.",
122  m_strXmlFileName.c_str(), m_strRootElemName.c_str());
123  LOGERROR("%s", m_bufErrMsg);
124  return RC_ERROR;
125  }
126 
127  else if( strcasecmp(m_pElemRoot->Value(), m_strRootElemName.c_str()) )
128  {
129  setErrorMsg("Not a valid %s XML document: Root <%s> element not <%s>.",
130  m_strXmlFileName.c_str(), m_pElemRoot->Value(),
131  m_strRootElemName.c_str());
132  LOGERROR("%s", m_bufErrMsg);
133  return RC_ERROR;
134  }
135 
136  setModifiedState(false);
137 
138  LOGDIAG3("XMl document %s loaded.", m_strXmlFileName.c_str());
139 
140  return OK;
141 }
char m_bufErrMsg[256]
error message buffer
Definition: Xml.h:330
TiXmlDocument m_xmlDoc
parsed xml DOM
Definition: Xml.h:327
std::string m_strXmlFileName
xml file path name
Definition: Xml.h:322
std::string m_strRootElemName
xml top-level root element name
Definition: Xml.h:318
void setErrorMsg(const char *sFmt,...)
Set XML error message.
Definition: Xml.cxx:411
void setModifiedState(bool bModified)
Set DOM modified state.
Definition: Xml.h:216
TiXmlElement * m_pElemRoot
top-level root element
Definition: Xml.h:328
void Xml::makeXmlHead ( )
protectedvirtual

Make XML document head string.

The XML head string contains XML declarations plus the root element opening statement.

Definition at line 366 of file Xml.cxx.

References m_strRootElemName, m_strXmlHead, m_strXsiUrl, m_strXslUrl, rnr::XmlDecl, and rnr::XmlNsXsi.

Referenced by createTemplateFile().

367 {
368  stringstream ssStylesheetStmt;
369  stringstream ssRootStmt;
370  stringstream ssHead;
371 
372  // no stylesheet
373  if( m_strXslUrl.empty() )
374  {
375  ssStylesheetStmt << "";
376  }
377 
378  // xslt stylesheet
379  else
380  {
381  ssStylesheetStmt << "<?xml-stylestheet type=\"text/xsl\" href=\""
382  << m_strXslUrl
383  << "\"?>\n";
384  }
385 
386  // no schema
387  if( m_strXsiUrl.empty() )
388  {
389  ssRootStmt << "<" << m_strRootElemName << ">";
390  }
391 
392  // validation schema
393  else
394  {
395  ssRootStmt << "<" << m_strRootElemName << " " << XmlNsXsi << "\n"
396  << " xsi:noNamespaceSchemaLocation=\"" << m_strXsiUrl << "\">";
397  }
398 
399  ssHead << XmlDecl << "\n"
400  << ssStylesheetStmt.str() << "\n"
401  << ssRootStmt.str() << ">\n\n";
402 
403  m_strXmlHead = ssHead.str();
404 }
const char *const XmlNsXsi
Definition: Xml.h:76
std::string m_strXsiUrl
xml schema instance
Definition: Xml.h:319
std::string m_strRootElemName
xml top-level root element name
Definition: Xml.h:318
std::string m_strXslUrl
xml style sheet
Definition: Xml.h:320
std::string m_strXmlHead
xml document head
Definition: Xml.h:324
const char *const XmlDecl
Definition: Xml.h:73
void Xml::makeXmlTail ( )
protectedvirtual

Make XML document tail string.

The XML tail string contains the root element closing statement.

Definition at line 406 of file Xml.cxx.

References m_strRootElemName, and m_strXmlTail.

Referenced by createTemplateFile().

407 {
408  m_strXmlTail = "\n</" + m_strRootElemName + ">\n";
409 }
std::string m_strXmlTail
xml document tail
Definition: Xml.h:325
std::string m_strRootElemName
xml top-level root element name
Definition: Xml.h:318
int Xml::saveFile ( const std::string &  strXmlFileName)
virtual

Save DOM to XML file.

Parameters
strXmlFileNameXML file path name.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 143 of file Xml.cxx.

References m_bufErrMsg, m_strXmlFileName, m_xmlDoc, setErrorMsg(), and setModifiedState().

144 {
145  if( strXmlFileName.empty() )
146  {
147  setErrorMsg("No file name.");
148  LOGERROR("%s", m_bufErrMsg);
149  return RC_ERROR;
150  }
151 
152  m_strXmlFileName = strXmlFileName;
153 
154  // save xml file
155  if( !m_xmlDoc.SaveFile(m_strXmlFileName.c_str()) )
156  {
157  setErrorMsg("%s[r%d,c%d]: %s.",
158  m_strXmlFileName.c_str(), m_xmlDoc.ErrorRow(),
159  m_xmlDoc.ErrorCol(), m_xmlDoc.ErrorDesc());
160  LOGERROR("%s", m_bufErrMsg);
161  return RC_ERROR;
162  }
163 
164  setModifiedState(false);
165 
166  LOGDIAG3("XMl document %s saved.", m_strXmlFileName.c_str());
167 
168  return OK;
169 }
char m_bufErrMsg[256]
error message buffer
Definition: Xml.h:330
TiXmlDocument m_xmlDoc
parsed xml DOM
Definition: Xml.h:327
std::string m_strXmlFileName
xml file path name
Definition: Xml.h:322
void setErrorMsg(const char *sFmt,...)
Set XML error message.
Definition: Xml.cxx:411
void setModifiedState(bool bModified)
Set DOM modified state.
Definition: Xml.h:216
void Xml::setErrorMsg ( const char *  sFmt,
  ... 
)
protected

Set XML error message.

Parameters
sFmtFormat string.
...Format variable arguments.

Definition at line 411 of file Xml.cxx.

References m_bufErrMsg.

Referenced by createTemplateFile(), fileExists(), loadFile(), and saveFile().

412 {
413  va_list ap;
414 
415  // format error message
416  va_start(ap, sFmt);
417  vsnprintf(m_bufErrMsg, sizeof(m_bufErrMsg), sFmt, ap);
418  m_bufErrMsg[sizeof(m_bufErrMsg)-1] = 0;
419  va_end(ap);
420 }
char m_bufErrMsg[256]
error message buffer
Definition: Xml.h:330
void rnr::Xml::setModifiedState ( bool  bModified)
inline

Set DOM modified state.

Parameters
bModifiedModified state: true or false.

Definition at line 216 of file Xml.h.

References m_bModified.

Referenced by loadFile(), and saveFile().

217  {
218  m_bModified = bModified;
219  }
bool m_bModified
xml [not] modified
Definition: Xml.h:329
void rnr::Xml::setSchemaInstance ( const std::string &  strXsiUrl)
inline

Set XML document schema instance.

Parameters
strXsiUrlURL of schema.

Definition at line 176 of file Xml.h.

References m_strXsiUrl.

177  {
178  m_strXsiUrl = strXsiUrl;
179  }
std::string m_strXsiUrl
xml schema instance
Definition: Xml.h:319
void rnr::Xml::setStylesheet ( const std::string &  strXslUrl)
inline

Set XML Extended Style Sheet.

Parameters
strXslUrlURL of stylesheet.

Definition at line 186 of file Xml.h.

References m_strXslUrl.

187  {
188  m_strXslUrl = strXslUrl;
189  }
std::string m_strXslUrl
xml style sheet
Definition: Xml.h:320
int Xml::strToDouble ( const std::string &  str,
double &  val 
)

Convert string to double-precision floating-point number.

Parameters
[in]strString in hex, decimal, or octal format.
[out]valConverted double value.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 340 of file Xml.cxx.

Referenced by getErrorMsg().

341 {
342  if( sscanf(str.c_str(), "%lf", &val) != 1 )
343  {
344  LOGERROR("%s: Bad floating-point number string representation.",
345  str.c_str());
346  return RC_ERROR;
347  }
348 
349 
350  return OK;
351 }
int Xml::strToInt ( const std::string &  str,
int &  val 
)

Convert string to integer.

Parameters
[in]strString in hex, decimal, or octal format.
[out]valConverted integer value.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 298 of file Xml.cxx.

Referenced by getErrorMsg().

299 {
300  long long int val1; // must use 64-bit for linaro 32bit
301 
302  if( sscanf(str.c_str(), "%lli", &val1) != 1 )
303  {
304  LOGERROR("%s: Bad integer string representation.", str.c_str());
305  return RC_ERROR;
306  }
307 
308  val = (int)val1;
309 
310  return OK;
311 }

The documentation for this class was generated from the following files: