fe/serialization/xml/Expression.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/fe/serialization/xml/Expression.cpp
22 
23  \brief Auxiliary classes and functions to serialize filter expressions from a XML document.
24 */
25 
26 // TerraLib
27 #include "../../../core/translator/Translator.h"
28 #include "../../../fe/BinaryOperator.h"
29 #include "../../../fe/Expression.h"
30 #include "../../../fe/Globals.h"
31 #include "../../../fe/Literal.h"
32 #include "../../../fe/PropertyName.h"
33 #include "../../../xml/AbstractWriter.h"
34 #include "../../../xml/Reader.h"
35 #include "../../Exception.h"
36 #include "Expression.h"
37 
38 // STL
39 #include <cassert>
40 
41 // Boost
42 #include <boost/format.hpp>
43 
44 /* @name Expression Reader Methods */
45 //@{
46 
50 
51 //@}
52 
53 void te::fe::serialize::Expression::reg(const std::string& expName, const ExpressionReadFnctType& fnct)
54 {
55  m_fncts[expName] = fnct;
56 }
57 
59 {
60  std::string name;
61  reader.getNodeType() == te::xml::VALUE ? name = "Literal" : name = reader.getElementLocalName();
62 
63  ExpressionFnctIdxType::const_iterator it = m_fncts.find(name);
64 
65  if(it == m_fncts.end())
66  throw Exception((boost::format(TE_TR("Could not find a reader for the following expression type: %1%.")) % name).str());
67 
68  assert(it->second);
69 
70  return it->second(reader);
71 }
72 
74 {
75  assert(exp);
76  assert(&writer);
77 
78  if(m_writer != &writer)
79  m_writer = &writer;
80 
81  exp->accept(*this);
82 }
83 
85 {
86  std::string name = "ogc:";
87  name += visited.getName();
88 
90 
91  te::fe::Expression* exp1 = visited.getFirst();
92  assert(exp1);
93  exp1->accept(*this);
94 
95  te::fe::Expression* exp2 = visited.getSecond();
96  assert(exp2);
97  exp2->accept(*this);
98 
100 }
101 
103 {
104  // (???)
105 }
106 
108 {
109  m_writer->writeElement("ogc:Literal", visited.getValue());
110  //m_writer->writeValue(visited.getValue());
111 }
112 
114 {
115  m_writer->writeElement("ogc:PropertyName", visited.getName());
116 }
117 
119 
121  : m_writer(nullptr)
122 {
123  // Binary Operators
128 
129  // Functions (???)
130 
131  // Literal
133 
134  // PropertyName
136 }
137 
139 {
140  assert(reader.getNodeType() == te::xml::START_ELEMENT);
141  assert(reader.getElementLocalName() == te::fe::Globals::sm_add ||
145 
146  te::fe::BinaryOperator* op = nullptr;
149  else if(reader.getElementLocalName() == te::fe::Globals::sm_sub)
151  else if(reader.getElementLocalName() == te::fe::Globals::sm_mul)
154 
155  std::unique_ptr<te::fe::BinaryOperator> exp(op);
156 
157  reader.next();
158 
159  exp->setFirst(te::fe::serialize::Expression::getInstance().read(reader));
160 
161  exp->setSecond(te::fe::serialize::Expression::getInstance().read(reader));
162 
163  assert(reader.getNodeType() == te::xml::END_ELEMENT);
164  reader.next();
165 
166  return exp.release();
167 }
168 
170 {
171  assert(reader.getNodeType() == te::xml::START_ELEMENT);
172 
173  std::unique_ptr<te::fe::Literal> exp(new te::fe::Literal(""));
174 
175  if(reader.getElementLocalName() == "Literal")
176  {
177  reader.next();
178  if(reader.getNodeType() == te::xml::END_ELEMENT)
179  {
180  reader.next();
181  }
182  else
183  {
184  assert(reader.getNodeType() == te::xml::VALUE);
185  std::string value = reader.getElementValue();
186  exp->setValue(value);
187  reader.next();
188 
189  assert(reader.getNodeType() == te::xml::END_ELEMENT);
190  reader.next();
191  }
192 
193  return exp.release();
194  }
195 
196  // Else, no <Literal> tags
197  std::string value = reader.getElementValue();
198  exp->setValue(value);
199 
200  assert(reader.getNodeType() == te::xml::END_ELEMENT);
201  reader.next();
202 
203  return exp.release();
204 }
205 
207 {
208  assert(reader.getNodeType() == te::xml::START_ELEMENT);
209  assert(reader.getElementLocalName() == "PropertyName");
210 
211  reader.next();
212 
213  assert(reader.getNodeType() == te::xml::VALUE);
214  std::string value = reader.getElementValue();
215  std::unique_ptr<te::fe::PropertyName> exp(new te::fe::PropertyName(value));
216 
217  reader.next();
218 
219  assert(reader.getNodeType() == te::xml::END_ELEMENT);
220  reader.next();
221 
222  return exp.release();
223 }
const std::string & getName() const
It returns the property name.
boost::function< te::fe::Expression *(te::xml::Reader &)> ExpressionReadFnctType
static const char * sm_mul
This class models a XML reader object.
Definition: xml/Reader.h:55
virtual void writeStartElement(const std::string &qName)=0
void visit(const te::fe::BinaryOperator &visited)
Base exception class for plugin module.
te::fe::Expression * read(te::xml::Reader &reader) const
void write(const te::fe::Expression *exp, te::xml::AbstractWriter &writer)
virtual std::string getElementLocalName() const =0
It returns the local part of the element name in the case of an element node.
virtual ReturnType accept(VisitorType &guest) const =0
It call the visit method from the guest object.
This class models a XML writer object.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
static const char * sm_div
virtual void writeElement(const std::string &qName, const std::string &value)=0
A function is a named procedure that performs a distinct computation.
Definition: fe/Function.h:54
This class can be used to represent binary operation expressions.
static T & getInstance()
It returns a reference to the singleton instance.
Definition: Singleton.h:126
This class is used to encode the name of any property of an object.
This is an abstract class that models a Filter Encoding expression.
Definition: fe/Expression.h:50
Expression * getSecond() const
It returns the second operand.
Expression * getFirst() const
It returns the first operand.
This is an abstract class that models a Filter Encoding expression.
static const char * sm_add
This class can be used to represent literal values.
Definition: fe/Literal.h:56
static const char * sm_sub
void reg(const std::string &expName, const ExpressionReadFnctType &fnct)
virtual void writeEndElement(const std::string &qName)=0
te::fe::Expression * LiteralReader(te::xml::Reader &reader)
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.
const std::string & getValue() const
It returns the literal value.
Definition: fe/Literal.cpp:36
te::fe::Expression * BinaryOperatorReader(te::xml::Reader &reader)
const char * getName() const
It returns the operator name.
virtual bool next()=0
It gets the next event to be read.
te::fe::Expression * PropertyNameReader(te::xml::Reader &reader)