xmlTag.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | foam-extend: Open Source CFD
4  \\ / O peration |
5  \\ / A nd | For copyright notice see file Copyright
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of foam-extend.
10 
11  foam-extend is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by the
13  Free Software Foundation, either version 3 of the License, or (at your
14  option) any later version.
15 
16  foam-extend is distributed in the hope that it will be useful, but
17  WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::xmlTag
26 
27 Description
28  Simple XML tag class allowing child tags and attributes. Specialized
29  output stream operators are provided to display or write the XML
30  structure.
31 
32 Author
33  Ivor Clifford <ivor.clifford@psi.ch>
34 
35 \*---------------------------------------------------------------------------*/
36 
37 #ifndef XMLtag_H
38 #define XMLtag_H
39 
40 #include "OStringStream.H"
41 #include "HashTable.H"
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 /*---------------------------------------------------------------------------*\
49  Class xmlTag Declaration
50 \*---------------------------------------------------------------------------*/
51 
52 class xmlTag
53 :
54  public OStringStream
55 {
56  // Private data
57 
58  //- Tag name
59  word name_;
60 
61  //- Attributes
63 
64  //- Child tags
66 
67 public:
68 
69  // Constructors
70 
71  //- Null constructor
72  xmlTag()
73  :
74  OStringStream(),
75  name_("unknown"),
76  attributes_(),
77  children_()
78  {}
79 
80  //- Construct given tag name
81  xmlTag(const word& name)
82  :
83  OStringStream(),
84  name_(name),
85  attributes_(),
86  children_()
87  {}
88 
89  //- Construct as copy
90  xmlTag(const xmlTag& tag)
91  :
92  OStringStream(tag),
93  name_(tag.name_),
95  children_(tag.children_)
96  {}
97 
98  //- Destructor
99  ~xmlTag()
100  {}
101 
102 
103  // Member Functions
104 
105  //- Add an attribute
106  template<class T>
107  void addAttribute(const keyType& key, const T& value)
108  {
109  OStringStream os;
110  os << value;
111  attributes_.insert(key, os.str());
112  };
113 
114  //- Add a fileName attribute
115  void addAttribute(const keyType& key, const fileName& value)
116  {
117  attributes_.insert(key, value);
118  };
119 
120  //- Add a string attribute
121  void addAttribute(const keyType& key, const string& value)
122  {
123  attributes_.insert(key, value);
124  };
125 
126  //- Add a word attribute
127  void addAttribute(const keyType& key, const word& value)
128  {
129  attributes_.insert(key, value);
130  };
131 
132  //- Add a child
133  xmlTag& addChild(const xmlTag& tag)
134  {
135  children_.append(tag);
136 
137  return children_[children_.size()-1];
138  };
139 
140  //- Create and add a new child
141  xmlTag& addChild(const word& name)
142  {
143  return addChild(xmlTag(name));
144  }
145 
146  // Member Operators
147 
148  void operator=(const xmlTag& tag)
149  {
150  name_ = tag.name_;
151  attributes_ = tag.attributes_;
152  children_ = tag.children_;
154  Foam::operator<<(*this, tag.str().c_str());
155  };
156 
157  // Friend IOstream Operators
158 
159  friend Ostream& operator<<(Ostream&, const xmlTag&);
160 
161  template<class Form, class Cmpt, int nCmpt>
163 
164  friend xmlTag& operator<<(xmlTag&, const labelledTri&);
165 
166  template<class T, unsigned Size>
167  friend xmlTag& operator<<(xmlTag&, const FixedList<T, Size>&);
168 
169  template<class T>
170  friend xmlTag& operator<<(xmlTag&, const LongList<T>&);
171 
172  template<class T>
173  friend xmlTag& operator<<(xmlTag&, const UList<T>&);
174 };
175 
176 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
177 
178 //- Write the tag in XML format to the supplied output stream
179 Ostream& operator<<(Ostream& os, const xmlTag& tag)
180 {
181  // Tag name
182  os << indent << '<' << tag.name_;
183 
184  // Attributes and text
185  for
186  (
188  iter != tag.attributes_.cend();
189  ++iter
190  )
191  {
192  os << token::SPACE << iter.key() << '=' << iter();
193  }
194 
195  if (tag.str().size() || tag.children_.size())
196  {
197  os << '>' << nl;
198 
199  // Children
200  os.incrIndent();
201 
202  forAll(tag.children_, i)
203  {
204  os << tag.children_[i];
205  }
206 
207  os.decrIndent();
208 
209  // Tag text
210  os << tag.str().c_str();
211 
212  // Close tag
213  os << indent << "</" << tag.name_ << '>' << endl;
214  }
215  else
216  {
217  // Empty element tag
218  os << "/>" << endl;
219  }
220  return os;
221 }
222 
223 
224 //- Append the supplied data to the tag text
225 template<class T>
226 xmlTag& operator<<(xmlTag& tag, const UList<T>& data)
227 {
228  forAll(data, i)
229  {
230  tag << data[i] << token::SPACE;
231  }
232 
233  tag << nl;
234 
235  return tag;
236 }
237 
238 //- Append the supplied data to the tag text
239 template<class T>
240 xmlTag& operator<<(xmlTag& tag, const LongList<T>& data)
241 {
242  forAll(data, i)
243  {
244  tag << data[i] << token::SPACE;
245  }
246 
247  tag << nl;
248 
249  return tag;
250 }
251 
252 
253 //- Append the supplied data to the tag text
254 template<class Form, class Cmpt, int nCmpt>
255 xmlTag& operator<<(xmlTag& tag, const VectorSpace<Form, Cmpt, nCmpt>& data)
256 {
257  forAll(data, i)
258  {
259  tag << data[i] << token::SPACE;
260  }
261 
262  tag << nl;
263 
264  return tag;
265 }
266 
267 //- Append the supplied data to the tag text
268 template<class T, unsigned Size>
269 xmlTag& operator<<(xmlTag& tag, const FixedList<T, Size>& data)
270 {
271  forAll(data, i)
272  {
273  tag << data[i] << token::SPACE;
274  }
275 
276  tag << nl;
277 
278  return tag;
279 }
280 
281 //- Append the supplied data to the tag text
282 xmlTag& operator<<(xmlTag& tag, const labelledTri& data)
283 {
284  const triFace& tFace = data;
285 
286  return tag << tFace;
287 }
288 
289 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
290 
291 } // End namespace Foam
292 
293 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
294 
295 #endif
296 
297 // ************************************************************************* //
Foam::xmlTag::addChild
xmlTag & addChild(const word &name)
Create and add a new child.
Definition: xmlTag.H:140
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
HashTable.H
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:56
Foam::xmlTag::addChild
xmlTag & addChild(const xmlTag &tag)
Add a child.
Definition: xmlTag.H:132
Foam::xmlTag::addAttribute
void addAttribute(const keyType &key, const word &value)
Add a word attribute.
Definition: xmlTag.H:126
Foam::HashTable::const_iterator
An STL-conforming const_iterator.
Definition: HashTable.H:470
Foam::xmlTag::xmlTag
xmlTag()
Null constructor.
Definition: xmlTag.H:71
Foam::OStringStream::str
string str() const
Return the string.
Definition: OStringStream.H:107
Foam::xmlTag::addAttribute
void addAttribute(const keyType &key, const string &value)
Add a string attribute.
Definition: xmlTag.H:120
Foam::VectorSpace
Templated vector space.
Definition: VectorSpace.H:52
Foam::LongList< T >
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:56
Foam::xmlTag::~xmlTag
~xmlTag()
Destructor.
Definition: xmlTag.H:98
Foam::OSstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: OSstream.H:88
OStringStream.H
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:55
Foam::xmlTag
Simple XML tag class allowing child tags and attributes. Specialized output stream operators are prov...
Definition: xmlTag.H:51
Foam::xmlTag::operator=
void operator=(const xmlTag &tag)
Definition: xmlTag.H:147
Foam::xmlTag::xmlTag
xmlTag(const xmlTag &tag)
Construct as copy.
Definition: xmlTag.H:89
Foam::operator<<
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:130
Foam::xmlTag::xmlTag
xmlTag(const word &name)
Construct given tag name.
Definition: xmlTag.H:80
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::indent
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:221
Foam::xmlTag::name_
word name_
Tag name.
Definition: xmlTag.H:58
Foam::HashTable
An STL-conforming hash table.
Definition: HashTable.H:61
Foam::OStringStream
Output to memory buffer stream.
Definition: OStringStream.H:49
Foam::FixedList
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:53
Foam::UList< T >
Foam::OStringStream::rewind
void rewind()
Rewind the OStringStream.
Definition: OStringStream.H:119
Foam::labelledTri
Triangle with additional region number.
Definition: labelledTri.H:49
Foam::xmlTag::children_
DynamicList< xmlTag > children_
Child tags.
Definition: xmlTag.H:64
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::xmlTag::operator<<
friend Ostream & operator<<(Ostream &, const xmlTag &)
Write the tag in XML format to the supplied output stream.
Definition: xmlTag.H:178
Foam::xmlTag::attributes_
HashTable< string > attributes_
Attributes.
Definition: xmlTag.H:61
Foam::xmlTag::addAttribute
void addAttribute(const keyType &key, const fileName &value)
Add a fileName attribute.
Definition: xmlTag.H:114
Foam::token::SPACE
@ SPACE
Definition: token.H:95
Foam::xmlTag::addAttribute
void addAttribute(const keyType &key, const T &value)
Add an attribute.
Definition: xmlTag.H:106