osbxml.h

Go to the documentation of this file.
00001 // OSB library ************************************************ -*- C++ -*-
00006 /*
00007   AUTHOR(S): Ramlan Rusdijanto (rr)
00008 
00009   RCS information
00010    $Name: OSB_060808 $
00011    $Revision: 1.21 $
00012 
00013   License
00014    OSB rating and billing library for communication networks
00015    Copyright (C) 2004, 2005, 2006  OSB systems
00016 
00017    This file may be distributed and/or modify under the terms of the
00018    GNU General Public License (GPL) as published by the Free Software
00019    Foundation which is provided in the file LICENSE.GPL included in the
00020    packaging of this file.
00021 
00022    The file is distributed in the hope that it will be useful, but WITHOUT
00023    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00024    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00025    for more details.
00026 
00027    Holders of a OSB Commercial License may use this file under the terms
00028    and conditions of this commercial license.
00029  */
00030 #ifndef _OSBXML_H_
00031 #define _OSBXML_H_
00032 
00033 // *********************************************************************
00034 // included header files
00035 // + standard includes
00036 #include <string>
00037 #include <iosfwd>
00038 #include <stack>
00039 
00040 // + local includes
00041 #include "osberror.h"
00042 
00043 // *********************************************************************
00044 // namespace extensions
00045 namespace OSB_LIB {
00046     class OsbXml {
00047     public:
00048         // Args: output destination
00049         OsbXml(std::ostream&);
00050         // Args: output destination, root element name
00051         OsbXml(std::ostream&, std::string);
00052         // Args: output destination, root element name, indentation
00053         OsbXml(std::ostream&, std::string, std::string);
00054 
00055         ~OsbXml() { endRoot(); }
00056 
00057         // ToDo: It should be possible to have more than one attribute
00058 
00059         // Arg: element name
00060         void addElement(std::string);
00061 
00062         // Args: element name, element value
00063         template <typename T>
00064           void addElement(std::string, const T&);
00065 
00066         // Args: element name, attribute name, attribute value
00067         template <typename T>
00068           void addElement(std::string, std::string, const T&);
00069 
00070         // Args: element name, element value,
00071         //       attribute name, attribute value
00072         template <typename T1, typename T2>
00073           void addElement(std::string, const T1&, std::string, const T2&);
00074 
00075         // Arg: root element name
00076         void addRoot(std::string);
00077 
00078         // Args: root element name, attribute name, attribute value
00079         template <typename T>
00080           void addRoot(std::string, std::string, const T&);
00081 
00082         // Arg: element name
00083         void addChild(std::string);
00084 
00085         // Args: element name, attribute name, attribute value
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_;            // Indentation string
00101         std::string currIndent_;        // Indentation string repeated
00102         int indentLevel_;               // Current indentation level
00103 
00104     };
00105 
00106 // *********************************************************************
00107 // inline and template definitions
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 }                                       // namespace OSB_LIB
00182 #endif                                  // #ifndef _OSBXML_H_

Generated on Sat Sep 2 14:06:33 2006 for OSB Library by  doxygen 1.4.7