Utils.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 Utils.h
22 
23  \brief This file contains several utilities functions for dealing with Xerces in TerraLib.
24 */
25 
26 #ifndef __TERRALIB_XERCES_INTERNAL_UTILS_H
27 #define __TERRALIB_XERCES_INTERNAL_UTILS_H
28 
29 // TerraLib
30 #include "Config.h"
31 
32 // Xerces-C++
33 #include <xercesc/dom/DOM.hpp>
34 #include <xercesc/sax2/Attributes.hpp>
35 #include <xercesc/util/XMLString.hpp>
36 
37 // STL
38 #include <string>
39 
40 namespace te
41 {
42  namespace xerces
43  {
44  /*!
45  \brief It return true if the two strings are identical, otherwise, it returns false.
46 
47  \param s1 The first string to compare.
48  \param s2 The second string to compare.
49 
50  \return True if the two strings are identical, otherwise, it returns false.
51 
52  \note This function is intend to be inlined and just convert
53  the XMLCh string to a char* and then compare the two strings.
54  */
55  inline bool Equals(const XMLCh* const s1, const char* const s2)
56  {
57  char* s1Aux = xercesc::XMLString::transcode(s1);
58 
59  if(strcmp(s1Aux, s2) == 0)
60  {
61  xercesc::XMLString::release(&s1Aux);
62  return true;
63  }
64  else
65  {
66  xercesc::XMLString::release(&s1Aux);
67  return false;
68  }
69  }
70 
71  /*!
72  \brief It return true if the two strings are different, otherwise, it returns false.
73 
74  \param s1 The first string to compare.
75  \param s2 The second string to compare.
76 
77  \return True if the two strings are different, otherwise, it returns false.
78 
79  \note This function is intend to be inlined and just convert
80  the XMLCh string to a char* and then compare the two strings.
81  */
82  inline bool NotEquals(const XMLCh* const s1, const char* const s2)
83  {
84  char* s1Aux = xercesc::XMLString::transcode(s1);
85 
86  if(strcmp(s1Aux, s2) == 0)
87  {
88  xercesc::XMLString::release(&s1Aux);
89  return false;
90  }
91  else
92  {
93  xercesc::XMLString::release(&s1Aux);
94  return true;
95  }
96  }
97 
98  /*!
99  \brief It converts the XML string to an integer value.
100 
101  \param value An XML string containing a valid int value.
102 
103  \return An int value.
104  */
105  inline int ToInt(const XMLCh* value)
106  {
107  char* sAux = xercesc::XMLString::transcode(value);
108  int iVal = atoi(sAux);
109  xercesc::XMLString::release(&sAux);
110  return iVal;
111  }
112 
113  /*!
114  \brief It converts the XML string to a standard C++ string.
115 
116  \param value An XML string containing a valid string value.
117 
118  \return An int value.
119 
120  \note If the pointer is NULL this method will return an empty string.
121  */
122  inline std::string ToString(const XMLCh* const value)
123  {
124  if(value == 0)
125  return std::string("");
126 
127  char* sAux = xercesc::XMLString::transcode(value);
128  std::string s(sAux);
129  return s;
130  }
131 
132  /*!
133  \brief It returns the given attribute value.
134 
135  \param attrs The attributes attached to an element, if any.
136  \param attName The name of the attribute we are looking for its value.
137 
138  \return The attribute value or NULL if it is not found.
139 
140  \note The caller will take the ownership of the returned pointer. Use
141  xercesc::XMLString::release(&val) in order to release the returned value.
142  */
143  inline char* GetAttributeVal(const xercesc::Attributes& attrs, const XMLCh* const attName)
144  {
145  const XMLSize_t nattributes = attrs.getLength();
146 
147  for(XMLSize_t i = 0; i < nattributes; ++i)
148  if(xercesc::XMLString::equals(attrs.getLocalName(i), attName))
149  return xercesc::XMLString::transcode(attrs.getValue(i));
150 
151  return 0;
152  }
153 
154  /*!
155  \brief It reads the XML file using the DOM API.
156 
157  \param xmlFile The XML file to be read.
158  \param doNamespace If true the namespace processing will be done.
159  \param doSchema If true the parser will attempt to use the XML schema.
160  \param valid If true the parser will valid the XML file against the schema.
161 
162  \return The read dcument.
163 
164  \exception Exception If the parser can not read the file it will raise an exception.
165  */
166  TEXERCESEXPORT xercesc::DOMDocument* readXML(const std::string& xmlFile,
167  const bool doNamespace = false,
168  const bool doSchema = false,
169  const bool valid = false);
170 
171  } // end namespace xerces
172 } // end namespace te
173 
174 #endif // __TERRALIB_XERCES_INTERNAL_UTILS_H
175 
bool Equals(const XMLCh *const s1, const char *const s2)
It return true if the two strings are identical, otherwise, it returns false.
Definition: Utils.h:55
char * GetAttributeVal(const xercesc::Attributes &attrs, const XMLCh *const attName)
It returns the given attribute value.
Definition: Utils.h:143
bool NotEquals(const XMLCh *const s1, const char *const s2)
It return true if the two strings are different, otherwise, it returns false.
Definition: Utils.h:82
int ToInt(const XMLCh *value)
It converts the XML string to an integer value.
Definition: Utils.h:105
URI C++ Library.
#define TEXERCESEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:83
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.
std::string ToString(const XMLCh *const value)
It converts the XML string to a standard C++ string.
Definition: Utils.h:122