nidas  v1.2-1520
XMLParser.h
Go to the documentation of this file.
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2 // vim: set shiftwidth=4 softtabstop=4 expandtab:
3 /*
4  ********************************************************************
5  ** NIDAS: NCAR In-situ Data Acquistion Software
6  **
7  ** 2005, Copyright University Corporation for Atmospheric Research
8  **
9  ** This program is free software; you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation; either version 2 of the License, or
12  ** (at your option) any later version.
13  **
14  ** This program is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** The LICENSE.txt file accompanying this software contains
20  ** a copy of the GNU General Public License. If it is not found,
21  ** write to the Free Software Foundation, Inc.,
22  ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  **
24  ********************************************************************
25 */
26 
27 #ifndef NIDAS_CORE_XMLPARSER_H
28 #define NIDAS_CORE_XMLPARSER_H
29 
30 #include "XMLException.h"
31 
33 #include <nidas/util/IOException.h>
34 
35 #include <xercesc/dom/DOMImplementation.hpp>
36 #include <xercesc/dom/DOMErrorHandler.hpp>
37 #include <xercesc/dom/DOMDocument.hpp>
38 #include <xercesc/sax/InputSource.hpp>
39 
40 #if XERCES_VERSION_MAJOR < 3
41 #include <xercesc/dom/DOMBuilder.hpp>
42 #else
43 #include <xercesc/dom/DOMLSParser.hpp>
44 #endif
45 
46 #include <string>
47 #include <map>
48 #include <list>
49 
50 namespace nidas { namespace core {
51 
53 public:
54  static xercesc::DOMImplementation *getImplementation()
56  static void terminate();
57 
58 private:
59  static xercesc::DOMImplementation *_impl;
61 };
62 
63 class XMLErrorHandler : public xercesc::DOMErrorHandler
64 {
65 public:
66  // -----------------------
67  // Constructors and Destructor
68  // ----------------------
71 
72  // --------------------------
73  // Implementation of the DOM ErrorHandler interface
74  // -------------------------
75  bool handleError(const xercesc::DOMError& domError);
76 
77  void resetErrors();
78 
79  int getWarningCount() const { return _warningMessages.size(); }
80 
81  const std::list<std::string>& getWarningMessages() const
82  { return _warningMessages; }
83 
84  const XMLException* getXMLException() const { return _xmlException; }
85 
86 private :
87 
88  // -----------------------------
89  // Unimplemented constructors and operators
90  // ----------------------------
92 
93  void operator=(const XMLErrorHandler&);
94 
98  std::list<std::string> _warningMessages;
99 
104 
105 };
106 
111 xercesc::DOMDocument* parseXMLConfigFile(const std::string& xmlFileName)
113 
117 class XMLParser {
118 public:
119 
125 
130  virtual ~XMLParser();
131 
145  void setDOMValidation(bool val);
146 
157  void setDOMValidateIfSchema(bool val);
158 
166  void setDOMNamespaces(bool val);
167 
175  void setXercesSchema(bool val);
176 
188  void setXercesSchemaFullChecking(bool val);
189 
200  void setDOMDatatypeNormalization(bool val);
201 
215  void setXercesUserAdoptsDOMDocument(bool val);
216 
217  void setXercesHandleMultipleImports(bool val);
218 
219  void setXercesDoXInclude(bool val);
220 
221  xercesc::DOMDocument* parse(const std::string& xmlFile,bool verbose=true)
223 
224  xercesc::DOMDocument* parse(xercesc::InputSource& source)
226 
227 protected:
228 
229  xercesc::DOMImplementation *_impl;
230 
231 #if XERCES_VERSION_MAJOR < 3
232  xercesc::DOMBuilder *_parser;
233 #else
234  xercesc::DOMLSParser *_parser;
235 #endif
237 
238 private:
239 
241  XMLParser(const XMLParser&);
242 
244  XMLParser& operator=(const XMLParser&);
245 };
246 
252 class XMLCachingParser : public XMLParser {
253 public:
254 
255  static XMLCachingParser* getInstance()
257 
258  static void destroyInstance();
259 
268  xercesc::DOMDocument* parse(const std::string& xmlFile)
270 
274  /*
275  xercesc::DOMDocument* parse(xercesc::InputSource& source)
276  throw(nidas::core::XMLException)
277  {
278  return XMLParser::parse(source);
279  }
280  */
281 
282  static time_t getFileModTime(const std::string& name) throw(nidas::util::IOException);
283 
284 protected:
287 
288 protected:
291 
292  std::map<std::string,time_t> _modTimeCache;
293  std::map<std::string,xercesc::DOMDocument*> _docCache;
294 
296 
297 private:
300 
303 };
304 
305 }} // namespace nidas namespace core
306 
307 #endif
308 
void setXercesDoXInclude(bool val)
Definition: XMLParser.cc:195
xercesc::DOMDocument * parse(const std::string &xmlFile)
Parse from a file.
Definition: XMLParser.cc:380
static int verbose
Definition: lidar_vel.cc:45
XMLException * _xmlException
Accumulated error messages.
Definition: XMLParser.h:103
void setXercesSchema(bool val)
Enable/disable schema support.
Definition: XMLParser.cc:153
void setDOMValidation(bool val)
DOMBuilder::setFilter is not yet implemented in xerces c++ 2.6.0 void setFilter(xercesc::DOMBuilderFi...
Definition: XMLParser.cc:129
xercesc::DOMDocument * parseXMLConfigFile(const std::string &xmlFileName)
Utility function which creates a temporary XMLParser, sets the options we typically want and parses t...
Definition: XMLParser.cc:269
static xercesc::DOMImplementation * getImplementation()
Definition: XMLParser.cc:54
nidas::util::Mutex _cacheLock
Definition: XMLParser.h:295
Exception which can be built from an xerces::XMLException, xercesc::SAXException, or xercesc::DOMExce...
Definition: XMLException.h:43
static nidas::util::Mutex _lock
Definition: XMLParser.h:60
Derived class of XMLParser that keeps its DOMDocuments when parsing an XML disk file, and returns the cached DOMDocument if the file hasn&#39;t changed.
Definition: XMLParser.h:252
XMLParser & operator=(const XMLParser &)
No assignment.
void resetErrors()
Definition: XMLParser.cc:331
~XMLErrorHandler()
Definition: XMLParser.cc:296
void setXercesUserAdoptsDOMDocument(bool val)
Control who owns DOMDocument pointer.
Definition: XMLParser.cc:177
xercesc::DOMImplementation * _impl
Definition: XMLParser.h:229
std::map< std::string, xercesc::DOMDocument * > _docCache
Definition: XMLParser.h:293
const std::list< std::string > & getWarningMessages() const
Definition: XMLParser.h:81
void operator=(const XMLErrorHandler &)
int getWarningCount() const
Definition: XMLParser.h:79
static nidas::util::Mutex _instanceLock
Definition: XMLParser.h:290
XMLCachingParser & operator=(const XMLCachingParser &)
No assignment.
~XMLCachingParser()
Definition: XMLParser.cc:370
XMLCachingParser()
Definition: XMLParser.cc:365
void setDOMNamespaces(bool val)
Enable/disable namespace processing.
Definition: XMLParser.cc:145
static void destroyInstance()
Definition: XMLParser.cc:356
static void terminate()
Definition: XMLParser.cc:76
XMLErrorHandler _errorHandler
Definition: XMLParser.h:236
virtual ~XMLParser()
Nuke the parser.
Definition: XMLParser.cc:207
static xercesc::DOMImplementation * _impl
Definition: XMLParser.h:59
Definition: IOException.h:37
void setXercesHandleMultipleImports(bool val)
Definition: XMLParser.cc:185
static XMLCachingParser * _instance
Definition: XMLParser.h:289
XMLParser()
Constructor.
Definition: XMLParser.cc:89
XMLErrorHandler()
Definition: XMLParser.cc:292
bool handleError(const xercesc::DOMError &domError)
Definition: XMLParser.cc:305
Definition: XMLParser.h:63
static time_t getFileModTime(const std::string &name)
Parse from an InputSource.
Definition: XMLParser.cc:407
xercesc::DOMBuilder * _parser
Definition: XMLParser.h:232
void setDOMValidateIfSchema(bool val)
Enable/disable schema validation.
Definition: XMLParser.cc:137
A C++ wrapper for a POSIX mutex.
Definition: ThreadSupport.h:154
std::map< std::string, time_t > _modTimeCache
Definition: XMLParser.h:292
Wrapper class around xerces-c DOMBuilder to parse XML.
Definition: XMLParser.h:117
std::list< std::string > _warningMessages
Accumulated warning messages.
Definition: XMLParser.h:98
Definition: XMLParser.h:52
const XMLException * getXMLException() const
Definition: XMLParser.h:84
xercesc::DOMDocument * parse(const std::string &xmlFile, bool verbose=true)
Definition: XMLParser.cc:216
void setDOMDatatypeNormalization(bool val)
Enable/disable datatype normalization.
Definition: XMLParser.cc:169
void setXercesSchemaFullChecking(bool val)
Enable/disable full schema constraint checking, including checking which may be time-consuming or mem...
Definition: XMLParser.cc:161
static XMLCachingParser * getInstance()
Definition: XMLParser.cc:345