00001 #pragma once
00002 #ifndef XMLNODE_H
00003 #define XMLNODE_H
00004
00005 #include "Types.h"
00006 #include "Property.h"
00007 #include "AbstractPropertyContainer.h"
00008 #include "Util.h"
00009
00010 #include <iostream>
00011 #include <iomanip>
00012 #include <fstream>
00013 #include <map>
00014
00015 #ifdef RT_RAPIDXML
00016 using namespace rapidxml;
00017 #endif
00018
00019 namespace RenderTools {
00020
00021 class XMLNode : public enable_shared_from_this< XMLNode > {
00022 public:
00023 XMLNode( void );
00024 XMLNode( const string tagname );
00025 virtual ~XMLNode( void );
00026
00027 const string toString( void ) const;
00028 void load( const string value );
00029 bool save( const string filename );
00030 void setParent( XMLNodePtr parent );
00031 const XMLNodePtr addChild( const string name, const string value );
00032 const XMLNodePtr addChild( const string className );
00033 const XMLNodePtr addChild( XMLNodePtr child );
00034 void setTagName( const string name );
00035 const string getTagName( void ) const;
00036 bool hasChildren( void ) const;
00037 bool isParent( const XMLNodePtr xml ) const;
00038 bool isChild( const XMLNodePtr xml ) const;
00039 const XMLNodePtr getParent( void ) const;
00040 const XMLNodePtr getChild( const string name ) const;
00041 const XMLNodePtr nextChild( const XMLNodePtr previous ) const;
00042 XMLNodePtr nextChild( XMLNodePtr previous );
00043 const XMLNodePtr findRoot( void );
00044 const XMLNodePtr findByFullName( const string className, const string propertyName, const XMLNodePtr xml = XMLNodePtr() );
00045 const XMLNodePtr findByFullName( const string fullName, const XMLNodePtr xml = XMLNodePtr() );
00046 bool hasAttrib( const string name ) const;
00047 const string getFullName( void );
00048 static void setVerbose( bool state = true );
00049 static bool isVerbose( void );
00050
00051 template< class T >
00053 shared_ptr< T > getSharedPtr( void ){
00054 return( dynamic_pointer_cast< T, XMLNode >( shared_from_this() ) );
00055 }
00056
00057 template< class T >
00059 const shared_ptr< T > getSharedPtr( void ) const {
00060 return( const_cast< XMLNode * >( this )->getSharedPtr< T >() );
00061 }
00062
00063 template< class T >
00064 inline void setAttrib( const string name, const T value ){
00065 stringstream ss;
00066 ss << value;
00067 m_attributes[ cleanup( name ) ] = ss.str();
00068 }
00069
00070 template< class T >
00071 inline const T getAttrib( const string name ){
00072 string s = m_attributes[ cleanup( name ) ];
00073 stringstream ss( s );
00074 T t;
00075 ss >> t;
00076 return( t );
00077 }
00078
00079 template< class T >
00080 inline const T getChildValue( const string name ){
00081 XMLNodePtr child = getChild( name );
00082 if( child ){
00083 return( child->getAttrib<T>( "value" ) );
00084 }
00085 else{
00086 return( T() );
00087 }
00088 }
00089
00090 private:
00091 #ifdef RT_RAPIDXML
00092 void populate( const xml_node<> * node );
00093 #endif
00094 XMLNodeWeakPtr m_parent;
00095 string m_tagName;
00096 map<string, string> m_attributes;
00097 XMLNodeList m_children;
00098 static bool s_verbose;
00099 };
00100
00101 template<>
00102 inline void XMLNode::setAttrib<string>( const string name, const string value ){
00103 m_attributes[ cleanup( name ) ] = value;
00104 }
00105
00106 template<>
00107 inline const string XMLNode::getAttrib<string>( const string name ){
00108 return( m_attributes[ cleanup( name ) ] );
00109 }
00110
00111 };
00112
00113 #endif