00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef _OSBXML_H_
00031 #define _OSBXML_H_
00032
00033
00034
00035
00036 #include <string>
00037 #include <iosfwd>
00038 #include <stack>
00039
00040
00041 #include "osberror.h"
00042
00043
00044
00045 namespace OSB_LIB {
00046 class OsbXml {
00047 public:
00048
00049 OsbXml(std::ostream&);
00050
00051 OsbXml(std::ostream&, std::string);
00052
00053 OsbXml(std::ostream&, std::string, std::string);
00054
00055 ~OsbXml() { endRoot(); }
00056
00057
00058
00059
00060 void addElement(std::string);
00061
00062
00063 template <typename T>
00064 void addElement(std::string, const T&);
00065
00066
00067 template <typename T>
00068 void addElement(std::string, std::string, const T&);
00069
00070
00071
00072 template <typename T1, typename T2>
00073 void addElement(std::string, const T1&, std::string, const T2&);
00074
00075
00076 void addRoot(std::string);
00077
00078
00079 template <typename T>
00080 void addRoot(std::string, std::string, const T&);
00081
00082
00083 void addChild(std::string);
00084
00085
00086 template <typename T>
00087 void addChild(std::string, std::string, const T&);
00088
00089 void endChild();
00090 void endRoot();
00091 void addComment(std::string comment);
00092
00093 void setIndent(std::string newIndent);
00094
00095 private:
00096 std::stack<std::string> stack_;
00097 bool rootCreated_;
00098 std::ostream& xml_;
00099
00100 std::string indent_;
00101 std::string currIndent_;
00102 int indentLevel_;
00103
00104 };
00105
00106
00107
00108
00109 inline void OsbXml::addElement(std::string name)
00110 {
00111 if (!rootCreated_) throw DefException(28) << name;
00112 xml_ << currIndent_;
00113 xml_ << "<" << name << "/>\n";
00114 }
00115
00116 template <typename T>
00117 void OsbXml::addElement(std::string name, const T& value)
00118 {
00119 if (!rootCreated_) throw DefException(28) << name;
00120 xml_ << currIndent_;
00121 xml_ << "<" << name << ">" << value << "</" << name << ">\n";
00122 }
00123
00124 template <typename T>
00125 void OsbXml::addElement(std::string name, std::string attrName,
00126 const T& attrValue)
00127 {
00128 if (!rootCreated_) throw DefException(28) << name;
00129 xml_ << currIndent_;
00130 xml_ << "<" << name << " " << attrName << "=\""
00131 << attrValue << "\"/>\n";
00132 }
00133
00134 template <typename T1, typename T2>
00135 void OsbXml::addElement(std::string name, const T1& value,
00136 std::string attrName, const T2& attrValue)
00137 {
00138 if (!rootCreated_) throw DefException(28) << name;
00139 xml_ << currIndent_;
00140 xml_ << "<" << name << " " << attrName << "=\""
00141 << attrValue << "\">" << value << "</" << name << ">\n";
00142 }
00143
00144 inline void OsbXml::addChild(std::string name)
00145 {
00146 xml_ << currIndent_;
00147 xml_ << "<" << name << ">\n";
00148 stack_.push(name);
00149 currIndent_ += indent_;
00150 ++indentLevel_;
00151 rootCreated_ = true;
00152 }
00153
00154 template <typename T>
00155 void OsbXml::addChild(std::string name,
00156 std::string attrName, const T& attrValue)
00157 {
00158 xml_ << currIndent_;
00159 xml_ << "<" << name << " " << attrName << "=\""
00160 << attrValue << "\">\n";
00161 stack_.push(name);
00162 currIndent_ += indent_;
00163 ++indentLevel_;
00164 rootCreated_ = true;
00165 }
00166
00167 inline void OsbXml::addRoot(std::string name)
00168 {
00169 if (rootCreated_) throw DefException(37) << name;
00170 addChild(name);
00171 }
00172
00173 template <typename T>
00174 void OsbXml::addRoot(std::string name,
00175 std::string attrName, const T& attrValue)
00176 {
00177 if (rootCreated_) throw DefException(37) << name;
00178 addChild(name, attrName, attrValue);
00179 }
00180
00181 }
00182 #endif // #ifndef _OSBXML_H_