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 QtXmlDocument::QtXmlDocument ( QDomDocument document ) 00024 : m_document ( document ) 00025 { 00026 } 00027 00028 QtXmlDocument:: 00029 QtXmlDocument ( const std::string & name ) 00030 { 00031 m_document = QDomDocument ( name.c_str () ); 00032 } 00033 00034 /* virtual */ 00035 XmlElement * QtXmlDocument::documentElement ( ) const 00036 { 00037 QDomElement root = m_document.documentElement (); 00038 00039 return new QtXmlElement ( root ); 00040 } 00041 00042 /* virtual */ 00043 XmlElement * 00044 QtXmlDocument:: 00045 createElement ( const std::string & tagName ) 00046 { 00047 QDomElement element = m_document.createElement ( tagName.c_str() ); 00048 00049 return new QtXmlElement ( element ); 00050 } 00051 00052 XmlTextNode * 00053 QtXmlDocument:: 00054 createTextNode ( const std::string & tag ) 00055 { 00056 QDomText node = m_document.createTextNode ( tag.c_str () ); 00057 00058 return new QtXmlTextNode ( node ); 00059 } 00060 00061 /* virtual */ 00062 void QtXmlDocument::appendChild ( XmlElement & child ) 00063 { 00064 const QtXmlElement & qtelem 00065 = dynamic_cast < const QtXmlElement & > ( child ); 00066 00067 m_document.appendChild ( *qtelem.m_node ); 00068 } 00069 00070 XmlDocument::Status 00071 QtXmlDocument:: 00072 saveToFile ( const std::string & filename ) 00073 { 00074 QFile filedev ( filename.c_str() ); 00075 00076 bool ok = filedev.open ( IO_WriteOnly ); 00077 if ( ! ok ) { 00078 return WriteError; 00079 } 00080 00081 QTextStream ts ( &filedev ); 00082 m_document.save ( ts, 2 ); 00083 filedev.close (); 00084 00085 return Success; 00086 } 00087 00088 XmlDocument::Status 00089 QtXmlDocument:: 00090 setContent ( const std::string & filename ) 00091 { 00092 QFile file ( filename.c_str() ); 00093 bool ok = file.open ( IO_ReadOnly ); 00094 if ( ! ok ) { 00095 file.close (); 00096 return OpenError; // 00097 } 00098 ok = m_document.setContent ( &file ); 00099 if ( ! ok ) { 00100 file.close (); 00101 return ParseError; 00102 } 00103 00104 QFileInfo info ( file ); 00105 QString dir = info.dirPath(); 00106 QDir::setCurrent ( dir ); 00107 00108 file.close (); 00109 00110 return Success; 00111 }