wsdlpull 1.23
Loading...
Searching...
No Matches
WsdlParser.cpp
Go to the documentation of this file.
1/*
2 * wsdlpull - A C++ parser for WSDL (Web services description
3 * language) 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 */
21
22#ifdef _WIN32
23#include <windows.h>
24#else
25#include <unistd.h>
26#endif
27
28#include "xmlpull/osdir.h"
30#include "wsdlparser/Soap.h"
31
32using namespace std;
33namespace WsdlPull{
34
36
37WsdlParser::WsdlParser(istream & in, ostream & out,
38 const std::string & schemaPath)
39 :errorOccured_(false),
40 ostr(out),
41 istr(in),
42 state_ (START),
43 element_(START),
44 Doc_(0),
45 xParser_(0),
46 MAX_EXT_XML(256),
47 schemaPath_(schemaPath)
48{
49 initialize(false);
50}
51
52WsdlParser::WsdlParser(const std::string & Uri, ostream & out,
53 const std::string & schemaPath)
54 :errorOccured_(false),
55 ostr(out),
56 istr(std::cin),
57 state_ (START),
58 element_(START),
59 Doc_(0),
60 xParser_(0),
61 MAX_EXT_XML(256),
62 schemaPath_(schemaPath)
63{
64 uri_ = Uri.substr(0,Uri.rfind('/') + 1);
65 if(XmlUtils::fetchUri(Uri,wsdlFileName))
66 {
67 xmlStream.open(wsdlFileName.c_str());
68 initialize(true);
69 }
70 else{
71 std::string e= "Unable to connect to ";
72 error(e + Uri);
73 }
74}
75
76void
77WsdlParser::initialize(bool file)
78{
79 if (schemaPath_.empty()) {
80
81#if defined SCHEMADIR
82 schemaPath_= SCHEMADIR;
83#else
84 schemaPath_= "src/schemas";
85#endif
86 }
87
88 if(file)
89 xParser_= new XmlPullParser(xmlStream);
90 else
91 xParser_= new XmlPullParser(istr);
92
94 xParser_->require(xParser_->START_DOCUMENT, "", "");
95 messages_.clear();
96 bindings_.clear();
97 porttypes_.clear();
98 wsdlExtensions_.clear();
99 schemaParser_.clear();
100
101 //add the schema for wsdl1.0 to parse arrayType
102 SchemaParser * sParser=0;
104
105 sParser = new SchemaParser (wsdlUri,wsdlUri,ostr);
106 }
107 else {
108
109 sParser = new SchemaParser (schemaPath_+"wsdl10.xsd",
110 wsdlUri,ostr,schemaPath_);
111
112 }
113 sParser->parseSchemaTag();
114 schemaParser_.push_back(sParser);
115
116
117 soap_ = new Soap(schemaPath_);
119 soap2_ = new Soap(schemaPath_,Soap::SOAP12);
121
122}
123
124
126{
127 size_t i = 0;
128 for (list < const Message * >::iterator mi =
129 messages_.begin(); mi != messages_.end();
130 mi++)
131 delete(*mi);
132 for (list < Binding * >::iterator bi =
133 bindings_.begin(); bi != bindings_.end();
134 bi++)
135 delete(*bi);
136
137 for (list < Service*>::iterator si =services_.begin();
138 si != services_.end();
139 si++)
140 delete(*si);
141
142 for (list < PortType * >::iterator pti =
143 porttypes_.begin(); pti != porttypes_.end();
144 pti++)
145 delete(*pti);
146
147 for (i = 0; i < schemaParser_.size(); i++)
148 delete schemaParser_[i];
149
150 // for (i = 0; i < Ops_.size(); i++)
151 // delete Ops_[i];
152
153 for (list < string * >::iterator sti =
154 docs_list_.begin(); sti != docs_list_.end();
155 sti++)
156 delete(*sti);
157
158 for (vector<ExtensionInfo>::iterator ie = wsdlExtensions_.begin();
159 ie != wsdlExtensions_.end();
160 ie++)
161 delete ie->we;
162
163 delete xParser_;
164 xmlStream.close();
165
166 // delete all the temp files
167 oslink::directory dir(".");
168 while (dir) {
169 std::string fname = dir.next();
170 if (fname.find(".wp-tmp") != std::string::npos)
171 {
172#ifdef WIN32
173 ::DeleteFile(fname.c_str());
174#else
175 unlink(fname.c_str());
176#endif
177 }
178 }
179}
180
181const Binding *
183{
184 if (element_ != BINDING)
185 {
186 error ("Attempted to extract a Binding when ,no binding was parsed",1);
187 return 0;
188 }
189 else
190 {
191 return bindings_.back();
192 }
193}
194
195void
197{
198 ExtensionInfo exi;
199 exi.we=ext;
200 exi.spe=0;
201 wsdlExtensions_.push_back(exi);
202}
203
204
205const Binding *
207{
208 Qname qn(q);
209 if (!qn.getPrefix().empty())
211 else
212 qn.setNamespace(tnsUri_);
213 if (tnsUri_ != qn.getNamespace())
214 return 0;
215 for (list <Binding * >::iterator pBinding =
216 bindings_.begin(); pBinding != bindings_.end();
217 pBinding++)
218 if ((*pBinding)->getName() == qn.getLocalName())
219 return *pBinding;
220 return 0;
221}
222
223
224const Service *
226{
227 if (element_ != SERVICE){
228
229 error ("Attempted to extract a Service when ,no service was parsed",1);
230 return 0;
231 }
232 else{
233
234 return services_.back();
235 }
236}
237
238const Service *
240{
241 Qname qn(q);
242 if (!qn.getPrefix().empty())
244 else
245 qn.setNamespace(tnsUri_);
246 if (tnsUri_ != qn.getNamespace())
247 return 0;
248
249 for (list <Service * >::iterator si =services_.begin();
250 si != services_.end();
251 si++)
252 if ((*si)->getName() == qn.getLocalName())
253 return *si;
254
255 return 0;
256}
257
258 void
260 {
261 if (services_.size() > 0)
262 {
263 from = services_.begin();
264 to = services_.end();
265 }
266 }
267
268const PortType *
270{
271 if (element_ != PORT_TYPE)
272 {
273 error ("Attempted to extract a PortType when ,no PortType was parsed",1);
274 return 0;
275 }
276 else
277 {
278 return porttypes_.back();
279 }
280}
281
282
283const PortType *
285{
286 string name = qn.getLocalName();
287
288 if (!qn.getPrefix().empty()){
289 if(getNamespace(qn.getPrefix())!=tnsUri_)
290 return 0;
291 }
292
293 for (PortType::cPortTypeIterator pPortType =porttypes_.begin();
294 pPortType != porttypes_.end();
295 pPortType++)
296 if ((*pPortType)->getName() == name)
297 return *pPortType;
298 return 0;
299}
300
301
302bool
306{
307 const PortType *pt = getPortType(portType);
308 if(pt){
309 return pt->getOperations(begin,end);
310 }
311 else
312 return false;
313}
314
315
316
317const Operation *
318WsdlParser::getOperation(const Qname & portType, const Qname & q)
319{
320 const PortType *pt = getPortType(portType);
321 int num = pt->getNumOps();
322 if (num > 0)
323 {
324 const Operation *op = NULL;
325 for (int i = 0; i < num; i++)
326 {
327 op = pt->getOperation(i);
328 if (op->getName() == q.getLocalName())
329 return op;
330 }
331 }
332 return 0;
333}
334
335
336const Message *
338{
339 if (element_ != MESSAGE)
340 {
341 error ("Attempted to extract a Message when ,no Message was parsed",1);
342 return 0;
343 }
344 else
345 {
346 return messages_.back();
347 }
348}
349
350
351const Message *
352WsdlParser::pgetMessage(const Qname & qn)
353{
354 const Message*m=getMessage(qn);
355 if(m==0){
356 Message* newMessage = new Message(*this);
357 newMessage->setName(qn.getLocalName());
358 putMessage(newMessage);
359 return newMessage;
360 }else{
361 return m;
362 }
363}
364
365
366const Message *
368{
369 string name = qn.getLocalName();
370 if(!qn.getNamespace().empty() &&
371 tnsUri_ != qn.getNamespace())
372 return 0;
373
374 for (list < const Message * >::iterator pMessage =
375 messages_.begin(); pMessage != messages_.end();
376 pMessage++)
377 if ((*pMessage)->getName() == name)
378 return *pMessage;
379
380 return 0;
381}
382
383
384const SchemaParser *
385WsdlParser::getSchemaParser(string targetNamespace) const
386{
387 if (targetNamespace == Schema::SchemaUri)
388 return 0;
389 for (size_t i = 0; i < schemaParser_.size(); i++){
390 if (schemaParser_[i]->getNamespace() == targetNamespace)
391 return (const SchemaParser *) schemaParser_[i];
392
393 if (schemaParser_[i]->isImported(targetNamespace)) {
394
395 return schemaParser_[i]->getImportedSchemaParser(targetNamespace);
396 }
397 }
398 return 0;
399}
400
401
402
405{
406 if (id >= 0)
407 return true;
408
409 else
410 return false;
411}
412
413
414int
415WsdlParser::peek(bool lookahead)
416{
417
418 //event Type returned by XML pull parser
419 int event_type, tmp_event_type = xParser_->getEventType();
420 int tmpState = state_;
421 if (state_ == END)
422 return state_;
423
424 do
425 {
426 if (lookahead == true || state_ == START || state_ == NONE)
427 xParser_->nextTag();
428
429 else
430 return state_;
431 event_type = xParser_->getEventType();
432 string tag = xParser_->getName();
433 switch (event_type)
434 {
436 if (state_ != START)
437 error("Syntax error at the start");
438 break;
440 if (xParser_->getNamespace() != wsdlUri
441 && xParser_->getNamespace() != Schema::SchemaUri)
442 state_ = EXTENSIBILITY;
443
444 else if (tag == "definitions")
445 state_ = DEFINITION;
446
447 else if (tag == "documentation")
448 state_ = DOCUMENTATION;
449
450 else if (tag == "annotation")
451 state_ = ANNOTATION;
452
453 else if (tag == "import")
454 state_ = IMPORT;
455
456 else if (tag == "schema")
457 state_ = SCHEMA;
458
459 else if (tag == "types")
460 state_ = TYPES;
461
462 else if (tag == "message")
463 state_ = MESSAGE;
464
465 else if (tag == "port")
466 state_ = PORT;
467
468 else if (tag == "operation")
469 state_ = OPERATION;
470
471 else if (tag == "portType")
472 state_ = PORT_TYPE;
473
474 else if (tag == "input")
475 state_ = INPUT;
476
477 else if (tag == "output")
478 state_ = OUTPUT;
479
480 else if (tag == "fault")
481 state_ = FAULT;
482
483 else if (tag == "part")
484 state_ = PART;
485
486 else if (tag == "binding")
487 state_ = BINDING;
488
489 else if (tag == "service")
490 state_ = SERVICE;
491
492 else
493 error("Unknown Tag " + tag);
494 break;
496 if (tag == "definitions")
497 state_ = END;
498
499 else
500 {
501 /*
502 If its one of the top level Wsdl elements
503 set the State to NONE
504 */
505 if (tag == "types" ||
506 tag == "message"||
507 tag == "documentation"||
508 tag == "annotation"||
509 tag == "portType" ||
510 tag == "import" ||
511 (tag == "binding" &&
512 state_ != EXTENSIBILITY) ||
513 tag == "service")
514 return state_ = NONE;
515 else
516 return peek(lookahead); //get the next tag
517 }
518 break;
524 xParser_->getText();
525 break;
527 error("Doc Declaration ??");
528 break;
529 default:
530 error("Unknown Wsdl tag");
531 break;
532 }
533 } while (event_type != xParser_->END_DOCUMENT
534 && tmpState == state_ &&event_type ==
535 tmp_event_type);
536 return state_;
537}
538
539
540 //this method looks at the top level Wsdl elements
541int
542WsdlParser::next()
543{
544 try
545 {
546 switch (peek(false))
547 {
548 case START:
549 element_ = START;
550 break;
551 case DEFINITION:
552 parseDefinitions();
553 peek();
554 element_ = DEFINITION;
555 break;
556 case DOCUMENTATION:
557 Doc_=parseDoc();
558 element_ = DOCUMENTATION;
559 break;
560 case ANNOTATION:
561 parseAnnotation();
562 element_ = ANNOTATION;
563 break;
564 case IMPORT:
565 parseImport();
566 element_ = IMPORT;
567 break;
568 case TYPES:
569 parseTypes();
570 element_ = TYPES;
571 break;
572 case MESSAGE:
573 parseMessage();
574 element_ = MESSAGE;
575 break;
576 case PORT_TYPE:
577 parsePortType();
578 element_ = PORT_TYPE;
579 break;
580 case EXTENSIBILITY:
581 handleExtensibilityElement(DEFINITION);
582 peek();
583 element_ = EXTENSIBILITY;
584 break;
585 case SERVICE:
586 parseService();
587 element_ = SERVICE;
588 break;
589 case BINDING:
590 parseBinding();
591 element_ = BINDING;
592 break;
593 case END:
594 element_ = END;
595 return state_;
596 default:
597 error("Syntax error");
598 }
599 return state_;
600 }
601 catch(WsdlException we)
602 {
603 we.line = xParser_->getLineNumber();
604 we.col = xParser_->getColumnNumber();
605 errorOccured_ = true;
606 element_ = END;
607 // ostr.seekp(0);we loose the other errors
608 // ostr.clear();
609 ostr << we.description << " at " << we.line << "," << we.col << std::endl;
610 return state_ = END;
611 }
612 catch(XmlPullParserException xe)
613 {
614 // ostr.seekp(0);
615 // ostr.clear();
616 errorOccured_ = true;
617 ostr<<xe.description<<std::endl;
618 element_ = END;
619 return state_ = END;
620 }
621}
622
623
624 /*
625 Parse a documentation tag
626 */
627string*
628WsdlParser::parseDoc()
629{
630 string* documentation = new string();
631 if (state_ != DOCUMENTATION)
632 error("syntax error");
633
634 do
635 {
636 xParser_->nextToken();
637 if (xParser_->getEventType() == xParser_->TEXT)
638 *documentation += xParser_->getText();
639 if (xParser_->getEventType() == xParser_->END_TAG
640 && xParser_->getName() == "documentation")
641 break;
642 } while (true);
643 docs_list_.push_back(documentation);
644 peek();
645
646 return documentation;
647}
648
649
650 /*
651 Parse Annotation
652 */
653void
654WsdlParser::parseAnnotation()
655{
656 if (state_ != ANNOTATION)
657 error("syntax error");
658
659 do
660 {
661 xParser_->nextToken();
662 if (xParser_->getEventType() == xParser_->END_TAG
663 &&xParser_->getName() == "annotation")
664 break;
665 } while (true);
666 peek();
667}
668
669
670 /*Parses the definition tag
671 If any extensibility namespaces are defined then the relevant
672 information is stored
673 */
674void
675WsdlParser::parseDefinitions()
676{
677 if (state_ != DEFINITION)
678 error("syntax error");
679
680 tnsUri_ = xParser_->getAttributeValue("", "targetNamespace");
681 int i = 0;
682 for (i = xParser_->getNamespaceCount(xParser_->getDepth()) - 1;
683 i > xParser_->getNamespaceCount(xParser_->getDepth() - 1) - 1; i--)
684 {
685 if (xParser_->getNamespaceUri(i) == tnsUri_)
686 tnsPrefix_ = xParser_->getNamespacePrefix(i);
687
688 if (xParser_->getNamespaceUri(i) == soap_->getEncodingUri()) {
689 //add the schema for soap encoding uri
690
691 SchemaParser * sParser = new SchemaParser(soap_->getEncodingSchema(),
692 soap_->getEncodingUri(),ostr,schemaPath_);
693 if (sParser->parseSchemaTag())
694 schemaParser_.push_back(sParser);
695
696 }
697 if (xParser_->getNamespaceUri(i) == soap2_->getEncodingUri()) {
698 //add the schema for soap1.2 encoding uri
699
700 SchemaParser * sParser = new SchemaParser(soap2_->getEncodingSchema(),
701 soap2_->getEncodingUri(),ostr,schemaPath_);
702 if (sParser->parseSchemaTag())
703 schemaParser_.push_back(sParser);
704 }
705
706 /*
707 * Associate the extension prefixes with the handlers.
708 * It is asssumed that by this time all the extensibility handlers have been registered .
709 * Check if the namespace defined here matches that Uri ,whose namespace the handler handles .
710 */
711 for (size_t j = 0; j < wsdlExtensions_.size(); j++)
712 if (wsdlExtensions_[j].we != 0 &&
713 wsdlExtensions_[j].we->isNamespaceHandler(xParser_->getNamespaceUri(i)))
714 {
715 wsdlExtensions_[j].we->setNamespacePrefix(xParser_->
716 getNamespacePrefix
717 (i));
718 //each extensibility handler allocates element ids in assigned range
719 wsdlExtensions_[j].we->setStartId(MAX_EXT_XML * j + 1);
720
721 /*
722 * If there is a schema associated with the extensibility namespace
723 * use the schema parser to parse its types.
724 */
725
726 SchemaParser * xtmpSchemaParser =
727 new SchemaParser(wsdlExtensions_[j].we->getExtensibilitySchema(),
728 wsdlExtensions_ [j].we->getNamespace(),ostr,schemaPath_);
729
730 //import the wsdl definition file as many binding schemas reference it
731 xtmpSchemaParser->addImport(schemaParser_[0]);
732 if (xtmpSchemaParser->parseSchemaTag())
733 {
734 wsdlExtensions_[j].spe = xtmpSchemaParser;
735 wsdlExtensions_[j].we->
736 setSchemaParser(xtmpSchemaParser);
737 wsdlExtensions_[j].we->setWsdlParser(this);
738 }
739 else {
740
741 std::string err = "Error parsing the schema for the namespace ";
742 err +=wsdlExtensions_[j].we->getNamespace();
743 err +="\n";
744 err +="Unable to locate the file ";
745 err += wsdlExtensions_[j].we->getExtensibilitySchema();
746 err +="\n";
747 error(err);
748
749 }
750 }
751 }
752 int num_attr = xParser_->getAttributeCount();
753 if (num_attr < 0)
754 error("Atleast a targetNamespace attribute is needed");
755 for (i = 0; i < num_attr; i++)
756 {
757 if (xParser_->getAttributeName(i) == "name")
758 {
759 name_ = xParser_->getAttributeValue(i);
760 continue;
761 }
762
763 else if (xParser_->getAttributeName(i) != "targetNamespace")
764 { //this is to handle extensibility attributes
765 handleExtensibilityAttributes(xParser_->getAttributePrefix(i),
766 xParser_->getAttributeName(i));
767 }
768 }
769 return;
770}
771
772
773void
774WsdlParser::parseImport()
775{
776 if (state_ != IMPORT)
777 error("syntax error");
778 Imports imp (xParser_->getAttributeValue("", "namespace"),
779 xParser_->getAttributeValue("", "location"));
780 if (imp.ns == getNamespace() ) {
781
782 std::string fname;
783 ifstream wsdlStream;
784 if(!imp.loc.empty())
785 {
786 if(XmlUtils::fetchUri(imp.loc,fname))
787 {
788 /*
789 * If the schema definition was retrieved successfully
790 * process it and add all type definitions and
791 * declaration to the current namespace
792 */
793 wsdlStream.open(fname.c_str());
794
795 XmlPullParser * xpp = new XmlPullParser(wsdlStream);
796 XmlPullParser * tmpXparser=xParser_;
797 xParser_=xpp;
798
799 xParser_->setFeature(FEATURE_PROCESS_NAMESPACES, true);
800 xParser_->require(XmlPullParser::START_DOCUMENT, "", "");
801 while (getNextElement () != WsdlParser::END);
802 xParser_=tmpXparser;
803 delete xpp;
804 }else{
805 error("Error while opening the included wsdl " + imp.loc);
806 }
807 }else{
808 error("location is a required attribute for <import>");
809 }
810 imports_.push_back(imp);
811
812 xParser_->nextTag();
813 }
814 peek();
815}
816
817
818void
819WsdlParser::parseMessage()
820{
821 if (state_ != MESSAGE)
822 error("syntax error");
823
824 Message * m =0;
825 int num_att = xParser_->getAttributeCount();
826 std::string n=xParser_->getAttributeValue("", "name");
827 m=const_cast<Message*>(getMessage(n));
828 if(!m){
829 m= new Message(*this);
830 m->setName(n);
831 putMessage(m);
832 }
833
834 for (int i = 0; i < num_att; i++){
835
836 if (!(xParser_->getAttributePrefix(i)).empty())
837 m->addExtAttribute(handleExtensibilityAttributes
838 (xParser_->getAttributePrefix(i),
839 xParser_->getAttributeName(i)));
840
841 }
842 if (m->getName() == "")
843 error("syntax error <message> name required");
844 peek();
845 try
846 {
847 if (state_ == DOCUMENTATION)
848 {
849 m->setDocumentation(parseDoc());
850 // peek();
851 }
852
853 //parse all the parts in the message
854 //TODO .if a part has a type reference ,check that only one part is allowed in the message
855 if (state_ == PART)
856 {
857 while (state_ == PART)
858 {
859 string p_name;
860 int type_id = 0, schemaId = -1;
861 Element* e=0;
863 int num_att = xParser_->getAttributeCount();
864 int p_extId = 0;
865 for (int i = 0; i < num_att; i++)
866 {
867 if ("name" == xParser_->getAttributeName(i) &&
868 //Wsdl attribute name must have a null prefix
869 (xParser_->getAttributePrefix(i)).empty())
870 p_name = xParser_->getAttributeValue(i);
871
872 else if (("type" == xParser_->getAttributeName(i)
873 &&xParser_->getAttributePrefix(i).empty())
874 ||("element" == xParser_->getAttributeName(i)
875 &&xParser_->getAttributePrefix(i).empty()))
876 {
877 if (reftype != Part::None)
878 error
879 ("either type or element must occur(only once) in part ");
880 if ("type" == xParser_->getAttributeName(i))
881 reftype = Part::Type;
882
883 else
884 reftype = Part::Elem;
885 Qname type(xParser_->getAttributeValue(i));
886 type.setNamespace(getNamespace(type.getPrefix()));
887 if (reftype == Part::Type)
888 {
889
890 //get the type id
891 type_id = getTypeId(type);
892 if (type_id == 0)
893 error("Could not resolve type " +
894 type.getNamespace() + ":" +
895 type.getLocalName());
896 }
897
898 else
899 {
900 //get the element id
901 e = getElement(type);
902 if (e== 0 )
903 error("Could not resolve element " +
904 type.getNamespace() + ":" +
905 type.getLocalName());
906 }
907
908 //if the ref type is "element",the id is that of a global element and not a type
909 //get the schema parser of the namespace to which "type" belongs
910 schemaId = getSchema(type,reftype == Part::Type);
911 }
912
913 else if (!(xParser_->getAttributePrefix(i)).empty())
914 p_extId = handleExtensibilityAttributes(xParser_->
915 getAttributePrefix
916 (i),
917 xParser_->
918
919 getAttributeName
920 (i));
921
922 else
923 error("Syntax error");
924 }
925 peek();
926 if (state_ == DOCUMENTATION)
927 {
928 parseDoc();
929 // peek();
930 }
931 if(reftype==Part::Elem)
932 m->addPart(p_name, reftype, (void*)(e) , schemaId);
933 else
934 m->addPart(p_name, reftype, (void*)(&type_id) , schemaId);
935 m->addExtElement(p_extId);
936 }
937 }
938 }
939 catch(WsdlException we)
940 {
941 we.line = xParser_->getLineNumber();
942 we.col = xParser_->getColumnNumber();
943 throw we;
944 }
945
946 //now parse the extensibility elements
947 if (state_ == EXTENSIBILITY)
948 {
949 while (state_ == EXTENSIBILITY)
950 {
951 m->addExtElement(handleExtensibilityElement(MESSAGE));
952 peek();
953 }
954 }
955
956
957 return;
958}
959
960
961
962PortType *
963WsdlParser::parsePortType()
964{
965 if (state_ != PORT_TYPE)
966 return 0;
967
968 PortType * pt = new PortType(*this);
969 int num_att = xParser_->getAttributeCount();
970 for (int i = 0; i < num_att; i++){
971
972 if ("name" == xParser_->getAttributeName(i) &&
973 //Wsdl attribute name must have a null prefix
974 (xParser_->getAttributePrefix(i)).empty())
975 pt->setName(xParser_->getAttributeValue(i));
976
977 else if (!(xParser_->getAttributePrefix(i)).empty()) {
978
979 pt->addExtAttribute(handleExtensibilityAttributes
980 (xParser_->getAttributePrefix(i),
981 xParser_->getAttributeName(i)));
982 }
983 else {
984
985 error("Syntax error.Unrecognized attribute");
986 }
987 }
988 if (pt->getName() == "")
989 error("syntax error <PortType> name required");
990
991 peek();
992 if (state_ == DOCUMENTATION) {
993
994 pt->setDocumentation(parseDoc());
995 // peek();
996 }
997 if (state_ == OPERATION) {
998
999 //parse all the operations in the port type
1000 while (state_ == OPERATION){
1001
1002 Operation * op = parseOperation(pt);
1003 pt->addOp(op);
1004 }
1005 if (state_ == EXTENSIBILITY) {
1006
1007 //now parse the extensibility elements
1008 while (state_ == EXTENSIBILITY){
1009
1010 pt->addExtElement(handleExtensibilityElement(PORT_TYPE));
1011 peek();
1012 }
1013 }
1014 }
1015 putPortType(pt);
1016 return pt;
1017}
1018
1019
1020//Returns an operation element
1021Operation *
1022WsdlParser::parseOperation(PortType * p)
1023{
1024 Operation * op = new Operation(*this,p);
1025 if (state_ != OPERATION)
1026 error("syntax error");
1027
1028 int num_att = xParser_->getAttributeCount();
1029 for (int i = 0; i < num_att; i++){
1030
1031 if ("name" == xParser_->getAttributeName(i) &&
1032 (xParser_->getAttributePrefix(i)).empty())
1033 op->setName(xParser_->getAttributeValue(i));
1034
1035 //Wsdl attribute name must have a null prefix
1036
1037 else if (!(xParser_->getAttributePrefix(i)).empty()) {
1038
1039 op->addExtAttribute(handleExtensibilityAttributes
1040 (xParser_->getAttributePrefix(i),
1041 xParser_->getAttributeName(i)));
1042 }
1043
1044 else if ("parameterOrder" == xParser_->getAttributeName(i)) {
1045
1046 }
1047
1048 else
1049 error("Syntax error..unrecognized attribute");
1050 }
1051 if (op->getName() == "")
1052 error("syntax error <operation> name required");
1053 peek();
1054 if (state_ == DOCUMENTATION)
1055 {
1056 op->setDocumentation(parseDoc());
1057 // peek();
1058 }
1059 if (state_ == INPUT)
1060 {
1061 op->setMessage(pgetMessage(Qname(xParser_->getAttributeValue("", "message"))),
1062 Input,
1063 xParser_->getAttributeValue("", "name"));
1064
1065 processMessageExtensibility(op,WsdlPull::Input);
1066 peek();
1067 if (state_ == OUTPUT)
1068 {
1069 op->setMessage(pgetMessage(Qname(xParser_->getAttributeValue("", "message"))),
1070 Output,
1071 xParser_->getAttributeValue("", "name"));
1072
1073 processMessageExtensibility(op,WsdlPull::Output);
1074 peek();
1075 }
1076 while (state_ == FAULT)
1077 {
1078 op->setMessage(pgetMessage(Qname(xParser_->getAttributeValue("", "message"))),
1079 Fault,
1080 xParser_->getAttributeValue("", "name"));
1081
1082 processMessageExtensibility(op,WsdlPull::Fault);
1083 peek();
1084 }
1085 }
1086
1087 else if (state_ == OUTPUT)
1088 {
1089 op->setMessage(pgetMessage(Qname(xParser_->getAttributeValue("", "message"))),
1090 Output,
1091 xParser_->getAttributeValue("", "name"));
1092 processMessageExtensibility(op,WsdlPull::Output);
1093 peek();
1094 if (state_ == INPUT)
1095 {
1096 op->setMessage(pgetMessage(Qname(xParser_->getAttributeValue("", "message"))),
1097 Input,
1098 xParser_->getAttributeValue("", "name"));
1099 processMessageExtensibility(op,WsdlPull::Input);
1100 peek();
1101 }
1102 while (state_ == FAULT)
1103 {
1104 op->setMessage(pgetMessage(Qname(xParser_->getAttributeValue("", "message"))),
1105 Fault,
1106 xParser_->getAttributeValue("", "name"));
1107 processMessageExtensibility(op,WsdlPull::Fault);
1108 peek();
1109 }
1110 }
1111 if (state_ == DOCUMENTATION)
1112 {
1113 op->setDocumentation(parseDoc());
1114 // peek();
1115 }
1116 if (state_ == EXTENSIBILITY)
1117 while (state_ == EXTENSIBILITY)
1118 {
1119 op->addExtElement(handleExtensibilityElement(OPERATION));
1120 peek();
1121 }
1122
1123 // Ops_.push_back(op);
1124 return op;
1125}
1126
1127
1128void
1129WsdlParser::processMessageExtensibility(Operation * op,
1131{
1132
1133 int num_att = xParser_->getAttributeCount();
1134 std::string message_name;
1135 for (int i = 0; i < num_att; i++){
1136
1137 if ("name" == xParser_->getAttributeName(i) &&
1138 (xParser_->getAttributePrefix(i)).empty())
1139 message_name = xParser_->getAttributeValue(i);
1140
1141 //Wsdl attribute name must have a null prefix
1142
1143 else if (!(xParser_->getAttributePrefix(i)).empty()) {
1144
1145 op->addMessageExtensibility(mtype,handleExtensibilityAttributes
1146 (xParser_->getAttributePrefix(i),
1147 xParser_->getAttributeName(i)));
1148 }
1149 }
1150}
1151
1152void
1153WsdlParser::parseTypes()
1154{
1155 peek();
1156 if (state_ == DOCUMENTATION)
1157 {
1158 parseDoc();
1159 // peek();
1160 }
1161 try
1162 {
1163 while (state_ == SCHEMA)
1164 {
1165 SchemaParser *sParser=new SchemaParser(xParser_, tnsUri_,ostr,schemaPath_);
1166 sParser->setUri(uri_);
1167 sParser->addImport(schemaParser_[0]);//wsdl schema for wsdl namespace (wsdl:arrayType)
1168
1169 for (size_t s = 1 ;s<schemaParser_.size();s++){
1170 //add the soap encoding schemas to parse elements like soap:array
1171
1172 if (schemaParser_[s]->getNamespace() == soap_->getEncodingUri())
1173 sParser->addImport(schemaParser_[s]);//soap1.1 encoding schema
1174 if (schemaParser_[s]->getNamespace() == soap2_->getEncodingUri())
1175 sParser->addImport(schemaParser_[s]);//soap1.2 encoding schema
1176 }
1177
1178
1179
1180 if (!sParser->parseSchemaTag())
1181 error("Error parsing schema types for "+tnsUri_);
1182 else
1183 schemaParser_.push_back(sParser);
1184 peek();
1185 error(sParser->getNamespace() +" schema parsed",2);
1186 }
1187 for (size_t i = 1; i < schemaParser_.size(); i++)
1188 {
1189
1190 for (size_t j = 1; j < schemaParser_.size(); j++) {
1191
1192 if (schemaParser_[i]->isImported(schemaParser_[j]->getNamespace()))
1193 schemaParser_[i]->addImport(schemaParser_[j]);
1194 }
1195
1196
1197 if (!schemaParser_[i]->finalize())
1198 error("Invalid schema");
1199 }
1200
1201 }
1202 catch(SchemaParserException spe)
1203 {
1204 WsdlException we(spe.description);
1205 we.col = spe.col;
1206 we.line = spe.line;
1207 we.WsdlState = state_;
1208 throw we;
1209 }
1210}
1211
1212
1213void
1214WsdlParser::putMessage(Message * m)
1215{
1216
1217 //m->setId (nMessage++);
1218 messages_.push_back(m);
1219}
1220
1221
1222void
1223WsdlParser::putBinding(Binding * bn)
1224{
1225 bindings_.push_back(bn);
1226}
1227
1228void
1229WsdlParser::putPortType(PortType * pt)
1230{
1231 porttypes_.push_back(pt);
1232}
1233
1234
1235int
1236WsdlParser::handleExtensibilityElement(int parent)
1237{
1238 WsdlExtension * we = getExtensibilityHandler(xParser_->getNamespace());
1239 if (we == 0) {
1240 xParser_->skipSubTree();
1241 return 0;
1242 }
1243
1244 else
1245 return we->handleElement(parent, xParser_);
1246}
1247
1248
1249int
1250WsdlParser::handleExtensibilityAttributes(string prefix, string name)
1251{
1252 WsdlExtension * we = getExtensibilityHandler(getNamespace(prefix));
1253 if (we == 0)
1254 return 0;
1255
1256 else
1257 return we->handleAttribute(state_, name, xParser_);
1258}
1259
1260WsdlExtension *
1262{
1263 for (size_t i = 0; i < wsdlExtensions_.size(); i++)
1264 if (wsdlExtensions_[i].we != 0 &&
1265 (wsdlExtensions_[i].we->isNamespaceHandler(Ns)))
1266 return wsdlExtensions_[i].we;
1267 return 0;
1268}
1269
1272{
1273
1274 if (extId == 0)
1275 return 0;
1276
1277 for (size_t i = 0; i < wsdlExtensions_.size(); i++)
1278 if (wsdlExtensions_[i].we != 0 &&
1279 (extId >= wsdlExtensions_[i].we->getStartId()&&
1280 extId < MAX_EXT_XML + wsdlExtensions_[i].we->getStartId()))
1281 return wsdlExtensions_[i].we;
1282 return 0;
1283}
1284
1285
1286void
1287WsdlParser::parseBinding()
1288{
1289
1290 Binding * bn = new Binding(*this);
1291 const PortType *pt = 0;
1292 int opBinding, inputBinding, outputBinding, faultBinding, index,
1293 bindingInfo;
1294 opBinding = inputBinding = outputBinding = faultBinding = index =
1295 bindingInfo = 0;
1296 if (state_ != BINDING)
1297 error("syntax error");
1298 int num_att = xParser_->getAttributeCount();
1299 int i;
1300 WsdlExtension* bindingExtension;
1301
1302 for (i = 0; i < num_att; i++)
1303 {
1304 if ("name" == xParser_->getAttributeName(i) &&
1305 (xParser_->getAttributePrefix(i)).empty())
1306 bn->setName(xParser_->getAttributeValue(i));
1307
1308 else if ("type" == xParser_->getAttributeName(i) &&
1309 (xParser_->getAttributePrefix(i)).empty())
1310 {
1311 Qname q(xParser_->getAttributeValue(i));
1312 pt = getPortType(q);
1313 if (!pt)
1314 error("Unknown port type "+ q.getLocalName());
1315 bn->setPortType(pt);
1316 (const_cast<PortType*>(pt))->setBinding(bn);
1317 }
1318
1319 else
1320 error("Syntax error..unrecognized attribute");
1321 }
1322 peek();
1323
1324 if (state_ == DOCUMENTATION) {
1325
1326 bn->setDocumentation(parseDoc());
1327 // peek();
1328 }
1329 if (state_ == EXTENSIBILITY) {
1330
1331 while (state_ == EXTENSIBILITY) {
1332
1333 bn->setBindingInfo(bindingInfo =
1334 handleExtensibilityElement(BINDING));
1335 bindingExtension=getExtensibilityHandler(bindingInfo);
1336
1337 if(bindingExtension)
1338 bn->setBindingMethod(bindingExtension->getNamespace());
1339 peek();
1340 }
1341 }
1342 while (state_ == OPERATION){
1343
1344 num_att = xParser_->getAttributeCount();
1345 const Operation *op = NULL;
1346 for (i = 0; i < num_att; i++){
1347
1348 if ("name" == xParser_->getAttributeName(i) &&
1349 (xParser_->getAttributePrefix(i)).empty()){
1350
1351 Qname q(xParser_->getAttributeValue(i));
1352 op = pt->getOperation(q);
1353 }
1354
1355 else
1356 error("Unrecognized attribute");
1357 }
1358 index = bn->addOperation(op);
1359 peek();
1360
1361 if (state_ == DOCUMENTATION) {
1362
1363 parseDoc();
1364 }
1365
1366 while (state_ == EXTENSIBILITY) {
1367
1368 opBinding = handleExtensibilityElement(OPERATION);
1369 if(opBinding) bn->addOpBinding(index, opBinding);
1370 peek();
1371 }
1372
1373 if (state_ == DOCUMENTATION) {
1374
1375 parseDoc();
1376 }
1377 if (state_ == INPUT) {
1378
1379 peek();
1380 while (state_ == EXTENSIBILITY){
1381
1382 inputBinding = handleExtensibilityElement(OPERATION);
1383 if(inputBinding) bn->addInputBinding(index, inputBinding);
1384 peek();
1385 }
1386 }
1387 if (state_ == OUTPUT) {
1388
1389 peek();
1390 while (state_ == EXTENSIBILITY){
1391
1392 outputBinding = handleExtensibilityElement(OPERATION);
1393 if(outputBinding) bn->addOutputBinding(index, outputBinding);
1394 peek();
1395 }
1396 }
1397 while (state_ == FAULT) {
1398
1399 peek();
1400 while (state_ == EXTENSIBILITY){
1401
1402 faultBinding = handleExtensibilityElement(OPERATION);
1403 peek();
1404 if(faultBinding) bn->addFaultBinding(index, faultBinding);
1405 }
1406 }
1407 }
1408 putBinding(bn);
1409}
1410
1411
1412void
1413WsdlParser::parseService()
1414{
1415 if (state_ != SERVICE)
1416 error("Syntax error");
1417 string serviceName;
1418 Service * sv = new Service(*this);
1419 int num_att = xParser_->getAttributeCount();
1420 int i;
1421 for (i = 0; i < num_att; i++) {
1422
1423 if ("name" == xParser_->getAttributeName(i) &&
1424 (xParser_->getAttributePrefix(i)).empty())
1425 serviceName = xParser_->getAttributeValue(i);
1426
1427 else
1428 error("Unrecognized attribute");
1429 }
1430 sv->setName(serviceName);
1431 peek();
1432 if (state_ == DOCUMENTATION) {
1433
1434 sv->setDocumentation(parseDoc());
1435 }
1436 while (state_ == PORT) {
1437
1438 string bnName,portName;
1439 Binding * bn = 0;;
1440 int serviceExtId = 0;
1441 num_att = xParser_->getAttributeCount();
1442 for (i = 0; i < num_att; i++) {
1443
1444 if ("binding" == xParser_->getAttributeName(i) &&
1445 (xParser_->getAttributePrefix(i)).empty()) {
1446
1447 bnName = xParser_->getAttributeValue(i);
1448 }
1449 else if ("name" == xParser_->getAttributeName(i)) {
1450
1451 portName = xParser_->getAttributeValue(i);
1452 }
1453 }
1454 // Qname bindingName(bnName);
1455 bn = (Binding *) getBinding(bnName);
1456 peek();
1457 if (state_ == DOCUMENTATION) {
1458
1459 parseDoc();
1460 // peek();
1461 }
1462 if (state_ == EXTENSIBILITY) {
1463
1464 serviceExtId = handleExtensibilityElement(BINDING);
1465 peek();
1466 }
1467 if (bn != 0)
1468 bn->addServiceExtId(serviceExtId);
1469
1470 sv->addPort(portName,bn,serviceExtId);
1471 }
1472 services_.push_back(sv);
1473}
1474
1475
1476/*
1477 * returns the id of the schema to which "type"
1478 * or "element" is defined
1479 */
1480int
1481WsdlParser::getSchema(const Qname & name,bool isType)
1482{
1483 Qname type = name;
1484 type.setNamespace(getNamespace(type.getPrefix()));
1485
1486 //this is a primitve type ,simple instance of schemaparser will do.
1487 if (name.getNamespace() == Schema::SchemaUri)
1488 return 0;
1489
1490
1491 for (size_t i = 0; i < schemaParser_.size(); i++) {
1492
1493 //check in the schema parser which defines the namespace or imports it
1494
1495 if( schemaParser_[i]->getNamespace() == type.getNamespace()){
1496
1497 //check for definitions
1498
1499 if ((isType && schemaParser_[i]->getType(name,false) != 0) ||
1500 (!isType && schemaParser_[i]->getElement(name,false) != 0))
1501
1502 return i;
1503
1504 }
1505 }
1506 return -1;
1507}
1508
1509Element *
1510WsdlParser::getElement(const Qname& name)
1511{
1512 int i = getSchema(name,false);
1513 if (i >= 0)
1514 return const_cast<Element*>(schemaParser_[i]->getElement(name));
1515 else
1516 return 0;
1517}
1518
1519int
1520WsdlParser::getTypeId(const Qname & type)
1521{
1522
1523 int i = getSchema(type,true);
1524 Qname t=type;
1525
1526 if (i >= 0)
1527 return schemaParser_[i]->getTypeId(t);
1528
1529 else
1530 return 0;
1531}
1532
1533void
1534WsdlParser::getSchemaParsers(std::vector<SchemaParser* >::iterator & from,
1535 std::vector<SchemaParser* >::iterator & to)
1536{
1537
1538 from=schemaParser_.begin();
1539 from++;
1540 from++;
1541 to=schemaParser_.end();
1542 return ;
1543}
1544
1545void
1546WsdlParser::error(string s,int level)
1547{
1548 if(level==0){
1549
1550 WsdlException we(s);
1551 if(xParser_){
1552
1553 we.line = xParser_->getLineNumber();
1554 we.col = xParser_->getColumnNumber();
1555 }
1556 we.WsdlState = state_;
1557 errorOccured_ = true;
1558 throw we;
1559 }
1560#ifdef LOGGING
1561 else if (level == 1) {
1562
1563 ostr<<"Wsdl parser warning : "<<s<<endl;
1564 }
1565 else if (level == 2) {
1566
1567 ostr<<"Wsdl parser info : "<<s<<endl;
1568 }
1569#endif
1570}
1571
1572bool
1574 Binding::cBindingIterator & end)const
1575{
1576 if(bindings_.size()>0){
1577
1578 begin=bindings_.begin();
1579 end=bindings_.end();
1580 return true;
1581 }
1582 else
1583 return false;
1584}
1585
1586bool
1589{
1590 if(porttypes_.size()>0){
1591
1592 begin=porttypes_.begin();
1593 end=porttypes_.end();
1594 return true;
1595 }
1596 else
1597 return false;
1598}
1599
1600int
1602{
1603 return schemaParser_.size() - 2;
1604 //soap-enc and wsdl schema are parsed by default
1605}
1606
1607void
1608WsdlParser::setSchemaPath(const std::string & schemaPath)
1609{
1610 schemaPath_ = schemaPath;
1611
1612 for (vector<ExtensionInfo>::iterator ie = wsdlExtensions_.begin();
1613 ie != wsdlExtensions_.end();
1614 ie++)
1615 ie->we->setSchemaPath(schemaPath);
1616
1617 // soap_->setSchemaPath(schemaPath);
1618}
1619
1620}
#define FEATURE_PROCESS_NAMESPACES
Definition: XmlPullParser.h:40
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
std::string getNamespace(void) const
void setUri(const std::string &u)
Definition: SchemaParser.h:447
bool addImport(std::string ns, std::string location="")
void addOpBinding(int index, int oBn)
Definition: Binding.h:259
void setPortType(const PortType *pt)
Definition: Binding.h:227
void addOutputBinding(int index, int opBn)
Definition: Binding.h:266
void addFaultBinding(int index, int fBn)
Definition: Binding.h:279
void setBindingMethod(const std::string &ns)
Definition: Binding.h:300
void addInputBinding(int index, int ipBn)
Definition: Binding.h:272
std::list< Binding * >::const_iterator cBindingIterator
Definition: Binding.h:40
void setBindingInfo(int id)
Definition: Binding.h:234
int addOperation(const Operation *op)
Definition: Binding.h:249
std::vector< Operation * >::const_iterator cOpIterator
Definition: Operation.h:57
const Operation * getOperation(int index) const
Definition: PortType.h:109
std::list< PortType * >::const_iterator cPortTypeIterator
Definition: PortType.h:34
bool getOperations(Operation::cOpIterator &start, Operation::cOpIterator &finish) const
Definition: PortType.h:140
int getNumOps(void) const
Definition: PortType.h:102
std::string getEncodingUri(void) const
Definition: Soap.cpp:117
std::string getEncodingSchema(void) const
Definition: Soap.cpp:93
void setDocumentation(std::string *s)
Definition: WsdlElement.h:133
void setName(std::string nam)
Definition: WsdlElement.h:127
std::string getName() const
Definition: WsdlElement.h:110
virtual std::string getNamespace() const =0
const PortType * getPortType()
Definition: WsdlParser.cpp:269
const Service * getService()
Definition: WsdlParser.cpp:225
WsdlExtension * getExtensibilityHandler(const std::string &ns)
int getNumSchemas() const
static bool useLocalSchema_
Definition: WsdlParser.h:259
void getSchemaParsers(std::vector< SchemaParser * >::iterator &from, std::vector< SchemaParser * >::iterator &to)
void addExtensibilityHandler(WsdlExtension *ext)
Definition: WsdlParser.cpp:196
std::list< Service * >::iterator ServiceIterator
Definition: WsdlParser.h:150
std::string getNamespace(void)
Definition: WsdlParser.h:441
const SchemaParser * getSchemaParser(std::string targetNamespace) const
Definition: WsdlParser.cpp:385
void getServices(ServiceIterator &from, ServiceIterator &to)
Definition: WsdlParser.cpp:259
bool getBindings(Binding::cBindingIterator &begin, Binding::cBindingIterator &end) const
const Binding * getBinding()
Definition: WsdlParser.cpp:182
WsdlParser(std::istream &in=std::cin, std::ostream &out=std::cout, const std::string &schemaPath="")
Definition: WsdlParser.cpp:37
bool getPortTypes(PortType::cPortTypeIterator &begin, PortType::cPortTypeIterator &end) const
const Message * getMessage()
Definition: WsdlParser.cpp:337
bool getOperations(const Qname &portType, Operation::cOpIterator &begin, Operation::cOpIterator &end)
Definition: WsdlParser.cpp:303
const Operation * getOperation(const Qname &portType, const Qname &q)
Definition: WsdlParser.cpp:318
void setSchemaPath(const std::string &schemaPath)
void require(int type, std::string ns, std::string name)
int getColumnNumber()
Definition: XmlPullParser.h:68
int getLineNumber()
Definition: XmlPullParser.h:64
std::string getName()
Definition: XmlPullParser.h:79
std::string getNamespace(std::string prefix)
std::string getAttributeValue(int index)
std::string getText()
std::string getNamespaceUri(int pos)
std::string getAttributePrefix(int index)
int getNamespaceCount(int depth)
std::string getAttributeName(int index)
std::string getNamespacePrefix(int pos)
int getAttributeCount()
Definition: XmlPullParser.h:88
void setFeature(std::string feature, bool value)
const std::string SchemaUri
Definition: Schema.h:92
@ Output
Definition: Operation.h:45
bool isValidWsdlElement(int id)
Definition: WsdlParser.cpp:404
const std::string wsdlUri
Definition: WsdlParser.h:39
bool WSDLPULL_EXPORT fetchUri(std::string uri, std::string &path)
Definition: XmlUtils.cpp:108