wsdlpull svntrunk
Loading...
Searching...
No Matches
schema.cpp
Go to the documentation of this file.
1/*
2 * wsdlpull - A C++ parser for WSDL (Web services description language)
3 * Copyright (C) 2005-2007 Vivek Krishna
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20//This file parses a sample schema document and validates/generates an instance
21
22#include <iostream>
23#include <fstream>
24#include <string>
31using namespace std;
32using namespace Schema;
33
34void
35usage(void)
36{
37 cout << "Usage: schema [options] <schema_file_name> [-i schema instance file name]"<<endl;
38 cout << "Example:schema po.xsd -i po.xsi"<<endl;
39 cout << "Example:schema first-building-blocks.xsd -i first.xml "<<endl;
40 std::cout<<"Options"<<std::endl;
41 std::cout<<" -x host[:port] Use HTTP proxy on given port"<<std::endl;
42 std::cout<<" -U user[:password] Specify Proxy authentication"<<std::endl;
43 std::cout<<" -g generate an xml instance for a top level element in the schema"<<std::endl;
44 std::cout<<" -v Verbose mode"<<std::endl;
45 cout << endl;
46}
47
48int
49main (int argc, char *argv[])
50{
51 ifstream schfs;
52 ifstream insfs;
53 SchemaParser * sp=0;
54 bool brkloop =false;
55 bool accept_password =false;
56 unsigned char lvl = 0;
57 bool genInstance = false,validate=false;
58 int i =1;
59 for (;i<argc && !brkloop;){
60 switch(argv[i][0]){
61 case '-'://option
62 {
63 std::string opt(argv[i]+1);
64 if (opt=="v"){
65 lvl = 2;
66 i++;
67 }
68 else if (opt == "g"){
69
70 genInstance = true;
71 i++;
72 }
73 else if (opt == "v") {
74 validate=true;
75 i++;
76 }
77 else if (opt == "x"){
78 opt = argv[i+1];
79 size_t pos=opt.find(':');
81 if(pos==std::string::npos){
82
84 }
85 XmlUtils::setProxy (true);
86 i+=2;
87 }
88 else if (opt == "U"){
89 opt = argv[i+1];
90 size_t pos=opt.find(':');
91 XmlUtils::setProxyUser (opt.substr(0,pos));
92 if(pos!=std::string::npos)
93 XmlUtils::setProxyPass (opt.substr(pos+1));
94 else
95 accept_password = true;
96 i+=2;
97 XmlUtils::setProxy (true);
98 }
99 else if (opt =="h"){
100 usage();
101 return 0;
102 }
103 else{
104 std::cerr<<"Unknown option "<<argv[i]<<std::endl;
105 usage();
106 return 2;
107 }
108 break;
109 }
110 default:
111 brkloop = true;
112 //end of options
113 break;
114 }
115 }
116
117 if (XmlUtils::getProxy () && accept_password){
118
120 std::cout<<endl;
121 }
122
123 if (i < argc){
124
125 sp = new SchemaParser (argv[i]);
126 i++;
127 }
128 else
129 {
130 usage();
131 return 2;
132 }
133
134 try{
135
136 if (!sp)
137 return 1;
138 sp->setWarningLevel(lvl);
139 if (sp->parseSchemaTag ())
140 {
141 if (lvl >=2)
142 cout << "Successfully parsed schema " <<sp->getNamespace() << endl;
143 //sp->print (cout);
144 }
145 else {
146
147 std::cerr<<"Could not successfully parse "<<argv[i-1]<<std::endl;
148 std::cerr<<"Run the command with -v option for more details"<<std::endl;
149 return 1;
150 }
151
152 if (genInstance ) {
153
154 std::string elemName;
155
156 Schema::Element element;
158 //the global element for which to generate the instance
159 if (i <=argc && argv[i]){
160 bool found = false;
161 elemName = std::string(argv[i]);
162
163 for ( Schema::SchemaParser::ElementList::const_iterator eli= el.begin();
164 eli!=el.end() && !found;
165 eli++)
166 {
167 if (eli->getName() == elemName){
168 found = true;
169 element = *eli;
170
171
172 }
173 }
174 if (!found) {
175
176 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;
177 return 1;
178 }
179 }
180 else {
181 int n = 0;
182 for ( Schema::SchemaParser::ElementList::const_iterator eli= el.begin();
183 eli!=el.end();
184 eli++,n++)
185 {
186 if (n !=0)
187 std::cout<<n<<"."<<eli->getName()<<std::endl;
188 }
189 if (n<= 1){
190 std::cout<<"No top level elements to generate instance.. exiting"<<std::endl;
191 return 0;
192 }
193 std::cout<<"Which element should I generate an instance for [1.."<<n-1<<"]?";
194 std::cin>>n;
195
196 n++; // locate the element in the list (first element bydefault is <schema> so skip it
197 for ( Schema::SchemaParser::ElementList::const_iterator eli1= el.begin();
198 eli1!=el.end() && n ;
199 eli1++,n--) element = *eli1;
200 }
201
202 SchemaValidator * sv = new SchemaValidator(sp);
203 return sv->instance(element.getName(),(Schema::Type)element.getType());
204 }
205 else if (i <argc )
206 {
207 std::string xmlDoc;
208 XmlUtils::fetchUri(argv[i+1],xmlDoc);
209 insfs.open (xmlDoc.c_str()); //open the schema instance file
210 if (insfs.fail ())
211 {
212 cerr << "An Error occrred while opening " << argv[i+1] << endl;
213 return 1;
214 }
215 i++;
216 XmlPullParser * xpp = new XmlPullParser (insfs);
219 SchemaValidator * sv= new SchemaValidator(sp);
220 while (xpp->getEventType () != xpp->END_DOCUMENT)
221 {
222 xpp->nextTag ();
223 if (xpp->getEventType () == xpp->END_DOCUMENT)
224 break;
225 Qname elemName (xpp->getName ());
226 elemName.setNamespace(xpp->getNamespace());
227 const Element * e = sp->getElement (elemName);
228 if(e){
229 int typeId = e->getType () ;
230 //for each element in the instance doc we call the
231 //validator with the parser instance of the instance file
232 // and the element's type identifier
233 TypeContainer * t = sv->validate (xpp, typeId);
234
235 cout << "{"<<elemName.getNamespace () << "}" << elemName.
236 getLocalName ()<<std::endl;
237 //once validated the element instance is stored
238 //in the type container from which values can be
239 //obtained or just printed
240 t->print(cout);
241 std::cout<<std::endl;
242 delete t;
243 }
244 else{
245
247 typ.setNamespace(xpp->getNamespace(typ.getPrefix()));
248
249 if (typ.getNamespace() == sp->getNamespace() ||
250 typ.getNamespace().empty()) {
251 int typeId = sp->getTypeId(typ);
252
253 TypeContainer * t = sv->validate (xpp, typeId);
254
255 cout << "{"<<elemName.getNamespace () << "}" << elemName.
256 getLocalName ()<<std::endl;
257 //once validated the element instance is stored
258 //in the type container from which values can be
259 //obtained or just printed
260 t->print(cout);
261 std::cout<<std::endl;
262 delete t;
263
264 }
265 else {
266 std::cerr<<"Unknown element {"<<elemName.getNamespace()<<"}"<<elemName.getLocalName()<<std::endl;
267 }
268 }
269
270 }
271 }
272 delete sp;
273 return 0;
274 }
275
276
277 catch (SchemaParserException spe)
278 {
279 cerr<<"An Exception occurred ...@"<<spe.line
280 <<":"<<spe.col<<endl;
281
282 cerr<<spe.description<<endl;
283 }
284 catch (XmlPullParserException xpe)
285 {
286 cerr<<"An Exception occurred ...@"<<xpe.line
287 <<":"<<xpe.col<<endl;
288
289 cerr<<xpe.description<<endl;
290 }
291 return 1;
292}
#define FEATURE_PROCESS_NAMESPACES
Definition Qname.h:31
std::string getLocalName(void) const
Definition Qname.h:76
void setNamespace(std::string uri)
Definition Qname.h:97
std::string getPrefix(void) const
Definition Qname.h:83
std::string getNamespace(void) const
Definition Qname.h:90
int getType() const
Definition Element.h:147
std::string getName() const
Definition Element.h:125
std::string getNamespace(void) const
void setWarningLevel(int l)
const ElementList & getElements() const
std::list< Element > ElementList
const Element * getElement(const Qname &element, bool checkImports=true) const
int getTypeId(const Qname &, bool create=false)
bool instance(const std::string &tag, Schema::Type type_id)
TypeContainer * validate(XmlPullParser *xpp, int typeId, TypeContainer *ipTc=0)
void print(std::ostream &os)
void require(int type, std::string ns, std::string name)
std::string getName()
std::string getNamespace(std::string prefix)
std::string getAttributeValue(int index)
void setFeature(std::string feature, bool value)
const std::string SchemaInstaceUri
Definition Schema.h:93
bool WSDLPULL_EXPORT getProxy()
Definition XmlUtils.cpp:510
std::string WSDLPULL_EXPORT getProxyHost()
Definition XmlUtils.cpp:524
void WSDLPULL_EXPORT setProxyPass(const std::string &sProxyPass)
Definition XmlUtils.cpp:559
bool WSDLPULL_EXPORT fetchUri(std::string uri, std::string &path)
Definition XmlUtils.cpp:293
std::string WSDLPULL_EXPORT acceptSecretKey(const std::string &field)
Definition XmlUtils.cpp:434
void WSDLPULL_EXPORT setProxy(const bool bProxy)
Definition XmlUtils.cpp:517
void WSDLPULL_EXPORT setProxyUser(const std::string &sProxyUser)
Definition XmlUtils.cpp:545
void WSDLPULL_EXPORT setProxyHost(const std::string &sProxyHost)
Definition XmlUtils.cpp:531
int main(int argc, char *argv[])
Definition schema.cpp:49
void usage(void)
Definition schema.cpp:35