All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Utils.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011-2011 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/serialization/se/Utils.cpp
22 
23  \brief Utility methods for Symbology serialization.
24 */
25 
26 // TerraLib
27 #include "../../fe/PropertyName.h"
28 #include "../../se/Symbolizer.h"
29 #include "../../xml/Reader.h"
30 #include "../../xml/Writer.h"
31 #include "../xlink/SimpleLink.h"
32 #include "../fe/Expression.h"
33 #include "Description.h"
34 #include "ParameterValue.h"
35 #include "SelectedChannel.h"
36 #include "Utils.h"
37 
38 // STL
39 #include <cassert>
40 
41 void te::serialize::WriteStringPtrHelper(const std::string& elementName, const std::string* s, te::xml::Writer& writer)
42 {
43  if(s != 0 && !s->empty()) writer.writeElement(elementName, *s);
44 }
45 
46 void te::serialize::WriteParameterValuePtrHelper(const std::string& elementName, const te::se::ParameterValue* p, te::xml::Writer& writer)
47 {
48  if(p == 0)
49  return;
50 
51  writer.writeStartElement(elementName);
52  Save(p, writer);
53  writer.writeEndElement(elementName);
54 }
55 
57 {
58  if(link == 0)
59  return;
60 
61  writer.writeStartElement("se:BaseSymbolizer");
62  WriteOnlineResourceHelper(link, writer);
63  writer.writeEndElement("se:BaseSymbolizer");
64 }
65 
67 {
68  if(link == 0)
69  return;
70 
71  writer.writeStartElement("se:OnlineResource");
72  Save(link, writer);
73  writer.writeEndElement("se:OnlineResource");
74 }
75 
77 {
78  assert(symbolizer);
79 
80  writer.writeAttribute("version", symbolizer->getVersion());
81  //writer.writeAttribute("uom", symbolizer->getUom()); // TODO: URI from te::common:: UnitOfMeasure!
82  WriteStringPtrHelper("se:Name", &symbolizer->getName(), writer);
83  Save(symbolizer->getDescription(), writer);
84  WriteBaseSymbolizerHelper(symbolizer->getBaseSymbolizer(), writer);
85 }
86 
88 {
89  if(reader.hasAttrs())
90  {
91  // TODO: Verify first if the symbolizer has the attributes version and uom!
92 
93  // Version
94  std::string version = reader.getAttr("version");
95  symbolizer->setVersion(version);
96 
97  // Uom
98  std::string uom = reader.getAttr("uom");
99  //symbolizer->setUom(version); // TODO: te:common:: UnitOfMeasure from URI!
100  }
101 
102  reader.next();
103 
104  // Name
105  if(reader.getElementLocalName() == "Name")
106  {
107  reader.next();
108  assert(reader.getNodeType() == te::xml::VALUE);
109  symbolizer->setName(reader.getElementValue());
110  reader.next();
111  assert(reader.getNodeType() == te::xml::END_ELEMENT);
112  reader.next();
113  }
114 
115  // Description
116  if(reader.getElementLocalName() == "Description")
117  symbolizer->setDescription(ReadDescription(reader));
118 
119  // TODO: BaseSymbolizer
120 }
121 
122 void te::serialize::WriteSelectedChannelHelper(const std::string& elementName, const te::se::SelectedChannel* sc, te::xml::Writer& writer)
123 {
124  if(sc == 0)
125  return;
126 
127  writer.writeStartElement(elementName);
128  Save(sc, writer);
129  writer.writeEndElement(elementName);
130 }
131 
133 {
134  if(p == 0)
135  return;
136 
137  writer.writeStartElement("se:Geometry");
138  te::serialize::Expression::getInstance().write(p, writer);
139  writer.writeEndElement("se:Geometry");
140 }
141 
143 {
144  assert(reader.getNodeType() == te::xml::START_ELEMENT);
145  assert(reader.getElementLocalName() == "Geometry");
146 
147  reader.next();
148 
150  assert(exp);
151 
152  std::auto_ptr<te::fe::PropertyName> pName(dynamic_cast<te::fe::PropertyName*>(exp));
153  assert(pName.get());
154 
155  assert(reader.getNodeType() == te::xml::END_ELEMENT);
156  reader.next();
157 
158  return pName.release();
159 }
Support for SelectedChannel serialization.
This class is used to encode the name of any property of an object.
Definition: PropertyName.h:54
const std::string & getName() const
Definition: Symbolizer.cpp:72
const std::string & getVersion() const
Definition: Symbolizer.cpp:104
void WriteSymbolizerHelper(const te::se::Symbolizer *symbolizer, te::xml::Writer &writer)
Definition: Utils.cpp:76
Utility methods for Symbology serialization.
TEDATAACCESSEXPORT void Save(const std::string &fileName)
Definition: Serializer.cpp:191
A Symbolizer describes how a feature is to appear on a map.
Definition: Symbolizer.h:80
This class models a XML reader object.
Definition: Reader.h:55
void setName(const std::string &name)
Definition: Symbolizer.cpp:67
void WriteParameterValuePtrHelper(const std::string &elementName, const te::se::ParameterValue *p, te::xml::Writer &writer)
Definition: Utils.cpp:46
virtual bool hasAttrs() const =0
It tells if the element has attributes in the case of an element node.
virtual bool next()=0
It gets the next event to be read.
TESERIALIZATIONEXPORT te::se::Description * ReadDescription(te::xml::Reader &reader)
Definition: Description.cpp:36
Simple link.
Definition: SimpleLink.h:48
void WriteSelectedChannelHelper(const std::string &elementName, const te::se::SelectedChannel *sc, te::xml::Writer &writer)
Definition: Utils.cpp:122
A selected channel to be display.
const te::xl::SimpleLink * getBaseSymbolizer() const
Definition: Symbolizer.cpp:94
virtual void writeStartElement(const std::string &qName)
Definition: Writer.cpp:44
void WriteOnlineResourceHelper(const te::xl::SimpleLink *link, te::xml::Writer &writer)
Definition: Utils.cpp:66
virtual void writeAttribute(const std::string &attName, const std::string &value)
Definition: Writer.cpp:90
virtual std::string getElementValue() const =0
It returns the element data value in the case of VALUE node.
virtual void writeEndElement(const std::string &qName)
Definition: Writer.cpp:156
te::fe::PropertyName * ReadGeometryPropertyHelper(te::xml::Reader &reader)
Definition: Utils.cpp:142
void setDescription(Description *d)
Definition: Symbolizer.cpp:77
void setVersion(const std::string &version)
Definition: Symbolizer.cpp:99
This is an abstract class that models a Filter Encoding expression.
Definition: Expression.h:50
const Description * getDescription() const
Definition: Symbolizer.cpp:83
Support for Description serialization.
void ReadSymbolizerHelper(te::se::Symbolizer *symbolizer, te::xml::Reader &reader)
Definition: Utils.cpp:87
virtual void writeElement(const std::string &qName, const std::string &value)
Definition: Writer.cpp:54
void WriteGeometryPropertyHelper(const te::fe::PropertyName *p, te::xml::Writer &writer)
Definition: Utils.cpp:132
void WriteStringPtrHelper(const std::string &elementName, const std::string *s, te::xml::Writer &writer)
Definition: Utils.cpp:41
static Expression & getInstance()
It returns a reference to the singleton instance.
virtual NodeType getNodeType() const =0
It return the type of node read.
virtual std::string getElementLocalName() const =0
It returns the local part of the element name in the case of an element node.
void WriteBaseSymbolizerHelper(const te::xl::SimpleLink *link, te::xml::Writer &writer)
Definition: Utils.cpp:56
The &quot;ParameterValueType&quot; uses WFS-Filter expressions to give values for SE graphic parameters...
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.
This class models a XML writer object.
Definition: Writer.h:52