00001 #include "xmlserializeable.h" 00002 #include <iostream> 00003 #include <iomanip> 00004 00005 #include <qmessagebox.h> 00006 00007 bool XMLSerializeable::writeTextTag( QDomDocument &doc, 00008 QDomElement &elem, 00009 std::string tagName, 00010 std::string text, 00011 std::string id ) { 00012 QDomElement newElem = doc.createElement( tagName.c_str() ); 00013 if ( id != "" ) 00014 newElem.setAttribute( "id", (char*)id.c_str() ); 00015 elem.appendChild( newElem ); 00016 QDomText textNode = doc.createTextNode( text.c_str() ); 00017 newElem.appendChild( textNode ); 00018 return true; 00019 } 00020 00021 std::string XMLSerializeable::readTextTag( 00022 QDomElement elem, 00023 std::string tagName, 00024 std::string id ) { 00025 std::string ret = ""; 00026 if ( elem.isNull() ) 00027 return ""; 00028 00029 QDomNode n = elem.firstChild(); 00030 QDomElement tmp; 00031 bool isTag = true; 00032 while ( !n.isNull() ) { 00033 isTag = true; 00034 if ( !n.isElement() ) 00035 isTag = false; 00036 00037 tmp = n.toElement(); 00038 if ( tagName != "" && tagName != (const char*)tmp.tagName() ) 00039 isTag = false; 00040 00041 if ( id != "" && id != (const char*)tmp.attribute( "id" ) ) 00042 isTag = false; 00043 00044 if ( isTag ) { 00045 ret = (const char*)tmp.text(); 00046 break; 00047 } 00048 n = n.nextSibling(); 00049 } 00050 return ret; 00051 } 00052 00053 bool XMLSerializeable::fromXMLFile( std::string fname, std::string docname ) { 00054 QDomElement e; 00055 QDomNode node; 00056 QDomDocument doc; 00057 00058 QFile f( fname.c_str() ); 00059 if ( !f.open( IO_ReadOnly ) ) 00060 return false; 00061 00062 if ( !doc.setContent( &f ) ) { 00063 f.close(); 00064 return false; 00065 } 00066 f.close(); 00067 00068 // skip past any fluff and find the root node - dls 5/20/04 00069 //node = doc.firstChild(); 00070 node = doc.namedItem(docname.c_str()); 00071 00072 if ( !node.isElement() ) 00073 return false; 00074 e = node.toElement(); 00075 00076 if ( docname != (const char*)e.tagName() ) 00077 return false; 00078 00079 node = node.firstChild(); 00080 if ( !node.isElement() ) 00081 return false; 00082 e = node.toElement(); 00083 00084 return fromXML( e ); 00085 } 00086 00087 bool XMLSerializeable::toXMLFile( std::string fname, std::string docname ) { 00088 QDomDocument doc( docname.c_str() ); 00089 00090 // insert XML tag at beginning of file - dls 5/20/04 00091 QDomNode xmlInstructionNode = doc.createProcessingInstruction ("xml", "version=\"1.0\" encoding=\"UTF-8\"") ; 00092 doc.insertBefore(xmlInstructionNode, doc.firstChild()) ; 00093 00094 // create root node, complete with Schema declaration - dls 5/20/04 00095 QDomElement e = doc.createElement( docname.c_str() ); 00096 e.setAttribute ( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") ; 00097 e.setAttribute ( "xsi:noNamespaceSchemaLocation", "http://astrogeology.usgs.gov/Projects/glimsview/Schema/GLIMSProject/GLIMSProject.xsd") ; 00098 e.setAttribute ( "version", qDateToString(&mCurrentVersion).c_str() ) ; 00099 doc.appendChild( e ); 00100 if ( !toXML( doc, e ) ) 00101 return false; 00102 00103 std::ofstream f( fname.c_str() ); 00104 if ( f ) { 00105 QString qstr = doc.toString(); 00106 if ( qstr.isNull() ) 00107 return false; 00108 00109 f.write( (const char*)qstr, qstr.length() ); 00110 f.flush(); 00111 } else { 00112 return false; 00113 } 00114 f.close(); 00115 return true; 00116 } 00117 00118 QDomElement 00119 XMLSerializeable::getElemByTagName( QDomElement &elem, 00120 std::string name ) { 00121 QDomElement ret; 00122 QDomNode node = elem; 00123 QDomElement e; 00124 00125 while ( node.isElement() && !node.isNull() ) { 00126 e = node.toElement(); 00127 if ( name == (const char*)e.tagName() ) 00128 return e; 00129 node = node.nextSibling(); 00130 } 00131 return ret; 00132 } 00133 00134 00135 std::string XMLSerializeable::qDateToString (const QDate * aQDate) { 00136 if ( aQDate->isNull() || !(aQDate->isValid()) ) return NULL ; 00137 00138 // dump date components into a stringstream 00139 std::stringstream dateStream ; 00140 dateStream << std::setfill ('0') << std::right 00141 << std::setw(4) << aQDate->year() << std::setw(1) << "-" 00142 << std::setw(2) << aQDate->month() << std::setw(1) << "-" 00143 << std::setw(2) << aQDate->day() ; 00144 00145 // return the stringstream as a string 00146 return dateStream.str() ; 00147 } 00148 00149 std::string XMLSerializeable::qDateTimeToString (const QDateTime * aQDateTime) { 00150 if ( aQDateTime->isNull() || !aQDateTime->isValid() ) return NULL ; 00151 00152 QDate aQDate = aQDateTime->date() ; 00153 QTime aQTime = aQDateTime->time() ; 00154 00155 // dump date components into a stringstream 00156 std::stringstream dateTimeStream ; 00157 dateTimeStream << qDateToString (&aQDate) 00158 << std::setfill ('0') << std::right 00159 << std::setw(1) << "T" 00160 << std::setw(2) << aQTime.hour() << std::setw(1) << ":" 00161 << std::setw(2) << aQTime.minute() << std::setw(1) << ":" 00162 << std::setw(2) << aQTime.second() ; 00163 00164 // return the stringstream as a string 00165 return dateTimeStream.str() ; 00166 } 00167 00168 00169 00170 bool XMLSerializeable::stringToQDate (const std::string aStrDate, QDate * aQDate) { 00171 00172 std::string aStrDateCopy = aStrDate ; 00173 00174 // check format to make certain it appears to be YYYY-MM-DD 00175 if ( aStrDateCopy.length() < 10 ) return false ; 00176 00177 if ( aStrDateCopy[4] != '-' || 00178 aStrDateCopy[7] != '-' ) { 00179 return false ; 00180 } 00181 00182 for ( unsigned int i = 0 ; i < aStrDateCopy.length() ; i++ ) { 00183 if ( aStrDateCopy[i] < '0' || aStrDateCopy[i] > '9' ) { 00184 aStrDateCopy[i] = ' ' ; 00185 } 00186 } 00187 00188 std::stringstream dateStream ; 00189 dateStream << aStrDateCopy ; 00190 00191 int year = 0 ; 00192 int month = 0 ; 00193 int day = 0 ; 00194 dateStream >> year >> month >> day ; 00195 00196 if ( !aQDate ) { 00197 return false ; 00198 } 00199 aQDate->setYMD(year, month, day) ; 00200 00201 return true ; 00202 00203 } 00204 00205 bool XMLSerializeable::stringToQDateTime (const std::string aStrDateTime, QDateTime * aQDateTime) { 00206 00207 QDate aQDate = aQDateTime->date() ; 00208 QTime aQTime = aQDateTime->time() ; 00209 00210 // make certain there's enough characters for YYYY-MM-DDTHH:MM:SS 00211 if ( aStrDateTime.length() < 19 ) return false ; 00212 00213 if ( !stringToQDate (aStrDateTime, &aQDate) ) return false ; 00214 00215 aQDateTime->setDate (aQDate) ; 00216 00217 // check format to make certain it appears to be THH:MM:SS 00218 00219 if ( aStrDateTime[10] != 'T' || 00220 aStrDateTime[13] != ':' || 00221 aStrDateTime[16] != ':' ) { 00222 return false ; 00223 } 00224 00225 char * aStrDateTimeCopy = new char[aStrDateTime.length()] ; 00226 strcpy (aStrDateTimeCopy, aStrDateTime.c_str()) ; 00227 for ( unsigned int i = 0 ; i < aStrDateTime.length() ; i++ ) { 00228 if ( aStrDateTimeCopy[i] < '0' || aStrDateTimeCopy[i] > '9' ) { 00229 aStrDateTimeCopy[i] = ' ' ; 00230 } 00231 } 00232 00233 std::stringstream timeStream ; 00234 timeStream << aStrDateTimeCopy[11] ; 00235 int hour = 0 ; 00236 int minutes = 0 ; 00237 int seconds = 0 ; 00238 timeStream >> hour >> minutes >> seconds ; 00239 aQTime.setHMS (hour, minutes, seconds) ; 00240 aQDateTime->setTime (aQTime) ; 00241 00242 delete aStrDateTimeCopy ; 00243 00244 return true ; 00245 00246 } 00247 00248 00249 void XMLSerializeable::setCurrentVersion (std::string versionString) { 00250 mCurrentVersion = *(new QDate(2100, 1, 1)) ; 00251 stringToQDate (versionString, &mCurrentVersion) ; 00252 } 00253 00254 void XMLSerializeable::setCurrentVersion (QDate versionQDate) { 00255 mCurrentVersion = versionQDate ; 00256 } 00257 00258 QDate XMLSerializeable::getCurrentVersion () { 00259 return mCurrentVersion ; 00260 } 00261 00262 std::string XMLSerializeable::getCurrentVersionStr () { 00263 return qDateToString(&mCurrentVersion) ; 00264 } 00265 00266
Home |
Search |
Disclaimers & Privacy |
Contact Us GLIMSView Maintainer: dsoltesz@usgs.gov |