wms/client/1.1.1/XMLParser.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2017 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 #include "XMLParser.h"
22 
23 #include <iostream>
24 
25 //BOOST
26 #include <boost/algorithm/string.hpp>
27 
28 #include "../../../../../common/StringUtils.h"
29 #include "../../../../core/Utils.h"
30 #include "../../../../core/Exception.h"
31 
33 {
34  m_version = "1.1.1";
35 }
36 
38 
40 {
41 
42  // Read XML file
43  std::unique_ptr<te::xml::Reader> reader(te::xml::ReaderFactory::make("XERCES"));
44  reader->setValidationScheme(false);
45  reader->setDoSchema(false);
46  reader->setIgnoreWhiteSpaces(true);
47  reader->setCacheGrammarFromParse(false);
48  reader->setUseCachedGrammarInParse(false);
49 
50  reader->read(xmlPath);
51 
52  // Validate the XML file
53  if(!reader->next())
54  {
55  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Could not read the WMS 1.1.1 GetCapabilities response XML file!"));
56  }
57 
58  if(!boost::iequals(reader->getElementLocalName(), "WMT_MS_Capabilities"))
59  {
60  throw te::ws::core::WrongVersionException() << te::ErrorDescription(TE_TR("Could not find a WMS 1.1.1 GetCapabilities response in the received XML!"));
61  }
62 
64 
65  while (reader->next())
66  {
67  if(boost::iequals(reader->getElementLocalName(), "Capability"))
68  {
69  capabilities.m_capability = this->parseCapability(reader);
70  }
71  }
72 
73  return capabilities;
74 }
75 
77 {
78  Layer layer;
79 
80  if (reader->hasAttrs())
81  {
82  for (size_t i = 0; i < reader->getNumberOfAttrs(); i++)
83  {
84  if(boost::iequals(reader->getAttrLocalName(i), "queryable"))
85  layer.m_queryable = (reader->getAttrAsInt32("queryable") != 0) ? true : false;
86  else if (boost::iequals(reader->getAttrLocalName(i), "cascaded"))
87  layer.m_cascaded = (reader->getAttrAsInt32("cascaded") != 0) ? true : false;
88  else if (boost::iequals(reader->getAttrLocalName(i), "opaque"))
89  layer.m_opaque = (reader->getAttrAsInt32("opaque") != 0) ? true : false;
90  else if (boost::iequals(reader->getAttrLocalName(i), "noSubsets"))
91  layer.m_noSubsets = (reader->getAttrAsInt32("noSubsets") != 0) ? true : false;
92  else if (boost::iequals(reader->getAttrLocalName(i), "fixedWidth"))
93  layer.m_fixedWidth = (reader->getAttrAsInt32("fixedWidth") != 0) ? true : false;
94  else if (boost::iequals(reader->getAttrLocalName(i), "fixedHeight"))
95  layer.m_fixedHeight = (reader->getAttrAsInt32("fixedHeight") != 0) ? true : false;
96  }
97  }
98 
99  std::vector<std::string> srsList;
100 
101  while (reader->next() && !(reader->getNodeType() == te::xml::END_ELEMENT && boost::iequals(reader->getElementLocalName(), "Layer")))
102  {
103  if(reader->getNodeType() == te::xml::VALUE && boost::iequals(reader->getElementLocalName(), "Name"))
104  {
105  layer.m_name = reader->getElementValue();
106  }
107  else if(reader->getNodeType() == te::xml::VALUE && boost::iequals(reader->getElementLocalName(), "Title"))
108  {
109  layer.m_title = reader->getElementValue();
110  }
111  else if(reader->getNodeType() == te::xml::VALUE && boost::iequals(reader->getElementLocalName(), "Abstract"))
112  {
113  layer.m_abstract = reader->getElementValue();
114  }
115  else if(reader->getNodeType() == te::xml::VALUE && boost::iequals(reader->getElementLocalName(), "SRS"))
116  {
117  srsList.push_back(reader->getElementValue());
118  }
119  else if(reader->getNodeType() == te::xml::START_ELEMENT && boost::iequals(reader->getElementLocalName(), "KeywordList"))
120  {
121  std::vector<std::string> keywordList;
122 
123  while (reader->next() && !(reader->getNodeType() == te::xml::END_ELEMENT && boost::iequals(reader->getElementLocalName(), "KeywordList")))
124  {
125  if(reader->getNodeType() == te::xml::VALUE && boost::iequals(reader->getElementLocalName(), "Keyword"))
126  {
127  keywordList.push_back(reader->getElementValue());
128  }
129  }
130 
131  layer.m_keywordList = keywordList;
132  }
133  else if (reader->getNodeType() == te::xml::START_ELEMENT && boost::iequals(reader->getElementLocalName(), "LatLonBoundingBox"))
134  {
135  layer.m_geoBoundingBox = this->parseGeographicBoundingBox(reader);
136  }
137  else if (reader->getNodeType() == te::xml::START_ELEMENT && boost::iequals(reader->getElementLocalName(), "BoundingBox") && reader->hasAttrs())
138  {
139  layer.m_boundingBoxes.push_back(this->parseBoundingBox(reader));
140  }
141  else if (boost::iequals(reader->getElementLocalName(), "Dimension") && reader->hasAttrs())
142  {
143  layer.m_dimensions.push_back(this->parseDimension(reader));
144  }
145  else if (reader->getNodeType() == te::xml::START_ELEMENT && boost::iequals(reader->getElementLocalName(), "Style"))
146  {
147  layer.m_styles.push_back(this->parseStyle(reader));
148  }
149  else if (reader->getNodeType() == te::xml::START_ELEMENT && boost::iequals(reader->getElementLocalName(), "ScaleHint") && reader->hasAttrs())
150  {
151  for (unsigned int i = 0; i < reader->getNumberOfAttrs(); i++)
152  {
153  if(boost::iequals(reader->getAttrLocalName(i), "min"))
154  layer.m_minScaleDenominator = reader->getAttrAsDouble("min");
155  else if (boost::iequals(reader->getAttrLocalName(i), "max"))
156  layer.m_maxScaleDenominator = reader->getAttrAsDouble("max");
157  }
158  }
159  else if (reader->getNodeType() == te::xml::START_ELEMENT && boost::iequals(reader->getElementLocalName(), "Layer"))
160  {
161  Layer subLayer = this->parseLayers(reader);
162  layer.m_layers.push_back(subLayer);
163  }
164  }
165 
166  layer.m_crs = srsList;
167 
168  return layer;
169 }
170 
172 {
173 
174  te::ws::ogc::wms::GeographicBoundingBox latLonBoundingBox;
175 
176  latLonBoundingBox.m_westBoundLongitude = reader->getAttrAsDouble("minx");
177  latLonBoundingBox.m_eastBoundLongitude = reader->getAttrAsDouble("maxx");
178  latLonBoundingBox.m_southBoundLatitude = reader->getAttrAsDouble("miny");
179  latLonBoundingBox.m_northBoundLatitude = reader->getAttrAsDouble("maxy");
180 
181  return latLonBoundingBox;
182 }
183 
185 {
186 
187  te::ws::ogc::wms::Dimension dimension;
188 
189  for (unsigned int i = 0; i < reader->getNumberOfAttrs(); i++)
190  {
191  if(boost::iequals(reader->getAttrLocalName(i), "name"))
192  dimension.m_name = reader->getAttr("name");
193  else if (boost::iequals(reader->getAttrLocalName(i), "units"))
194  dimension.m_units = reader->getAttr("units");
195  else if (boost::iequals(reader->getAttrLocalName(i), "unitSymbol"))
196  dimension.m_unitSymbol = reader->getAttr("unitSymbol");
197  }
198 
199  reader->next();
200  reader->next();
201 
202  if (boost::iequals(reader->getElementLocalName(), "Extent"))
203  {
204  for (unsigned int i = 0; i < reader->getNumberOfAttrs(); i++)
205  {
206  if (boost::iequals(reader->getAttrLocalName(i), "default"))
207  dimension.m_default = reader->getAttr("default");
208  else if (boost::iequals(reader->getAttrLocalName(i), "nearestValue"))
209  dimension.m_nearestValue = (reader->getAttrAsInt32("nearestValue") != 0) ? true : false;
210  }
211 
212  while (reader->next() && !(reader->getNodeType() == te::xml::END_ELEMENT && boost::iequals(reader->getElementLocalName(), "Extent")))
213  {
214  if(reader->getNodeType() == te::xml::VALUE)
215  {
216  std::string allowedValuesStr = reader->getElementValue();
217 
218  std::vector<std::string> allowedValues = te::common::SplitString(allowedValuesStr, ',');
219 
220  dimension.m_allowedValues = allowedValues;
221  }
222  }
223  }
224 
225  return dimension;
226 }
227 
229 {
230  BoundingBox box;
231 
232  box.m_crs = reader->getAttr("SRS");
233 
234  box.m_minX = reader->getAttrAsDouble("minx");
235  box.m_minY = reader->getAttrAsDouble("miny");
236  box.m_maxX = reader->getAttrAsDouble("maxx");
237  box.m_maxY = reader->getAttrAsDouble("maxy");
238 
239  return box;
240 }
virtual BoundingBox parseBoundingBox(const std::unique_ptr< te::xml::Reader > &reader)
TECOMMONEXPORT std::vector< std::string > SplitString(const std::string &str, const char &delimiter)
Definition: StringUtils.cpp:32
virtual Style parseStyle(const std::unique_ptr< te::xml::Reader > &reader)
std::vector< std::string > m_crs
static te::xml::Reader * make()
It creates a new XML reader using the dafault implementation.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
boost::error_info< struct tag_error_description, std::string > ErrorDescription
The base type for error report messages.
The Capability WMS 1.3.0 struct.
std::vector< Dimension > m_dimensions
te::da::DataSourceCapabilities capabilities
std::vector< std::string > m_allowedValues
The Dimension WMS 1.3.0 struct.
The Layer WMS 1.3.0 struct.
virtual Layer parseLayers(const std::unique_ptr< te::xml::Reader > &reader)
virtual WMSCapabilities parseCapabilities(const std::string &xmlPath)
Method responsible to parse a WMS 1.1.1 XML GetCapabilities file and create an WMSCapabilities instan...
Base exception class for WS Core Runtime Library.
std::vector< std::string > m_keywordList
std::string m_version
WMS version that XMLParser instance could read. Default implementation is 1.3.0.
virtual Capability parseCapability(const std::unique_ptr< te::xml::Reader > &reader)
Exception to be used when a XML has an unsupported version by XMLParser.
virtual GeographicBoundingBox parseGeographicBoundingBox(const std::unique_ptr< te::xml::Reader > &reader)
GeographicBoundingBox m_geoBoundingBox
The GeographicBoundingBox WMS 1.3.0 struct.
virtual Dimension parseDimension(const std::unique_ptr< te::xml::Reader > &reader)
std::vector< BoundingBox > m_boundingBoxes
The BoundingBox WMS 1.3.0 struct.