Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

QgarAppDescr.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------*
00002  | Library QgarLib, graphics analysis and recognition                  |
00003  | Copyright (C) 2003  Qgar Project, LORIA                             |
00004  |                                                                     |
00005  | This library is free software; you can redistribute it and/or       |
00006  | modify it under the terms of the GNU Lesser General Public          |
00007  | License version 2.1, as published by the Free Software Foundation.  |
00008  |                                                                     |
00009  | This library is distributed in the hope that it will be useful,     |
00010  | but WITHOUT ANY WARRANTY; without even the implied warranty of      |
00011  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                |
00012  | See the GNU Lesser General Public License for more details.         |
00013  |                                                                     |
00014  | The GNU Lesser General Public License is included in the file       |
00015  | LICENSE.LGPL, in the root directory of the Qgar packaging. See      |
00016  | http://www.gnu.org/licenses/lgpl.html for the terms of the license. |
00017  | To receive a paper copy, write to the Free Software Foundation,     |
00018  | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.       |
00019  |                                                                     |
00020  | Contact Project Qgar for any information:                           |
00021  |   LORIA - équipe Qgar                                               |
00022  |   B.P. 239, 54506 Vandoeuvre-lès-Nancy Cedex, France                |
00023  |   email: qgar-contact@loria.fr                                      |
00024  |   http://www.qgar.org/                                              |
00025  *---------------------------------------------------------------------*/
00026 
00027 
00028 /**
00029  * @file  QgarAppDescr.C
00030  * @brief Implementation of class qgar::QgarAppDescr.
00031  *
00032  *        See file QgarAppDescr.H for the interface.
00033  *
00034  * @author <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Jan Rendek">Jan Rendek</a>
00035  * @date   May 14, 2003  15:29
00036  * @since  Qgar 
00037  */
00038 
00039 
00040 
00041 // STD
00042 #include <fstream>
00043 #include <iostream>
00044 #include <sstream>
00045 // STL
00046 #include <algorithm>
00047 #include <functional>
00048 // QGAR
00049 #include <qgarlib/QgarAppDescr.H>
00050 #include <qgarlib/QgarErrorDeveloper.H>
00051 #include <qgarlib/QgarErrorUser.H>
00052 #include <qgarlib/stl.H>
00053 #include <qgarlib/sax/Attributes.H>
00054 #include <qgarlib/sax/SAXParseException.H>
00055 #include <qgarlib/sax/XMLReader.H>
00056 #include <qgarlib/sax/XMLReaderFactory.H>
00057 
00058 
00059 
00060 using namespace std;
00061 using namespace qgxml;
00062 
00063 
00064 
00065 // ---------------------------------------------------------------------
00066 // S T A T I C   M E M B E R   I N I T
00067 // ---------------------------------------------------------------------
00068 
00069 namespace qgar {
00070 
00071 const char* const QgarAppDescr::ELT_APPLICATION   = "application";
00072 const char* const QgarAppDescr::ELT_DESCR         = "descr";
00073 const char* const QgarAppDescr::ELT_DOCUMENTATION = "documentation";
00074 const char* const QgarAppDescr::ELT_PARAMLIST     = "paramlist";
00075 const char* const QgarAppDescr::ELT_PARAM         = "param";
00076 const char* const QgarAppDescr::ELT_NAME          = "name";
00077 const char* const QgarAppDescr::ELT_AUTHOR        = "author";
00078 const char* const QgarAppDescr::ELT_COPYRIGHT     = "copyright";
00079 const char* const QgarAppDescr::ELT_DOC           = "doc";
00080 const char* const QgarAppDescr::ELT_BRIEF         = "brief";
00081 const char* const QgarAppDescr::ELT_LONG          = "long";
00082   
00083 const char* const QgarAppDescr::ATT_REVISION     = "revision";
00084 const char* const QgarAppDescr::ATT_LANG         = "lang";
00085 const char* const QgarAppDescr::ATT_NAME         = "name";
00086 const char* const QgarAppDescr::ATT_FLAG         = "flag";
00087 const char* const QgarAppDescr::ATT_REQUIRED     = "required";
00088 const char* const QgarAppDescr::ATT_PASSING_MODE = "passing-mode";
00089 const char* const QgarAppDescr::ATT_FORMAT       = "format";
00090 const char* const QgarAppDescr::ATT_TYPE         = "type";
00091 
00092 
00093 
00094 // ---------------------------------------------------------------------
00095 // C O N S T R U C T O R S
00096 // ---------------------------------------------------------------------
00097 
00098 // Default constructor
00099 QgarAppDescr::QgarAppDescr(const char * const filename)
00100   
00101   throw(runtime_error)
00102   
00103   : _parseData(0)
00104 
00105 {
00106   ifstream stream(filename);
00107   init(InputSource(stream));
00108 }
00109 
00110 // ---------------------------------------------------------------------
00111 
00112 QgarAppDescr::QgarAppDescr(const string& filename)
00113 
00114   throw(runtime_error)
00115  
00116   : _parseData(0)
00117 
00118 {
00119   ifstream stream(filename.c_str());
00120   init(InputSource(stream));
00121 }
00122 
00123 // ---------------------------------------------------------------------
00124 
00125 QgarAppDescr::QgarAppDescr(istream& stream)
00126 
00127   throw(runtime_error)
00128 
00129   : _parseData(0)
00130 
00131 {
00132   init(InputSource(stream));
00133 }
00134 
00135 // ---------------------------------------------------------------------
00136 
00137 QgarAppDescr::QgarAppDescr(const string& name,
00138                            const string& copyright,
00139                            const string& briefDoc,
00140                            const string& longDoc,
00141                            const vector<QgarAppParamDescr *>& pVect)
00142   : _name(name),
00143     _copyright(copyright),
00144     _briefDoc(briefDoc),
00145     _longDoc(longDoc),
00146     _parseData(0)
00147 {
00148   
00149   // Duplicate parameters vector
00150 
00151   transform(pVect.begin(),
00152             pVect.end(), 
00153             back_inserter(_paramVect),
00154             qstlCloneObject());
00155 }
00156 
00157 // ---------------------------------------------------------------------
00158 
00159 // Copy constructor
00160 QgarAppDescr::QgarAppDescr(const QgarAppDescr& rhs)
00161  
00162   : _name(rhs._name),
00163     _copyright(rhs._copyright),
00164     _briefDoc(rhs._briefDoc),
00165     _longDoc(rhs._longDoc)
00166 {
00167 
00168   // Duplicate parsing data if any
00169   _parseData = (rhs._parseData)? new ParseData(*rhs._parseData) : 0;
00170 
00171   // Duplicate parameters vector
00172   _paramVect.reserve(rhs._paramVect.size());
00173 
00174   transform(rhs._paramVect.begin(),
00175             rhs._paramVect.end(), 
00176             back_inserter(_paramVect),
00177             qstlCloneObject());
00178 }
00179 
00180 
00181 
00182 // ---------------------------------------------------------------------
00183 // D E S T R U C T O R
00184 // ---------------------------------------------------------------------
00185 
00186 QgarAppDescr::~QgarAppDescr()
00187 {
00188 
00189   for_each(_paramVect.begin(), _paramVect.end(), qstlDeleteObject());
00190 
00191   delete _parseData;
00192 }
00193 
00194 
00195 
00196 // ---------------------------------------------------------------------
00197 // M E T H O D S
00198 // ---------------------------------------------------------------------
00199 
00200 void
00201 QgarAppDescr::addParamDescr(QgarAppParamDescr * descr)
00202 {
00203   _paramVect.push_back(new QgarAppParamDescr(*descr));
00204 }
00205 
00206 // ---------------------------------------------------------------------
00207 
00208 string
00209 QgarAppDescr::toXml() const
00210 {
00211   throw QgarErrorDeveloper(__FILE__, __LINE__,
00212                            "std::string qgar::QgarAppDescr::toXml() const",
00213                            "Function not yet implemented!");
00214   
00215   return string();
00216 }
00217 
00218 // ---------------------------------------------------------------------
00219 
00220 void
00221 QgarAppDescr::init(InputSource source)
00222 
00223   throw (runtime_error)
00224 
00225 {
00226   // Create parse data structure
00227   _parseData = new ParseData();
00228 
00229   // Parse input source
00230 
00231   // Parse
00232   XMLReader* parser = XMLReaderFactory::createXMLReader();
00233   parser->setContentHandler(this);
00234   parser->parse(source);
00235 
00236   // Clean-up
00237   delete _parseData;
00238   _parseData = 0;
00239   delete parser;
00240 }
00241 
00242 
00243 
00244 // ---------------------------------------------------------------------
00245 // D E F A U L T H A N D L E R   R E D E F I N I T I O N
00246 // ---------------------------------------------------------------------
00247 
00248 void 
00249 QgarAppDescr::characters(const char * ch, 
00250                          const unsigned int start, 
00251                          const unsigned int length)
00252 {
00253   // Append parsed data to the buffer.
00254   _parseData->buffer += string(ch);
00255 }
00256 
00257 // ---------------------------------------------------------------------
00258 
00259 void 
00260 QgarAppDescr::startElement(const string& uri,
00261                            const string& localName,
00262                            const string& qName,
00263                            const Attributes& atts)
00264 {
00265   // Check if document is really an application description
00266 
00267   if ((_parseData->firstElt) && (localName != "application"))
00268     {
00269       throw QgarErrorUser(__FILE__, __LINE__,
00270                           "void qgar::startElement(const std::string&, const std::string&,const std::string&, const qgxml::Attributes&)",
00271                           "Invalid document type.");
00272     }
00273   else
00274     {
00275       _parseData->firstElt = false;
00276     }
00277 
00278 
00279   // Reset buffer
00280 
00281   _parseData->buffer = "";
00282 
00283 
00284   // Redirect action on the function matching the tag.
00285 
00286   if (localName == ELT_PARAM)
00287     {
00288       startParam(atts);
00289     }
00290 }
00291 
00292 // ---------------------------------------------------------------------
00293 
00294 void 
00295 QgarAppDescr::endElement(const string& uri, 
00296                          const string& localName,
00297                          const string& qName)
00298 {
00299   // Redirect action on the function matching the tag.
00300 
00301   if (localName == ELT_PARAM)
00302     {
00303       endParam();
00304     }
00305   else if (localName == ELT_NAME)
00306     {
00307       endName();
00308     }
00309   else if (localName == ELT_AUTHOR)
00310     {
00311       endAuthor();
00312     }
00313   else if (localName == ELT_COPYRIGHT)
00314     {
00315       endCopyright();
00316     }
00317   else if (localName == ELT_BRIEF)
00318     {
00319       endBrief();
00320     }
00321   else if (localName == ELT_LONG)
00322     {
00323       endLong();
00324     }
00325 }
00326   
00327 // ---------------------------------------------------------------------
00328 
00329 void
00330 QgarAppDescr::fatalError(const SAXParseException& exception)
00331 {
00332   throw runtime_error(exception.getMessage());
00333 }
00334 
00335 // ---------------------------------------------------------------------
00336 // A C C E S S
00337 // ---------------------------------------------------------------------
00338 
00339 string
00340 QgarAppDescr::name() const
00341 {
00342   return _name;
00343 }
00344 
00345 // ---------------------------------------------------------------------
00346 
00347 string
00348 QgarAppDescr::author() const
00349 {
00350   return _author;
00351 }
00352 
00353 // ---------------------------------------------------------------------
00354 string
00355 QgarAppDescr::copyright() const
00356 {
00357   return _copyright;
00358 }
00359 
00360 // ---------------------------------------------------------------------
00361 
00362 string
00363 QgarAppDescr::briefDoc() const
00364 {
00365   return _briefDoc;
00366 }
00367 
00368 // ---------------------------------------------------------------------
00369 
00370 string
00371 QgarAppDescr::longDoc() const
00372 {
00373   return _longDoc;
00374 }
00375 
00376 // ---------------------------------------------------------------------
00377 
00378 vector<QgarAppParamDescr *>
00379 QgarAppDescr::paramVect() const
00380 {
00381   vector<QgarAppParamDescr *> retval;
00382 
00383   transform(_paramVect.begin(),
00384             _paramVect.end(),
00385             back_inserter(retval),
00386             qstlCloneObject());
00387 
00388   return retval;
00389 }
00390 
00391 
00392 // ---------------------------------------------------------------------
00393 
00394 const string&
00395 QgarAppDescr::accessName() const
00396 {
00397   return _name;
00398 }
00399 
00400 // ---------------------------------------------------------------------
00401 
00402 const string&
00403 QgarAppDescr::accessAuthor() const
00404 {
00405   return _author;
00406 }
00407 
00408 // ---------------------------------------------------------------------
00409 const string&
00410 QgarAppDescr::accessCopyright() const
00411 {
00412   return _copyright;
00413 }
00414 
00415 // ---------------------------------------------------------------------
00416 
00417 const string&
00418 QgarAppDescr::accessBriefDoc() const
00419 {
00420   return _briefDoc;
00421 }
00422 
00423 // ---------------------------------------------------------------------
00424 
00425 const string&
00426 QgarAppDescr::accessLongDoc() const
00427 {
00428   return _longDoc;
00429 }
00430 
00431 // ---------------------------------------------------------------------
00432 
00433 const vector<QgarAppParamDescr *>&
00434 QgarAppDescr::accessParamVect() const
00435 {
00436   return _paramVect;
00437 }
00438 
00439 
00440 
00441 // ---------------------------------------------------------------------
00442 // T R A N S F O R M A T I O N
00443 // ----------------------------------------------------------------------
00444 
00445 void
00446 QgarAppDescr::setName(const string& name)
00447 {
00448   _name = name;
00449 }
00450 
00451 // ----------------------------------------------------------------------
00452 
00453 void
00454 QgarAppDescr::setAuthor(const string& author)
00455 {
00456   _author = author;
00457 }
00458 
00459 // ----------------------------------------------------------------------
00460 void
00461 QgarAppDescr::setCopyright(const string& copyright)
00462 {
00463   _copyright = copyright;
00464 }
00465 
00466 // ----------------------------------------------------------------------
00467 
00468 void
00469 QgarAppDescr::setBriefDoc(const string& briefDoc)
00470 {
00471   _briefDoc = briefDoc;
00472 }
00473 
00474 // ----------------------------------------------------------------------
00475 
00476 void
00477 QgarAppDescr::setLongDoc(const string& longDoc)
00478 {
00479   _longDoc = longDoc;
00480 }
00481 
00482 // ----------------------------------------------------------------------
00483 
00484 void
00485 QgarAppDescr::setParamVect(const vector<QgarAppParamDescr*>& pVect)
00486 {
00487   // Delete all previous items
00488   for_each(_paramVect.begin(), _paramVect.end(), qstlDeleteObject());
00489   
00490   // Insert new ones
00491   // Duplicate parameters vector
00492   insert_iterator < vector<QgarAppParamDescr *> >
00493     ii(_paramVect, _paramVect.begin());
00494   transform(pVect.begin(), pVect.end(), ii, qstlCloneObject());
00495 }
00496 
00497 
00498 
00499 // ---------------------------------------------------------------------
00500 // P A R S I N G   M E T H O D S
00501 // ----------------------------------------------------------------------
00502 
00503 void
00504 QgarAppDescr::startParam(const Attributes& atts)
00505 {
00506   // Set flag indicating that we're parsing a parameter now
00507   _parseData->state = PARAM;
00508 
00509   // Reset current parameter
00510   delete  _parseData->currentParam;
00511   _parseData->currentParam = new QgarAppParamDescr();
00512 
00513 
00514   //-- Read Parameter Attributes
00515 
00516   for (int i = 0; i < atts.getLength(); ++i)
00517     {
00518       string name  = atts.getLocalName(i);
00519       string value = atts.getValue(i);
00520 
00521       if (name == "name")
00522         {
00523           (_parseData->currentParam)->setName(value);
00524         }
00525 
00526 
00527       else if (name == "flag")
00528         {
00529           (_parseData->currentParam)->setFlag(value);
00530         }
00531 
00532 
00533       else if (name == "required")
00534         {
00535           if (value == "false")
00536             {
00537               (_parseData->currentParam)->setRequired(false);
00538             }
00539           else
00540             {
00541               (_parseData->currentParam)->setRequired(true);
00542             }
00543         }
00544 
00545 
00546       else if (name == "passing-mode")
00547         {
00548           QgarAppParamDescr::ParamPassing mode;
00549 
00550           if (value == "in")
00551             {
00552               mode = QgarAppParamDescr::QGE_IN;
00553             }
00554           else if (value == "out")
00555             {
00556               mode = QgarAppParamDescr::QGE_OUT;
00557             }
00558           else if (value == "inout")
00559             {
00560               mode = QgarAppParamDescr::QGE_INOUT;
00561             }
00562 
00563           (_parseData->currentParam)->setPassingMode(mode);
00564         }
00565 
00566 
00567       else if (name == "type")
00568         {
00569           if ( (value == "grayscale") ||
00570                (value == "binary") ||
00571                (value == "vectorial") )
00572             {
00573               (_parseData->currentParam)->setType(QgarAppParamDescr::QGE_IMAGE);
00574             }
00575           else
00576             {
00577               (_parseData->currentParam)->setType(QgarAppParamDescr::QGE_NUM);
00578             }
00579         }
00580 
00581 
00582       else if (name == "format")
00583         {
00584           QgarAppParamDescr::ParamFormat format;
00585 
00586           if (value == "PBM")
00587             {
00588               format = QgarAppParamDescr::QGE_PBM;
00589             }
00590           else if (value == "PPM")
00591             {
00592               format = QgarAppParamDescr::QGE_PPM;
00593             }
00594           else if (value == "PGM")
00595             {
00596               format = QgarAppParamDescr::QGE_PGM;
00597             }
00598           else if (value == "DXF")
00599             {
00600               format = QgarAppParamDescr::QGE_DXF;
00601             }
00602           else if (value == "SVG")
00603             {
00604               format = QgarAppParamDescr::QGE_SVG;
00605             }
00606           else if (value == "int")
00607             {
00608               format = QgarAppParamDescr::QGE_INT;
00609             }
00610           else if (value == "real")
00611             {
00612               format = QgarAppParamDescr::QGE_REAL;
00613             }
00614           else
00615             {
00616               format = QgarAppParamDescr::QGE_UNKNOWN_FORMAT;
00617             }
00618 
00619           (_parseData->currentParam)->setFormat(format);
00620         }
00621 
00622 
00623       else if (name == "default")
00624         {
00625           (_parseData->currentParam)->setDefaultValue(value);      
00626         }
00627 
00628 
00629       else if (name == "min")
00630         {
00631           (_parseData->currentParam)->setMinValue(value);      
00632         }
00633 
00634 
00635       else if (name == "max")
00636         {
00637           (_parseData->currentParam)->setMaxValue(value);      
00638         }
00639     }
00640 }
00641 
00642 // ---------------------------------------------------------------------
00643 
00644 void
00645 QgarAppDescr::endParam()
00646 {
00647   // A parameter has been parsed, store it in the parameter vect.
00648   addParamDescr(_parseData->currentParam);
00649 
00650   _parseData->state = MAIN;
00651 }
00652 
00653 // ---------------------------------------------------------------------
00654 
00655 void
00656 QgarAppDescr::endName()
00657 {
00658   setName(_parseData->buffer);
00659 }
00660 
00661 // ---------------------------------------------------------------------
00662 
00663 void
00664 QgarAppDescr::endAuthor()
00665 {
00666   setAuthor(_parseData->buffer);
00667 }
00668 
00669 // ---------------------------------------------------------------------
00670 
00671 void
00672 QgarAppDescr::endCopyright()
00673 {
00674   setCopyright(_parseData->buffer);
00675 }
00676 
00677 // ---------------------------------------------------------------------
00678 
00679 void
00680 QgarAppDescr::endBrief()
00681 {
00682   if (_parseData->state == MAIN)
00683     setBriefDoc(_parseData->buffer);
00684 
00685   else
00686     (_parseData->currentParam)->setBriefDoc(_parseData->buffer);
00687 }
00688 
00689 // ---------------------------------------------------------------------
00690 
00691 void
00692 QgarAppDescr::endLong()
00693 {
00694   if (_parseData->state == MAIN)
00695     setLongDoc(_parseData->buffer);
00696 
00697   else
00698     (_parseData->currentParam)->setLongDoc(_parseData->buffer);
00699 }
00700 
00701 
00702 
00703 // ---------------------------------------------------------------------
00704 // O P E R A T O R S
00705 // ----------------------------------------------------------------------
00706 
00707 // Assignment operator
00708 QgarAppDescr& QgarAppDescr::operator=(const QgarAppDescr& rhs)
00709 {
00710   if (this != &rhs) {
00711 
00712     _name      = rhs._name;
00713     _copyright = rhs._copyright;
00714     _briefDoc  = rhs._briefDoc;
00715     _longDoc   = rhs._longDoc;
00716     _paramVect = rhs._paramVect;
00717 
00718     _parseData = (rhs._parseData)? new ParseData(*rhs._parseData) : 0;
00719   }
00720   
00721   return *this;
00722 }
00723 
00724 // ----------------------------------------------------------------------
00725 
00726 } // namespace qgar