27 #include "../common/Exception.h"
28 #include "../common/Translator.h"
37 #include <xercesc/sax2/SAX2XMLReader.hpp>
38 #include <xercesc/sax2/XMLReaderFactory.hpp>
45 m_ignoreWhiteSpaces(false)
47 m_parser = xercesc::XMLReaderFactory::createXMLReader();
55 m_parser->setFeature(xercesc::XMLUni::fgSAX2CoreNameSpaces,
true);
56 m_parser->setFeature(xercesc::XMLUni::fgXercesSchema,
true);
57 m_parser->setFeature(xercesc::XMLUni::fgSAX2CoreValidation,
true);
58 m_parser->setFeature(xercesc::XMLUni::fgXercesUseCachedGrammarInParse,
true);
59 m_parser->setFeature(xercesc::XMLUni::fgXercesCacheGrammarFromParse,
true);
76 m_parser->setFeature(xercesc::XMLUni::fgSAX2CoreNameSpaces, d);
81 m_parser->setFeature(xercesc::XMLUni::fgXercesSchema, d);
86 m_parser->setFeature(xercesc::XMLUni::fgSAX2CoreValidation, d);
91 m_parser->setFeature(xercesc::XMLUni::fgXercesUseCachedGrammarInParse, d);
96 m_parser->setFeature(xercesc::XMLUni::fgXercesCacheGrammarFromParse, d);
101 m_ignoreWhiteSpaces = d;
112 m_token =
new xercesc::XMLPScanToken;
116 if(!m_parser->parseFirst(fileURI.c_str(), *m_token))
120 std::string m =
TE_TR(
"Could not start reading the file due to the following problem: ");
132 catch(
const xercesc::XMLException& e)
134 std::string m =
TE_TR(
"Error when reading the XML document. Exception messages is: ");
141 throw Exception(
TE_TR(
"Unexpected error reading the XML document!"));
151 bool parserStatus =
true;
155 while(
true && parserStatus)
157 if(m_readerH->isInContractedForm())
160 m_readerH->setInContractedForm(
false);
164 parserStatus = m_parser->parseNext(*m_token);
166 if(m_parser->getErrorCount() != 0)
168 std::string errmsg = m_errH->getErrors();
169 throw Exception(errmsg);
179 catch(
const xercesc::XMLException& e)
181 std::string m =
TE_TR(
"Error parsing the XML document. Exception messages is: ");
186 catch(
const std::exception& )
192 throw Exception(
TE_TR(
"Unexpected error in parse next!"));
200 return m_readerH->getNodeType();
205 return ToString(m_readerH->getElementURI());
210 return ToString(m_readerH->getElementLocalName());
215 return ToString(m_readerH->getElementQName());
220 return ToString(m_readerH->getElementValue());
225 return m_readerH->getDataLen();
230 return (m_readerH->getElementAttrs() != 0) && (m_readerH->getElementAttrs()->getLength() > 0);
235 return (hasAttrs() ? m_readerH->getElementAttrs()->getLength() : 0);
240 assert(m_readerH->getElementAttrs());
242 const XMLCh* value = m_readerH->getElementAttrs()->getValue(
StrToXMLCh(name).getXMLCh());
249 assert(m_readerH->getElementAttrs());
250 assert(i < m_readerH->getElementAttrs()->getLength());
252 const XMLCh* value = m_readerH->getElementAttrs()->getValue(i);
259 assert(m_readerH->getElementAttrs());
260 assert(i < m_readerH->getElementAttrs()->getLength());
262 const XMLCh* name = m_readerH->getElementAttrs()->getLocalName(i);
269 assert(m_readerH->getElementAttrs());
270 assert(m_readerH->getElementAttrs()->getLength() > i);
272 const XMLCh* name = m_readerH->getElementAttrs()->getQName(i);
279 assert(m_readerH->getElementAttrs());
280 assert(m_readerH->getElementAttrs()->getLength() > i);
282 const XMLCh* name = m_readerH->getElementAttrs()->getURI(i);
289 assert(m_readerH->getElementAttrs());
291 return m_readerH->getElementAttrs()->getIndex(
StrToXMLCh(name).getXMLCh());
296 return m_readerH->getNumberOfNamespaces();
301 const std::pair<const XMLCh*, const XMLCh*>& nns = m_readerH->getNamespace(i);
309 m_parser->setInputBufferSize(size);
319 m_parser->parseReset(*m_token);
321 catch(
const xercesc::XMLException& e)
323 std::string m =
TE_TR(
"Error reseting the parser. Exception messages is: ");
330 throw Exception(
TE_TR(
"Unexpected error reseting the parser!"));
std::string getAttrLocalName(std::size_t i) const
It returns the local part of the attribute name for the i-th attribute.
A class for converting a standard string to a Xerces string (XMLCh).
bool next()
It gets the next event to be read.
std::string getElementURI() const
It returns the URI of the associated namespace in the case of an element node.
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.
A class for converting a standard string to a Xerces string (XMLCh).
This class implements Xerces interface for error handlers.
std::string getAttrURI(std::size_t i) const
It returns the attribute URI of the associated namespace in the case of an element node...
#define TE_TR(message)
It marks a string in order to get translated.
ErrorHandler * m_errH
A pointer to an error handler.
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.
const std::string getErrors() const
It returns a concatenation of all reported error messages.
void setValidationScheme(bool d)
If true the parser will perform a validation scheme.
NodeType
The type of node read by XML reader.
This class implements Xerces interface for a handler that receives general document events...
std::size_t getNumberOfNamespaces() const
xercesc::SAX2XMLReader * m_parser
A pointer to the parser used by the reader.
std::string getAttrQName(std::size_t i) const
It returns the qualified name for the i-th attribute.
ReaderHandler * m_readerH
A pointer to a content handler.
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.
This class implements Xerces interface for a handler that receives general document events...
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.
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.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
std::string getElementValue() const
It returns the element data value in the case of VALUE node.
#define TE_XERCES_READER_MAX_BUFFSIZE
It defines the default internal buffer size for parsing a XERCES file.
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
std::string ToString(const XMLCh *const value)
It converts the XML string to a standard C++ string.
void reset()
It resets the parser.