src/wsdlparser/WsdlParser.h

00001 /* 
00002  * Wsdlpull - A C++ parser  for WSDL  (Web services description language)
00003  * Copyright (C) 2005-2007 Vivek Krishna
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public
00016  * License along with this library; if not, write to the Free
00017  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018  *
00019  */
00020 #ifndef _WSDLPARSERH
00021 #define _WSDLPARSERH
00022 
00023 #include "xmlpull/wsdlpull_export.h"
00024 #include "xmlpull/XmlPullParser.h"
00025 #include "xmlpull/XmlPullParserException.h"
00026 #include "schemaparser/SchemaParser.h"
00027 
00028 #include "wsdlparser/PortType.h"
00029 #include "wsdlparser/Message.h"
00030 #include "wsdlparser/Operation.h"
00031 #include "wsdlparser/Binding.h"
00032 #include "wsdlparser/Service.h"
00033 #include "wsdlparser/WsdlExtension.h"
00034 
00035 
00036 namespace WsdlPull{
00037 
00038   const std::string wsdlUri="http://schemas.xmlsoap.org/wsdl/";
00039 
00040 
00041 class Soap;
00042 //Wsdl pull parser
00043 class WSDLPULL_EXPORT WsdlParser
00044 {
00045  public:
00046   /** @name Constructors and Destructors */
00047   //@{
00048   /**
00049    * The default constructor for WsdlParser
00050    * @param input stream for the wsdl file
00051    * @param output stream for any error outputs
00052    * @param schemaPath path to directory containing XML schema files
00053    *
00054    */
00055   WsdlParser(std::istream & in = std::cin, std::ostream & out = std::cout,
00056              const std::string & schemaPath = "");
00057   WsdlParser(const std::string& Uri,std::ostream & out = std::cout,
00058              const std::string & schemaPath = "");
00059   ~WsdlParser();
00060   //@}
00061 
00062   /**
00063    * setSchemaPath
00064    * Set path to location of XML schema files, used by parser.
00065    */
00066   void setSchemaPath(const std::string & schemaPath);
00067 
00068     /** @name methods used for parsing */
00069   //@{
00070   /**
00071    * getNextElement processes the next Wsdl ELement in the stream and
00072    * returns the type of the root Wsdl element parsed.
00073    * @return  one of  START,DEFINITION,DOCUMENTATION,ANNOTATION,
00074    *            TYPES,MESSAGE,PORT_TYPE,BINDING,SERVICE,IMPORT
00075    */
00076   int getNextElement();
00077 
00078   /**
00079    * getEventType
00080    * returns the type Wsdl element parsed.
00081    * @return  one of the Wsdl event types
00082    */
00083   int getEventType();
00084 
00085   //@}
00086 
00087   /** @name Various Getter methods*/
00088   //@{
00089   //NOTE:The caller must *NOT* free the pointers returned by the getter apis
00090   //This is true for all apis except whenever the api returns a pointer to 
00091   //a list of elements which is not one  of the root wsdl elements ,
00092   //such as getOperations(port type) which is the only exception
00093   /**
00094    * getTargetNamespace
00095    * @return target namespace of the wsdl document
00096    */
00097   std::string getNamespace(void);
00098 
00099   /**
00100    * getName
00101    * @return name of the wsdl document
00102    */
00103   std::string getName();
00104 
00105   /**
00106    * getDocumentation Returns the documentation string for the WSDL file
00107    *                  This method must be called after <code>getEventType</code> returns DOCUMENT
00108    * @return documentation std::string .
00109    */
00110   const std::string*  getDocumentation();
00111 
00112   /**
00113    * getBinding .This method must be called after
00114    *  <code>getEventType</code> returns BINDING
00115    * @return reference to the Binding element just parsed.
00116    * @exception WsdlException
00117    */
00118   const Binding *getBinding();
00119 
00120   /**
00121    * getBinding .
00122    * @param Qname refering to the binding element
00123    * @return reference to the Binding element
00124    * @exception WsdlException
00125    */
00126   const Binding *getBinding(const Qname & q);
00127 
00128   /**
00129    * getBindings
00130    * @return iterator to a list of binding elements
00131    * @exception WsdlException
00132    */
00133   bool  getBindings(Binding::cBindingIterator & begin,
00134                     Binding::cBindingIterator & end)const;
00135 
00136   /**
00137    * getService .This method must be called after
00138    *  <code>getEventType</code> returns SERVICE
00139    * @return reference to the Binding element just parsed.
00140    * @exception WsdlException
00141    */
00142   const Service *getService();
00143 
00144   /**
00145    * getService.
00146    * @param Qname refering to the service element
00147    * @return reference to the Service element
00148    * @exception WsdlException
00149    */
00150     typedef std::list<Service*>::iterator ServiceIterator;
00151   const Service *getService(const Qname & q);
00152     void getServices(ServiceIterator &from, ServiceIterator &to);
00153 
00154   /**
00155    * getPortType .This method must be called after
00156    *  <code>getEventType</code> returns PORT_TYPE
00157    * @return reference to the port type element just parsed.
00158    * @exception WsdlException
00159    */
00160   const PortType *getPortType();
00161 
00162   /**
00163    * getPortType
00164    * @param Qname refering to the port type element
00165    * @return reference to the port type element
00166    * @exception WsdlException
00167    */
00168   const PortType *getPortType(const Qname & q);
00169 
00170   /**
00171    * getPortTypes
00172    * @return iterators to a container of port types
00173    * @exception WsdlException
00174    */
00175   bool getPortTypes(PortType::cPortTypeIterator& begin,
00176                     PortType::cPortTypeIterator& end)const;
00177 
00178   /**
00179    * getOperations
00180    * @param Qname refering to the port type element
00181    * @return true if the port type is found.
00182    returns the start and ending iterators to operations
00183    * @exception WsdlException
00184    */
00185   bool getOperations(const Qname & portType,
00186                      Operation::cOpIterator& begin,
00187                      Operation::cOpIterator& end);
00188 
00189   /**
00190    * getOperation
00191    * @param Qname refering to the port type element
00192    * @param Qname refering to the operation element
00193    * @return reference to the operation element
00194    * @exception WsdlException
00195    */
00196   const Operation *getOperation(const Qname & portType, 
00197                                 const Qname & q);
00198 
00199   /**
00200    * getMessage .This method must be called after
00201    *  <code>getEventType</code> returns MESSAGE
00202    * @return reference to the message element just parsed.
00203    * @exception WsdlException
00204    */
00205   const Message *getMessage();
00206 
00207   /**
00208    * getMessage .
00209    * @param Qname of the message element
00210    * @return reference to the message element
00211    * @exception WsdlException
00212    */
00213   const Message *getMessage(const Qname & q);
00214 
00215   /**
00216    * getMessages
00217    * @return list of all messages  parsed.
00218    * @exception WsdlException
00219    */
00220   std::list < const Message *>& getMessages();
00221 
00222   /**
00223    * getSchemaParsers
00224    * @return iterators to list of schema parsers 
00225    *         the first element is the start of the list
00226    *         second being the end of it
00227    * @exception WsdlException
00228    */
00229 
00230   void getSchemaParsers(std::vector<SchemaParser* >::iterator & from,
00231                         std::vector<SchemaParser* >::iterator & to) ;
00232   
00233   /**
00234    * getNumSchemas
00235    * @return number of schemas in the wsdl document
00236    * @exception WsdlException
00237    */
00238   int getNumSchemas() const;
00239 
00240   /**
00241    * getSchemaParser
00242    * @param target namespace of the schema
00243    * @return pointer to the schema parser for the namespace
00244    * @exception WsdlException
00245    */
00246   const SchemaParser *getSchemaParser(std::string targetNamespace) const;
00247 
00248 
00249   /**
00250    * to query the status of wsdl parsing
00251    */
00252   bool status()const;
00253   std::string wsdlPath()const;
00254   std::ostream & logger();
00255   std::string &getWsdlFileName() {
00256     return wsdlFileName;
00257   }
00258   
00259   static bool useLocalSchema_;//if false fetch the schemas from the web
00260   //@}
00261 
00262   
00263   /** @name Extensibility handler methods*/
00264   //@{
00265   /**
00266    * addExtensibilityHandler Registers an extensibility handler
00267    *                         for Wsdl extensibility namespaces
00268    * @param pointer to the instance of WsdlExtension
00269    * @exception WsdlException
00270    */
00271   void addExtensibilityHandler(WsdlExtension * ext);
00272   /**
00273    * getExtensibilityHandler returns theextensibility handler
00274    *                         for the namespaces
00275    * @param namespace
00276    * @exception WsdlException
00277    */
00278   WsdlExtension *  getExtensibilityHandler(const std::string & ns);
00279    //return the extensibility handler for the given id.
00280    //WsdlParser only stores ids for the extensibility elements.
00281   WsdlExtension * getExtensibilityHandler(int extId);
00282   //@}
00283 
00284  
00285   /** @name Miscellaneous methods*/
00286   //@{
00287   /**
00288    * setFeature enables one of the features of the parser
00289    * @return   true or false 
00290    */
00291   bool setFeature (int feature_id);
00292   //@}
00293 
00294 
00295   /**
00296    *The various states of the Wsdl Parser
00297    */
00298   enum
00299     {
00300       NONE ,
00301       START,
00302       DEFINITION,
00303       DOCUMENTATION,
00304       ANNOTATION,
00305       IMPORT,
00306       SCHEMA,
00307       TYPES,
00308       MESSAGE,
00309       PART,
00310       PORT_TYPE,
00311       OPERATION,
00312       INPUT,
00313       OUTPUT,
00314       FAULT,
00315       BINDING,
00316       EXTENSIBILITY,
00317       SERVICE,
00318       PORT,
00319       END
00320     };
00321 
00322 #ifdef LOGGING
00323   //for debugging
00324   void print(std::ostream & out);
00325 #endif
00326 
00327   //{@ Deprecated
00328   /**
00329    * getSchemaParser
00330    * @param index of the schema parser .index:0..getNumSchemas-1
00331    * @return pointer to the schema parser for the namespace
00332    * @exception WsdlException
00333    */
00334   const SchemaParser *getSchemaParser(int schemaId) const;
00335   //@}
00336 
00337  private:
00338   /* 
00339    *  private methods for parsing
00340    */
00341   int peek(bool lookahead = true);
00342   int next();
00343   void error(std::string s,int level=0);
00344   std::string getNamespace(std::string prefix);
00345   Element * getElement(const Qname &);
00346   int getTypeId(const Qname &);
00347     int getSchema(const Qname & name,bool type = true);
00348   std::string* parseDoc();
00349   void parseDefinitions();
00350   void parseMessage();
00351   void parseAnnotation();
00352   void parseImport();
00353   PortType *parsePortType();
00354   Operation *parseOperation(PortType* p);
00355   void processMessageExtensibility(Operation * op,
00356                                    WsdlPull::MessageType mtype);
00357   void parseTypes();
00358   void parseBinding();
00359   void parseService();
00360   int handleExtensibilityAttributes(std::string prefix, std::string name);
00361   int handleExtensibilityElement(int);
00362   void putMessage(Message * m);
00363   void putPortType(PortType * pt);
00364   void putBinding(Binding * bn);
00365   void initialize(bool);
00366   bool errorOccured_;
00367   XmlPullParser* openSchemaFile(const std::string& path);
00368   const Message *pgetMessage(const Qname& q);
00369   std::string name_, tnsPrefix_, tnsUri_;
00370   std::vector<SchemaParser*> schemaParser_;
00371 
00372   typedef struct
00373   {
00374     WsdlExtension *we;
00375     SchemaParser *spe;
00376   } ExtensionInfo;
00377   std::list < const Message *> messages_;
00378   std::vector<ExtensionInfo> wsdlExtensions_;
00379   std::list < PortType *>porttypes_;
00380   std::list < Binding *>bindings_;
00381   std::list < Service*> services_;
00382 
00383   class Imports
00384   {
00385   public:
00386     Imports(std::string,std::string);
00387     std::string ns, loc;
00388   };
00389   std::vector<Imports> imports_;
00390   std::list < std::string *> docs_list_;
00391   std::ostream & ostr;
00392   std::istream & istr;
00393   int state_;
00394   int element_;                            //the last Wsdl element parsed
00395   std::string* Doc_;
00396   XmlPullParser * xParser_;
00397   std::ifstream xmlStream;
00398   std::string wsdlFileName;
00399   Soap * soap_;
00400   const int MAX_EXT_XML;
00401   std::string schemaPath_; // Path to schemas, used by parser during parsing.
00402   std::string uri_; //The uri to use to resolve imports.1 level up the location of the wsdl file
00403 };
00404 
00405 
00406 inline
00407 WsdlParser::Imports::Imports(std::string nameSpace,std::string location)
00408   :ns(nameSpace),
00409      loc(location)
00410 {
00411 }
00412 
00413 
00414 inline
00415 std::string
00416 WsdlParser::getNamespace(std::string prefix)
00417 {
00418   return xParser_->getNamespace(prefix);
00419 }
00420 
00421 
00422 //public  APIs
00423 inline
00424 int  
00425 WsdlParser::getNextElement()
00426 {
00427   next();
00428   return element_;
00429 }
00430 
00431 inline
00432 const std::string*
00433 WsdlParser::getDocumentation() 
00434 {
00435   return  (const std::string*) Doc_;
00436 }
00437 
00438 inline
00439 std::string
00440 WsdlParser::getNamespace(void)
00441 {
00442   return tnsUri_;
00443 }
00444 
00445 inline
00446 std::string
00447 WsdlParser::getName()
00448 {
00449   return name_;
00450 }
00451 
00452 
00453 
00454 inline
00455 int
00456 WsdlParser::getEventType()
00457 {
00458   return element_;
00459 }
00460 
00461 
00462 inline
00463 std::list < const Message *>&
00464 WsdlParser::getMessages()
00465 {
00466   return messages_;
00467 }
00468 
00469 //This is deprecated
00470 //the ids 0 and 1 are return the schema parsers used to internally parse the soap schema.
00471 //Ideally you should use getSchemaParser(std::string).If you want to iterate over all the schema parsers
00472 //use getSchemaParsers
00473 inline
00474 const SchemaParser *
00475 WsdlParser::getSchemaParser(int schemaId) const
00476 {
00477   return (const SchemaParser *) schemaParser_[schemaId];
00478 }
00479 
00480 inline
00481 std::string
00482 WsdlParser::wsdlPath()const
00483 {
00484   return wsdlFileName           ;
00485 }
00486 inline
00487 bool
00488 WsdlParser::status()const
00489 {
00490  return !errorOccured_;
00491 }
00492 
00493 inline
00494 std::ostream&
00495 WsdlParser::logger()
00496 {
00497   return ostr;
00498 }
00499 }
00500 /*  */
00501 #endif                                            /*  */

Generated on Sun Aug 10 00:07:40 2008 for wsdlpull by  doxygen 1.4.6