All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "../../../common/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 
89  m_writer->writeStartElement(name);
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 
99  m_writer->writeEndElement(name);
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 {
120 }
121 
123  : m_writer(0)
124 {
125  // Binary Operators
130 
131  // Functions (???)
132 
133  // Literal
135 
136  // PropertyName
138 }
139 
141 {
142  assert(reader.getNodeType() == te::xml::START_ELEMENT);
143  assert(reader.getElementLocalName() == te::fe::Globals::sm_add ||
147 
148  te::fe::BinaryOperator* op = 0;
151  else if(reader.getElementLocalName() == te::fe::Globals::sm_sub)
153  else if(reader.getElementLocalName() == te::fe::Globals::sm_mul)
156 
157  std::auto_ptr<te::fe::BinaryOperator> exp(op);
158 
159  reader.next();
160 
161  exp->setFirst(te::fe::serialize::Expression::getInstance().read(reader));
162 
163  exp->setSecond(te::fe::serialize::Expression::getInstance().read(reader));
164 
165  assert(reader.getNodeType() == te::xml::END_ELEMENT);
166  reader.next();
167 
168  return exp.release();
169 }
170 
172 {
173  assert(reader.getNodeType() == te::xml::START_ELEMENT);
174 
175  std::auto_ptr<te::fe::Literal> exp(new te::fe::Literal(""));
176 
177  if(reader.getElementLocalName() == "Literal")
178  {
179  reader.next();
180  if(reader.getNodeType() == te::xml::END_ELEMENT)
181  {
182  reader.next();
183  }
184  else
185  {
186  assert(reader.getNodeType() == te::xml::VALUE);
187  std::string value = reader.getElementValue();
188  exp->setValue(value);
189  reader.next();
190 
191  assert(reader.getNodeType() == te::xml::END_ELEMENT);
192  reader.next();
193  }
194 
195  return exp.release();
196  }
197 
198  // Else, no <Literal> tags
199  std::string value = reader.getElementValue();
200  exp->setValue(value);
201 
202  assert(reader.getNodeType() == te::xml::END_ELEMENT);
203  reader.next();
204 
205  return exp.release();
206 }
207 
209 {
210  assert(reader.getNodeType() == te::xml::START_ELEMENT);
211  assert(reader.getElementLocalName() == "PropertyName");
212 
213  reader.next();
214 
215  assert(reader.getNodeType() == te::xml::VALUE);
216  std::string value = reader.getElementValue();
217  std::auto_ptr<te::fe::PropertyName> exp(new te::fe::PropertyName(value));
218 
219  reader.next();
220 
221  assert(reader.getNodeType() == te::xml::END_ELEMENT);
222  reader.next();
223 
224  return exp.release();
225 }
const std::string & getName() const
It returns the property name.
boost::function< te::fe::Expression *(te::xml::Reader &)> ExpressionReadFnctType
Definition: Expression.h:61
static const char * sm_mul
Definition: Globals.h:69
ExpressionFnctIdxType m_fncts
Definition: Expression.h:105
This class models a XML reader object.
Definition: Reader.h:55
void visit(const te::fe::BinaryOperator &visited)
Definition: Expression.cpp:84
te::fe::Expression * read(te::xml::Reader &reader) const
Definition: Expression.cpp:58
void write(const te::fe::Expression *exp, te::xml::AbstractWriter &writer)
Definition: Expression.cpp:73
This class models a XML writer object.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
virtual std::string getElementLocalName() const =0
It returns the local part of the element name in the case of an element node.
static const char * sm_div
Definition: Globals.h:70
A function is a named procedure that performs a distinct computation.
Definition: 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:120
This class is used to encode the name of any property of an object.
Definition: PropertyName.h:54
This is an abstract class that models a Filter Encoding expression.
Definition: 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
Definition: Globals.h:67
This class can be used to represent literal values.
Definition: Literal.h:56
static const char * sm_sub
Definition: Globals.h:68
virtual ReturnType accept(VisitorType &guest) const =0
It call the visit method from the guest object.
virtual NodeType getNodeType() const =0
It return the type of node read.
void reg(const std::string &expName, const ExpressionReadFnctType &fnct)
Definition: Expression.cpp:53
virtual std::string getElementValue() const =0
It returns the element data value in the case of VALUE node.
te::fe::Expression * LiteralReader(te::xml::Reader &reader)
Definition: Expression.cpp:171
const std::string & getValue() const
It returns the literal value.
Definition: Literal.cpp:38
te::fe::Expression * BinaryOperatorReader(te::xml::Reader &reader)
Definition: Expression.cpp:140
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)
Definition: Expression.cpp:208