wsdlpull
1.23
Toggle main menu visibility
Loading...
Searching...
No Matches
src
tools
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>
25
#include "
xmlpull/XmlPullParser.h
"
26
#include "
xmlpull/XmlPullParserException.h
"
27
#include "
schemaparser/SchemaParser.h
"
28
#include "
schemaparser/SchemaValidator.h
"
29
#include "
schemaparser/TypeContainer.h
"
30
#include "
schemaparser/SchemaParserException.h
"
31
using namespace
std;
32
using namespace
Schema
;
33
34
void
35
usage
(
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
48
int
49
main
(
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(
':'
);
80
XmlUtils::setProxyHost
(opt);
81
if
(pos==std::string::npos){
82
83
XmlUtils::setProxyHost
(
XmlUtils::getProxyHost
() +
":80"
);
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
119
XmlUtils::setProxyPass
(
XmlUtils::acceptSecretKey
(
"Proxy Password"
));
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;
157
const
Schema::SchemaParser::ElementList
& el = sp->
getElements
();
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);
217
xpp->
setFeature
(
FEATURE_PROCESS_NAMESPACES
,
true
);
218
xpp->
require
(
XmlPullParser::START_DOCUMENT
,
""
,
""
);
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
246
Qname
typ(xpp->
getAttributeValue
(
Schema::SchemaInstaceUri
,
"type"
));
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
}
SchemaParserException.h
SchemaParser.h
SchemaValidator.h
TypeContainer.h
XmlPullParserException.h
XmlPullParser.h
FEATURE_PROCESS_NAMESPACES
#define FEATURE_PROCESS_NAMESPACES
Definition
XmlPullParser.h:40
Qname
Definition
Qname.h:31
Qname::getLocalName
std::string getLocalName(void) const
Definition
Qname.h:76
Qname::setNamespace
void setNamespace(std::string uri)
Definition
Qname.h:97
Qname::getPrefix
std::string getPrefix(void) const
Definition
Qname.h:83
Qname::getNamespace
std::string getNamespace(void) const
Definition
Qname.h:90
Schema::Element
Definition
Element.h:31
Schema::Element::getType
int getType() const
Definition
Element.h:147
Schema::Element::getName
std::string getName() const
Definition
Element.h:125
Schema::SchemaParserException
Definition
SchemaParserException.h:29
Schema::SchemaParserException::line
int line
Definition
SchemaParserException.h:34
Schema::SchemaParserException::description
std::string description
Definition
SchemaParserException.h:33
Schema::SchemaParserException::col
int col
Definition
SchemaParserException.h:34
Schema::SchemaParser
Definition
SchemaParser.h:43
Schema::SchemaParser::getNamespace
std::string getNamespace(void) const
Definition
SchemaParser.cpp:1888
Schema::SchemaParser::setWarningLevel
void setWarningLevel(int l)
Definition
SchemaParser.h:417
Schema::SchemaParser::getElements
const ElementList & getElements() const
Definition
SchemaParser.h:403
Schema::SchemaParser::parseSchemaTag
bool parseSchemaTag()
Definition
SchemaParser.cpp:148
Schema::SchemaParser::ElementList
std::list< Element > ElementList
Definition
SchemaParser.h:50
Schema::SchemaParser::getElement
const Element * getElement(const Qname &element, bool checkImports=true) const
Definition
SchemaParser.cpp:1757
Schema::SchemaParser::getTypeId
int getTypeId(const Qname &, bool create=false)
Definition
SchemaParser.cpp:1613
Schema::SchemaValidator
Definition
SchemaValidator.h:35
Schema::SchemaValidator::instance
bool instance(const std::string &tag, Schema::Type type_id)
Definition
SchemaValidator.cpp:894
Schema::SchemaValidator::validate
TypeContainer * validate(XmlPullParser *xpp, int typeId, TypeContainer *ipTc=0)
Definition
SchemaValidator.cpp:55
Schema::TypeContainer
Definition
TypeContainer.h:44
Schema::TypeContainer::print
void print(std::ostream &os)
Definition
TypeContainer.cpp:340
XmlPullParserException
Definition
XmlPullParserException.h:31
XmlPullParserException::description
std::string description
Definition
XmlPullParserException.h:46
XmlPullParserException::line
int line
Definition
XmlPullParserException.h:47
XmlPullParserException::col
int col
Definition
XmlPullParserException.h:47
XmlPullParser
Definition
XmlPullParser.h:47
XmlPullParser::nextTag
int nextTag()
Definition
XmlPullParser.cpp:1191
XmlPullParser::END_DOCUMENT
@ END_DOCUMENT
Definition
XmlPullParser.h:128
XmlPullParser::START_DOCUMENT
@ START_DOCUMENT
Definition
XmlPullParser.h:127
XmlPullParser::getEventType
int getEventType()
Definition
XmlPullParser.h:105
XmlPullParser::require
void require(int type, std::string ns, std::string name)
Definition
XmlPullParser.cpp:1207
XmlPullParser::getName
std::string getName()
Definition
XmlPullParser.h:79
XmlPullParser::getNamespace
std::string getNamespace(std::string prefix)
Definition
XmlPullParser.cpp:972
XmlPullParser::getAttributeValue
std::string getAttributeValue(int index)
Definition
XmlPullParser.cpp:1126
XmlPullParser::setFeature
void setFeature(std::string feature, bool value)
Definition
XmlPullParser.cpp:1238
Schema
Definition
Annotation.h:27
Schema::Type
Type
Definition
Schema.h:60
Schema::SchemaInstaceUri
const std::string SchemaInstaceUri
Definition
Schema.h:93
XmlUtils::getProxy
bool WSDLPULL_EXPORT getProxy()
Definition
XmlUtils.cpp:326
XmlUtils::getProxyHost
std::string WSDLPULL_EXPORT getProxyHost()
Definition
XmlUtils.cpp:340
XmlUtils::setProxyPass
void WSDLPULL_EXPORT setProxyPass(const std::string &sProxyPass)
Definition
XmlUtils.cpp:375
XmlUtils::fetchUri
bool WSDLPULL_EXPORT fetchUri(std::string uri, std::string &path)
Definition
XmlUtils.cpp:108
XmlUtils::acceptSecretKey
std::string WSDLPULL_EXPORT acceptSecretKey(const std::string &field)
Definition
XmlUtils.cpp:250
XmlUtils::setProxy
void WSDLPULL_EXPORT setProxy(const bool bProxy)
Definition
XmlUtils.cpp:333
XmlUtils::setProxyUser
void WSDLPULL_EXPORT setProxyUser(const std::string &sProxyUser)
Definition
XmlUtils.cpp:361
XmlUtils::setProxyHost
void WSDLPULL_EXPORT setProxyHost(const std::string &sProxyHost)
Definition
XmlUtils.cpp:347
main
int main(int argc, char *argv[])
Definition
schema.cpp:49
usage
void usage(void)
Definition
schema.cpp:35
Generated by
1.17.0