00001 00012 #include "QtXmlDocument.h" 00013 #include "QtXmlElement.h" 00014 #include "QtXmlTextNode.h" 00015 00016 #include <qdir.h> 00017 #include <qfile.h> 00018 #include <qfileinfo.h> 00019 #include <qtextstream.h> 00020 00021 using std::string; 00022 00023 namespace hippodraw { 00024 00025 QtXmlDocument::QtXmlDocument ( QDomDocument document ) 00026 : m_document ( document ) 00027 { 00028 } 00029 00030 QtXmlDocument:: 00031 QtXmlDocument ( const std::string & name ) 00032 { 00033 m_document = QDomDocument ( name.c_str () ); 00034 } 00035 00036 QtXmlDocument:: 00037 ~QtXmlDocument () 00038 { 00039 } 00040 00041 XmlElement * QtXmlDocument::documentElement ( ) const 00042 { 00043 QDomElement root = m_document.documentElement (); 00044 00045 return new QtXmlElement ( root ); 00046 } 00047 00048 XmlElement * 00049 QtXmlDocument:: 00050 createElement ( const std::string & tagName ) 00051 { 00052 QDomElement element = m_document.createElement ( tagName.c_str() ); 00053 00054 return new QtXmlElement ( element ); 00055 } 00056 00057 XmlTextNode * 00058 QtXmlDocument:: 00059 createTextNode ( const std::string & tag ) 00060 { 00061 QDomText node = m_document.createTextNode ( tag.c_str () ); 00062 00063 return new QtXmlTextNode ( node ); 00064 } 00065 00066 /* virtual */ 00067 void QtXmlDocument::appendChild ( XmlElement & child ) 00068 { 00069 const QtXmlElement & qtelem 00070 = dynamic_cast < const QtXmlElement & > ( child ); 00071 00072 m_document.appendChild ( qtelem.m_node ); 00073 } 00074 00075 XmlDocument::Status 00076 QtXmlDocument:: 00077 saveToFile ( const std::string & filename ) 00078 { 00079 QFile filedev ( filename.c_str() ); 00080 00081 #if QT_VERSION < 0x040000 00082 bool ok = filedev.open ( IO_WriteOnly ); 00083 #else 00084 bool ok = filedev.open ( QIODevice::WriteOnly ); 00085 #endif 00086 00087 if ( ! ok ) { 00088 return WriteError; 00089 } 00090 00091 QTextStream ts ( &filedev ); 00092 m_document.save ( ts, 2 ); 00093 filedev.close (); 00094 00095 return Success; 00096 } 00097 00098 XmlDocument::Status 00099 QtXmlDocument:: 00100 setContent ( const std::string & filename ) 00101 { 00102 QFile file ( filename.c_str() ); 00103 #if QT_VERSION < 0x040000 00104 bool ok = file.open ( IO_ReadOnly ); 00105 #else 00106 bool ok = file.open ( QIODevice::ReadOnly ); 00107 #endif 00108 00109 if ( ! ok ) { 00110 file.close (); 00111 return OpenError; // 00112 } 00113 ok = m_document.setContent ( &file ); 00114 if ( ! ok ) { 00115 file.close (); 00116 return ParseError; 00117 } 00118 00119 QFileInfo info ( file ); 00120 #if QT_VERSION < 0x040000 00121 QString dir = info.dirPath(); 00122 #else 00123 QString dir = info.path(); 00124 #endif 00125 QDir::setCurrent ( dir ); 00126 00127 file.close (); 00128 00129 return Success; 00130 } 00131 00132 } // namespace hippodraw 00133