All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Utils.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file Utils.cpp
22 
23  \brief This file contains several utilities functions for dealing with Xerces in TerraLib.
24 */
25 
26 // TerraLib
27 #include "../common/Translator.h"
28 #include "ErrorHandler.h"
29 #include "Exception.h"
30 #include "Utils.h"
31 
32 // Xerces-C++
33 #include <xercesc/parsers/XercesDOMParser.hpp>
34 #include <xercesc/sax/SAXException.hpp>
35 #include <xercesc/sax2/XMLReaderFactory.hpp>
36 #include <xercesc/util/XMLException.hpp>
37 
38 // STL
39 #include <cassert>
40 #include <memory>
41 
42 xercesc::DOMDocument* te::xerces::readXML(const std::string& xmlFile,
43  const bool doNamespace,
44  const bool doSchema,
45  const bool valid)
46 {
47  assert(xmlFile.empty() == false);
48 
49  std::auto_ptr<xercesc::XercesDOMParser> parser(new xercesc::XercesDOMParser());
50  std::auto_ptr<ErrorHandler> errHandler(new ErrorHandler);
51  parser->setErrorHandler(errHandler.get());
52 
53  xercesc::XercesDOMParser::ValSchemes valScheme = valid ? xercesc::XercesDOMParser::Val_Always : xercesc::XercesDOMParser::Val_Never;
54 
55  //parser->setErrorHandler(errH);
56 
57  parser->setDoNamespaces(doNamespace);
58  parser->setDoSchema(doSchema);
59  parser->setValidationScheme(valScheme);
60  parser->cacheGrammarFromParse(true);
61  //parser->setIncludeIgnorableWhitespace(false);
62 
63  try
64  {
65  parser->parse(xmlFile.c_str());
66  }
67  catch(const xercesc::SAXException& e)
68  {
69  std::string m = TE_TR("Could not read the XML file: ");
70  m += xmlFile;
71  m += TE_TR(", due to the following error: ");
72  m += ToString(e.getMessage());
73 
74  throw Exception(m);
75  }
76  catch(const xercesc::DOMException& e)
77  {
78  std::string m = TE_TR("Could not read the XML file: ");
79  m += xmlFile;
80  m += TE_TR(", due to the following error: ");
81  m += ToString(e.getMessage());
82 
83  throw Exception(m);
84  }
85  catch(const xercesc::XMLException& e)
86  {
87  std::string m = TE_TR("Could not read the XML file: ");
88  m += xmlFile;
89  m += TE_TR(", due to the following error: ");
90  m += ToString(e.getMessage());
91 
92  throw Exception(m);
93  }
94 
95  if(parser->getErrorCount() > 0)
96  {
97  std::string m = TE_TR("Could not read the XML file: ");
98  m += xmlFile;
99  m += ", due to the following problem: ";
100  m += errHandler->getErrors();
101 
102  throw Exception(m);
103  }
104 
105 // take the ownershipo of the document and release the parser
106  xercesc::DOMDocument* doc = parser->adoptDocument();
107 
108  if(doc == 0)
109  throw Exception(TE_TR("Unable to read the XML document!"));
110 
111  return doc;
112 }
This class implements Xerces interface for error handlers.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
TEXERCESEXPORT xercesc::DOMDocument * readXML(const std::string &xmlFile, const bool doNamespace=false, const bool doSchema=false, const bool valid=false)
It reads the XML file using the DOM API.
Definition: Utils.cpp:42
An exception class for the xerces module.
This class implements Xerces interface for error handlers.
Definition: ErrorHandler.h:50
std::string ToString(const XMLCh *const value)
It converts the XML string to a standard C++ string.
Definition: Utils.h:122