src/tools/schema.cpp

00001 /* 
00002  * wsdlpull - A C++ parser  for WSDL  (Web services description language)
00003  * Copyright (C) 2005-2007 Vivek Krishna
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public
00016  * License along with this library; if not, write to the Free
00017  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018  */
00019 
00020 //This file parses a sample schema document and validates/generates an instance
00021   
00022 #include <iostream>
00023 #include <fstream>
00024 #include <string>
00025 #include "xmlpull/XmlPullParser.h"
00026 #include "xmlpull/XmlPullParserException.h"
00027 #include "schemaparser/SchemaParser.h"
00028 #include "schemaparser/SchemaValidator.h"
00029 #include "schemaparser/TypeContainer.h"
00030 #include "schemaparser/SchemaParserException.h"
00031 using namespace std;
00032 using namespace Schema;
00033 
00034 void
00035 usage(void)
00036 {
00037   cout << "Usage: schema [options] <schema_file_name> [-i] <schema instance file name>"<<endl;
00038   cout << "Example:schema po.xsd -i po.xsi"<<endl;
00039   cout << "Example:schema first-building-blocks.xsd -i first.xml "<<endl;
00040   std::cout<<"Options"<<std::endl;
00041   std::cout<<"   -x host[:port] Use HTTP proxy on given port"<<std::endl;
00042   std::cout<<"   -U user[:password] Specify Proxy authentication"<<std::endl;
00043   std::cout<<"   -v Verbose mode"<<std::endl;
00044   cout << endl;
00045 }
00046 
00047 int 
00048 main (int argc, char *argv[]) 
00049 {
00050   ifstream schfs;
00051   ifstream insfs;
00052   SchemaParser * sp=0;
00053   bool brkloop =false;
00054   bool accept_password =false;
00055   unsigned char  lvl = 0;
00056   bool genInstance = false;
00057   int i =1;
00058   for (;i<argc && !brkloop;){
00059     switch(argv[i][0]){
00060     case '-'://option
00061       {
00062         std::string opt(argv[i]+1);
00063         if (opt=="v"){
00064           lvl = 2;
00065           i++;
00066         }
00067         else if (opt == "g"){
00068 
00069           genInstance = true;
00070           i++;
00071         }
00072         else if (opt == "x"){
00073           opt = argv[i+1];
00074           size_t pos=opt.find(':');
00075           XmlUtils::setProxyHost (opt);
00076           if(pos==std::string::npos){
00077             
00078             XmlUtils::setProxyHost (XmlUtils::getProxyHost () + ":80");
00079           }
00080           XmlUtils::setProxy (true);
00081           i+=2;
00082         }
00083         else if (opt == "U"){
00084           opt = argv[i+1];
00085           size_t pos=opt.find(':');
00086           XmlUtils::setProxyUser (opt.substr(0,pos));
00087           if(pos!=std::string::npos)
00088             XmlUtils::setProxyPass (opt.substr(pos+1));
00089           else
00090             accept_password = true;
00091           i+=2;
00092           XmlUtils::setProxy (true);
00093         }
00094         else if (opt =="h"){
00095           usage();
00096           exit(0);
00097         }
00098         else{
00099           std::cerr<<"Unknown option "<<argv[i]<<std::endl;
00100           usage();
00101           exit(2);
00102         }
00103         break;
00104       }
00105     default:
00106       brkloop = true;
00107       //end of options
00108       break;
00109     }
00110   }
00111 
00112   if (XmlUtils::getProxy () && accept_password){
00113      
00114     XmlUtils::setProxyPass (XmlUtils::acceptSecretKey("Proxy Password"));
00115     std::cout<<endl;
00116   }
00117 
00118   if (i < argc){
00119 
00120     sp = new SchemaParser (argv[i]);
00121     i++;
00122   }
00123   else
00124     {
00125       usage();
00126       return 2;
00127     }
00128   
00129   try{
00130 
00131     if (!sp)
00132       return 1;
00133     sp->setWarningLevel(lvl);
00134     if (sp->parseSchemaTag ())
00135       {
00136         if (lvl >=2)
00137           cout << "Successfully parsed schema  " <<sp->getNamespace() << endl;
00138         //sp->print (cout);
00139       }
00140     else {
00141 
00142       std::cerr<<"Could not parse "<<argv[i-1]<<std::endl;
00143       return 1;
00144     }
00145   
00146     if (genInstance ) {
00147 
00148       std::string elemName;
00149 
00150       Schema::Element element;
00151       const Schema::SchemaParser::ElementList & el = sp->getElements(); 
00152       //the global element for which to generate the instance
00153       if (i <=argc && argv[i]){
00154         bool found = false;
00155         elemName = std::string(argv[i]);
00156                   
00157         for ( Schema::SchemaParser::ElementList::const_iterator eli= el.begin();
00158               eli!=el.end() && !found;
00159               eli++)
00160           {
00161             if (eli->getName()  == elemName){ 
00162               found = true;
00163               element = *eli;
00164               
00165               
00166             }
00167           }
00168         if (!found) {
00169                           
00170           std::cerr<<"element '"<<elemName<<"' not found in the schema.Try 'schema -g "<<argv[2]<<"'  to see the list of elements in the schema"<<std::endl;
00171           return 1;       
00172         }
00173       }
00174       else {
00175         int n = 0;
00176         for ( Schema::SchemaParser::ElementList::const_iterator eli= el.begin();
00177               eli!=el.end();
00178               eli++,n++)
00179           {
00180             if (n !=0)
00181               std::cout<<n<<"."<<eli->getName()<<std::endl;
00182           }
00183         std::cout<<"Which element should I generate an instance for [1.."<<n-1<<"]?";
00184         std::cin>>n;
00185 
00186         n++; // locate the element in the list (first element bydefault is <schema> so skip it
00187         for ( Schema::SchemaParser::ElementList::const_iterator eli= el.begin();
00188               eli!=el.end() && n ;
00189               eli++,n--) element = *eli;
00190       } 
00191   
00192       SchemaValidator * sv = new SchemaValidator(sp);
00193       return sv->instance(element.getName(),(Schema::Type)element.getType());    
00194     }
00195     else if (i <argc )
00196       {
00197         std::string xmlDoc;
00198         XmlUtils::fetchUri(argv[i+1],xmlDoc);
00199         insfs.open (xmlDoc.c_str());    //open the schema instance file
00200         if (insfs.fail ())
00201           {
00202             cerr << "An Error occrred while opening " << argv[i+1] << endl;
00203             return 1;
00204           }
00205         i++;
00206         XmlPullParser * xpp = new XmlPullParser (insfs);
00207         xpp->setFeature (FEATURE_PROCESS_NAMESPACES, true);
00208         xpp->require (XmlPullParser::START_DOCUMENT, "", "");
00209         SchemaValidator * sv= new SchemaValidator(sp);
00210         while (xpp->getEventType () != xpp->END_DOCUMENT)
00211           {
00212             xpp->nextTag ();
00213             if (xpp->getEventType () == xpp->END_DOCUMENT)
00214               break;
00215             Qname elemName (xpp->getName ());
00216             elemName.setNamespace(xpp->getNamespace());
00217             const Element * e = sp->getElement (elemName);
00218             if(e){
00219               int typeId = e->getType () ;
00220               //for each element in the instance doc we call the
00221               //validator with the parser instance of the instance file
00222               // and the element's type identifier
00223               TypeContainer * t = sv->validate (xpp, typeId);
00224 
00225               cout << "{"<<elemName.getNamespace () << "}" << elemName. 
00226                 getLocalName ()<<std::endl;
00227               //once validated the element instance is stored
00228               //in the type container from which values can be
00229               //obtained or just printed
00230               t->print(cout);
00231               std::cout<<std::endl;
00232             }else{
00233               std::cerr<<"Unkown element "<<elemName.getLocalName()<<std::endl;
00234             }
00235           }
00236       }
00237     delete sp;
00238     return 0;
00239   }
00240   catch (SchemaParserException spe)
00241     {
00242       cerr<<"An Exception occurred ...@"<<spe.line
00243           <<":"<<spe.col<<endl;
00244 
00245       cerr<<spe.description<<endl;
00246     }
00247   catch (XmlPullParserException xpe)
00248     {
00249       cerr<<"An Exception occurred ...@"<<xpe.line
00250           <<":"<<xpe.col<<endl;
00251 
00252       cerr<<xpe.description<<endl;
00253     }
00254   return 1;
00255 }

Generated on Sun Aug 5 20:01:34 2007 for wsdlpull by  doxygen 1.4.6