src/Env/Param/ParamXMLPrinter.h

説明を見る。
00001 // 
00002 // Copyright (c) 2005-2008 Kenichi Watanabe.
00003 // Copyright (c) 2005-2008 Yasuhiro Watari.
00004 // Copyright (c) 2005-2008 Hironori Ichibayashi.
00005 // Copyright (c) 2008-2009 Kazuo Horio.
00006 // Copyright (c) 2009-2013 Naruki Kurata.
00007 // Copyright (c) 2005-2013 Ryota Shioya.
00008 // Copyright (c) 2005-2013 Masahiro Goshima.
00009 // 
00010 // This software is provided 'as-is', without any express or implied
00011 // warranty. In no event will the authors be held liable for any damages
00012 // arising from the use of this software.
00013 // 
00014 // Permission is granted to anyone to use this software for any purpose,
00015 // including commercial applications, and to alter it and redistribute it
00016 // freely, subject to the following restrictions:
00017 // 
00018 // 1. The origin of this software must not be misrepresented; you must not
00019 // claim that you wrote the original software. If you use this software
00020 // in a product, an acknowledgment in the product documentation would be
00021 // appreciated but is not required.
00022 // 
00023 // 2. Altered source versions must be plainly marked as such, and must not be
00024 // misrepresented as being the original software.
00025 // 
00026 // 3. This notice may not be removed or altered from any source
00027 // distribution.
00028 // 
00029 // 
00030 
00031 
00032 //
00033 // Custom XML printer for tiny XML
00034 //
00035 // Modified the original 'TiXmlPrinter' to insert a line break for each attribute.
00036 // # Do not inherit the original 'TiXmlPrinter'@because all member
00037 //   values are private and cannot use those.
00038 //
00039 
00040 #ifndef ENV_PARAM_PARAM_XML_PRINTER_H
00041 #define ENV_PARAM_PARAM_XML_PRINTER_H
00042 
00043 namespace Onikiri
00044 {
00045     class ParamXMLPrinter : public TiXmlVisitor
00046     {
00047 
00048     public:
00049 
00050         ParamXMLPrinter()
00051         {
00052             m_simpleTextPrint = false;
00053             m_indetStr = "  ";
00054             m_depth = 0;
00055         }
00056 
00057         const char* CStr() const
00058         {
00059             return m_buffer.c_str();
00060         }
00061 
00062         void IncDepth()
00063         {
00064             m_depth++;
00065         }
00066 
00067         void DecDepth()
00068         {
00069             m_depth--;
00070         }
00071 
00072         void Indent()
00073         {
00074             for( int i = 0; i < m_depth; i++ ){
00075                 m_buffer += m_indetStr;
00076             }
00077         }
00078 
00079         void LineBreak()
00080         {
00081             m_buffer += "\n";
00082         }
00083 
00084         bool VisitEnter( const TiXmlDocument& )
00085         {
00086             return true;
00087         }
00088 
00089         bool VisitExit( const TiXmlDocument& )
00090         {
00091             return true;
00092         }
00093 
00094         bool VisitEnter( const TiXmlElement& element, const TiXmlAttribute* firstAttribute )
00095         {
00096             Indent();
00097             m_buffer += "<";
00098             m_buffer += element.Value();
00099 #if 0
00100             for( const TiXmlAttribute* attrib = firstAttribute; attrib; attrib = attrib->Next() ){
00101                 m_buffer += " ";
00102                 attrib->Print( 0, 0, &m_buffer );
00103             }
00104 #else
00105             IncDepth(); 
00106             for( const TiXmlAttribute* attrib = firstAttribute; attrib; attrib = attrib->Next() ){
00107                 LineBreak();
00108                 Indent();
00109                 attrib->Print( 0, 0, &m_buffer );
00110             }
00111             DecDepth(); 
00112             if( firstAttribute ){
00113                 LineBreak();
00114                 Indent();
00115             }
00116 #endif
00117             if( !element.FirstChild() ){
00118                 m_buffer += " />";
00119                 LineBreak();
00120             }
00121             else{
00122                 m_buffer += ">";
00123                 if( element.FirstChild()->ToText() &&
00124                     element.LastChild() == element.FirstChild() && 
00125                     element.FirstChild()->ToText()->CDATA() == false 
00126                 ){
00127                         m_simpleTextPrint = true;
00128                 }
00129                 else{
00130                     LineBreak();
00131                 }
00132             }
00133             IncDepth(); 
00134             return true;
00135         }
00136 
00137 
00138         bool VisitExit( const TiXmlElement& element )
00139         {
00140             DecDepth();
00141             if( !element.FirstChild() ){
00142                 // nothing.
00143             }
00144             else{
00145                 if( m_simpleTextPrint ){
00146                     m_simpleTextPrint = false;
00147                 }
00148                 else{
00149                     Indent();
00150                 }
00151                 m_buffer += "</";
00152                 m_buffer += element.Value();
00153                 m_buffer += ">";
00154                 LineBreak();
00155             }
00156             return true;
00157         }
00158 
00159 
00160         bool Visit( const TiXmlText& text )
00161         {
00162             if( text.CDATA() ){
00163                 Indent();
00164                 m_buffer += "<![CDATA[";
00165                 m_buffer += text.Value();
00166                 m_buffer += "]]>";
00167                 LineBreak();
00168             }
00169             else if( m_simpleTextPrint ){
00170                 //TIXML_STRING str;
00171                 //TiXmlBase::EncodeString( text.ValueTStr(), &str );
00172 
00173                 // Text data is output as raw data.
00174                 m_buffer += text.ValueTStr();
00175             }
00176             else{
00177                 Indent();
00178                 TIXML_STRING str;
00179                 TiXmlBase::EncodeString( text.ValueTStr(), &str );
00180                 m_buffer += str;
00181                 LineBreak();
00182             }
00183             return true;
00184         }
00185 
00186 
00187         bool Visit( const TiXmlDeclaration& declaration )
00188         {
00189             Indent();
00190             declaration.Print( 0, 0, &m_buffer );
00191             LineBreak();
00192             return true;
00193         }
00194 
00195 
00196         bool Visit( const TiXmlComment& comment )
00197         {
00198             Indent();
00199             m_buffer += "<!--";
00200             m_buffer += comment.Value();
00201             m_buffer += "-->";
00202             LineBreak();
00203             return true;
00204         }
00205 
00206 
00207         bool Visit( const TiXmlUnknown& unknown )
00208         {
00209             Indent();
00210             m_buffer += "<";
00211             m_buffer += unknown.Value();
00212             m_buffer += ">";
00213             LineBreak();
00214             return true;
00215         }
00216 
00217     protected:
00218         TIXML_STRING m_buffer;
00219         TIXML_STRING m_indetStr;
00220         int m_depth;
00221         bool m_simpleTextPrint;
00222 
00223     };
00224 };
00225 
00226 #endif
00227 

Onikiri2に対してTue Jun 18 14:34:21 2013に生成されました。  doxygen 1.4.7