00001
00012 #include "QtXmlElement.h"
00013
00014 #include <qstring.h>
00015
00016 #ifdef SSTREAM_DEFECT
00017 #include <strstream>
00018 using std::ostrstream;
00019 #else
00020 #include <sstream>
00021 using std::ostringstream;
00022 #endif
00023
00024 #include <cassert>
00025
00026 using std::list;
00027 using std::string;
00028
00029 QtXmlElement::QtXmlElement ( const QtXmlElement & element )
00030 : QtXmlNode ( element )
00031 {
00032 #if QT_VERSION < 0x040000
00033 QDomElement * elem = dynamic_cast < QDomElement * > ( element.m_node );
00034 assert ( elem != 0 );
00035 #else
00036 QDomElement * elem = reinterpret_cast < QDomElement * > ( element.m_node );
00037 #endif
00038
00039
00040 m_element = new QDomElement ( *elem );
00041 m_node = m_element;
00042 }
00043
00046 QtXmlElement::QtXmlElement ( const QDomElement & element )
00047 : QtXmlNode ( )
00048 {
00049
00050 m_element = new QDomElement ( element );
00051 m_node = m_element;
00052 }
00053
00054 QtXmlElement::
00055 ~QtXmlElement()
00056 {
00057 delete m_element;
00058 }
00059
00060 string
00061 QtXmlElement::tagName () const
00062 {
00063 QString tagname = m_element -> tagName ();
00064
00065 return string ( tagname.latin1() );
00066 }
00067
00068 int QtXmlElement::getID () const
00069 {
00070 QString value = m_element -> attribute ( "id" );
00071 if ( value == QString::null ) return 0;
00072
00073 bool ok;
00074 int id = value.toInt ( &ok );
00075 if ( ok == false ) id = 0;
00076
00077 return id;
00078 }
00079
00080 #ifdef CLONE_DEFECT
00081 XmlElement *
00082 #else
00083 QtXmlElement *
00084 #endif
00085 QtXmlElement::
00086 getNode ( const std::string & tagName ) const
00087 {
00088
00089 QString tagname ( tagName.c_str () );
00090 QDomNode node = m_element -> namedItem ( tagname );
00091 if ( node.isNull () ) return 0;
00092
00093 QDomElement element = node.toElement ();
00094 assert ( element.isNull () == false );
00095 return new QtXmlElement ( element );
00096 }
00097
00100 void
00101 QtXmlElement::
00102 fillNodeList ( const std::string & tagName,
00103 std::list < XmlElement * > & nodeList ) const
00104 {
00105 QString tag_name ( tagName.c_str () );
00106 QDomNodeList nodelist = m_element -> elementsByTagName ( tag_name );
00107 unsigned int size ( nodelist.count () );
00108 for ( unsigned int i = 0; i < size; i++ ) {
00109 QDomNode node = nodelist.item ( i );
00110 QDomElement element = node.toElement ();
00111 nodeList.push_back ( new QtXmlElement ( element ) );
00112 }
00113
00114 }
00115
00116 void
00117 QtXmlElement::
00118 setAttribute ( const std::string & name, int value )
00119 {
00120 assert ( ! name.empty () );
00121 m_element -> setAttribute ( name.c_str(), value );
00122 }
00123
00124
00125 void
00126 QtXmlElement::
00127 setAttribute ( const std::string & name, bool yes )
00128 {
00129 assert ( ! name.empty () );
00130
00131 unsigned int value = yes ? 1 : 0;
00132 m_element -> setAttribute ( name.c_str(), value );
00133 }
00134
00135 void
00136 QtXmlElement::
00137 setAttribute ( const std::string & name, unsigned int value )
00138 {
00139 assert ( ! name.empty () );
00140 m_element -> setAttribute ( name.c_str(), value );
00141 }
00142
00143 void
00144 QtXmlElement::
00145 setAttribute ( const std::string & name, float value )
00146 {
00147 assert ( ! name.empty () );
00148 double v = value;
00149
00150 setAttribute ( name, v );
00151 }
00152
00153 void
00154 QtXmlElement::
00155 setAttribute ( const std::string & name, double value )
00156 {
00157 assert ( ! name.empty () );
00158
00159
00160 m_element -> setAttribute ( name.c_str(), value );
00161
00162
00163
00164
00165
00166
00167
00168 }
00169
00170 void
00171 QtXmlElement::
00172 setAttribute ( const std::string & name,
00173 const std::string & value )
00174 {
00175 assert ( ! name.empty () );
00176 m_element -> setAttribute ( name.c_str(), value.c_str() );
00177 }
00178
00179 bool
00180 QtXmlElement::
00181 attribute ( const std::string & name, int & value ) const
00182 {
00183 QString rstring = m_element -> attribute ( name.c_str() );
00184 if ( rstring == QString::null ) return false;
00185
00186 bool ok;
00187 int val = rstring.toInt ( & ok );
00188 if ( ! ok ) return false;
00189
00190 value = val;
00191
00192 return true;
00193 }
00194
00195 bool
00196 QtXmlElement::
00197 attribute ( const std::string & name, bool & value ) const
00198 {
00199 QString rstring = m_element -> attribute ( name.c_str() );
00200 if ( rstring == QString::null ) return false;
00201
00202 bool ok;
00203 int val = rstring.toInt ( & ok );
00204 if ( ! ok ) return false;
00205
00206 value = ( val = 1 ) ? true : false;
00207
00208 return true;
00209 }
00210
00211 bool
00212 QtXmlElement::
00213 attribute ( const std::string & name, unsigned int & value ) const
00214 {
00215 QString rstring = m_element -> attribute ( name.c_str() );
00216 if ( rstring == QString::null ) return false;
00217
00218 bool ok;
00219 int val = rstring.toUInt ( & ok );
00220 if ( ! ok ) return false;
00221
00222 value = val;
00223
00224 return true;
00225 }
00226
00227 bool
00228 QtXmlElement::
00229 attribute ( const std::string & name, float & value ) const
00230 {
00231 QString rstring = m_element -> attribute ( name.c_str() );
00232 if ( rstring == QString::null ) return false;
00233
00234 bool ok;
00235 float val = rstring.toFloat ( & ok );
00236 if ( ! ok ) return false;
00237
00238 value = val;
00239
00240 return true;
00241 }
00242
00243 bool
00244 QtXmlElement::
00245 attribute ( const std::string & name, double & value ) const
00246 {
00247 QString rstring = m_element -> attribute ( name.c_str() );
00248 if ( rstring == QString::null ) return false;
00249
00250 bool ok;
00251 float val = rstring.toDouble ( & ok );
00252 if ( ! ok ) return false;
00253
00254 value = val;
00255
00256 return true;
00257 }
00258
00259 bool
00260 QtXmlElement::
00261 attribute ( const std::string & name, std::string & value ) const
00262 {
00263 QString val = m_element -> attribute ( name.c_str() );
00264 if ( val == QString::null ) return false;
00265
00266 value = val.latin1 ();
00267
00268 return true;
00269 }
00270
00271 const string &
00272 QtXmlElement::
00273 getText () const
00274 {
00275 static string text;
00276 QString t = m_element -> text ();
00277 text = t.latin1 ();
00278
00279 return text;
00280 }