CLAM-Development  1.4.0
XmlWriteContext.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #ifndef __XMLWRITECONTEXT_HXX__
23 #define __XMLWRITECONTEXT_HXX__
24 
25 #include <ostream>
26 #include <string>
27 #include "Assert.hxx"
28 
29 namespace CLAM
30 {
44 {
45  public:
46  XmlWriteContext(std::ostream & targetStream) :
47  mTarget(targetStream)
48  {
49  CLAM_ASSERT(!sCurrentContext,
50  "An XML generation context is already defined");
51  sCurrentContext = this;
52  mLastWasAContent=false;
53  mElementInserted=false;
54  mTagOpened=false;
55  mCurrentLevel = 0;
56  mUseIndentation=false;
57  }
58 
60  {
61  sCurrentContext = 0;
62  }
63 
65  {
66  CLAM_ASSERT(sCurrentContext,"XML generation context not created");
67  return *sCurrentContext;
68  }
69 
70  std::ostream & GetTarget()
71  {
72  return mTarget;
73  }
74 
75  template <typename T>
76  void InsertContent(const T & content)
77  {
78  if (mTagOpened) EndOpenTag();
79  if (mElementInserted)
80  NewIndentedLineIfEnabled();
81  mTarget << content;
82  mLastWasAContent = true;
83  }
84 
85  void OpenElement(const std::string & name)
86  {
87  if (mTagOpened) EndOpenTag();
88  NewIndentedLineIfEnabled();
89  // TODO: Change the temporary to normal insertion with the upgrade to 0.5.2
90  mTarget << std::string("<") << name;
91  mTagOpened = true;
92  mElementInserted = false;
93  mLastWasAContent = false;
94  mCurrentLevel++;
95  }
96 
97  void CloseElement(const std::string & name)
98  {
99  mCurrentLevel--;
100  if (mLastWasAContent || mElementInserted)
101  CloseFullElement(name);
102  else
103  CloseEmptyElement();
104  mElementInserted = true;
105  mLastWasAContent = false;
106  }
107 
108  template <typename T>
109  void InsertAttribute(const std::string &name, const T & content)
110  {
111  CLAM_ASSERT(mTagOpened, "Appending attribute outside a tag");
112  mTarget << std::string(" ") << name << std::string("='")
113  << content << std::string("'");
114  }
115 
121  {
122  return mLastWasAContent;
123  }
124 
129  {
130  return mElementInserted;
131  }
132 
133  unsigned int CurrentLevel()
134  {
135  return mCurrentLevel;
136  }
137 
138  void UseIndentation(bool useIt)
139  {
140  mUseIndentation = useIt;
141  }
142 
143  private:
144 
145  void CloseFullElement(const std::string & name)
146  {
147  if (mElementInserted)
148  NewIndentedLineIfEnabled();
149  // TODO: Change the temporary to normal insertion with the upgrade to 0.5.2
150  mTarget << std::string("</") << name << std::string(">");
151  }
152 
153  void CloseEmptyElement()
154  {
155  CLAM_ASSERT(mTagOpened,"No open tag to close");
156  // TODO: Change the temporary to normal insertion with the upgrade to 0.5.2
157  mTarget << std::string(" />");
158  mTagOpened = false;
159  }
160 
161  void EndOpenTag()
162  {
163  CLAM_ASSERT(mTagOpened,"No open tag to close");
164  // TODO: Change the temporary to normal insertion with the upgrade to 0.5.2
165  mTarget << std::string(">");
166  mTagOpened = false;
167  }
168 
169  void NewIndentedLineIfEnabled()
170  {
171  if (!mUseIndentation) return;
172  // TODO: Change the temporary to normal insertion with the upgrade to 0.5.2
173  if (mCurrentLevel!=0 || mLastWasAContent || mElementInserted)
174  mTarget << std::string("\n");
175  mTarget << std::string(mCurrentLevel,'\t');
176  }
177 
178  private:
179 
180  std::ostream & mTarget;
181  bool mLastWasAContent;
182  bool mElementInserted;
183  bool mTagOpened;
184  bool mUseIndentation;
185  unsigned int mCurrentLevel;
186 
187  static XmlWriteContext * sCurrentContext;
188 
189 };
190 
191 } // ns CLAM
192 
193 #endif//__XMLWRITECONTEXT_HXX__
194