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 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   int i =1;
00057   for (;i<argc && !brkloop;){
00058     switch(argv[i][0]){
00059     case '-'://option
00060       {
00061         std::string opt(argv[i]+1);
00062         if (opt=="v"){
00063           lvl = 2;
00064           i++;
00065         }
00066         else if (opt == "x"){
00067           opt = argv[i+1];
00068           size_t pos=opt.find(':');
00069           XmlUtils::setProxyHost (opt);
00070           if(pos==std::string::npos){
00071             
00072             XmlUtils::setProxyHost (XmlUtils::getProxyHost () + ":80");
00073           }
00074           XmlUtils::setProxy (true);
00075           i+=2;
00076         }
00077         else if (opt == "U"){
00078           opt = argv[i+1];
00079           size_t pos=opt.find(':');
00080           XmlUtils::setProxyUser (opt.substr(0,pos));
00081           if(pos!=std::string::npos)
00082             XmlUtils::setProxyPass (opt.substr(pos+1));
00083           else
00084             accept_password = true;
00085           i+=2;
00086           XmlUtils::setProxy (true);
00087         }
00088         else if (opt =="h"){
00089           usage();
00090           exit(0);
00091         }
00092         else{
00093           std::cerr<<"Unknown option "<<argv[i]<<std::endl;
00094           usage();
00095           exit(2);
00096         }
00097         break;
00098       }
00099     default:
00100       brkloop = true;
00101       //end of options
00102       break;
00103     }
00104   }
00105 
00106   if (XmlUtils::getProxy () && accept_password){
00107      
00108     XmlUtils::setProxyPass (XmlUtils::acceptSecretKey("Proxy Password"));
00109     std::cout<<endl;
00110   }
00111 
00112   if (i < argc){
00113 
00114     sp = new SchemaParser (argv[i]);
00115     i++;
00116   }
00117   else
00118     {
00119       usage();
00120       return 2;
00121     }
00122   
00123   try{
00124 
00125     if (!sp)
00126       return 1;
00127     sp->setWarningLevel(lvl);
00128     if (sp->parseSchemaTag ())
00129       {
00130         cout << "Successfully parsed schema  " <<sp->getNamespace() << endl;
00131         //sp->print (cout);
00132       }
00133     else return 1;
00134   
00135     if (i <argc )
00136       {
00137         std::string xmlDoc;
00138         XmlUtils::fetchUri(argv[i+1],xmlDoc);
00139         insfs.open (xmlDoc.c_str());    //open the schema instance file
00140         if (insfs.fail ())
00141           {
00142             cerr << "An Error occrred while opening " << argv[i+1] << endl;
00143             return 1;
00144           }
00145         i++;
00146         XmlPullParser * xpp = new XmlPullParser (insfs);
00147         xpp->setFeature (FEATURE_PROCESS_NAMESPACES, true);
00148         xpp->require (XmlPullParser::START_DOCUMENT, "", "");
00149         SchemaValidator * sv= new SchemaValidator(sp);
00150         while (xpp->getEventType () != xpp->END_DOCUMENT)
00151           {
00152             xpp->nextTag ();
00153             if (xpp->getEventType () == xpp->END_DOCUMENT)
00154               break;
00155             Qname elemName (xpp->getName ());
00156             elemName.setNamespace(xpp->getNamespace());
00157             const Element * e = sp->getElement (elemName);
00158             if(e){
00159               int typeId = e->getType () ;
00160               //for each element in the instance doc we call the
00161               //validator with the parser instance of the instance file
00162               // and the element's type identifier
00163               TypeContainer * t = sv->validate (xpp, typeId);
00164 
00165               cout << "{"<<elemName.getNamespace () << "}" << elemName. 
00166                 getLocalName ()<<std::endl;
00167               //once validated the element instance is stored
00168               //in the type container from which values can be
00169               //obtained or just printed
00170               t->print(cout);
00171               std::cout<<std::endl;
00172             }else{
00173               std::cerr<<"Unkown element "<<elemName.getLocalName()<<std::endl;
00174             }
00175           }
00176       }
00177     delete sp;
00178     return 0;
00179   }
00180   catch (SchemaParserException spe)
00181     {
00182       cerr<<"An Exception occurred ...@"<<spe.line
00183           <<":"<<spe.col<<endl;
00184 
00185       cerr<<spe.description<<endl;
00186     }
00187   catch (XmlPullParserException xpe)
00188     {
00189       cerr<<"An Exception occurred ...@"<<xpe.line
00190           <<":"<<xpe.col<<endl;
00191 
00192       cerr<<xpe.description<<endl;
00193     }
00194   return 1;
00195 }
00196 

Generated on Sun Nov 26 03:04:43 2006 for wsdlpull by  doxygen 1.4.6