src/terralib/xerces/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 "../core/translator/Translator.h"
28 #include "../xml/Exception.h"
29 #include "ErrorHandler.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::unique_ptr<xercesc::XercesDOMParser> parser(new xercesc::XercesDOMParser());
50  std::unique_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  parser->setLoadExternalDTD(false);
63 
64  try
65  {
66  parser->parse(xmlFile.c_str());
67  }
68  catch(const xercesc::SAXException& e)
69  {
70  std::string m = TE_TR("Could not read the XML file: ");
71  m += xmlFile;
72  m += TE_TR(", due to the following error: ");
73  m += ToString(e.getMessage());
74 
75  throw te::xml::Exception(m);
76  }
77  catch(const xercesc::DOMException& e)
78  {
79  std::string m = TE_TR("Could not read the XML file: ");
80  m += xmlFile;
81  m += TE_TR(", due to the following error: ");
82  m += ToString(e.getMessage());
83 
84  throw te::xml::Exception(m);
85  }
86  catch(const xercesc::XMLException& e)
87  {
88  std::string m = TE_TR("Could not read the XML file: ");
89  m += xmlFile;
90  m += TE_TR(", due to the following error: ");
91  m += ToString(e.getMessage());
92 
93  throw te::xml::Exception(m);
94  }
95 
96  if(parser->getErrorCount() > 0)
97  {
98  std::string m = TE_TR("Could not read the XML file: ");
99  m += xmlFile;
100  m += ", due to the following problem: ";
101  m += errHandler->getErrors();
102 
103  throw te::xml::Exception(m);
104  }
105 
106 // take the ownershipo of the document and release the parser
107  xercesc::DOMDocument* doc = parser->adoptDocument();
108 
109  if(doc == nullptr)
110  throw te::xml::Exception(TE_TR("Unable to read the XML document!"));
111 
112  return doc;
113 }
This class implements Xerces interface for error handlers.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
TEXMLEXPORT 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.
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.