All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Writer.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 // TerraLib
21 #include "Writer.h"
22 
23 // STL
24 #include <iosfwd>
25 #include <iostream>
26 
27 // Boost
28 #include <boost/lexical_cast.hpp>
29 
31  : m_ostr(0),
32  m_isOpened(false)
33 {
34  //m_ostr = new std::ofstream();//m_uri.c_str(), std::ios_base::trunc);
35  /*
36  std::ofstream fout(uri.c_str(), std::ios_base::trunc);
37 
38  te::xml::Writer w(fout);
39 
40  Project p(project);
41 
42  XMLFormatter::format(&p, true);
43 
44  Save(p, w);
45 
46  XMLFormatter::format(&p, false);
47 
48  fout.close();*/
49 }
50 
51 /*
52 te::xml::Writer::Writer(std::ostream& ostr)
53  : m_ostr(ostr),
54  m_isOpened(false)
55 {
56 }
57 */
58 
60 {
61  if(m_ostr)
62  delete m_ostr;
63 }
64 
65 void te::xml::Writer::writeStartDocument(const std::string& encoding, const std::string& standalone)
66 {
67  *m_ostr << "<?xml version=\"1.0\" encoding=\"" << encoding << "\" standalone=\"" << standalone << "\"?>";
68 }
69 
70 void te::xml::Writer::writeStartElement(const std::string& qName)
71 {
72  if(m_isOpened)
73  *m_ostr << ">";
74 
75  *m_ostr << "<" << qName;
76 
77  m_isOpened = true;
78 }
79 
80 std::string Function2Ascii(std::string funcName)
81 {
82  if(funcName == "<")
83  return "&#60;";
84  else if(funcName == ">")
85  return "&#62;";
86  else if(funcName == "<>")
87  return "&#60;&#62;";
88  else if(funcName == "<=")
89  return "&#60;=";
90  else if(funcName == ">=")
91  return "&#62;=";
92  else
93  return funcName;
94 }
95 
96 void te::xml::Writer::writeElement(const std::string& qName, const std::string& value)
97 {
98  std::string v = value;
99 
100  v = Function2Ascii(v);
101 
102  if(m_isOpened)
103  {
104  *m_ostr << ">";
105  m_isOpened = false;
106  }
107 
108  *m_ostr << "<" << qName << ">" << v << "</" << qName << ">";
109 }
110 
111 void te::xml::Writer::writeElement(const std::string& qName, const double& value)
112 {
113  writeElement(qName, boost::lexical_cast<std::string>(value));
114 }
115 
116 void te::xml::Writer::writeElement(const std::string& qName, boost::int32_t value)
117 {
118  writeElement(qName, boost::lexical_cast<std::string>(value));
119 }
120 
121 void te::xml::Writer::writeElement(const std::string& qName, boost::uint32_t value)
122 {
123  writeElement(qName, boost::lexical_cast<std::string>(value));
124 }
125 
126 void te::xml::Writer::writeElement(const std::string& qName, boost::int64_t value)
127 {
128  writeElement(qName, boost::lexical_cast<std::string>(value));
129 }
130 
131 void te::xml::Writer::writeElement(const std::string& qName, boost::uint64_t value)
132 {
133  writeElement(qName, boost::lexical_cast<std::string>(value));
134 }
135 
136 void te::xml::Writer::writeAttribute(const std::string& attName, const std::string& value)
137 {
138  *m_ostr << " " << attName << "=\"" << value << "\"";
139 }
140 
141 void te::xml::Writer::writeAttribute(const std::string& attName, const double& value)
142 {
143  writeAttribute(attName, boost::lexical_cast<std::string>(value));
144 }
145 
146 void te::xml::Writer::writeAttribute(const std::string& attName, boost::int32_t value)
147 {
148  writeAttribute(attName, boost::lexical_cast<std::string>(value));
149 }
150 
151 void te::xml::Writer::writeAttribute(const std::string& attName, boost::uint32_t value)
152 {
153  writeAttribute(attName, boost::lexical_cast<std::string>(value));
154 }
155 
156 void te::xml::Writer::writeAttribute(const std::string& attName, boost::int64_t value)
157 {
158  writeAttribute(attName, boost::lexical_cast<std::string>(value));
159 }
160 
161 void te::xml::Writer::writeAttribute(const std::string& attName, boost::uint64_t value)
162 {
163  writeAttribute(attName, boost::lexical_cast<std::string>(value));
164 }
165 
166 void te::xml::Writer::writeValue(const std::string& value)
167 {
168  if(m_isOpened)
169  {
170  *m_ostr << ">";
171  m_isOpened = false;
172  }
173 
174  *m_ostr << value;
175 }
176 
177 void te::xml::Writer::writeValue(const double& value)
178 {
179  writeValue(boost::lexical_cast<std::string>(value));
180 }
181 
182 void te::xml::Writer::writeValue(boost::int32_t value)
183 {
184  writeValue(boost::lexical_cast<std::string>(value));
185 }
186 
187 void te::xml::Writer::writeValue(boost::uint32_t value)
188 {
189  writeValue(boost::lexical_cast<std::string>(value));
190 }
191 
192 void te::xml::Writer::writeValue(boost::int64_t value)
193 {
194  writeValue(boost::lexical_cast<std::string>(value));
195 }
196 
197 void te::xml::Writer::writeValue(boost::uint64_t value)
198 {
199  writeValue(boost::lexical_cast<std::string>(value));
200 }
201 
202 void te::xml::Writer::writeEndElement(const std::string& qName)
203 {
204  if(m_isOpened)
205  {
206  *m_ostr << ">";
207  m_isOpened = false;
208  }
209 
210  *m_ostr << "</" << qName << ">";
211 }
212 
214 {
215 
216 }
void writeStartElement(const std::string &qName)
Definition: Writer.cpp:70
void writeStartDocument(const std::string &encoding, const std::string &standalone)
Definition: Writer.cpp:65
void writeAttribute(const std::string &attName, const std::string &value)
Definition: Writer.cpp:136
void writeValue(const std::string &value)
Definition: Writer.cpp:166
void writeElement(const std::string &qName, const std::string &value)
Definition: Writer.cpp:96
~Writer()
Constructor.
Definition: Writer.cpp:59
void writeEndElement(const std::string &qName)
Definition: Writer.cpp:202
std::string Function2Ascii(std::string funcName)
Definition: Writer.cpp:80
void writeToFile()
Definition: Writer.cpp:213
This class models a XML writer object.