V-ART
xmlbase.cpp
Go to the documentation of this file.
1 
5 #include "vart/xmlbase.h"
6 #include <xercesc/util/PlatformUtils.hpp>
7 #include <xercesc/util/OutOfMemoryException.hpp>
8 #include <xercesc/dom/DOMException.hpp>
9 #include <xercesc/dom/DOMImplementation.hpp>
10 #include <xercesc/dom/DOMNamedNodeMap.hpp>
11 #include <xercesc/sax/SAXParseException.hpp>
12 #include <iostream>
13 #include <sstream>
14 #include <cstring>
15 
16 using XERCES_CPP_NAMESPACE::XercesDOMParser;
17 using XERCES_CPP_NAMESPACE::XMLPlatformUtils;
18 using XERCES_CPP_NAMESPACE::XMLException;
19 using XERCES_CPP_NAMESPACE::OutOfMemoryException;
20 using XERCES_CPP_NAMESPACE::DOMException;
21 using XERCES_CPP_NAMESPACE::DOMImplementation;
22 using namespace std;
23 
25 {
26  parserPtr = NULL;
27  documentPtr = NULL;
28 }
29 
30 bool VART::XmlBase::ParseFile(const std::string& fileName)
31 {
32  static XercesDOMParser::ValSchemes ValScheme = XercesDOMParser::Val_Auto; //Val_Never Val_Always
33  bool bDoNamespaces = false;
34  bool bDoSchema = false;
35  bool bSchemaFullChecking = false;
36  bool bDoCreate = false;
37  ErrorHandler* errorHPtr;
38 
39  try
40  {
41  XMLPlatformUtils::Initialize();
42  }
43  catch(const XMLException &toCatch)
44  {
45  cerr << "Error during Xerces-c Initialization.\n"
46  << " Exception message:"
47  << TempCString(toCatch.getMessage()) << endl;
48  return false;
49  }
50 
51  if (parserPtr)
52  delete parserPtr;
53 
54  parserPtr = new XercesDOMParser;
55  parserPtr->setValidationScheme(ValScheme);
56  parserPtr->setDoNamespaces(bDoNamespaces);
57  parserPtr->setDoSchema(bDoSchema);
58  parserPtr->setValidationSchemaFullChecking(bSchemaFullChecking);
59  parserPtr->setCreateEntityReferenceNodes(bDoCreate);
60 
61 
62  // Create the error handler and install it
63  errorHPtr = new ErrorHandler;
64  parserPtr->setErrorHandler(errorHPtr);
65 
66  // Parse the XML file, catching any XML exceptions that might propogate
67  // out of it.
68  try
69  {
70  parserPtr->parse(fileName.c_str());
71  }
72  catch (const OutOfMemoryException&)
73  {
74  cerr << "OutOfMemoryException" << endl;
75  delete errorHPtr;
76  return false;
77  }
78  catch (const XMLException& e)
79  {
80  cerr << "An error occurred during parsing\n Message: "
81  << TempCString(e.getMessage()) << endl;
82  delete errorHPtr;
83  return false;
84  }
85  catch (const DOMException& e)
86  {
87  const unsigned int maxChars = 2047;
88  XMLCh errText[maxChars + 1];
89 
90  cerr << "\nDOM Error during parsing: '" << fileName << "'\n"
91  << "DOMException code is: " << e.code << endl;
92  if (DOMImplementation::loadDOMExceptionMsg(e.code, errText, maxChars))
93  cerr << "Message is: " << TempCString(errText) << endl;
94  delete errorHPtr;
95  return false;
96  }
97  catch (...)
98  {
99  cerr << "An error occurred during parsing\n " << endl;
100  delete errorHPtr;
101  return false;
102  }
103  if (errorHPtr->getSawErrors())
104  {
105  // Clean up the error handler. The parser does not adopt handlers
106  // since they could be many objects or one object installed for multiple
107  // handlers.
108  delete errorHPtr;
109  return false;
110  }
111  else
112  {
113  //...clean up the error handler. The parser does not adopt handlers
114  // since they could be many objects or one object installed for multiple
115  // handlers.
116  delete errorHPtr;
117 
118  // Since parsing was sucessful, intialize the document pointer, i.e.:
119  // extract the DOM tree, get the list of all the elements.
120  documentPtr = parserPtr->getDocument();
121  return true;
122  }
123 }
124 
126 {
127  //Delete the parser itself. Must be done prior to calling Terminate, below.
128  if (parserPtr)
129  delete parserPtr;
130  //Call the termination method
131  XMLPlatformUtils::Terminate();
132 
133 }
134 
135 //static
136 bool VART::XmlBase::GetAttributeValue(XERCES_CPP_NAMESPACE::DOMNamedNodeMap* nodePtr,
137  const char itemName[], float* valuePtr)
138 {
139 
140  TempCString str(nodePtr->getNamedItem(XercesString(itemName))->getNodeValue());
141  return str.GetValue(valuePtr);
142 }
143 
144 //static
145 bool VART::XmlBase::GetAttributeValue(XERCES_CPP_NAMESPACE::DOMNamedNodeMap* nodePtr,
146  const char itemName[], string* valuePtr)
147 {
148  (*valuePtr) = TempCString(nodePtr->getNamedItem(XercesString(itemName))->getNodeValue());
149  return true;
150 }
151 
152 //static
153 bool VART::XmlBase::GetAttributeValue(XERCES_CPP_NAMESPACE::DOMNamedNodeMap* nodePtr,
154  const char itemName[], bool* valuePtr)
155 {
156  TempCString str(nodePtr->getNamedItem(XercesString(itemName))->getNodeValue());
157  return str.GetValue(valuePtr);
158 }
159 
160 bool VART::XmlBase::TempCString::operator== (const char* other) const
161 {
162  return (strcmp(cString,other) == 0);
163 }
164 
165 bool VART::XmlBase::TempCString::GetValue(int* valuePtr) const
166 {
167  istringstream inputStream;
168 
169  inputStream.str(cString);
170  inputStream >> (*valuePtr);
171  return inputStream.good();
172 }
173 
174 
175 bool VART::XmlBase::TempCString::GetValue(float* valuePtr) const
176 {
177  istringstream inputStream;
178 
179  inputStream.str(cString);
180  inputStream >> (*valuePtr);
181  return inputStream.good();
182 }
183 
184 bool VART::XmlBase::TempCString::GetValue(bool* valuePtr) const
185 {
186  string value(cString);
187  if ((value == "true") || (value == "1"))
188  {
189  *valuePtr = true;
190  return true;
191  }
192  if ((value == "false") || (value == "0"))
193  {
194  *valuePtr = false;
195  return true;
196  }
197  return false;
198 }
199 
201 {
202 }
203 
205 {
206 }
207 
208 //Overrides of the SAX ErrorHandler interface
209 void VART::XmlBase::ErrorHandler::error(const XERCES_CPP_NAMESPACE::SAXParseException& e)
210 {
211  fSawErrors = true;
212  cerr << "\nError at file " << TempCString(e.getSystemId())
213  << ", line " << e.getLineNumber()
214  << ", char " << e.getColumnNumber()
215  << "\n Message: " << TempCString(e.getMessage()) << endl;
216 }
217 
218 void VART::XmlBase::ErrorHandler::fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& e)
219 {
220  fSawErrors = true;
221  cerr << "\nFatal Error at file " << TempCString(e.getSystemId())
222  << ", line " << e.getLineNumber()
223  << ", char " << e.getColumnNumber()
224  << "\n Message: " << TempCString(e.getMessage()) << endl;
225 }
226 
227 void VART::XmlBase::ErrorHandler::warning(const XERCES_CPP_NAMESPACE::SAXParseException& e)
228 {
229  cerr << "\nWarning at file '" << TempCString(e.getSystemId())
230  << "', line " << e.getLineNumber()
231  << ", char " << e.getColumnNumber()
232  << "\n Message: " << TempCString(e.getMessage()) << endl;
233 }
234 
236 {
237  fSawErrors = false;
238 }
239 
241 {
242  return fSawErrors;
243 }
void warning(const XERCES_CPP_NAMESPACE::SAXParseException &e)
Definition: xmlbase.cpp:227
Header file for V-ART class "XmlBase".
void fatalError(const XERCES_CPP_NAMESPACE::SAXParseException &e)
Definition: xmlbase.cpp:218
XmlBase()
Creates an unitialized object.
Definition: xmlbase.cpp:24
void error(const XERCES_CPP_NAMESPACE::SAXParseException &e)
Definition: xmlbase.cpp:209
static bool GetAttributeValue(XERCES_CPP_NAMESPACE::DOMNamedNodeMap *nodePtr, const char itemName[], float *valuePtr)
Gets an item value for certain node.
Definition: xmlbase.cpp:136
void Terminate()
Terminates Xerces objects.
Definition: xmlbase.cpp:125
Class for auto conversion from C strings to Xerces strings.
Definition: xmlbase.h:77
bool ParseFile(const std::string &fileName)
Checks if the file comforms to DTD.
Definition: xmlbase.cpp:30
bool getSawErrors() const
Definition: xmlbase.cpp:240
bool operator==(const char *other) const
Check against a C string.
Definition: xmlbase.cpp:160
bool GetValue(int *valuePtr) const
Definition: xmlbase.cpp:165
Class for auto conversion from Xerces strings to C strings.
Definition: xmlbase.h:47
Provides pretty errors messages for Xerces.
Definition: xmlbase.h:25