00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifdef _WIN32
00023 #include <windows.h>
00024 #else
00025 #include <unistd.h>
00026 #endif
00027
00028 #include "xmlpull/osdir.h"
00029 #include "wsdlparser/WsdlParser.h"
00030 #include "wsdlparser/Soap.h"
00031
00032 using namespace std;
00033 namespace WsdlPull{
00034
00035 bool WsdlParser::useLocalSchema_=true;
00036
00037 WsdlParser::WsdlParser(istream & in, ostream & out,
00038 const std::string & schemaPath)
00039 :errorOccured_(false),
00040 ostr(out),
00041 istr(in),
00042 state_ (START),
00043 element_(START),
00044 Doc_(0),
00045 xParser_(0),
00046 MAX_EXT_XML(256),
00047 schemaPath_(schemaPath)
00048 {
00049 initialize(false);
00050 }
00051
00052 WsdlParser::WsdlParser(const std::string & Uri, ostream & out,
00053 const std::string & schemaPath)
00054 :errorOccured_(false),
00055 ostr(out),
00056 istr(std::cin),
00057 state_ (START),
00058 element_(START),
00059 Doc_(0),
00060 xParser_(0),
00061 MAX_EXT_XML(256),
00062 schemaPath_(schemaPath)
00063 {
00064 uri_ = Uri.substr(0,Uri.rfind('/') + 1);
00065 if(XmlUtils::fetchUri(Uri,wsdlFileName))
00066 {
00067 xmlStream.open(wsdlFileName.c_str());
00068 initialize(true);
00069 }
00070 else{
00071 std::string e= "Unable to connect to ";
00072 error(e + Uri);
00073 }
00074 }
00075
00076 void
00077 WsdlParser::initialize(bool file)
00078 {
00079 if (schemaPath_.empty()) {
00080
00081 #if defined SCHEMADIR
00082 schemaPath_= SCHEMADIR;
00083 #else
00084 schemaPath_= "src/schemas";
00085 #endif
00086 }
00087
00088 if(file)
00089 xParser_= new XmlPullParser(xmlStream);
00090 else
00091 xParser_= new XmlPullParser(istr);
00092
00093 xParser_->setFeature(FEATURE_PROCESS_NAMESPACES, true);
00094 xParser_->require(xParser_->START_DOCUMENT, "", "");
00095 messages_.clear();
00096 bindings_.clear();
00097 porttypes_.clear();
00098 wsdlExtensions_.clear();
00099 schemaParser_.clear();
00100
00101
00102 SchemaParser * sParser=0;
00103 if (WsdlPull::WsdlParser::useLocalSchema_ == false ) {
00104
00105 sParser = new SchemaParser (wsdlUri,wsdlUri,ostr);
00106 }
00107 else {
00108
00109 sParser = new SchemaParser (schemaPath_+"wsdl10.xsd",
00110 wsdlUri,ostr,schemaPath_);
00111
00112 }
00113 sParser->parseSchemaTag();
00114 schemaParser_.push_back(sParser);
00115
00116
00117 soap_ = new Soap(schemaPath_);
00118 addExtensibilityHandler (soap_);
00119 soap2_ = new Soap(schemaPath_,Soap::SOAP12);
00120 addExtensibilityHandler (soap2_);
00121
00122 }
00123
00124
00125 WsdlParser::~WsdlParser()
00126 {
00127 size_t i = 0;
00128 for (list < const Message * >::iterator mi =
00129 messages_.begin(); mi != messages_.end();
00130 mi++)
00131 delete(*mi);
00132 for (list < Binding * >::iterator bi =
00133 bindings_.begin(); bi != bindings_.end();
00134 bi++)
00135 delete(*bi);
00136
00137 for (list < Service*>::iterator si =services_.begin();
00138 si != services_.end();
00139 si++)
00140 delete(*si);
00141
00142 for (list < PortType * >::iterator pti =
00143 porttypes_.begin(); pti != porttypes_.end();
00144 pti++)
00145 delete(*pti);
00146
00147 for (i = 0; i < schemaParser_.size(); i++)
00148 delete schemaParser_[i];
00149
00150
00151
00152
00153 for (list < string * >::iterator sti =
00154 docs_list_.begin(); sti != docs_list_.end();
00155 sti++)
00156 delete(*sti);
00157
00158 for (vector<ExtensionInfo>::iterator ie = wsdlExtensions_.begin();
00159 ie != wsdlExtensions_.end();
00160 ie++)
00161 delete ie->we;
00162
00163 delete xParser_;
00164 xmlStream.close();
00165
00166
00167 oslink::directory dir(".");
00168 while (dir) {
00169 std::string fname = dir.next();
00170 if (fname.find(".wp-tmp") != std::string::npos)
00171 {
00172 #ifdef WIN32
00173 ::DeleteFile(fname.c_str());
00174 #else
00175 unlink(fname.c_str());
00176 #endif
00177 }
00178 }
00179 }
00180
00181 const Binding *
00182 WsdlParser::getBinding()
00183 {
00184 if (element_ != BINDING)
00185 {
00186 error ("Attempted to extract a Binding when ,no binding was parsed",1);
00187 return 0;
00188 }
00189 else
00190 {
00191 return bindings_.back();
00192 }
00193 }
00194
00195 void
00196 WsdlParser::addExtensibilityHandler(WsdlExtension * ext)
00197 {
00198 ExtensionInfo exi;
00199 exi.we=ext;
00200 exi.spe=0;
00201 wsdlExtensions_.push_back(exi);
00202 }
00203
00204
00205 const Binding *
00206 WsdlParser::getBinding(const Qname & q)
00207 {
00208 Qname qn(q);
00209 if (!qn.getPrefix().empty())
00210 qn.setNamespace(getNamespace(qn.getPrefix()));
00211 else
00212 qn.setNamespace(tnsUri_);
00213 if (tnsUri_ != qn.getNamespace())
00214 return 0;
00215 for (list <Binding * >::iterator pBinding =
00216 bindings_.begin(); pBinding != bindings_.end();
00217 pBinding++)
00218 if ((*pBinding)->getName() == qn.getLocalName())
00219 return *pBinding;
00220 return 0;
00221 }
00222
00223
00224 const Service *
00225 WsdlParser::getService()
00226 {
00227 if (element_ != SERVICE){
00228
00229 error ("Attempted to extract a Service when ,no service was parsed",1);
00230 return 0;
00231 }
00232 else{
00233
00234 return services_.back();
00235 }
00236 }
00237
00238 const Service *
00239 WsdlParser::getService(const Qname & q)
00240 {
00241 Qname qn(q);
00242 if (!qn.getPrefix().empty())
00243 qn.setNamespace(getNamespace(qn.getPrefix()));
00244 else
00245 qn.setNamespace(tnsUri_);
00246 if (tnsUri_ != qn.getNamespace())
00247 return 0;
00248
00249 for (list <Service * >::iterator si =services_.begin();
00250 si != services_.end();
00251 si++)
00252 if ((*si)->getName() == qn.getLocalName())
00253 return *si;
00254
00255 return 0;
00256 }
00257
00258 void
00259 WsdlParser::getServices(ServiceIterator &from, ServiceIterator &to)
00260 {
00261 if (services_.size() > 0)
00262 {
00263 from = services_.begin();
00264 to = services_.end();
00265 }
00266 }
00267
00268 const PortType *
00269 WsdlParser::getPortType()
00270 {
00271 if (element_ != PORT_TYPE)
00272 {
00273 error ("Attempted to extract a PortType when ,no PortType was parsed",1);
00274 return 0;
00275 }
00276 else
00277 {
00278 return porttypes_.back();
00279 }
00280 }
00281
00282
00283 const PortType *
00284 WsdlParser::getPortType(const Qname & qn)
00285 {
00286 string name = qn.getLocalName();
00287
00288 if (!qn.getPrefix().empty()){
00289 if(getNamespace(qn.getPrefix())!=tnsUri_)
00290 return 0;
00291 }
00292
00293 for (PortType::cPortTypeIterator pPortType =porttypes_.begin();
00294 pPortType != porttypes_.end();
00295 pPortType++)
00296 if ((*pPortType)->getName() == name)
00297 return *pPortType;
00298 return 0;
00299 }
00300
00301
00302 bool
00303 WsdlParser::getOperations(const Qname & portType,
00304 Operation::cOpIterator& begin,
00305 Operation::cOpIterator& end)
00306 {
00307 const PortType *pt = getPortType(portType);
00308 if(pt){
00309 return pt->getOperations(begin,end);
00310 }
00311 else
00312 return false;
00313 }
00314
00315
00316
00317 const Operation *
00318 WsdlParser::getOperation(const Qname & portType, const Qname & q)
00319 {
00320 const PortType *pt = getPortType(portType);
00321 int num = pt->getNumOps();
00322 if (num > 0)
00323 {
00324 const Operation *op = NULL;
00325 for (int i = 0; i < num; i++)
00326 {
00327 op = pt->getOperation(i);
00328 if (op->getName() == q.getLocalName())
00329 return op;
00330 }
00331 }
00332 return 0;
00333 }
00334
00335
00336 const Message *
00337 WsdlParser::getMessage()
00338 {
00339 if (element_ != MESSAGE)
00340 {
00341 error ("Attempted to extract a Message when ,no Message was parsed",1);
00342 return 0;
00343 }
00344 else
00345 {
00346 return messages_.back();
00347 }
00348 }
00349
00350
00351 const Message *
00352 WsdlParser::pgetMessage(const Qname & qn)
00353 {
00354 const Message*m=getMessage(qn);
00355 if(m==0){
00356 Message* newMessage = new Message(*this);
00357 newMessage->setName(qn.getLocalName());
00358 putMessage(newMessage);
00359 return newMessage;
00360 }else{
00361 return m;
00362 }
00363 }
00364
00365
00366 const Message *
00367 WsdlParser::getMessage(const Qname & qn)
00368 {
00369 string name = qn.getLocalName();
00370 if(!qn.getNamespace().empty() &&
00371 tnsUri_ != qn.getNamespace())
00372 return 0;
00373
00374 for (list < const Message * >::iterator pMessage =
00375 messages_.begin(); pMessage != messages_.end();
00376 pMessage++)
00377 if ((*pMessage)->getName() == name)
00378 return *pMessage;
00379
00380 return 0;
00381 }
00382
00383
00384 const SchemaParser *
00385 WsdlParser::getSchemaParser(string targetNamespace) const
00386 {
00387 if (targetNamespace == Schema::SchemaUri)
00388 return 0;
00389 for (size_t i = 0; i < schemaParser_.size(); i++){
00390 if (schemaParser_[i]->getNamespace() == targetNamespace)
00391 return (const SchemaParser *) schemaParser_[i];
00392
00393 if (schemaParser_[i]->isImported(targetNamespace)) {
00394
00395 return schemaParser_[i]->getImportedSchemaParser(targetNamespace);
00396 }
00397 }
00398 return 0;
00399 }
00400
00401
00402
00403
00404 bool isValidWsdlElement(int id)
00405 {
00406 if (id >= 0)
00407 return true;
00408
00409 else
00410 return false;
00411 }
00412
00413
00414 int
00415 WsdlParser::peek(bool lookahead)
00416 {
00417
00418
00419 int event_type, tmp_event_type = xParser_->getEventType();
00420 int tmpState = state_;
00421 if (state_ == END)
00422 return state_;
00423
00424 do
00425 {
00426 if (lookahead == true || state_ == START || state_ == NONE)
00427 xParser_->nextTag();
00428
00429 else
00430 return state_;
00431 event_type = xParser_->getEventType();
00432 string tag = xParser_->getName();
00433 switch (event_type)
00434 {
00435 case XmlPullParser::START_DOCUMENT:
00436 if (state_ != START)
00437 error("Syntax error at the start");
00438 break;
00439 case XmlPullParser::START_TAG:
00440 if (xParser_->getNamespace() != wsdlUri
00441 && xParser_->getNamespace() != Schema::SchemaUri)
00442 state_ = EXTENSIBILITY;
00443
00444 else if (tag == "definitions")
00445 state_ = DEFINITION;
00446
00447 else if (tag == "documentation")
00448 state_ = DOCUMENTATION;
00449
00450 else if (tag == "annotation")
00451 state_ = ANNOTATION;
00452
00453 else if (tag == "import")
00454 state_ = IMPORT;
00455
00456 else if (tag == "schema")
00457 state_ = SCHEMA;
00458
00459 else if (tag == "types")
00460 state_ = TYPES;
00461
00462 else if (tag == "message")
00463 state_ = MESSAGE;
00464
00465 else if (tag == "port")
00466 state_ = PORT;
00467
00468 else if (tag == "operation")
00469 state_ = OPERATION;
00470
00471 else if (tag == "portType")
00472 state_ = PORT_TYPE;
00473
00474 else if (tag == "input")
00475 state_ = INPUT;
00476
00477 else if (tag == "output")
00478 state_ = OUTPUT;
00479
00480 else if (tag == "fault")
00481 state_ = FAULT;
00482
00483 else if (tag == "part")
00484 state_ = PART;
00485
00486 else if (tag == "binding")
00487 state_ = BINDING;
00488
00489 else if (tag == "service")
00490 state_ = SERVICE;
00491
00492 else
00493 error("Unknown Tag " + tag);
00494 break;
00495 case XmlPullParser::END_TAG:
00496 if (tag == "definitions")
00497 state_ = END;
00498
00499 else
00500 {
00501
00502
00503
00504
00505 if (tag == "types" ||
00506 tag == "message"||
00507 tag == "documentation"||
00508 tag == "annotation"||
00509 tag == "portType" ||
00510 tag == "import" ||
00511 (tag == "binding" &&
00512 state_ != EXTENSIBILITY) ||
00513 tag == "service")
00514 return state_ = NONE;
00515 else
00516 return peek(lookahead);
00517 }
00518 break;
00519 case XmlPullParser::TEXT:
00520 case XmlPullParser::ENTITY_REF:
00521 case XmlPullParser::COMMENT:
00522 case XmlPullParser::PROCESSING_INSTRUCTION:
00523 case XmlPullParser::CDSECT:
00524 xParser_->getText();
00525 break;
00526 case XmlPullParser::DOCDECL:
00527 error("Doc Declaration ??");
00528 break;
00529 default:
00530 error("Unknown Wsdl tag");
00531 break;
00532 }
00533 } while (event_type != xParser_->END_DOCUMENT
00534 && tmpState == state_ &&event_type ==
00535 tmp_event_type);
00536 return state_;
00537 }
00538
00539
00540
00541 int
00542 WsdlParser::next()
00543 {
00544 try
00545 {
00546 switch (peek(false))
00547 {
00548 case START:
00549 element_ = START;
00550 break;
00551 case DEFINITION:
00552 parseDefinitions();
00553 peek();
00554 element_ = DEFINITION;
00555 break;
00556 case DOCUMENTATION:
00557 Doc_=parseDoc();
00558 element_ = DOCUMENTATION;
00559 break;
00560 case ANNOTATION:
00561 parseAnnotation();
00562 element_ = ANNOTATION;
00563 break;
00564 case IMPORT:
00565 parseImport();
00566 element_ = IMPORT;
00567 break;
00568 case TYPES:
00569 parseTypes();
00570 element_ = TYPES;
00571 break;
00572 case MESSAGE:
00573 parseMessage();
00574 element_ = MESSAGE;
00575 break;
00576 case PORT_TYPE:
00577 parsePortType();
00578 element_ = PORT_TYPE;
00579 break;
00580 case EXTENSIBILITY:
00581 handleExtensibilityElement(DEFINITION);
00582 peek();
00583 element_ = EXTENSIBILITY;
00584 break;
00585 case SERVICE:
00586 parseService();
00587 element_ = SERVICE;
00588 break;
00589 case BINDING:
00590 parseBinding();
00591 element_ = BINDING;
00592 break;
00593 case END:
00594 element_ = END;
00595 return state_;
00596 default:
00597 error("Syntax error");
00598 }
00599 return state_;
00600 }
00601 catch(WsdlException we)
00602 {
00603 we.line = xParser_->getLineNumber();
00604 we.col = xParser_->getColumnNumber();
00605 errorOccured_ = true;
00606 element_ = END;
00607
00608
00609 ostr << we.description << " at " << we.line << "," << we.col << std::endl;
00610 return state_ = END;
00611 }
00612 catch(XmlPullParserException xe)
00613 {
00614
00615
00616 errorOccured_ = true;
00617 element_ = END;
00618 return state_ = END;
00619 }
00620 }
00621
00622
00623
00624
00625
00626 string*
00627 WsdlParser::parseDoc()
00628 {
00629 string* documentation = new string();
00630 if (state_ != DOCUMENTATION)
00631 error("syntax error");
00632
00633 do
00634 {
00635 xParser_->nextToken();
00636 if (xParser_->getEventType() == xParser_->TEXT)
00637 *documentation += xParser_->getText();
00638 if (xParser_->getEventType() == xParser_->END_TAG
00639 && xParser_->getName() == "documentation")
00640 break;
00641 } while (true);
00642 docs_list_.push_back(documentation);
00643 peek();
00644 return documentation;
00645 }
00646
00647
00648
00649
00650
00651 void
00652 WsdlParser::parseAnnotation()
00653 {
00654 if (state_ != ANNOTATION)
00655 error("syntax error");
00656
00657 do
00658 {
00659 xParser_->nextToken();
00660 if (xParser_->getEventType() == xParser_->END_TAG
00661 &&xParser_->getName() == "annotation")
00662 break;
00663 } while (true);
00664 peek();
00665 }
00666
00667
00668
00669
00670
00671
00672 void
00673 WsdlParser::parseDefinitions()
00674 {
00675 if (state_ != DEFINITION)
00676 error("syntax error");
00677
00678 tnsUri_ = xParser_->getAttributeValue("", "targetNamespace");
00679 int i = 0;
00680 for (i = xParser_->getNamespaceCount(xParser_->getDepth()) - 1;
00681 i > xParser_->getNamespaceCount(xParser_->getDepth() - 1) - 1; i--)
00682 {
00683 if (xParser_->getNamespaceUri(i) == tnsUri_)
00684 tnsPrefix_ = xParser_->getNamespacePrefix(i);
00685
00686 if (xParser_->getNamespaceUri(i) == soap_->getEncodingUri()) {
00687
00688
00689 SchemaParser * sParser = new SchemaParser(soap_->getEncodingSchema(),
00690 soap_->getEncodingUri(),ostr,schemaPath_);
00691 if (sParser->parseSchemaTag())
00692 schemaParser_.push_back(sParser);
00693
00694 }
00695 if (xParser_->getNamespaceUri(i) == soap2_->getEncodingUri()) {
00696
00697
00698 SchemaParser * sParser = new SchemaParser(soap2_->getEncodingSchema(),
00699 soap2_->getEncodingUri(),ostr,schemaPath_);
00700 if (sParser->parseSchemaTag())
00701 schemaParser_.push_back(sParser);
00702 }
00703
00704
00705
00706
00707
00708
00709 for (size_t j = 0; j < wsdlExtensions_.size(); j++)
00710 if (wsdlExtensions_[j].we != 0 &&
00711 wsdlExtensions_[j].we->isNamespaceHandler(xParser_->getNamespaceUri(i)))
00712 {
00713 wsdlExtensions_[j].we->setNamespacePrefix(xParser_->
00714 getNamespacePrefix
00715 (i));
00716
00717 wsdlExtensions_[j].we->setStartId(MAX_EXT_XML * j + 1);
00718
00719
00720
00721
00722
00723
00724 SchemaParser * xtmpSchemaParser =
00725 new SchemaParser(wsdlExtensions_[j].we->getExtensibilitySchema(),
00726 wsdlExtensions_ [j].we->getNamespace(),ostr,schemaPath_);
00727
00728
00729 xtmpSchemaParser->addImport(schemaParser_[0]);
00730 if (xtmpSchemaParser->parseSchemaTag())
00731 {
00732 wsdlExtensions_[j].spe = xtmpSchemaParser;
00733 wsdlExtensions_[j].we->
00734 setSchemaParser(xtmpSchemaParser);
00735 wsdlExtensions_[j].we->setWsdlParser(this);
00736 }
00737 else {
00738
00739 std::string err = "Error parsing the schema for the namespace ";
00740 err +=wsdlExtensions_[j].we->getNamespace();
00741 err +="\n";
00742 err +="Unable to locate the file ";
00743 err += wsdlExtensions_[j].we->getExtensibilitySchema();
00744 err +="\n";
00745 error(err);
00746
00747 }
00748 }
00749 }
00750 int num_attr = xParser_->getAttributeCount();
00751 if (num_attr < 0)
00752 error("Atleast a targetNamespace attribute is needed");
00753 for (i = 0; i < num_attr; i++)
00754 {
00755 if (xParser_->getAttributeName(i) == "name")
00756 {
00757 name_ = xParser_->getAttributeValue(i);
00758 continue;
00759 }
00760
00761 else if (xParser_->getAttributeName(i) != "targetNamespace")
00762 {
00763 handleExtensibilityAttributes(xParser_->getAttributePrefix(i),
00764 xParser_->getAttributeName(i));
00765 }
00766 }
00767 return;
00768 }
00769
00770
00771 void
00772 WsdlParser::parseImport()
00773 {
00774 if (state_ != IMPORT)
00775 error("syntax error");
00776 Imports imp (xParser_->getAttributeValue("", "namespace"),
00777 xParser_->getAttributeValue("", "location"));
00778 if (imp.ns == getNamespace() ) {
00779
00780 std::string fname;
00781 ifstream wsdlStream;
00782 if(!imp.loc.empty())
00783 {
00784 if(XmlUtils::fetchUri(imp.loc,fname))
00785 {
00786
00787
00788
00789
00790
00791 wsdlStream.open(fname.c_str());
00792
00793 XmlPullParser * xpp = new XmlPullParser(wsdlStream);
00794 XmlPullParser * tmpXparser=xParser_;
00795 xParser_=xpp;
00796
00797 xParser_->setFeature(FEATURE_PROCESS_NAMESPACES, true);
00798 xParser_->require(XmlPullParser::START_DOCUMENT, "", "");
00799 while (getNextElement () != WsdlParser::END);
00800 xParser_=tmpXparser;
00801 delete xpp;
00802 }else{
00803 error("Error while opening the included wsdl " + imp.loc);
00804 }
00805 }else{
00806 error("location is a required attribute for <import>");
00807 }
00808 imports_.push_back(imp);
00809
00810 xParser_->nextTag();
00811 }
00812 peek();
00813 }
00814
00815
00816 void
00817 WsdlParser::parseMessage()
00818 {
00819 if (state_ != MESSAGE)
00820 error("syntax error");
00821
00822 Message * m =0;
00823 int num_att = xParser_->getAttributeCount();
00824 std::string n=xParser_->getAttributeValue("", "name");
00825 m=const_cast<Message*>(getMessage(n));
00826 if(!m){
00827 m= new Message(*this);
00828 m->setName(n);
00829 putMessage(m);
00830 }
00831
00832 for (int i = 0; i < num_att; i++){
00833
00834 if (!(xParser_->getAttributePrefix(i)).empty())
00835 m->addExtAttribute(handleExtensibilityAttributes
00836 (xParser_->getAttributePrefix(i),
00837 xParser_->getAttributeName(i)));
00838
00839 }
00840 if (m->getName() == "")
00841 error("syntax error <message> name required");
00842 peek();
00843 try
00844 {
00845 if (state_ == DOCUMENTATION)
00846 {
00847 m->setDocumentation(parseDoc());
00848
00849 }
00850
00851
00852
00853 if (state_ == PART)
00854 {
00855 while (state_ == PART)
00856 {
00857 string p_name;
00858 int type_id = 0, schemaId = -1;
00859 Element* e=0;
00860 Part::PartRefType reftype = Part::None;
00861 int num_att = xParser_->getAttributeCount();
00862 int p_extId = 0;
00863 for (int i = 0; i < num_att; i++)
00864 {
00865 if ("name" == xParser_->getAttributeName(i) &&
00866
00867 (xParser_->getAttributePrefix(i)).empty())
00868 p_name = xParser_->getAttributeValue(i);
00869
00870 else if (("type" == xParser_->getAttributeName(i)
00871 &&xParser_->getAttributePrefix(i).empty())
00872 ||("element" == xParser_->getAttributeName(i)
00873 &&xParser_->getAttributePrefix(i).empty()))
00874 {
00875 if (reftype != Part::None)
00876 error
00877 ("either type or element must occur(only once) in part ");
00878 if ("type" == xParser_->getAttributeName(i))
00879 reftype = Part::Type;
00880
00881 else
00882 reftype = Part::Elem;
00883 Qname type(xParser_->getAttributeValue(i));
00884 type.setNamespace(getNamespace(type.getPrefix()));
00885 if (reftype == Part::Type)
00886 {
00887
00888
00889 type_id = getTypeId(type);
00890 if (type_id == 0)
00891 error("Could not resolve type " +
00892 type.getNamespace() + ":" +
00893 type.getLocalName());
00894 }
00895
00896 else
00897 {
00898
00899 e = getElement(type);
00900 if (e== 0 )
00901 error("Could not resolve element " +
00902 type.getNamespace() + ":" +
00903 type.getLocalName());
00904 }
00905
00906
00907
00908 schemaId = getSchema(type,reftype == Part::Type);
00909 }
00910
00911 else if (!(xParser_->getAttributePrefix(i)).empty())
00912 p_extId = handleExtensibilityAttributes(xParser_->
00913 getAttributePrefix
00914 (i),
00915 xParser_->
00916
00917 getAttributeName
00918 (i));
00919
00920 else
00921 error("Syntax error");
00922 }
00923 peek();
00924 if (state_ == DOCUMENTATION)
00925 {
00926 parseDoc();
00927
00928 }
00929 if(reftype==Part::Elem)
00930 m->addPart(p_name, reftype, (void*)(e) , schemaId);
00931 else
00932 m->addPart(p_name, reftype, (void*)(&type_id) , schemaId);
00933 m->addExtElement(p_extId);
00934 }
00935 }
00936 }
00937 catch(WsdlException we)
00938 {
00939 we.line = xParser_->getLineNumber();
00940 we.col = xParser_->getColumnNumber();
00941 throw we;
00942 }
00943
00944
00945 if (state_ == EXTENSIBILITY)
00946 {
00947 while (state_ == EXTENSIBILITY)
00948 {
00949 m->addExtElement(handleExtensibilityElement(MESSAGE));
00950 peek();
00951 }
00952 }
00953
00954
00955 return;
00956 }
00957
00958
00959
00960 PortType *
00961 WsdlParser::parsePortType()
00962 {
00963 if (state_ != PORT_TYPE)
00964 return 0;
00965
00966 PortType * pt = new PortType(*this);
00967 int num_att = xParser_->getAttributeCount();
00968 for (int i = 0; i < num_att; i++){
00969
00970 if ("name" == xParser_->getAttributeName(i) &&
00971
00972 (xParser_->getAttributePrefix(i)).empty())
00973 pt->setName(xParser_->getAttributeValue(i));
00974
00975 else if (!(xParser_->getAttributePrefix(i)).empty()) {
00976
00977 pt->addExtAttribute(handleExtensibilityAttributes
00978 (xParser_->getAttributePrefix(i),
00979 xParser_->getAttributeName(i)));
00980 }
00981 else {
00982
00983 error("Syntax error.Unrecognized attribute");
00984 }
00985 }
00986 if (pt->getName() == "")
00987 error("syntax error <PortType> name required");
00988
00989 peek();
00990 if (state_ == DOCUMENTATION) {
00991
00992 pt->setDocumentation(parseDoc());
00993
00994 }
00995 if (state_ == OPERATION) {
00996
00997
00998 while (state_ == OPERATION){
00999
01000 Operation * op = parseOperation(pt);
01001 pt->addOp(op);
01002 }
01003 if (state_ == EXTENSIBILITY) {
01004
01005
01006 while (state_ == EXTENSIBILITY){
01007
01008 pt->addExtElement(handleExtensibilityElement(PORT_TYPE));
01009 peek();
01010 }
01011 }
01012 }
01013 putPortType(pt);
01014 return pt;
01015 }
01016
01017
01018
01019 Operation *
01020 WsdlParser::parseOperation(PortType * p)
01021 {
01022 Operation * op = new Operation(*this,p);
01023 if (state_ != OPERATION)
01024 error("syntax error");
01025
01026 int num_att = xParser_->getAttributeCount();
01027 for (int i = 0; i < num_att; i++){
01028
01029 if ("name" == xParser_->getAttributeName(i) &&
01030 (xParser_->getAttributePrefix(i)).empty())
01031 op->setName(xParser_->getAttributeValue(i));
01032
01033
01034
01035 else if (!(xParser_->getAttributePrefix(i)).empty()) {
01036
01037 op->addExtAttribute(handleExtensibilityAttributes
01038 (xParser_->getAttributePrefix(i),
01039 xParser_->getAttributeName(i)));
01040 }
01041
01042 else if ("parameterOrder" == xParser_->getAttributeName(i)) {
01043
01044 }
01045
01046 else
01047 error("Syntax error..unrecognized attribute");
01048 }
01049 if (op->getName() == "")
01050 error("syntax error <operation> name required");
01051 peek();
01052 if (state_ == DOCUMENTATION)
01053 {
01054 op->setDocumentation(parseDoc());
01055
01056 }
01057 if (state_ == INPUT)
01058 {
01059 op->setMessage(pgetMessage(Qname(xParser_->getAttributeValue("", "message"))),
01060 Input,
01061 xParser_->getAttributeValue("", "name"));
01062
01063 processMessageExtensibility(op,WsdlPull::Input);
01064 peek();
01065 if (state_ == OUTPUT)
01066 {
01067 op->setMessage(pgetMessage(Qname(xParser_->getAttributeValue("", "message"))),
01068 Output,
01069 xParser_->getAttributeValue("", "name"));
01070
01071 processMessageExtensibility(op,WsdlPull::Output);
01072 peek();
01073 }
01074 while (state_ == FAULT)
01075 {
01076 op->setMessage(pgetMessage(Qname(xParser_->getAttributeValue("", "message"))),
01077 Fault,
01078 xParser_->getAttributeValue("", "name"));
01079
01080 processMessageExtensibility(op,WsdlPull::Fault);
01081 peek();
01082 }
01083 }
01084
01085 else if (state_ == OUTPUT)
01086 {
01087 op->setMessage(pgetMessage(Qname(xParser_->getAttributeValue("", "message"))),
01088 Output,
01089 xParser_->getAttributeValue("", "name"));
01090 processMessageExtensibility(op,WsdlPull::Output);
01091 peek();
01092 if (state_ == INPUT)
01093 {
01094 op->setMessage(pgetMessage(Qname(xParser_->getAttributeValue("", "message"))),
01095 Input,
01096 xParser_->getAttributeValue("", "name"));
01097 processMessageExtensibility(op,WsdlPull::Input);
01098 peek();
01099 }
01100 while (state_ == FAULT)
01101 {
01102 op->setMessage(pgetMessage(Qname(xParser_->getAttributeValue("", "message"))),
01103 Fault,
01104 xParser_->getAttributeValue("", "name"));
01105 processMessageExtensibility(op,WsdlPull::Fault);
01106 peek();
01107 }
01108 }
01109 if (state_ == DOCUMENTATION)
01110 {
01111 op->setDocumentation(parseDoc());
01112
01113 }
01114 if (state_ == EXTENSIBILITY)
01115 while (state_ == EXTENSIBILITY)
01116 {
01117 op->addExtElement(handleExtensibilityElement(OPERATION));
01118 peek();
01119 }
01120
01121
01122 return op;
01123 }
01124
01125
01126 void
01127 WsdlParser::processMessageExtensibility(Operation * op,
01128 WsdlPull::MessageType mtype)
01129 {
01130
01131 int num_att = xParser_->getAttributeCount();
01132 std::string message_name;
01133 for (int i = 0; i < num_att; i++){
01134
01135 if ("name" == xParser_->getAttributeName(i) &&
01136 (xParser_->getAttributePrefix(i)).empty())
01137 message_name = xParser_->getAttributeValue(i);
01138
01139
01140
01141 else if (!(xParser_->getAttributePrefix(i)).empty()) {
01142
01143 op->addMessageExtensibility(mtype,handleExtensibilityAttributes
01144 (xParser_->getAttributePrefix(i),
01145 xParser_->getAttributeName(i)));
01146 }
01147 }
01148 }
01149
01150 void
01151 WsdlParser::parseTypes()
01152 {
01153 peek();
01154 if (state_ == DOCUMENTATION)
01155 {
01156 parseDoc();
01157
01158 }
01159 try
01160 {
01161 while (state_ == SCHEMA)
01162 {
01163 SchemaParser *sParser=new SchemaParser(xParser_, tnsUri_,ostr,schemaPath_);
01164 sParser->setUri(uri_);
01165 sParser->addImport(schemaParser_[0]);
01166
01167 for (size_t s = 1 ;s<schemaParser_.size();s++){
01168
01169
01170 if (schemaParser_[s]->getNamespace() == soap_->getEncodingUri())
01171 sParser->addImport(schemaParser_[s]);
01172 if (schemaParser_[s]->getNamespace() == soap2_->getEncodingUri())
01173 sParser->addImport(schemaParser_[s]);
01174 }
01175
01176
01177
01178 if (!sParser->parseSchemaTag())
01179 error("Error parsing schema types for "+tnsUri_);
01180 else
01181 schemaParser_.push_back(sParser);
01182 peek();
01183 error(sParser->getNamespace() +" schema parsed",2);
01184 }
01185 for (size_t i = 1; i < schemaParser_.size(); i++)
01186 {
01187
01188 for (size_t j = 1; j < schemaParser_.size(); j++) {
01189
01190 if (schemaParser_[i]->isImported(schemaParser_[j]->getNamespace()))
01191 schemaParser_[i]->addImport(schemaParser_[j]);
01192 }
01193
01194
01195 if (!schemaParser_[i]->finalize())
01196 error("Invalid schema");
01197 }
01198
01199 }
01200 catch(SchemaParserException spe)
01201 {
01202 WsdlException we(spe.description);
01203 we.col = spe.col;
01204 we.line = spe.line;
01205 we.WsdlState = state_;
01206 throw we;
01207 }
01208 }
01209
01210
01211 void
01212 WsdlParser::putMessage(Message * m)
01213 {
01214
01215
01216 messages_.push_back(m);
01217 }
01218
01219
01220 void
01221 WsdlParser::putBinding(Binding * bn)
01222 {
01223 bindings_.push_back(bn);
01224 }
01225
01226 void
01227 WsdlParser::putPortType(PortType * pt)
01228 {
01229 porttypes_.push_back(pt);
01230 }
01231
01232
01233 int
01234 WsdlParser::handleExtensibilityElement(int parent)
01235 {
01236 WsdlExtension * we = getExtensibilityHandler(xParser_->getNamespace());
01237 if (we == 0) {
01238 xParser_->skipSubTree();
01239 return 0;
01240 }
01241
01242 else
01243 return we->handleElement(parent, xParser_);
01244 }
01245
01246
01247 int
01248 WsdlParser::handleExtensibilityAttributes(string prefix, string name)
01249 {
01250 WsdlExtension * we = getExtensibilityHandler(getNamespace(prefix));
01251 if (we == 0)
01252 return 0;
01253
01254 else
01255 return we->handleAttribute(state_, name, xParser_);
01256 }
01257
01258 WsdlExtension *
01259 WsdlParser::getExtensibilityHandler(const std::string &Ns)
01260 {
01261 for (size_t i = 0; i < wsdlExtensions_.size(); i++)
01262 if (wsdlExtensions_[i].we != 0 &&
01263 (wsdlExtensions_[i].we->isNamespaceHandler(Ns)))
01264 return wsdlExtensions_[i].we;
01265 return 0;
01266 }
01267
01268 WsdlExtension *
01269 WsdlParser::getExtensibilityHandler(int extId)
01270 {
01271
01272 if (extId == 0)
01273 return 0;
01274
01275 for (size_t i = 0; i < wsdlExtensions_.size(); i++)
01276 if (wsdlExtensions_[i].we != 0 &&
01277 (extId >= wsdlExtensions_[i].we->getStartId()&&
01278 extId < MAX_EXT_XML + wsdlExtensions_[i].we->getStartId()))
01279 return wsdlExtensions_[i].we;
01280 return 0;
01281 }
01282
01283
01284 void
01285 WsdlParser::parseBinding()
01286 {
01287
01288 Binding * bn = new Binding(*this);
01289 const PortType *pt = 0;
01290 int opBinding, inputBinding, outputBinding, faultBinding, index,
01291 bindingInfo;
01292 opBinding = inputBinding = outputBinding = faultBinding = index =
01293 bindingInfo = 0;
01294 if (state_ != BINDING)
01295 error("syntax error");
01296 int num_att = xParser_->getAttributeCount();
01297 int i;
01298 WsdlExtension* bindingExtension;
01299
01300 for (i = 0; i < num_att; i++)
01301 {
01302 if ("name" == xParser_->getAttributeName(i) &&
01303 (xParser_->getAttributePrefix(i)).empty())
01304 bn->setName(xParser_->getAttributeValue(i));
01305
01306 else if ("type" == xParser_->getAttributeName(i) &&
01307 (xParser_->getAttributePrefix(i)).empty())
01308 {
01309 Qname q(xParser_->getAttributeValue(i));
01310 pt = getPortType(q);
01311 if (!pt)
01312 error("Unknown port type "+ q.getLocalName());
01313 bn->setPortType(pt);
01314 (const_cast<PortType*>(pt))->setBinding(bn);
01315 }
01316
01317 else
01318 error("Syntax error..unrecognized attribute");
01319 }
01320 peek();
01321
01322 if (state_ == DOCUMENTATION) {
01323
01324 bn->setDocumentation(parseDoc());
01325
01326 }
01327 if (state_ == EXTENSIBILITY) {
01328
01329 while (state_ == EXTENSIBILITY) {
01330
01331 bn->setBindingInfo(bindingInfo =
01332 handleExtensibilityElement(BINDING));
01333 bindingExtension=getExtensibilityHandler(bindingInfo);
01334
01335 if(bindingExtension)
01336 bn->setBindingMethod(bindingExtension->getNamespace());
01337 peek();
01338 }
01339 }
01340 while (state_ == OPERATION){
01341
01342 num_att = xParser_->getAttributeCount();
01343 const Operation *op = NULL;
01344 for (i = 0; i < num_att; i++){
01345
01346 if ("name" == xParser_->getAttributeName(i) &&
01347 (xParser_->getAttributePrefix(i)).empty()){
01348
01349 Qname q(xParser_->getAttributeValue(i));
01350 op = pt->getOperation(q);
01351 }
01352
01353 else
01354 error("Unrecognized attribute");
01355 }
01356 index = bn->addOperation(op);
01357 peek();
01358
01359 while (state_ == EXTENSIBILITY) {
01360
01361 opBinding = handleExtensibilityElement(OPERATION);
01362 if(opBinding) bn->addOpBinding(index, opBinding);
01363 peek();
01364 }
01365
01366 if (state_ == DOCUMENTATION) {
01367
01368 parseDoc();
01369 }
01370 if (state_ == INPUT) {
01371
01372 peek();
01373 while (state_ == EXTENSIBILITY){
01374
01375 inputBinding = handleExtensibilityElement(OPERATION);
01376 if(inputBinding) bn->addInputBinding(index, inputBinding);
01377 peek();
01378 }
01379 }
01380 if (state_ == OUTPUT) {
01381
01382 peek();
01383 while (state_ == EXTENSIBILITY){
01384
01385 outputBinding = handleExtensibilityElement(OPERATION);
01386 if(outputBinding) bn->addOutputBinding(index, outputBinding);
01387 peek();
01388 }
01389 }
01390 while (state_ == FAULT) {
01391
01392 peek();
01393 while (state_ == EXTENSIBILITY){
01394
01395 faultBinding = handleExtensibilityElement(OPERATION);
01396 peek();
01397 if(faultBinding) bn->addFaultBinding(index, faultBinding);
01398 }
01399 }
01400 }
01401 putBinding(bn);
01402 }
01403
01404
01405 void
01406 WsdlParser::parseService()
01407 {
01408 if (state_ != SERVICE)
01409 error("Syntax error");
01410 string serviceName;
01411 Service * sv = new Service(*this);
01412 int num_att = xParser_->getAttributeCount();
01413 int i;
01414 for (i = 0; i < num_att; i++) {
01415
01416 if ("name" == xParser_->getAttributeName(i) &&
01417 (xParser_->getAttributePrefix(i)).empty())
01418 serviceName = xParser_->getAttributeValue(i);
01419
01420 else
01421 error("Unrecognized attribute");
01422 }
01423 sv->setName(serviceName);
01424 peek();
01425 if (state_ == DOCUMENTATION) {
01426
01427 sv->setDocumentation(parseDoc());
01428 }
01429 while (state_ == PORT) {
01430
01431 string bnName,portName;
01432 Binding * bn = 0;;
01433 int serviceExtId = 0;
01434 num_att = xParser_->getAttributeCount();
01435 for (i = 0; i < num_att; i++) {
01436
01437 if ("binding" == xParser_->getAttributeName(i) &&
01438 (xParser_->getAttributePrefix(i)).empty()) {
01439
01440 bnName = xParser_->getAttributeValue(i);
01441 }
01442 else if ("name" == xParser_->getAttributeName(i)) {
01443
01444 portName = xParser_->getAttributeValue(i);
01445 }
01446 }
01447
01448 bn = (Binding *) getBinding(bnName);
01449 peek();
01450 if (state_ == DOCUMENTATION) {
01451
01452 parseDoc();
01453
01454 }
01455 if (state_ == EXTENSIBILITY) {
01456
01457 serviceExtId = handleExtensibilityElement(BINDING);
01458 peek();
01459 }
01460 if (bn != 0)
01461 bn->addServiceExtId(serviceExtId);
01462
01463 sv->addPort(portName,bn,serviceExtId);
01464 }
01465 services_.push_back(sv);
01466 }
01467
01468
01469
01470
01471
01472
01473 int
01474 WsdlParser::getSchema(const Qname & name,bool isType)
01475 {
01476 Qname type = name;
01477 type.setNamespace(getNamespace(type.getPrefix()));
01478
01479
01480 if (name.getNamespace() == Schema::SchemaUri)
01481 return 0;
01482
01483
01484 for (size_t i = 0; i < schemaParser_.size(); i++) {
01485
01486
01487
01488 if( schemaParser_[i]->getNamespace() == type.getNamespace() ||
01489 schemaParser_[i]->isImported(type.getNamespace())) {
01490
01491
01492
01493 if ((isType && schemaParser_[i]->getTypeId(name) != Schema::XSD_INVALID) ||
01494 (!isType && schemaParser_[i]->getElement(name) != 0))
01495
01496 return i;
01497
01498 }
01499 }
01500 return -1;
01501 }
01502
01503 Element *
01504 WsdlParser::getElement(const Qname& name)
01505 {
01506 int i = getSchema(name,false);
01507 if (i >= 0)
01508 return const_cast<Element*>(schemaParser_[i]->getElement(name));
01509 else
01510 return 0;
01511 }
01512
01513 int
01514 WsdlParser::getTypeId(const Qname & type)
01515 {
01516
01517 int i = getSchema(type,true);
01518 Qname t=type;
01519
01520 if (i >= 0)
01521 return schemaParser_[i]->getTypeId(t);
01522
01523 else
01524 return 0;
01525 }
01526
01527 void
01528 WsdlParser::getSchemaParsers(std::vector<SchemaParser* >::iterator & from,
01529 std::vector<SchemaParser* >::iterator & to)
01530 {
01531
01532 from=schemaParser_.begin();
01533 from++;
01534 from++;
01535 to=schemaParser_.end();
01536 return ;
01537 }
01538
01539 void
01540 WsdlParser::error(string s,int level)
01541 {
01542 if(level==0){
01543
01544 WsdlException we(s);
01545 if(xParser_){
01546
01547 we.line = xParser_->getLineNumber();
01548 we.col = xParser_->getColumnNumber();
01549 }
01550 we.WsdlState = state_;
01551 errorOccured_ = true;
01552 throw we;
01553 }
01554 #ifdef LOGGING
01555 else if (level == 1) {
01556
01557 ostr<<"Wsdl parser warning : "<<s<<endl;
01558 }
01559 else if (level == 2) {
01560
01561 ostr<<"Wsdl parser info : "<<s<<endl;
01562 }
01563 #endif
01564 }
01565
01566 bool
01567 WsdlParser::getBindings(Binding::cBindingIterator & begin,
01568 Binding::cBindingIterator & end)const
01569 {
01570 if(bindings_.size()>0){
01571
01572 begin=bindings_.begin();
01573 end=bindings_.end();
01574 return true;
01575 }
01576 else
01577 return false;
01578 }
01579
01580 bool
01581 WsdlParser::getPortTypes(PortType::cPortTypeIterator& begin,
01582 PortType::cPortTypeIterator& end)const
01583 {
01584 if(porttypes_.size()>0){
01585
01586 begin=porttypes_.begin();
01587 end=porttypes_.end();
01588 return true;
01589 }
01590 else
01591 return false;
01592 }
01593
01594 int
01595 WsdlParser::getNumSchemas() const
01596 {
01597 return schemaParser_.size() - 2;
01598
01599 }
01600
01601 void
01602 WsdlParser::setSchemaPath(const std::string & schemaPath)
01603 {
01604 schemaPath_ = schemaPath;
01605
01606 for (vector<ExtensionInfo>::iterator ie = wsdlExtensions_.begin();
01607 ie != wsdlExtensions_.end();
01608 ie++)
01609 ie->we->setSchemaPath(schemaPath);
01610
01611
01612 }
01613
01614 }