00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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<<" -g generate an xml instance for a top level element in the schema"<<std::endl;
00044 std::cout<<" -v Verbose mode"<<std::endl;
00045 cout << endl;
00046 }
00047
00048 int
00049 main (int argc, char *argv[])
00050 {
00051 ifstream schfs;
00052 ifstream insfs;
00053 SchemaParser * sp=0;
00054 bool brkloop =false;
00055 bool accept_password =false;
00056 unsigned char lvl = 0;
00057 bool genInstance = false,validate=false;
00058 int i =1;
00059 for (;i<argc && !brkloop;){
00060 switch(argv[i][0]){
00061 case '-':
00062 {
00063 std::string opt(argv[i]+1);
00064 if (opt=="v"){
00065 lvl = 2;
00066 i++;
00067 }
00068 else if (opt == "g"){
00069
00070 genInstance = true;
00071 i++;
00072 }
00073 else if (opt == "v") {
00074 validate=true;
00075 i++;
00076 }
00077 else if (opt == "x"){
00078 opt = argv[i+1];
00079 size_t pos=opt.find(':');
00080 XmlUtils::setProxyHost (opt);
00081 if(pos==std::string::npos){
00082
00083 XmlUtils::setProxyHost (XmlUtils::getProxyHost () + ":80");
00084 }
00085 XmlUtils::setProxy (true);
00086 i+=2;
00087 }
00088 else if (opt == "U"){
00089 opt = argv[i+1];
00090 size_t pos=opt.find(':');
00091 XmlUtils::setProxyUser (opt.substr(0,pos));
00092 if(pos!=std::string::npos)
00093 XmlUtils::setProxyPass (opt.substr(pos+1));
00094 else
00095 accept_password = true;
00096 i+=2;
00097 XmlUtils::setProxy (true);
00098 }
00099 else if (opt =="h"){
00100 usage();
00101 exit(0);
00102 }
00103 else{
00104 std::cerr<<"Unknown option "<<argv[i]<<std::endl;
00105 usage();
00106 exit(2);
00107 }
00108 break;
00109 }
00110 default:
00111 brkloop = true;
00112
00113 break;
00114 }
00115 }
00116
00117 if (XmlUtils::getProxy () && accept_password){
00118
00119 XmlUtils::setProxyPass (XmlUtils::acceptSecretKey("Proxy Password"));
00120 std::cout<<endl;
00121 }
00122
00123 if (i < argc){
00124
00125 sp = new SchemaParser (argv[i]);
00126 i++;
00127 }
00128 else
00129 {
00130 usage();
00131 return 2;
00132 }
00133
00134 try{
00135
00136 if (!sp)
00137 return 1;
00138 sp->setWarningLevel(lvl);
00139 if (sp->parseSchemaTag ())
00140 {
00141 if (lvl >=2)
00142 cout << "Successfully parsed schema " <<sp->getNamespace() << endl;
00143
00144 }
00145 else {
00146
00147 std::cerr<<"Could not successfully parse "<<argv[i-1]<<std::endl;
00148 std::cerr<<"Run the command with -v option for more details"<<std::endl;
00149 return 1;
00150 }
00151
00152 if (genInstance ) {
00153
00154 std::string elemName;
00155
00156 Schema::Element element;
00157 const Schema::SchemaParser::ElementList & el = sp->getElements();
00158
00159 if (i <=argc && argv[i]){
00160 bool found = false;
00161 elemName = std::string(argv[i]);
00162
00163 for ( Schema::SchemaParser::ElementList::const_iterator eli= el.begin();
00164 eli!=el.end() && !found;
00165 eli++)
00166 {
00167 if (eli->getName() == elemName){
00168 found = true;
00169 element = *eli;
00170
00171
00172 }
00173 }
00174 if (!found) {
00175
00176 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;
00177 return 1;
00178 }
00179 }
00180 else {
00181 int n = 0;
00182 for ( Schema::SchemaParser::ElementList::const_iterator eli= el.begin();
00183 eli!=el.end();
00184 eli++,n++)
00185 {
00186 if (n !=0)
00187 std::cout<<n<<"."<<eli->getName()<<std::endl;
00188 }
00189 if (n<= 1){
00190 std::cout<<"No top level elements to generate instance.. exiting"<<std::endl;
00191 return 0;
00192 }
00193 std::cout<<"Which element should I generate an instance for [1.."<<n-1<<"]?";
00194 std::cin>>n;
00195
00196 n++;
00197 for ( Schema::SchemaParser::ElementList::const_iterator eli1= el.begin();
00198 eli1!=el.end() && n ;
00199 eli1++,n--) element = *eli1;
00200 }
00201
00202 SchemaValidator * sv = new SchemaValidator(sp);
00203 return sv->instance(element.getName(),(Schema::Type)element.getType());
00204 }
00205 else if (i <argc )
00206 {
00207 std::string xmlDoc;
00208 XmlUtils::fetchUri(argv[i+1],xmlDoc);
00209 insfs.open (xmlDoc.c_str());
00210 if (insfs.fail ())
00211 {
00212 cerr << "An Error occrred while opening " << argv[i+1] << endl;
00213 return 1;
00214 }
00215 i++;
00216 XmlPullParser * xpp = new XmlPullParser (insfs);
00217 xpp->setFeature (FEATURE_PROCESS_NAMESPACES, true);
00218 xpp->require (XmlPullParser::START_DOCUMENT, "", "");
00219 SchemaValidator * sv= new SchemaValidator(sp);
00220 while (xpp->getEventType () != xpp->END_DOCUMENT)
00221 {
00222 xpp->nextTag ();
00223 if (xpp->getEventType () == xpp->END_DOCUMENT)
00224 break;
00225 Qname elemName (xpp->getName ());
00226 elemName.setNamespace(xpp->getNamespace());
00227 const Element * e = sp->getElement (elemName);
00228 if(e){
00229 int typeId = e->getType () ;
00230
00231
00232
00233 TypeContainer * t = sv->validate (xpp, typeId);
00234
00235 cout << "{"<<elemName.getNamespace () << "}" << elemName.
00236 getLocalName ()<<std::endl;
00237
00238
00239
00240 t->print(cout);
00241 std::cout<<std::endl;
00242 delete t;
00243 }
00244 else{
00245
00246 Qname typ(xpp->getAttributeValue(Schema::SchemaInstaceUri, "type"));
00247 typ.setNamespace(xpp->getNamespace(typ.getPrefix()));
00248
00249 if (typ.getNamespace() == sp->getNamespace() ||
00250 typ.getNamespace().empty()) {
00251 int typeId = sp->getTypeId(typ);
00252
00253 TypeContainer * t = sv->validate (xpp, typeId);
00254
00255 cout << "{"<<elemName.getNamespace () << "}" << elemName.
00256 getLocalName ()<<std::endl;
00257
00258
00259
00260 t->print(cout);
00261 std::cout<<std::endl;
00262 delete t;
00263
00264 }
00265 else {
00266 std::cerr<<"Unknown element {"<<elemName.getNamespace()<<"}"<<elemName.getLocalName()<<std::endl;
00267 }
00268 }
00269
00270 }
00271 }
00272 delete sp;
00273 return 0;
00274 }
00275
00276
00277 catch (SchemaParserException spe)
00278 {
00279 cerr<<"An Exception occurred ...@"<<spe.line
00280 <<":"<<spe.col<<endl;
00281
00282 cerr<<spe.description<<endl;
00283 }
00284 catch (XmlPullParserException xpe)
00285 {
00286 cerr<<"An Exception occurred ...@"<<xpe.line
00287 <<":"<<xpe.col<<endl;
00288
00289 cerr<<xpe.description<<endl;
00290 }
00291 return 1;
00292 }