Reader.h
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 Reader.h
22 
23  \brief A class that models a XML reader object built on top of Xerces-C++.
24 */
25 
26 #ifndef __TERRALIB_XERCES_INTERNAL_READER_H
27 #define __TERRALIB_XERCES_INTERNAL_READER_H
28 
29 // TerraLib
30 #include "../xml/Reader.h"
31 #include "Config.h"
32 
33 // STL
34 #include <string>
35 
36 // Xerces-C++
37 #include <xercesc/util/XercesDefs.hpp>
38 
39 // Forward declaration
40 XERCES_CPP_NAMESPACE_BEGIN
41 class Attributes;
42 class SAX2XMLReader;
43 class XMLPScanToken;
44 XERCES_CPP_NAMESPACE_END
45 
46 namespace te
47 {
48  namespace xerces
49  {
50 // Forward declaration
51  class ErrorHandler;
52  class ReaderHandler;
53 
54  /*!
55  \class Reader
56 
57  \brief A class that models a XML reader object built on top of Xerces-C++
58 
59  This API is intend to simplify the task of reading XML files. We have
60  tried to model it close to the Libxml2 API (see http://xmlsoft.org/xmlreader.html for more information)
61  but keeping all Xerces-C++ support capabilities: schema validation and grammar cache.
62 
63  If you prefer to use the SAX or DOM parsers, please, look
64  the Xerces-C++ documentation available at http://xerces.apache.org/xerces-c.
65  */
66  class Reader : public te::xml::Reader
67  {
68  public:
69 
70  /*! \brief Default constructor. */
71  Reader();
72 
73  /*! \brief Destructor. */
74  ~Reader();
75 
76  void setDoNamespaces(bool d);
77 
78  void setDoSchema(bool d);
79 
80  void setValidationScheme(bool d);
81 
82  void setUseCachedGrammarInParse(bool d);
83 
84  void setCacheGrammarFromParse(bool d);
85 
86  void setIgnoreWhiteSpaces(bool d);
87 
88  void read(const std::string& fileURI);
89 
90  bool next();
91 
93 
94  std::string getElementURI() const;
95 
96  std::string getElementLocalName() const;
97 
98  std::string getElementQName() const;
99 
100  std::string getElementValue() const;
101 
102  std::size_t getElementDataLen() const;
103 
104  bool hasAttrs() const;
105 
106  std::size_t getNumberOfAttrs() const;
107 
108  std::string getAttr(const std::string& name) const;
109 
110  std::string getAttr(std::size_t i) const;
111 
112  std::string getAttrLocalName(std::size_t i) const;
113 
114  std::string getAttrQName(std::size_t i) const;
115 
116  std::string getAttrURI(std::size_t i) const;
117 
118  std::size_t getAttrPosition(const std::string& name) const;
119 
120  std::size_t getNumberOfNamespaces() const;
121 
122  void getNamespace(std::size_t i, std::pair<std::string, std::string>& ns) const;
123 
124  void setInternalBufferSize(const std::size_t size);
125 
126  /*!
127  \brief It resets the parser.
128 
129  \exception Exception It throws an exception if it is not possible to reset the parser.
130 
131  \note Xerces extended method.
132  */
133  void reset();
134 
135  private:
136 
137  xercesc::SAX2XMLReader* m_parser; //!< A pointer to the parser used by the reader.
138  ReaderHandler* m_readerH; //!< A pointer to a content handler.
139  ErrorHandler* m_errH; //!< A pointer to an error handler.
140  xercesc::XMLPScanToken* m_token; //!< This keeps the parser state.
141  bool m_ignoreWhiteSpaces; //!< A flag that indicates if the parser should ignore white spaces.
142  };
143 
144  } // end namespace xerces
145 } // end namespace te
146 
147 #endif // __TERRALIB_XERCES_INTERNAL_READER_H
148 
std::string getAttrLocalName(std::size_t i) const
It returns the local part of the attribute name for the i-th attribute.
A class that models a XML reader object built on top of Xerces-C++.
Definition: Reader.h:66
bool next()
It gets the next event to be read.
This class models a XML reader object.
Definition: Reader.h:55
std::string getElementURI() const
It returns the URI of the associated namespace in the case of an element node.
bool m_ignoreWhiteSpaces
A flag that indicates if the parser should ignore white spaces.
Definition: Reader.h:141
void setDoNamespaces(bool d)
It enables or disables the parser namespace processing.
Reader()
Default constructor.
std::size_t getAttrPosition(const std::string &name) const
It returns the attribute position.
void setIgnoreWhiteSpaces(bool d)
If true the parser will ignore the white space characters.
std::string getAttrURI(std::size_t i) const
It returns the attribute URI of the associated namespace in the case of an element node...
ErrorHandler * m_errH
A pointer to an error handler.
Definition: Reader.h:139
void read(const std::string &fileURI)
It prepare the given file to be read.
void setCacheGrammarFromParse(bool d)
If true it caches the grammar in the pool for re-use in subsequent parses.
void setValidationScheme(bool d)
If true the parser will perform a validation scheme.
NodeType
The type of node read by XML reader.
Definition: Enums.h:40
This class implements Xerces interface for a handler that receives general document events...
Definition: ReaderHandler.h:52
std::size_t getNumberOfNamespaces() const
xercesc::SAX2XMLReader * m_parser
A pointer to the parser used by the reader.
Definition: Reader.h:137
std::string getAttrQName(std::size_t i) const
It returns the qualified name for the i-th attribute.
URI C++ Library.
ReaderHandler * m_readerH
A pointer to a content handler.
Definition: Reader.h:138
xercesc::XMLPScanToken * m_token
This keeps the parser state.
Definition: Reader.h:140
void setInternalBufferSize(const std::size_t size)
It sets the maximal allowed buffer size used for parsing.
std::string getElementLocalName() const
It returns the local part of the element name in the case of an element node.
te::xml::NodeType getNodeType() const
It return the type of node read.
std::size_t getElementDataLen() const
It returns the element data value in the case of VALUE node.
This class implements Xerces interface for error handlers.
Definition: ErrorHandler.h:50
void setUseCachedGrammarInParse(bool d)
If true the reader will use cached grammar if it exists in the pool.
std::string getElementQName() const
It returns the qualified name in the case of an element node.
void setDoSchema(bool d)
It enables or disables the parser schema processing.
std::string getElementValue() const
It returns the element data value in the case of VALUE node.
std::size_t getNumberOfAttrs() const
It returns the number of attributes in the case of an element node.
bool hasAttrs() const
It tells if the element has attributes in the case of an element node.
std::string getAttr(const std::string &name) const
It returns the attribute value in the case of an element node with valid attributes.
void getNamespace(std::size_t i, std::pair< std::string, std::string > &ns) const
~Reader()
Destructor.
void reset()
It resets the parser.