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