Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

clause_body_string.cc

00001 /*
00002 *  Name:      clause_body_string.cc
00003 *  Author:    Rafael Jesus Alcantara Perez
00004 *  Summary:   Clause-body string
00005 *  Date:      $Date: 2003/04/14 00:18:35 $
00006 *  Revision:  $Revision: 1.1 $
00007 *
00008 *  Copyright (C) 1994-2002  Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00009 *
00010 *  This program is free software; you can redistribute it and/or modify
00011 *  it under the terms of the GNU General Public License as published by
00012 *  the Free Software Foundation; either version 2 of the License, or
00013 *  (at your option) any later version.
00014 *
00015 *  This program is distributed in the hope that it will be useful,
00016 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 *  GNU General Public License for more details.
00019 *
00020 *  You should have received a copy of the GNU General Public License
00021 *  along with this program; if not, write to the Free Software
00022 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00023 *  MA 02111-1307, USA.
00024 */
00025 
00026 #include <cstdarg>
00027 #include <mpcl/text/codegen/clause_body_string.hh>
00028 #include <mpcl/text/regex/matcher.hh>
00029 
00030 
00031 //
00032 //  C O N S T R U C T O R S
00033 //
00034 
00035 mpcl::text::codegen::TClauseBodyString& mpcl::text::codegen::TClauseBodyString::
00036 operator = (const TClauseBodyString& rkySOURCE_CLAUSE_BODY)
00037 {
00038 
00039   TString::operator = (rkySOURCE_CLAUSE_BODY);
00040   return *this;
00041   
00042 }  // operator =()
00043 
00044 
00045 void mpcl::text::codegen::TClauseBodyString::
00046 read (std::basic_istream<char_type, traits_type>& rtSOURCE_ISTREAM)
00047 {
00048 
00049   using text::regex::TMatcher;
00050   using std::basic_string;
00051 
00052   TMatcher   tMatcher (rtSOURCE_ISTREAM);
00053   
00054   tMatcher.setCaseSensitiveness (false);
00055   tMatcher.scan (pkcCLAUSE_BodyPattern, (basic_string<char>*) this, NULL);
00056 
00057 }  // read()
00058 
00059 
00060 //
00061 //  S E L E C T O R S
00062 //
00063 
00064 mpcl::text::TString mpcl::text::codegen::TClauseBodyString::
00065 instantiate ( const char* pkcFIRST_PARAMETER_NAME     ,
00066               const char* pkcFIRST_PARAMETER_VALUE... ) const
00067 {
00068   
00069   //
00070   //  yParameterTag ::= <%><parameter><%>
00071   //
00072   
00073   const char*   pkcTag;
00074   const char*   pkcValue;
00075   va_list       tVa_list;
00076   TString       yParameterTag;
00077   TString       yInstance (preprocess());
00078 
00079   //
00080   //  Then, replace parameters.
00081   //
00082   yParameterTag = Format ("%%%s%%", pkcFIRST_PARAMETER_NAME);
00083   if ( !gParameterExistenceSensitiveness )
00084   {
00085     yInstance.replaceAll (yParameterTag, pkcFIRST_PARAMETER_VALUE);
00086     va_start (tVa_list, pkcFIRST_PARAMETER_VALUE);
00087     while ( NULL != (pkcTag = va_arg (tVa_list, char*)) )
00088     { 
00089       if ( NULL == (pkcValue = va_arg (tVa_list, char*)) )
00090       {
00091         throw TBadInstantiateSyntaxException ("you must give tag-value pairs", __FILE__, __LINE__);
00092       }
00093       else
00094       {
00095         yParameterTag = Format ("%%%s%%", pkcTag);
00096         yInstance.replaceAll (yParameterTag, pkcValue);
00097       }
00098     }
00099   }
00100   else
00101   {
00102     if ( !yInstance.replaceAll (yParameterTag, pkcFIRST_PARAMETER_VALUE) )
00103     {
00104       TString   yMessage = Format ( "there is no tag called '%s' in this clause code" ,
00105                                     pkcFIRST_PARAMETER_NAME                           );
00106       
00107       throw TBadInstantiateSyntaxException (yMessage, __FILE__, __LINE__);
00108     }
00109     else
00110     {
00111       va_start (tVa_list, pkcFIRST_PARAMETER_VALUE);
00112       while ( NULL != (pkcTag = va_arg (tVa_list, char*)) )
00113       { 
00114         if ( NULL == (pkcValue = va_arg (tVa_list, char*)) )
00115         {
00116           throw TBadInstantiateSyntaxException ("you must give tag-value pairs", __FILE__, __LINE__);
00117         }
00118         else
00119         {
00120           yParameterTag = Format ("%%%s%%", pkcTag);
00121           if ( !yInstance.replaceAll (yParameterTag, pkcValue) )
00122           {
00123             TString   yMessage = Format ("there is no tag called '%s' in this clause code", pkcTag);
00124               
00125             throw TBadInstantiateSyntaxException (yMessage, __FILE__, __LINE__);
00126           }
00127         }
00128       }
00129     }
00130   }
00131   return yInstance;
00132   
00133 }  // instantiate()
00134 
00135 
00136 mpcl::text::TString mpcl::text::codegen::TClauseBodyString::
00137 instantiate ( const TStringToVariableMap& rktSOURCE_VARIABLE_MAP             ,
00138               bool                        gPARAMETER_EXISTENCE_SENSITIVENESS ) const
00139 {
00140   
00141   //
00142   //  yParameterTag ::=  <%><parameter><%>
00143   //
00144   
00145   TString                                yParameterTag;
00146   TString                                yInstance = preprocess();
00147   TStringToVariableMap::const_iterator   I         = rktSOURCE_VARIABLE_MAP.begin();
00148   TStringToVariableMap::const_iterator   ktEnd     = rktSOURCE_VARIABLE_MAP.end();
00149   
00150   //
00151   //  Replace parameters.
00152   //
00153   if ( !gPARAMETER_EXISTENCE_SENSITIVENESS )
00154   {
00155     for (; ( I != ktEnd ) ;++I)
00156     {
00157       yParameterTag = Format ("%%%s%%", I->first.c_str());
00158       yInstance.replaceAll (yParameterTag, I->second);
00159     }
00160   }
00161   else
00162   {
00163     for (; ( I != ktEnd ) ;++I)
00164     {
00165       yParameterTag = Format ("%%%s%%", I->first.c_str());
00166       if ( !yInstance.replaceAll (yParameterTag, I->second) )
00167       {
00168         if ( I->second.type() == TVariableString::eNormal )
00169         {
00170           //
00171           //  If parameter name isn't optional then
00172           //  there is an error.
00173           //
00174           TString   yMessage = Format ( "there is no tag called '%s' in this clause code" ,
00175                                         I->first.c_str()                                  );
00176           
00177           throw TBadInstantiateSyntaxException (yMessage, __FILE__, __LINE__);
00178         }
00179       }
00180     }
00181   }
00182   return yInstance;
00183   
00184 }  // instantiate()
00185 
00186 
00187 mpcl::text::TString mpcl::text::codegen::TClauseBodyString::
00188 instantiate ( const TStringToStringMap& rktSOURCE_STRING_MAP               ,
00189               bool                      gPARAMETER_EXISTENCE_SENSITIVENESS ) const
00190 {
00191   
00192   //
00193   //  yParameterTag ::= <%><parameter><%>
00194   //
00195   
00196   TString                              yParameterTag;
00197   TString                              yInstance = preprocess();
00198   TStringToStringMap::const_iterator   I         = rktSOURCE_STRING_MAP.begin();
00199   TStringToStringMap::const_iterator   ktEnd     = rktSOURCE_STRING_MAP.end();
00200   
00201   //
00202   //  Replace parameters.
00203   //
00204   if ( !gPARAMETER_EXISTENCE_SENSITIVENESS )
00205   {
00206     for (; ( I != ktEnd ) ;++I)
00207     {
00208       yParameterTag = Format ("%%%s%%", I->first.c_str());
00209       yInstance.replaceAll (yParameterTag, I->second);
00210     }
00211   }
00212   else
00213   {
00214     for (; ( I != ktEnd ) ;++I)
00215     {
00216       yParameterTag = Format ("%%%s%%", I->first.c_str());
00217       if ( !yInstance.replaceAll (yParameterTag, I->second) )
00218       {
00219         //
00220         //  If parameter name isn't optional then
00221         //  there is an error.
00222         //
00223         TString   yMessage = Format ( "there is no tag called '%s' in this clause code" ,
00224                                       I->first.c_str()                                  );
00225           
00226         throw TBadInstantiateSyntaxException (yMessage, __FILE__, __LINE__);
00227       }
00228     }
00229   }
00230   return yInstance;
00231   
00232 }  // instantiate()
00233 
00234 
00235 void mpcl::text::codegen::TClauseBodyString::
00236 write (std::basic_ostream<char_type, traits_type>& rtTARGET_OSTREAM) const
00237 {
00238   
00239   operator << (rtTARGET_OSTREAM, (std::string) *this);
00240   rtTARGET_OSTREAM << "</clause>\n";
00241   
00242 }  // write()
00243 
00244 
00245 mpcl::text::TString mpcl::text::codegen::TClauseBodyString::
00246 preprocess (void) const
00247 {
00248   
00249   TString   yInstance (*this);
00250   
00251   //
00252   //  Replace first, all entities.
00253   //
00254   InstantiateEntities (yInstance);
00255   return yInstance;
00256   
00257 }  // preprocess()

Generated on Mon Oct 13 02:35:22 2003 for MPCL by doxygen1.2.18