testXml.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2007, 2008, 2009, 2010, 2011 Instituto de Sistemas e Robotica, Instituto Superior Tecnico
00003 
00004 This file is part of MeRMaID.
00005 
00006 MeRMaID is free software: you can redistribute it and/or modify
00007 it under the terms of the GNU Lesser General Public License as published by
00008 the Free Software Foundation, either version 3 of the License, or
00009 (at your option) any later version.
00010 
00011 MeRMaID is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License
00017 along with MeRMaID.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 
00021 
00022 /**
00023  * @Filename testXml.cpp
00024  * @Description Tests the Xml framework implementation
00025  * @Status unknown
00026  * @Version $Id: testXml.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer Nelson Ramos (nmsra@mega.ist.utl.pt)
00028  */
00029 
00030 #include "config.h"
00031 
00032 #include <iostream>
00033 using std::cerr;
00034 using std::endl;
00035 #include <string>
00036 using std::string;
00037 
00038 #include "XmlDocument.hpp"
00039 #include "XmlElement.hpp"
00040 using namespace mermaid::support::xml;
00041 
00042 #include <FilePathSearch.hpp>
00043 using mermaid::support::system::FilePathSearch;
00044 
00045 
00046 
00047 
00048 
00049 void printChildren (shared_ptr<XmlElement> xmlElement, int tab)
00050 {
00051   XmlElementVector elements = xmlElement->getChildrenElements();
00052   
00053   for (XmlElementVector::iterator it = elements.begin();
00054        it != elements.end();
00055        it++) {
00056     shared_ptr<XmlElement> element = *it;
00057     
00058     cerr << string (tab, '\t');
00059     
00060     cerr << "ELEMENT Name: " << element->getName();
00061     
00062     cerr <<  "\tCharData: ";
00063     shared_ptr<XmlCharData> c = element->getFirstNonBlankCharData();
00064     if (c) {
00065       cerr << "\"" << static_cast<string> (*c) << "\"" << endl;
00066     }
00067     else {
00068       cerr << "NO_CHAR_DATA" << endl;
00069     }
00070     
00071     XmlAttributeVector attributes = element->getAttributes();
00072     for (XmlAttributeVector::iterator ait = attributes.begin();
00073          ait != attributes.end();
00074          ait++) {
00075       shared_ptr<XmlAttribute> attribute = *ait;
00076       
00077       cerr << string (tab, '\t');
00078       
00079       cerr << "  ATTRIBUTE name: " << attribute->getName();
00080       cerr << "\tvalue: \"" << attribute->getValue() << "\"";
00081       cerr << endl;
00082     }
00083     
00084     printChildren (element, tab + 1);
00085   }
00086 }
00087 
00088 
00089 
00090 int main (int argc, char * const argv[])
00091 {
00092   // insert code here...
00093   
00094   cerr << "Xml framework test" << endl;
00095   
00096   if (argc != 2) {
00097     cerr << "Usage: testXml [filename]" << endl;
00098     return (1);
00099   }
00100   
00101   FilePathSearch::addSearchPath ("./");
00102   FilePathSearch::addSearchPath ("/");
00103   
00104   XmlDocument doc;
00105   cerr << "Reading from file: " << argv[1] << endl;
00106   doc.parseFile (string (argv[1]), false);
00107   
00108   if (doc.isWellFormed()) {
00109     cerr << "XML is WELL FORMED" << endl;
00110   }
00111   else {
00112     cerr << "XML is NOT WELL FORMED" << endl;
00113     return (1);
00114   }
00115   
00116   if (doc.isValid()) {
00117     cerr << "XML is VALID" << endl;
00118   }
00119   else {
00120     cerr << "XML is NOT VALID" << endl;
00121     return (1);
00122   }
00123   
00124   shared_ptr<XmlElement> rootElement;
00125   
00126   cerr << "XML tree:" << endl;
00127   
00128   rootElement = doc.getRootElement();
00129   string rootName = rootElement->getName();
00130   shared_ptr<XmlCharData> charData = rootElement->getFirstNonBlankCharData();
00131   string rootCharData;
00132   if (charData) {
00133     rootCharData = *charData;
00134   }
00135   else {
00136     rootCharData = "-- NO CHAR DATA --";
00137   }
00138   cerr << "\tELEMENT Name: " << rootName;
00139   cerr << "\tCharData: \"" << rootCharData << "\"";
00140   cerr << endl;
00141   
00142   shared_ptr<XmlElement> rootPtr =
00143     shared_ptr<XmlElement> (new XmlElement ( (*rootElement)));
00144   printChildren (rootPtr, 2);
00145   
00146   cerr << "Parsed XML:" << endl;
00147   string rootString = *rootElement;
00148   cerr << rootString << endl;
00149   
00150   XmlElement* copy = new XmlElement (*rootElement);
00151   string copyString = *copy;
00152   
00153   if (rootString.compare (copyString) != 0) {
00154     cerr << "XmlElement copy doesn't produce the same output string" << endl;
00155     cerr << "Original was:" << endl;
00156     cerr << "\t\"" << rootString << "\"" << endl;
00157     cerr << "Copy was:" << endl;
00158     cerr << "\t\"" << copyString << "\"" << endl;
00159     return (1);
00160   }
00161   
00162   delete copy;
00163   
00164   // Test for correctly reading CDATA sections
00165   XmlElementVector children = rootElement->getChildrenElements();
00166   shared_ptr<XmlElement> d = children.getFirstElementWithName ("d");
00167   
00168   if (d == false) {
00169     cerr << "Unable to read element d" << endl;
00170     return (1);
00171   }
00172   
00173   shared_ptr<XmlCharData> data = d->getFirstNonBlankCharData();
00174   
00175   if ( (data == false)
00176        || (static_cast<string> (*data).compare ("CDATA") != 0)) {
00177     cerr << "Unable to read CDATA blocks" << endl;
00178     return (1);
00179   }
00180   
00181   
00182   string xml = "<test><a a1=\"123\" a2=\"456\">outer a</a><b><a a1=\"123\" >"
00183                "</a></b><c>jkl</c><d>mno</d></test>";
00184                
00185   cerr << "Reading from string: " << xml << endl;
00186   doc.parseString (xml);
00187   
00188   if (doc.isWellFormed()) {
00189     cerr << "XML is WELL FORMED" << endl;
00190   }
00191   else {
00192     cerr << "XML is NOT WELL FORMED" << endl;
00193     return (1);
00194   }
00195   
00196   if (doc.isValid()) {
00197     cerr << "XML is VALID" << endl;
00198   }
00199   else {
00200     cerr << "XML is NOT VALID" << endl;
00201     return (1);
00202   }
00203   
00204   cerr << "XML tree:" << endl;
00205   
00206   shared_ptr<XmlElement> stringElement = doc.getRootElement();
00207   string stringElementName = stringElement->getName();
00208   shared_ptr<XmlCharData> stringElementCharData = stringElement->getFirstNonBlankCharData();
00209   string stringElementCharDataString;
00210   if (stringElementCharData) {
00211     stringElementCharDataString = *stringElementCharData;
00212   }
00213   else {
00214     stringElementCharDataString = "-- NO CHAR DATA --";
00215   }
00216   
00217   cerr << "\tELEMENT Name: " << stringElementName;
00218   cerr << "\tCharData: \"" << stringElementCharDataString << "\"";
00219   cerr << endl;
00220   
00221   shared_ptr<XmlElement> stringElementPtr =
00222     shared_ptr<XmlElement> (new XmlElement ( (*stringElement)));
00223   printChildren (stringElementPtr, 2);
00224   
00225   string stringString = *stringElement;
00226   cerr << stringString << endl;
00227   
00228   XmlElement* stringCopy = new XmlElement (*stringElement);
00229   string stringCopyString = *stringCopy;
00230   
00231   if (stringString.compare (stringCopyString) != 0) {
00232     cerr << "XmlElement copy doesn't produce the same output string" << endl;
00233     cerr << "Original was:" << endl;
00234     cerr << stringString << endl;
00235     cerr << "Copy was:" << endl;
00236     cerr << stringCopyString << endl;
00237     return (1);
00238   }
00239   
00240   delete stringCopy;
00241   
00242   //****************************************************
00243   //* XmlCharData tests
00244   //****************************************************
00245   
00246   {
00247     XmlCharData c = XmlCharData();
00248     if (c.isBlank() == false) {
00249       cerr << "XmlCharData::isBlank() should return true "
00250            "after using default constructor" << endl;
00251       return (1);
00252     }
00253     
00254     string s = c;
00255     if (s.compare ("") != 0) {
00256       cerr << "XmlCharData::operator (string) should return empty "
00257            "string after using default constructor" << endl;
00258     }
00259     
00260     c.removeEndBlanks();
00261     s = c;
00262     if (s.compare ("") != 0) {
00263       cerr << "XmlCharData::operator (string) should return empty "
00264            "string after using removeEndBlanks after default constructor"
00265            << endl;
00266     }
00267   }
00268   
00269   {
00270     string init = string ("\t\n\r1 2\t3\n ");
00271     string noBlank = string ("1 2\t3");
00272     
00273     XmlCharData c = XmlCharData (init.c_str());
00274     
00275     if (c.isBlank() == true) {
00276       cerr << "XmlCharData::isBlank() should return false "
00277            "after using parameterized constructor" << endl;
00278       return (1);
00279     }
00280     
00281     string s = c;
00282 #if 0
00283     if (s.compare (init) == 0) {
00284       cerr << "XmlCharData::operator (string) should not return a string "
00285            "equal to the one with which it was initialized: blanks should "
00286            "be removed" << endl;
00287       return (1);
00288     }
00289 #endif
00290     
00291     c.removeEndBlanks();
00292     s = c;
00293     if (s.compare (noBlank) != 0) {
00294       cerr << "XmlCharData::operator (string) returned an unexpected string "
00295            "after call to XmlCharData::removeEndBlanks()" << endl;
00296       return (1);
00297     }
00298     
00299   }
00300   
00301   //****************************************************
00302   //* XmlDocument tests
00303   //****************************************************
00304   
00305   {
00306     XmlDocument doc;
00307     doc.parseFile (string (argv[1]));
00308     
00309     shared_ptr<XmlElement> n;
00310     n = doc.getRootElement();
00311     
00312   }
00313   
00314   //****************************************************
00315   //* XmlAttribute tests
00316   //****************************************************
00317   
00318   return 0;
00319 }
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3