serialization/xml/Style.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 terralib/se/serialization/xml/Style.cpp
22 
23  \brief Auxiliary classes and functions to serialize style informations from a XML document.
24 */
25 
26 // TerraLib
27 #include "../../../core/translator/Translator.h"
28 #include "../../../xml/AbstractWriter.h"
29 #include "../../../xml/Reader.h"
30 #include "../../../se/CoverageStyle.h"
31 #include "../../../se/FeatureTypeStyle.h"
32 #include "../../Exception.h"
33 //#include "Description.h"
34 //#include "Rule.h"
35 #include "SymbologySerializer.h"
36 #include "Style.h"
37 #include "Utils.h"
38 
39 // STL
40 #include <cassert>
41 
42 // Boost
43 #include <boost/format.hpp>
44 
47 
50 
51 void te::se::serialize::Style::reg(const std::string& styleType, const StyleFnctSerializeType& fncts)
52 {
53  m_fncts[styleType] = fncts;
54 }
55 
57 {
58  std::string styleType = reader.getElementLocalName();
59 
60  StyleFnctIdxType::const_iterator it = m_fncts.find(styleType);
61 
62  if(it == m_fncts.end())
63  throw Exception((boost::format(TE_TR("Could not find a reader for the following style type: %1%.")) % styleType).str());
64 
65  assert(it->second.second);
66 
67  return it->second.first(reader);
68 }
69 
71 {
72  assert(style);
73 
74  StyleFnctIdxType::const_iterator it = m_fncts.find(style->getType());
75 
76  if(it == m_fncts.end())
77  throw Exception((boost::format(TE_TR("Could not find a writer for the following style type: %1%.")) % style->getType()).str());
78 
79  assert(it->second.second);
80 
81  return it->second.second(style, writer);
82 }
83 
85 
87 {
90 }
91 
93 {
94  std::unique_ptr<te::se::FeatureTypeStyle> fts(new te::se::FeatureTypeStyle);
95 
96  // Version
97  if(reader.getNumberOfAttrs() > 0)
98  {
99  std::string version = reader.getAttr("version");
100  fts->setVersion(version);
101  }
102 
103  reader.next();
104 
105  // Name
106  if(reader.getElementLocalName() == "Name")
107  {
108  reader.next();
109  assert(reader.getNodeType() == te::xml::VALUE);
110  fts->setName(new std::string(reader.getElementValue()));
111  reader.next();
112 
113  assert(reader.getNodeType() == te::xml::END_ELEMENT);
114  reader.next();
115  }
116 
117  // Description
118  if(reader.getElementLocalName() == "Description")
119  fts->setDescription(te::se::serialize::ReadDescription(reader));
120 
121  // FeatureTypeName
122  if(reader.getElementLocalName() == "FeatureTypeName")
123  {
124  reader.next();
125  assert(reader.getNodeType() == te::xml::VALUE);
126  fts->setFeatureTypeName(new std::string(reader.getElementValue()));
127  reader.next();
128 
129  assert(reader.getNodeType() == te::xml::END_ELEMENT);
130  reader.next();
131  }
132 
133  // SemanticTypeIdentifier
134  while(reader.getNodeType() == te::xml::START_ELEMENT &&
135  reader.getElementLocalName() == "SemanticTypeIdentifier")
136  {
137  reader.next();
138  assert(reader.getNodeType() == te::xml::VALUE);
139  fts->push_back(reader.getElementValue());
140  reader.next();
141 
142  assert(reader.getNodeType() == te::xml::END_ELEMENT);
143  reader.next();
144  }
145 
146  // Rules
147  while(reader.getNodeType() == te::xml::START_ELEMENT &&
148  reader.getElementLocalName() == "Rule")
149  fts->push_back(te::se::serialize::ReadRule(reader));
150 
151  assert(reader.getNodeType() == te::xml::END_ELEMENT);
152  assert(reader.getElementLocalName() == "FeatureTypeStyle");
153 
154  reader.next();
155 
156  // TODO: OnlineResource
157 
158  return fts.release();
159 }
160 
162 {
163  std::unique_ptr<te::se::CoverageStyle> cs(new te::se::CoverageStyle);
164 
165  // Version
166  if(reader.getNumberOfAttrs() > 0)
167  {
168  std::string version = reader.getAttr("version");
169  cs->setVersion(version);
170  }
171 
172  reader.next();
173 
174  // Name
175  if(reader.getElementLocalName() == "Name")
176  {
177  reader.next();
178  assert(reader.getNodeType() == te::xml::VALUE);
179  cs->setName(new std::string(reader.getElementValue()));
180  reader.next();
181 
182  assert(reader.getNodeType() == te::xml::END_ELEMENT);
183  reader.next();
184  }
185 
186  // Description
187  if(reader.getElementLocalName() == "Description")
188  cs->setDescription(te::se::serialize::ReadDescription(reader));
189 
190  // CoverageName
191  if(reader.getElementLocalName() == "CoverageName")
192  {
193  reader.next();
194  assert(reader.getNodeType() == te::xml::VALUE);
195  cs->setCoverageName(new std::string(reader.getElementValue()));
196  reader.next();
197 
198  assert(reader.getNodeType() == te::xml::END_ELEMENT);
199  reader.next();
200  }
201 
202  // SemanticTypeIdentifier
203  while(reader.getNodeType() == te::xml::START_ELEMENT &&
204  reader.getElementLocalName() == "SemanticTypeIdentifier")
205  {
206  reader.next();
207  assert(reader.getNodeType() == te::xml::VALUE);
208  cs->push_back(reader.getElementValue());
209  reader.next();
210 
211  assert(reader.getNodeType() == te::xml::END_ELEMENT);
212  reader.next();
213  }
214 
215  // Rules
216  while(reader.getNodeType() == te::xml::START_ELEMENT &&
217  reader.getElementLocalName() == "Rule")
218  cs->push_back(te::se::serialize::ReadRule(reader));
219 
220  assert(reader.getNodeType() == te::xml::END_ELEMENT);
221  assert(reader.getElementLocalName() == "CoverageStyle");
222 
223  reader.next();
224 
225  // TODO: OnlineResource
226 
227  return cs.release();
228 }
229 
231 {
232  const te::se::FeatureTypeStyle* fts = dynamic_cast<const te::se::FeatureTypeStyle*>(style);
233 
234  if(fts == nullptr)
235  return;
236 
237  writer.writeStartElement("se:FeatureTypeStyle");
238  // Version
239  writer.writeAttribute("version", fts->getVersion());
240  // Namespace
241  //writer.writeAttribute("xmlns:se", "http://www.opengis.net/se");
242  //writer.writeAttribute("xmlns:ogc", "http://www.opengis.net/ogc");
243  // xlink
244  //writer.writeAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
245 
246  // Name
247  te::se::serialize::WriteStringPtrHelper("se:Name", fts->getName(), writer);
248  // Description
249  te::se::serialize::Save(fts->getDescription(), writer);
250  // FeatureTypeName
251  te::se::serialize::WriteStringPtrHelper("se:FeatureTypeName", fts->getFeatureTypeName(), writer);
252  // Semantic Type Identifiers
253  const std::vector<std::string>* semantics = fts->getSemanticTypeIdentifiers();
254  if(semantics != nullptr)
255  {
256  for(std::size_t i = 0; i < semantics->size(); ++i)
257  writer.writeElement("se:SemanticTypeIdentifier", semantics->at(i));
258  }
259 
260  // Rules
261  std::size_t nRules = fts->getRules().size();
262 
263  if(nRules > 0)
264  {
265  for(std::size_t i = 0; i != nRules; ++i)
266  te::se::serialize::Save(fts->getRule(i), writer);
267  }
268  else // OnlineResources
269  {
270  assert(fts->getOnlineResources().size() > 0);
271  for(std::size_t i = 0; i < fts->getOnlineResources().size(); ++i)
273  }
274 
275  writer.writeEndElement("se:FeatureTypeStyle");
276 }
277 
279 {
280  const te::se::CoverageStyle* cs = dynamic_cast<const te::se::CoverageStyle*>(style);
281 
282  if(cs == nullptr)
283  return;
284 
285  writer.writeStartElement("se:CoverageStyle");
286  // Version
287  writer.writeAttribute("version", cs->getVersion());
288  // Namespace
289  //writer.writeAttribute("xmlns:se", "http://www.opengis.net/se");
290  //writer.writeAttribute("xmlns:ogc", "http://www.opengis.net/ogc");
291  // xlink
292  //writer.writeAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
293 
294  // Name
295  te::se::serialize::WriteStringPtrHelper("se:Name", cs->getName(), writer);
296  // Description
298  // CoverageName
299  te::se::serialize::WriteStringPtrHelper("se:CoverageName", cs->getCoverageName(), writer);
300  // Semantic Type Identifiers
301  const std::vector<std::string>* semantics = cs->getSemanticTypeIdentifiers();
302  if(semantics != nullptr)
303  {
304  for(std::size_t i = 0; i < semantics->size(); ++i)
305  writer.writeElement("se:SemanticTypeIdentifier", semantics->at(i));
306  }
307 
308  // Rules
309  std::size_t nRules = cs->getRules().size();
310 
311  if(nRules > 0)
312  {
313  for(std::size_t i = 0; i != nRules; ++i)
314  te::se::serialize::Save(cs->getRule(i), writer);
315  }
316  else // OnlineResources
317  {
318  assert(cs->getOnlineResources().size() > 0);
319  for(std::size_t i = 0; i < cs->getOnlineResources().size(); ++i)
321  }
322 
323  writer.writeEndElement("se:CoverageStyle");
324 }
const std::string * getFeatureTypeName() const
std::pair< StyleReadFnctType, StyleWriteFnctType > StyleFnctSerializeType
Auxiliary classes and functions to serialize style informations from a XML document.
boost::function< void(const te::se::Style *, te::xml::AbstractWriter &)> StyleWriteFnctType
The Style defines the styling that is to be applied to a geographic dataset (vector geometries or cov...
Definition: Style.h:65
This class models a XML reader object.
Definition: xml/Reader.h:55
virtual void writeStartElement(const std::string &qName)=0
Base exception class for plugin module.
The CoverageStyle defines the styling that is to be applied to a subset of Coverage data...
Definition: CoverageStyle.h:45
void write(const te::se::Style *style, te::xml::AbstractWriter &writer) const
const std::string * getCoverageName() const
virtual std::string getElementLocalName() const =0
It returns the local part of the element name in the case of an element node.
void CoverageStyleWriter(const te::se::Style *layer, te::xml::AbstractWriter &writer)
Utility methods for the Symbology serialization.
This class models a XML writer object.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
te::se::Style * FeatureTypeStyleReader(te::xml::Reader &reader)
void WriteStringPtrHelper(const std::string &elementName, const std::string *s, te::xml::AbstractWriter &writer)
void FeatureTypeStyleWriter(const te::se::Style *style, te::xml::AbstractWriter &writer)
Rule * getRule(std::size_t i) const
Definition: Style.cpp:105
const std::vector< Rule * > & getRules() const
Definition: Style.cpp:94
virtual void writeElement(const std::string &qName, const std::string &value)=0
virtual std::string getAttr(const std::string &name) const =0
It returns the attribute value in the case of an element node with valid attributes.
boost::function< te::se::Style *(te::xml::Reader &)> StyleReadFnctType
The FeatureTypeStyle defines the styling that is to be applied to a dataset that can be viewed as a f...
const std::vector< te::xl::SimpleLink * > & getOnlineResources() const
Definition: Style.cpp:132
void reg(const std::string &styleType, const StyleFnctSerializeType &fncts)
virtual const std::string & getType() const =0
It returns the style type.
void WriteOnlineResourceHelper(const te::xl::SimpleLink *link, te::xml::AbstractWriter &writer)
TESEEXPORT void Save(const te::se::AnchorPoint *ap, te::xml::AbstractWriter &writer)
te::se::Style * read(te::xml::Reader &reader) const
Data serialization for the Symbology Encoder module.
virtual void writeAttribute(const std::string &attName, const std::string &value)=0
const std::string * getName() const
Definition: Style.cpp:59
const te::xl::SimpleLink * getOnlineResource(std::size_t i) const
Definition: Style.cpp:137
virtual std::size_t getNumberOfAttrs() const =0
It returns the number of attributes in the case of an element node.
virtual void writeEndElement(const std::string &qName)=0
virtual std::string getElementValue() const =0
It returns the element data value in the case of VALUE node.
virtual NodeType getNodeType() const =0
It return the type of node read.
TESEEXPORT te::se::Rule * ReadRule(te::xml::Reader &reader)
const std::vector< std::string > * getSemanticTypeIdentifiers() const
Definition: Style.cpp:89
TESEEXPORT te::se::Description * ReadDescription(te::xml::Reader &reader)
virtual bool next()=0
It gets the next event to be read.
const Description * getDescription() const
Definition: Style.cpp:70
te::se::Style * CoverageStyleReader(te::xml::Reader &reader)
const std::string & getVersion() const
Definition: Style.cpp:148