domutils.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                          domutils.cpp  -  description
00003                              -------------------
00004     begin                : Thu Feb 20 2003
00005     copyright            : (C) 2003-2006 by Ben Swerts
00006     email                : bswerts@users.sourceforge.net
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00019 
00030 
00031 
00032 
00034 
00035 // Qt header files
00036 #include <qdom.h>
00037 #include <qstring.h>
00038 #include <qstringlist.h>
00039 #include <qvaluelist.h>
00040 
00041 // Xbrabo header files
00042 #include <domutils.h>
00043 
00044 
00048 
00050 void DomUtils::readNode(QDomNode* node, QString* value)
00052 {  
00053   *value = node->toElement().attribute("value").stripWhiteSpace();
00054 }
00055 
00057 void DomUtils::readNode(QDomNode* node, QStringList* values)
00059 {
00060   //if(node->toElement().attribute("dataType") != "xsd:string")
00061   //  return;
00062   QDomNode childNode = node->namedItem("array");
00063   if(!childNode.isNull())
00064     *values =  QStringList::split(" ",childNode.toElement().text());
00065 }
00066 
00068 void DomUtils::readNode(QDomNode* node, unsigned int* value)
00070 {
00071   //if(node->toElement().attribute("dataType") != "xsd:unsignedShort")
00072   //  return;
00073   *value =  node->toElement().attribute("value","0").toUShort();
00074 }
00075 
00077 void DomUtils::readNode(QDomNode* node, int* value)
00079 {
00080   //if(node->toElement().attribute("dataType") != "xsd:int")
00081   //  return;
00082   *value =  node->toElement().attribute("value","0").toInt();
00083 }
00084 
00086 void DomUtils::readNode(QDomNode* node, float* value)
00088 {
00089   //if(node->toElement().attribute("dataType") != "xsd:float")
00090   //  return;
00091   *value =  node->toElement().attribute("value","0.0").toFloat();
00092 }
00093 
00095 void DomUtils::readNode(QDomNode* node, double* value)
00097 {
00098   //if(node->toElement().attribute("dataType") != "xsd:double")
00099   //  return;
00100   *value =  node->toElement().attribute("value","0.0").toDouble();
00101 }
00102 
00104 void DomUtils::readNode(QDomNode* node, bool* value)
00106 {
00107   //if(node->toElement().attribute("dataType") != "xsd:boolean")
00108   //  return;
00109   *value = node->toElement().attribute("value").stripWhiteSpace() == "true";
00110 }
00111 
00113 void DomUtils::readNode(QDomNode* node, std::vector<unsigned int>* values)
00115 {
00116   //if(node->toElement().attribute("dataType") != "xsd:unsigned")
00117   //  return;  
00118   values->clear();
00119   QDomNode childNode = node->namedItem("array");
00120   if(!childNode.isNull())
00121   {
00122     QStringList valueList = QStringList::split(" ",childNode.toElement().text());
00123     for(QStringList::Iterator it = valueList.begin(); it != valueList.end(); it++)
00124       values->push_back((*it).toUShort());  
00125   }
00126 }
00127 
00129 void DomUtils::readNode(QDomNode* node, std::vector<double>* values)
00131 {
00132   //if(node->toElement().attribute("dataType") != "xsd:float")
00133   //  return;
00134   values->clear();
00135   QDomNode childNode = node->namedItem("array");
00136   if(!childNode.isNull())
00137   {
00138     QStringList valueList = QStringList::split(" ",childNode.toElement().text());
00139     for(QStringList::Iterator it = valueList.begin(); it != valueList.end(); it++)
00140       values->push_back((*it).toDouble());
00141   }
00142 }
00143 
00145 void DomUtils::readNode(QDomNode* node, std::vector<bool>* values)
00147 {
00148   //if(node->toElement().attribute("dataType") != "xsd:string")
00149   //  return;  
00150   values->clear();
00151   QDomNode childNode = node->namedItem("array");
00152   if(!childNode.isNull())
00153   {
00154     QStringList valueList = QStringList::split(" ",childNode.toElement().text());
00155     for(QStringList::Iterator it = valueList.begin(); it != valueList.end(); it++)
00156       values->push_back(*it == "true");
00157   }
00158 }
00159 
00161 void DomUtils::readNode(QDomNode* node, QValueList<int>* values)
00163 {
00164   //if(node->toElement().attribute("dataType") != "xsd:int")
00165   //  return;
00166   values->clear();
00167   QDomNode childNode = node->namedItem("array");
00168   if(!childNode.isNull())
00169   {
00170     QStringList valueList = QStringList::split(" ",childNode.toElement().text());
00171     for(QStringList::Iterator it = valueList.begin(); it != valueList.end(); it++)
00172       values->push_back((*it).toInt());
00173   }
00174 }
00175 
00177 void DomUtils::makeNode(QDomElement* root, const QString nodeData, const QString dictRef, const QString attributeName, const QString attributeValue)
00180 {
00181   QDomElement childNode = root->ownerDocument().createElement("parameter");
00182   if(!dictRef.isEmpty())
00183     childNode.setAttribute("dictRef", ns + ":" + dictRef);
00185   //else
00186   //  childNode.setAttribute("dataType", "xsd:string"); // type is only needed when its not specified in the dictionary
00187   if(!attributeName.isEmpty())
00188     childNode.setAttribute(attributeName, attributeValue);  
00189   //QDomText textNode = root->ownerDocument().createTextNode(nodeData);
00190   childNode.setAttribute("value", nodeData);
00191   //childNode.appendChild(textNode);
00192   root->appendChild(childNode);
00193 }
00194 
00196 void DomUtils::makeNode(QDomElement* root, const QStringList nodeData, const QString dictRef, const QString attributeName, const QString attributeValue)
00198 {
00199   if(nodeData.size() == 0)  // an array should at least contain 1 element
00200     return;
00201   QDomElement childNode = root->ownerDocument().createElement("parameter");
00202   if(!dictRef.isEmpty())
00203     childNode.setAttribute("dictRef", ns + ":" + dictRef);
00204   //else
00205   //  childNode.setAttribute("dataType", "xsd:string");
00206   if(!attributeName.isEmpty())
00207     childNode.setAttribute(attributeName, attributeValue);
00208 
00209   QDomElement grandChildNode = root->ownerDocument().createElement("array");
00210   grandChildNode.setAttribute("size",QString::number(nodeData.size()));
00211   QDomText textNode = root->ownerDocument().createTextNode(nodeData.join(" "));
00212   grandChildNode.appendChild(textNode);
00213   childNode.appendChild(grandChildNode);
00214   root->appendChild(childNode);
00215 }
00216 
00218 void DomUtils::makeNode(QDomElement* root, const unsigned int nodeData, const QString dictRef)
00220 {
00221   QDomElement childNode = root->ownerDocument().createElement("parameter");
00222   if(!dictRef.isEmpty())
00223     childNode.setAttribute("dictRef", ns + ":" + dictRef);
00224   //else
00225   //  childNode.setAttribute("dataType", "xsd:unsigned");
00226   //QDomText textNode = root->ownerDocument().createTextNode(QString::number(nodeData));
00227   childNode.setAttribute("value", nodeData);
00228   //childNode.appendChild(textNode);
00229   root->appendChild(childNode);
00230 }
00231 
00233 void DomUtils::makeNode(QDomElement* root, const int nodeData, const QString dictRef)
00235 {
00236   QDomElement childNode = root->ownerDocument().createElement("parameter");
00237   if(!dictRef.isEmpty())
00238     childNode.setAttribute("dictRef", ns + ":" + dictRef);
00239   //else
00240   //  childNode.setAttribute("dataType", "xsd:int");
00241   //QDomText textNode = root->ownerDocument().createTextNode(QString::number(nodeData));
00242   childNode.setAttribute("value", nodeData);
00243   //childNode.appendChild(textNode);
00244   root->appendChild(childNode);
00245 }
00246 
00248 void DomUtils::makeNode(QDomElement* root, const float nodeData, const QString dictRef)
00250 {
00251   QDomElement childNode = root->ownerDocument().createElement("parameter");
00252   if(!dictRef.isEmpty())
00253     childNode.setAttribute("dictRef", ns + ":" + dictRef);
00254   //else
00255   //  childNode.setAttribute("dataType", "xsd:float");
00256   //QDomText textNode = root->ownerDocument().createTextNode(QString::number(nodeData));
00257   childNode.setAttribute("value", nodeData);
00258   //childNode.appendChild(textNode);
00259   root->appendChild(childNode);
00260 }
00261 
00263 void DomUtils::makeNode(QDomElement* root, const bool nodeData, const QString dictRef)
00265 {
00266   QDomElement childNode = root->ownerDocument().createElement("parameter");
00267   if(!dictRef.isEmpty())
00268     childNode.setAttribute("dictRef", ns + ":" + dictRef);
00269   //else
00270   //  childNode.setAttribute("dataType", "xsd:boolean");
00271   QString text;
00272   if(nodeData)
00273     text = "true";
00274   else
00275     text = "false";
00276   //QDomText textNode = root->ownerDocument().createTextNode(text);
00277   childNode.setAttribute("value", text);
00278   //childNode.appendChild(textNode);
00279   root->appendChild(childNode);
00280 }
00281 
00283 void DomUtils::makeNode(QDomElement* root, const std::vector<unsigned int> nodeData, const QString dictRef)
00285 {
00286   if(nodeData.size() == 0)  // an array should at least contain 1 element
00287     return;
00288   QDomElement childNode = root->ownerDocument().createElement("parameter");
00289   if(!dictRef.isEmpty())
00290     childNode.setAttribute("dictRef", ns + ":" + dictRef);
00291   //else
00292   //  childNode.setAttribute("dataType", "xsd:unsigned");
00293   QDomElement grandChildNode = root->ownerDocument().createElement("array");
00294   grandChildNode.setAttribute("size",QString::number(nodeData.size()));
00295   QString text;
00296   for(std::vector<unsigned int>::const_iterator it = nodeData.begin(); it < nodeData.end(); it++)
00297     text += " " + QString::number(*it);
00298   text.stripWhiteSpace();
00299   QDomText textNode = root->ownerDocument().createTextNode(text);
00300   grandChildNode.appendChild(textNode);
00301   childNode.appendChild(grandChildNode);
00302   root->appendChild(childNode);
00303 }
00304 
00306 void DomUtils::makeNode(QDomElement* root, const std::vector<double> nodeData, const QString dictRef, const QString attributeName, const QString attributeValue)
00308 {
00309   if(nodeData.size() == 0)  // an array should at least contain 1 element
00310     return;
00311   QDomElement childNode = root->ownerDocument().createElement("parameter");
00312   if(!dictRef.isEmpty())
00313     childNode.setAttribute("dictRef", ns + ":" + dictRef);
00314   //else
00315   //  childNode.setAttribute("dataType", "xsd:float");
00316   if(!attributeName.isEmpty())
00317     childNode.setAttribute(attributeName, attributeValue);    
00318   
00319   QDomElement grandChildNode = root->ownerDocument().createElement("array");
00320   grandChildNode.setAttribute("size",QString::number(nodeData.size()));
00321   QString text;
00322   for(std::vector<double>::const_iterator it = nodeData.begin(); it < nodeData.end(); it++)
00323     text += " " + QString::number(*it, 'f', 12);
00324   text.stripWhiteSpace();
00325   QDomText textNode = root->ownerDocument().createTextNode(text);
00326   grandChildNode.appendChild(textNode);
00327   childNode.appendChild(grandChildNode);
00328   root->appendChild(childNode);
00329 }
00330 
00332 void DomUtils::makeNode(QDomElement* root, const std::vector<bool> nodeData, const QString dictRef)
00334 {
00335   if(nodeData.size() == 0)  // an array should at least contain 1 element
00336     return;
00337   QDomElement childNode = root->ownerDocument().createElement("parameter");
00338   if(!dictRef.isEmpty())
00339     childNode.setAttribute("dictRef", ns + ":" + dictRef);
00340   //else
00341   //  childNode.setAttribute("dataType", "xsd:boolean");
00342 
00343   QDomElement grandChildNode = root->ownerDocument().createElement("array");
00344   grandChildNode.setAttribute("size",QString::number(nodeData.size()));
00345   QString text;
00346   for(std::vector<bool>::const_iterator it = nodeData.begin(); it != nodeData.end(); it++)
00347     text += " " + *it ? "true" : "false";
00348   text.stripWhiteSpace();
00349   QDomText textNode = root->ownerDocument().createTextNode(text);
00350   grandChildNode.appendChild(textNode);
00351   childNode.appendChild(grandChildNode);
00352   root->appendChild(childNode);
00353 }
00354 
00356 void DomUtils::makeNode(QDomElement* root, const QValueList<int> nodeData, const QString dictRef)
00358 {
00359   if(nodeData.size() == 0)  // an array should at least contain 1 element
00360     return;
00361   QDomElement childNode = root->ownerDocument().createElement("parameter");
00362   if(!dictRef.isEmpty())
00363     childNode.setAttribute("dictRef", ns + ":" + dictRef);
00364   //else
00365   //  childNode.setAttribute("dataType", "xsd:int");
00366   QDomElement grandChildNode = root->ownerDocument().createElement("array");
00367   grandChildNode.setAttribute("size",QString::number(nodeData.size()));
00368   QString text;
00369   for(QValueList<int>::const_iterator it = nodeData.begin(); it != nodeData.end(); it++)
00370     text += " " + QString::number(*it);
00371   text.stripWhiteSpace();
00372   QDomText textNode = root->ownerDocument().createTextNode(text);
00373   grandChildNode.appendChild(textNode);
00374   childNode.appendChild(grandChildNode);
00375   root->appendChild(childNode);
00376 }
00377 
00379 bool DomUtils::dictEntry(QDomNode& node, const QString value)
00381 {
00382   return(node.isElement() && node.toElement().attribute("dictRef") == ns + ":" + value);
00383 }
00384 
00388 
00390 DomUtils::DomUtils()
00392 {
00393 }
00394 
00396 DomUtils::~DomUtils()
00398 {
00399 }
00400 
00404 const QString DomUtils::uriDict10   = "http://brabosphere.sourceforge.net/dict/1.0";      //< The URI for version 1.0 of the Brabosphere CML dictionary
00405 const QString DomUtils::uriNSCML    = "http://www.xml-cml.org/schema";                    //< The URI for the CML namespace
00406 const QString DomUtils::uriNSDC     = "http://www.dublincore.org/dict";                   //< The URI for the Dublin Core namespace
00407 const QString DomUtils::uriDictCMLM = "http://www.xml-cml.org/dict/cmlMeasured";          //< The URI for the CML Measured dictionary
00408 const QString DomUtils::uriDictXSD = "http://www.w3.org/2001/XMLSchema";                  //< The URI for the XSD dictionary (datatypes)
00409 const QString DomUtils::uriDictAtomic = "http://www.xml-cml.org/units/atomic";            //< The URI for the CML Atomic Units dictionary
00410 const QString DomUtils::uriDictSI = "http://www.xml-cml.org/units/siUnits";               //< The URI for the CML SI Units dictionary
00411 
00412 const QString DomUtils::ns = "bs";                //< The namespace prefix for the Brabosphere dictionary
00413 const QString DomUtils::nsCMLM = "cmlm";          //< The namespace prefix for the CML Measured dictionary
00414 const QString DomUtils::nsXSD = "xsd";            //< The namespace prefix for the XSD dictionary
00415 const QString DomUtils::nsAtomic = "atomic";      //< The namespace prefix for the CML Atomic Units dictionary
00416 const QString DomUtils::nsSI = "siUnits";         //< The namespace prefix for the CML SI Units dictionary
00417 const QString DomUtils::nsDC = "dc";              //< The namespace prefix for the Dublin Core dictionary
00418 

Generated on Fri May 19 14:31:54 2006 for Brabosphere by  doxygen 1.4.6-NO