XMLOutput.h

Go to the documentation of this file.
00001 
00002 /****************************************************************************** 
00003  * 
00004  *  file:  XMLOutput.h
00005  * 
00006  *  Copyright (c) 2004, Michael E. Smoot
00007  *  All rights reverved.
00008  * 
00009  *  See the file COPYING in the top directory of this distribution for
00010  *  more information.
00011  *  
00012  *  THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 
00013  *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
00014  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
00015  *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
00016  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
00017  *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
00018  *  DEALINGS IN THE SOFTWARE.  
00019  *  
00020  *****************************************************************************/ 
00021 
00022 #ifndef TCLAP_XMLOUTPUT_H
00023 #define TCLAP_XMLOUTPUT_H
00024 
00025 #include <string>
00026 #include <vector>
00027 #include <list>
00028 #include <iostream>
00029 #include <algorithm>
00030 
00031 #include <tclap/CmdLineInterface.h>
00032 #include <tclap/CmdLineOutput.h>
00033 #include <tclap/XorHandler.h>
00034 #include <tclap/Arg.h>
00035 
00036 namespace TCLAP {
00037 
00042 class XMLOutput : public CmdLineOutput
00043 {
00044 
00045   public:
00046 
00052     virtual void usage(CmdLineInterface& c);
00053 
00059     virtual void version(CmdLineInterface& c);
00060 
00067     virtual void failure(CmdLineInterface& c, 
00068                  ArgException& e );
00069 
00070   protected:
00071 
00078     void substituteSpecialChars( std::string& s, char r, std::string& x );
00079     void removeChar( std::string& s, char r);
00080 
00081     void printShortArg(Arg* it);
00082     void printLongArg(Arg* it);
00083 };
00084 
00085 
00086 inline void XMLOutput::version(CmdLineInterface& _cmd) 
00087 { 
00088   std::cout << _cmd.getVersion() << std::endl;
00089 }
00090 
00091 inline void XMLOutput::usage(CmdLineInterface& _cmd ) 
00092 {
00093   std::list<Arg*> argList = _cmd.getArgList();
00094   std::string progName = _cmd.getProgramName();
00095   std::string version = _cmd.getVersion();
00096   XorHandler xorHandler = _cmd.getXorHandler();
00097   std::vector< std::vector<Arg*> > xorList = xorHandler.getXorList();
00098 
00099 
00100   std::cout << "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << std::endl;
00101   std::cout << "<executable>" << std::endl;
00102   std::cout << "<name>" << generateSafeString ( progName ) << "</name>" << std::endl;
00103   std::cout << "<version>" << generateSafeString ( version ) << "</version>" << std::endl;
00104   std::cout << "<description><CDATA[" << description << "]]></description>" << std::endl;
00105 
00106   // Start the parameters
00107   std::cout << "<parameters>" << std::endl;
00108   
00109   // Do the xor's first
00110   for ( int i = 0; (unsigned int)i < xorList.size(); i++ )
00111     {
00112     std::cout << "<xor>" << std::endl;
00113     for ( ArgVectorIterator it = xorList[i].begin(); 
00114           it != xorList[i].end(); it++ )
00115       {
00116       printLongArg((*it));
00117       }
00118     std::cout << "</xor>" << std::endl;
00119     }
00120   
00121   // rest of args
00122   for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
00123     {
00124     if ( !xorHandler.contains( (*it) ) )
00125       {
00126       printLongArg((*it));
00127       }
00128     }
00129   std::cout << "</parameters>" << std::endl;
00130   std::cout << "</executable>" << std::endl;
00131 }
00132 
00133 inline void XMLOutput::failure( CmdLineInterface& _cmd,
00134             ArgException& e ) 
00135 { 
00136     std::cout << e.what() << std::endl;
00137 }
00138 
00139 inline void XMLOutput::substituteSpecialChars( std::string& s,
00140              char r,
00141                            std::string& x )
00142 {
00143   std::string::size_type p;
00144   while ( (p = s.find_first_of(r)) != std::string::npos )
00145   {
00146     s.erase(p,1);
00147     s.insert(p,x);
00148   }
00149 }
00150 
00151 inline void XMLOutput::removeChar( std::string& s, char r)
00152 {
00153   std::string::size_type p;
00154   while ( (p = s.find_first_of(r)) != std::string::npos )
00155   {
00156     s.erase(p,1);
00157   }
00158 }
00159 
00160   inline std::string XMLOutput::generateSafeString ( std::string s )
00161   {
00162     std::string lt = "&lt;"; 
00163     std::string gt = "&gt;";
00164     std::string amp = "&amp;";
00165     std::string apos = "&apos;";
00166     std::string quot = "&quot;";
00167     
00168     std::out ( s );
00169     substituteSpecialChars(out,'<',lt);
00170     substituteSpecialChars(out,'>',gt);
00171     substituteSpecialChars(out,'&',amp);
00172     substituteSpecialChars(out,'\'',apos);
00173     substituteSpecialChars(out,'"',quot);
00174     removeChar(out,'[');
00175     removeChar(out,']');
00176     return out;
00177   }
00178     
00179   
00180 inline void XMLOutput::printShortArg(Arg* a)
00181 {
00182   std::string lt = "&lt;"; 
00183   std::string gt = "&gt;"; 
00184 
00185   std::string id = a->shortID();
00186   substituteSpecialChars(id,'<',lt);
00187   substituteSpecialChars(id,'>',gt);
00188   removeChar(id,'[');
00189   removeChar(id,']');
00190   
00191   std::string choice = "opt";
00192   if ( a->isRequired() )
00193     choice = "req";
00194 
00195   std::string repeat = "norepeat";
00196   if ( a->acceptsMultipleValues() )
00197     repeat = "repeat";
00198 
00199     
00200         
00201   std::cout << "<arg choice='" << choice 
00202         << "' repeat='" << repeat << "'>" 
00203         << id << "</arg>" << std::endl; 
00204 
00205 }
00206 
00207 inline void XMLOutput::printLongArg(Arg* a)
00208 {
00209 
00210   std::string id = a->longID();
00211   std::string desc = a->getDescription();
00212 
00213   
00214   if ( a->isValueRequired() )
00215     {
00216     std::cout << "<value ";
00217     }
00218   else
00219     {
00220     std::cout << "<switch ";
00221     }
00222 
00223   std::cout << "name=\"" << generateSafeString ( a->getName() ) << " ";
00224   std::cout << "flag=\"" << generateSafeString ( a->getFlag() ) << " ";
00225   if ( a->isRequired() )
00226     {
00227     std::cout << "required=\"true\" ";
00228     }
00229   else
00230     {
00231     std::cout << "required=\"false\" ";
00232     }
00233   if ( a->acceptsMultipleValues() )
00234     {
00235     std::cout << "allowrepeats=\"true\" ";
00236     }
00237   else
00238     {
00239     std::cout << "allowrepeats=\"false\" ";
00240     }
00241   std::cout << "type=\"" << a->getTypeDescription() << "\" ";
00242   std::cout << ">" << std::endl;
00243   
00244   
00245   
00246 
00247   std::cout << "<simplelist>" << std::endl;
00248 
00249   std::cout << "<member>" << std::endl;
00250   std::cout << id << std::endl;
00251   std::cout << "</member>" << std::endl;
00252 
00253   std::cout << "<member>" << std::endl;
00254   std::cout << desc << std::endl;
00255   std::cout << "</member>" << std::endl;
00256 
00257   std::cout << "</simplelist>" << std::endl;
00258 }
00259 
00260 } //namespace TCLAP
00261 #endif 

Generated on 6 Apr 2011 for Slicer3 by  doxygen 1.6.1