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

QgarAppDescr.H

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 #ifndef __QGARAPPDESCR_H_INCLUDED__
00029 #define __QGARAPPDESCR_H_INCLUDED__
00030 
00031 
00032 /**
00033  * @file   QgarAppDescr.H
00034  * @brief  Header file of class qgar::QgarAppDescr.
00035  *
00036  * @author <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Jan Rendek">Jan Rendek</a>
00037  * @date   May 14, 2003  15:29
00038  * @since  Qgar 2.1.1
00039  */
00040 
00041 
00042 // For RCS/CVS use: Do not delete
00043 /* $Id: QgarAppDescr.H,v 1.10 2005/10/14 17:05:47 masini Exp $ */
00044 
00045 
00046 
00047 // STD
00048 #include <stdexcept>
00049 #include <string>
00050 #include <vector>
00051 // QGAR
00052 #include <qgarlib/QgarAppParamDescr.H>
00053 #include <qgarlib/sax/DefaultHandler.H>
00054 #include <qgarlib/sax/InputSource.H>
00055 namespace qgar
00056 {
00057   // Avoid #include's when not necessary
00058   class QgarAppParamDescr;
00059 }
00060 // XML
00061 namespace qgxml
00062 {
00063   // Avoid #include's when not necessary
00064   class Attributes;
00065   class InputSource;
00066   class SAXParseException;
00067 }
00068 
00069 
00070 
00071 namespace qgar
00072 {
00073 
00074 
00075 /**
00076  * @ingroup APP
00077  *
00078  * @class QgarAppDescr QgarAppDescr.H "qgarlib/QgarAppDescr.H"
00079  *
00080  * @brief Parses an XML description of a Qgar application
00081  *   and builds its representation.
00082  *
00083  * Each Qgar application is linked to an XML document containing its
00084  * description and the description of its parameter.
00085  * 
00086  * The main elements of the description are:
00087  * - its name
00088  * - its author
00089  * - the copyright attached to it
00090  * - its documentation
00091  * - the description of each of its parameters.
00092  *
00093  * This class allows the user to create a representation of
00094  * such a description by taking care of the parsing of the XML
00095  * document. Once this representation is created, the user can query
00096  * the various elements composing the description.
00097  *
00098  * Each parameter description is stored into an instance of
00099  * class qgar::QgarAppParamDescr.
00100  *
00101  * The user may also serialize instances of this class to XML using
00102  * the qgar::QgarAppDescr::toXml() function member.
00103  *
00104  * @todo Insert a link to the document DTD.
00105  * 
00106  * @see Class qgar::QgarAppParamDescr
00107  * 
00108  * @author <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Jan Rendek">Jan Rendek</a>
00109  * @date   May 14, 2003  15:29
00110  * @since  Qgar 2.1.1
00111  */
00112 class QgarAppDescr
00113 
00114   : public qgxml::DefaultHandler
00115 
00116 {
00117 // -------------------------------------------------------------------
00118 // P U B L I C    M E M B E R S
00119 // -------------------------------------------------------------------
00120 public:
00121 
00122   /** 
00123    * @name TAGs  
00124    * 
00125    * @brief The tags composing the XML document describing an
00126    * application description.
00127    */
00128   //@{
00129 
00130   static const char * const ELT_APPLICATION;
00131   static const char * const ELT_DESCR;
00132   static const char * const ELT_DOCUMENTATION;
00133   static const char * const ELT_PARAMLIST;
00134   static const char * const ELT_PARAM;
00135   static const char * const ELT_NAME;
00136   static const char * const ELT_AUTHOR;
00137   static const char * const ELT_COPYRIGHT;
00138   static const char * const ELT_DOC;
00139   static const char * const ELT_BRIEF;
00140   static const char * const ELT_LONG;
00141   
00142   static const char * const ATT_REVISION;
00143   static const char * const ATT_LANG;
00144   static const char * const ATT_NAME;
00145   static const char * const ATT_FLAG;
00146   static const char * const ATT_REQUIRED;
00147   static const char * const ATT_PASSING_MODE;
00148   static const char * const ATT_FORMAT;
00149   static const char * const ATT_TYPE;
00150 
00151 //@}
00152 
00153 
00154   /** @name Constructors */
00155   //        ============
00156   //@{
00157 
00158   /** 
00159    * @brief Build a QgarAppDescr from a file
00160    *
00161    * @param filename The file to create the description from.
00162    */
00163   explicit QgarAppDescr(const char * const filename) 
00164     throw (std::runtime_error);
00165   
00166   /**
00167    * @brief Build a QgarAppDescr from a string
00168    *
00169    * @param filename The name of the file containing the XML document
00170    * to load the description from.
00171    */
00172   explicit QgarAppDescr(const std::string& filename) 
00173     throw (std::runtime_error);
00174 
00175   /**
00176    * @brief Build a QgarAppDescr from an input stream
00177    *
00178    * @param stream The input stream to parse the XML document from.
00179    */
00180   explicit QgarAppDescr(std::istream& stream) 
00181     throw (std::runtime_error);
00182 
00183   /**
00184    * @brief Build a QgarAppDescr from given data.
00185    *
00186    * @param name      The name of the application.
00187    * @param copyright The copyright notice attached to the application.
00188    * @param briefDoc  The brief documentation of this application.
00189    * @param longDoc   The detailed documentation of this application.
00190    * @param paramVect The descriptions of the application parameters.
00191    */
00192   QgarAppDescr(const std::string& name,
00193                const std::string& copyright,
00194                const std::string& briefDoc,
00195                const std::string& longDoc,
00196                const std::vector<QgarAppParamDescr *>& paramVect);
00197 
00198   /**
00199    * @brief Copy constructor
00200    */
00201   QgarAppDescr(const QgarAppDescr&);
00202 
00203   //@}
00204 
00205 
00206   /** @name Destructors */
00207   //        ===========
00208   //@{
00209 
00210   /**
00211    * @brief Destructor
00212    */
00213   virtual ~QgarAppDescr();
00214   
00215   //@}
00216 
00217 
00218   /** @name Parameters */
00219   //        ==========
00220   //@{
00221 
00222   /**
00223    * @brief Add the description of a parameter.
00224    *
00225    * @param descr the description of the parameter to be added
00226    */
00227   void addParamDescr(QgarAppParamDescr * descr);
00228   
00229   //@}
00230 
00231 
00232   /** @name XML Serialization */
00233   //        =================
00234   //@{
00235 
00236   /**
00237    * @brief Builds an XML document representating this application
00238    *   description
00239    *
00240    * @return 
00241    */
00242   std::string toXml() const;
00243 
00244 //@}
00245   
00246   /** @name DefaultHandler Redefinitions */
00247   //        ============================
00248   //@{
00249 
00250   /**
00251    * @brief
00252    *
00253    * @todo To be documented!
00254    */
00255   virtual void characters(const char * ch, 
00256                           const unsigned int start, 
00257                           const unsigned int length);
00258 
00259   /**
00260    * @brief
00261    *
00262    * @todo To be documented!
00263    */
00264   virtual void startElement(const std::string& uri,
00265                             const std::string& localName,
00266                             const std::string& qName,
00267                             const qgxml::Attributes& atts);
00268   
00269   /**
00270    * @brief
00271    *
00272    * @todo To be documented!
00273    */
00274   virtual void endElement(const std::string& uri,
00275                           const std::string& localName, 
00276                           const std::string& qName);
00277 
00278   /**
00279    * @brief
00280    *
00281    * @todo To be documented!
00282    */
00283   virtual void fatalError(const qgxml::SAXParseException& exception);
00284   
00285   //@}
00286 
00287 
00288   /** @name Access */
00289   //        ======
00290   //@{
00291 
00292   /**
00293    * @brief Retrieve the name of the application.
00294    *
00295    * @return The name of the applcation, or an empty string if no name
00296    * was provided.
00297    */
00298   std::string name() const;
00299 
00300   /**
00301    * @brief Retrieve the author of the application.
00302    *
00303    * @return The author of the application, or an empty string if no name
00304    * was provided..
00305    */
00306   std::string author() const;
00307 
00308   /**
00309    * @brief Retrieve the copyright notice attached to the application
00310    *
00311    * @return The copyright for the application, or an empty string if no
00312    * notice was provided.
00313    */
00314   std::string copyright() const;
00315 
00316   /**
00317    * @brief Retrieve the brief documentation of the application.
00318    *
00319    * @return The brief documentation of the application, or an empty
00320    * string if no documenation was provided.
00321    */
00322   std::string briefDoc() const;
00323 
00324   /**
00325    * @brief Retrieve the detailed documentation for the application.
00326    *
00327    * @return The detailed documentation of the application, or an empty
00328    * string if no documentation was provided.
00329    */
00330   std::string longDoc() const;
00331 
00332   /**
00333    * @brief Retrieve the description of the parameters of the
00334    * application.
00335    *
00336    * @return A vector containing the memory representation of the
00337    * parameters description, or an empty vector if the application has
00338    * no parameter.
00339    */
00340   std::vector<QgarAppParamDescr *> paramVect() const;
00341 
00342 
00343   /**
00344    * @brief Access to the name of the application
00345    *
00346    * @return A reference on the name of the application, or on an empty
00347    * string if no name was provided.
00348    */
00349   const std::string& accessName() const;
00350 
00351   /**
00352    * @brief Access to the author information of the application
00353    *
00354    * @return A reference on the information about the author of the
00355    * application or an empty string if no author was provided.
00356    */
00357   const std::string& accessAuthor() const;
00358 
00359   /**
00360    * @brief Access to the copyright notice attached to the application.
00361    *
00362    * @return A reference on the copyright notice, or on an empty string
00363    * if no notice was provided.
00364    */
00365   const std::string& accessCopyright() const;
00366 
00367   /**
00368    * @brief Access to the brief documentation of the application.
00369    *
00370    * @return A reference on the brief documentation of the application,
00371    * or on an empty string if no documentation was provided.
00372    */
00373   const std::string& accessBriefDoc() const;
00374 
00375   /**
00376    * @brief Access to the detailed documentation of the application
00377    *
00378    * @return A reference on the detailed documentation of the
00379    * application or on an empty string if no detailed documentation was
00380    * provided.
00381    */
00382   const std::string& accessLongDoc() const;
00383 
00384   /**
00385    * @brief Access to the descriptions of the parameters of the
00386    * application.
00387    *
00388    * @return A reference on the vector containing the memory
00389    * representation of the  parameters description, or an empty vector
00390    * if the application has no parameter.
00391    */
00392   const std::vector<QgarAppParamDescr *>& accessParamVect() const;
00393 
00394 //@}
00395 
00396   
00397   /** @name Transformation */
00398   //        ==============
00399   //@{
00400 
00401   /**
00402    * @brief Sets the name of the application.
00403    *
00404    * @param name The new name of the application.
00405    */
00406   void setName(const std::string& name);
00407 
00408   /**
00409    * @brief Sets the name of the author of the application.
00410    *
00411    * @param author The new name of the author(s) of the application.
00412    */
00413   void setAuthor(const std::string& author);
00414 
00415   /**
00416    * @brief Sets the copyright notice for the application.
00417    *
00418    * @param copyright The new copyright notice for the application.
00419    */
00420   void setCopyright(const std::string& copyright);
00421 
00422   /**
00423    * @brief Sets the brief documentation of the application.
00424    *
00425    * @param briefDoc The new brief documentation of the application.
00426    */
00427   void setBriefDoc(const std::string& briefDoc);
00428 
00429   /**
00430    * @brief Sets the detailed documentation of the application.
00431    *
00432    * @param longDoc The new detailed documentation of the application.
00433    */
00434   void setLongDoc(const std::string& longDoc);
00435 
00436   /**
00437    * @brief Sets the descriptions of the application parameters.
00438    *
00439    * @param paramVect A vector containing the new descriptions of the
00440    * application parameters.
00441    */
00442   void setParamVect(const std::vector<QgarAppParamDescr *>& paramVect);
00443 
00444   //@}
00445 
00446 
00447   /** @name Operators */
00448   //        =========
00449   //@{
00450 
00451   /**
00452    * @brief Assignment operator
00453    */
00454   QgarAppDescr& operator=(const QgarAppDescr& rhs);
00455 
00456   //@}
00457 
00458 
00459 // -------------------------------------------------------------------
00460 // P R O T E C T E D    M E M B E R S
00461 // -------------------------------------------------------------------
00462 protected:
00463 
00464   /**
00465    * @brief Creates an instance from an XML document located by an input
00466    * source.
00467    *
00468    * @param source The input source referencing the XML document to
00469    * create an instance from.
00470    *
00471    * @throw std::runtime_error This exception is thrown if the input
00472    * source could not be accessed, or if an error occured while parsing
00473    * the XML document.
00474    */  
00475   void init(qgxml::InputSource source) throw (std::runtime_error);
00476 
00477 
00478   /** 
00479    * @name Methods used for parsing elements 
00480    * @internal
00481    * @brief Theses methods are callbacks invoked from the DefaultHandler
00482    * function members implementations. 
00483    */
00484   //@{
00485 
00486   /**
00487    * @internal
00488    * @brief Callback invoked when the parser starts parsing a new
00489    * parameter section.
00490    *
00491    * The parameter sections are enclosed in a <ELT_PARAM> tag.
00492    *
00493    * @param atts The attributes of the <ELT_PARAM> element.
00494    *
00495    * @see qgar::QgarAppDescr::endParam()
00496    */
00497   void startParam(const qgxml::Attributes& atts);
00498 
00499   /**
00500    * @internal
00501    * @brief Callback invoked when the parser has finished the parsing of
00502    * a parameter section
00503    *
00504    * The parameter sections are enclosed by <ELT_PARAM> tags.
00505    *
00506    * @see qgar::QgarAppDescr::startParam()
00507    */
00508   void endParam();
00509 
00510   /**
00511    * @internal
00512    * @brief Callback invoked when the parser has finished the parsing of
00513    * the name section.
00514    *
00515    * The name section is enclosed in a pair of <ELT_NAME> </ELT_NAME>
00516    * tags.
00517    */
00518   void endName();
00519 
00520   /**
00521    * @internal
00522    * @brief Callback invoked when the parser has finished the parsing of
00523    * the author section.
00524    *
00525    * The author section is enclosed in a pair of <ELT_AUTHOR>
00526    * <ELT_AUTHOR/> tags.
00527    */
00528   void endAuthor();
00529 
00530   /**
00531    * @internal
00532    * @brief Callabck invoked when the parser has finished the parsing of
00533    * the copyright section.
00534    *
00535    * The copyright section is enclosed in a pair of <ELT_COPYRIGHT>
00536    * <ELT_COPYRIGHT/> tags.
00537    */
00538   void endCopyright();
00539 
00540   /**
00541    * @internal
00542    * @brief Callabck invoked when the parser has finished the parsing of
00543    * the brief documentation section.
00544    *
00545    * The copyright section is enclosed in a pair of <ELT_BRIEF>
00546    * <ELT_BRIEF/> tags.
00547    */
00548   void endBrief();
00549 
00550   /**
00551    * @internal
00552    * @brief Callabck invoked when the parser has finished the parsing of
00553    * the detailed documentation section.
00554    *
00555    * The copyright section is enclosed in a pair of <ELT_LONG>
00556    * <ELT_LONG/> tags.
00557    */
00558   void endLong();
00559 
00560   //@}
00561 
00562 
00563   /// The name of the application.
00564   std::string _name;
00565 
00566   /// The author of the application
00567   std::string _author;
00568 
00569   /// The copyright notice
00570   std::string _copyright;
00571 
00572   /// The brief documentation
00573   std::string _briefDoc;
00574 
00575   /// The documentation verbose version.
00576   std::string _longDoc;
00577 
00578   /// The descriptions of the application parameters
00579   std::vector<QgarAppParamDescr *> _paramVect;
00580 
00581 
00582   /** @name Data Used for Parsing */
00583   //        =====================
00584   //@{
00585 
00586   /**
00587    * @internal
00588    */
00589   enum ParseState
00590     {
00591       MAIN,
00592       PARAM
00593     };
00594 
00595   /**
00596    * @internal
00597    * @brief Structure holding all the data used while parsing.
00598    * @ingroup APP
00599    *
00600    * Using a structure to regroup all these data allows to dynamically
00601    * allocate the room to store it. This space can the be desallocated
00602    * when parsing is over. This means the that the overall memory cost
00603    * for these data is 1 pointer size when no parsing is performed.
00604    */
00605   struct ParseData
00606   {
00607 
00608     /**
00609      * @brief Default constructor
00610      */
00611     ParseData() : state(MAIN), firstElt(true), currentParam(0)
00612     {
00613       /* EMPTY */
00614     }
00615 
00616     /**
00617      * @brief Copy-Constructor
00618      */
00619     ParseData(const ParseData& rhs) : 
00620       state(rhs.state), firstElt(rhs.firstElt)
00621     { 
00622       currentParam = (rhs.currentParam) ? 
00623         new QgarAppParamDescr(*rhs.currentParam) : 0;
00624     }
00625 
00626     /**
00627      * @brief Destructor
00628      */
00629     ~ParseData() {
00630       delete currentParam;
00631     }
00632 
00633     /// Parsing state: Are we parsing the application or a param ?
00634     ParseState state;
00635     
00636     /**
00637      * @brief Flag used to indicate if the parsed element is the first
00638      * being parsed
00639      *
00640      * This flag is mainly used to test if the XML document is of the
00641      * correct document tag (ie. first tag is 'application').
00642      */
00643     bool firstElt;
00644 
00645     /// Buffer to store parsed characters of an element
00646     std::string buffer;
00647     
00648     /**
00649      * @brief Pointer to the current parameter being parsed.
00650      */
00651     qgar::QgarAppParamDescr* currentParam;
00652     
00653   } *_parseData; /**<
00654                   * @internal 
00655                   * @brief A pointer to the struct holding parsing data
00656                   */ 
00657   //@}
00658 
00659 // -------------------------------------------------------------------
00660 }; // class QgarAppDescr
00661 
00662 
00663 } // namespace qgar
00664 
00665 
00666 #endif /* __QGARAPPDESCR_H_INCLUDED__ */