src/schemaparser/SchemaParser.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  */
00021 #ifndef _SCHEMAPARSERH
00022 #define _SCHEMAPARSERH
00023 
00024 
00025 #include "xmlpull/wsdlpull_export.h"
00026 #include "xmlpull/XmlPullParser.h"
00027 #include "schemaparser/Schema.h"
00028 #include "schemaparser/SchemaParserException.h"
00029 #include "schemaparser/Group.h"
00030 #include "schemaparser/Element.h"
00031 #include "schemaparser/Constraint.h"
00032 #include "schemaparser/AttributeGroup.h"
00033 #include "schemaparser/ComplexType.h"
00034 #include "schemaparser/SimpleType.h"
00035 #include "schemaparser/TypesTable.h"
00036 
00037 
00038 namespace Schema {
00039 
00040 //class Schema Parser
00041 class WSDLPULL_EXPORT SchemaParser
00042 {
00043  public:
00044 
00045   /**
00046    * typedefs 
00047    */
00048   //@{
00049   typedef std::list<Element> ElementList;
00050   typedef std::list<Attribute> AttributeList;
00051   typedef std::list<Group> GroupList;
00052   typedef std::list<AttributeGroup*> AttributeGroupList;
00053   typedef std::list<Constraint*> ConstraintList;
00054   typedef std::list<Qname>  QNameList;
00055   typedef std::list < const XSDType *> ConstTypeList;
00056 
00057     typedef struct
00058     {
00059       SchemaParser* sParser;
00060       std::string ns;
00061     } ImportedSchema ;
00062 
00063   //@}
00064   
00065   /** @name Constructors and Destructors */
00066   //@{
00067 
00068   /**
00069    * The constructor for SchemaParser
00070    * @param the URI schema definition file.
00071    * @param target namespace
00072    * @param output stream for any error outputs
00073    * @param confPath The path where schema files for soap and other namespaces are located.
00074    *        This is required only if you have stored them other than src/schemas on windows.
00075    *        On *nix it is almost never required if you install using the make install
00076    */
00077   SchemaParser(const std::string&  Uri, std::string tns = "", 
00078                std::ostream & log = std::cout,const std::string & confPath="");
00079 
00080   /**
00081    * The constructor for SchemaParser
00082    * @param XmlPullParser instance for the schema definition file.
00083    * @param target namespace
00084    * @param output stream for any error outputs
00085    * @param confPath The path where schema files for soap and other namespaces are located.
00086    *        This is required only if you have stored them other than src/schemas on windows.
00087    *        On *nix it is almost never required if you install using the make install
00088    */
00089   SchemaParser(XmlPullParser * parser, std::string tns = "",
00090                std::ostream & log = std::cout,const std::string & confPath="");
00091 
00092   ~SchemaParser();
00093 
00094   //@}
00095 
00096   /** @name methods used for parsing */
00097   //@{
00098   /**
00099    * parseSchemaTag
00100    * @return true if parsing was successful ,false otherwise
00101    */
00102   bool parseSchemaTag();
00103 
00104   //@}
00105 
00106   /** @name Various Getter methods*/
00107   //@{
00108 
00109   /**
00110    * getType
00111    * @param Qname refering to the type
00112    * @return pointer to the type
00113    */
00114   const XSDType *getType(const Qname & type) ;
00115 
00116   /**
00117    * @param the types unique id
00118    * @return pointer to the type
00119    */
00120   const XSDType *getType(int id) const;
00121 
00122   /**
00123     * @param the types unique id
00124     * @param the namespace of the type
00125     * @return pointer to the type
00126     */
00127     const XSDType *getType(int id, std::string &nameSpace);
00128 
00129     /**
00130    * @return a std::list of all types defined in the schema
00131    *         including anonymous types
00132    *         caller *MUST* free the std::list but not the std::list members
00133    */
00134   ConstTypeList *getAllTypes() const;
00135 
00136   /**
00137    * @param Qname of the element
00138    * @return  pointer to a globally defined element in the schema
00139    */
00140   const Element *getElement(const Qname & element) const;
00141   
00142   /**
00143    *
00144    * returns the std::list of all the  global elements in the schema
00145    * @param void
00146    * @return std::list<Element>
00147    */
00148   const ElementList&  getElements() const;
00149 
00150   /**
00151    * @return number of globally defined elements in the schema
00152    */
00153   int getNumElements() const;
00154 
00155   /**
00156    * getAttribute
00157    * @param Qname of the attribute
00158    * @return  pointer to a globally defined attribute in the schema
00159    */
00160   Attribute *getAttribute(const Qname & attribute) ;
00161 
00162   /**
00163    *
00164    * returns a std::list of global attributes in the schema
00165    * @param void
00166    * @return std::list<Attribute>
00167    */
00168   const AttributeList&  getAttributes()const;
00169   
00170   /**
00171    * @return number of globally defined attributes in the schema
00172    */
00173   int getNumAttributes() const;
00174 
00175 
00176   /**
00177    * @return target namespace of the schema document
00178    */
00179   std::string getNamespace(void) const;
00180 
00181   /**
00182    * @return number of types defined in the schema (includes anonymous types)
00183    */
00184   int getNumTypes() const;
00185 
00186 
00187   /**
00188    * getTypeId :Search for a type ,if not present create one
00189    * @param Qname of the type
00190    * @param bool:create
00191    * @return type id
00192    */
00193   int getTypeId(const Qname &, bool create = false);
00194 
00195   /**
00196    * isBasicType
00197    * @param unique type identifier
00198    * @return  true if its a basic schema type false otherwise
00199    */
00200   bool isBasicType(int sType) const;
00201 
00202   /**
00203    * getBasicContentType
00204    *
00205    * If the type has a simple content model then this method returns
00206    * the basic schema type which defines its contents
00207    * For example calling on a type like below would return Schema::STRING
00208    
00209    <xsd:complexType>
00210    <xsd:simpleContent> 
00211    <xsd:extension base = "xsd:std::string">
00212    <xsd:attribute name = "lang" type = "xsd:std::string"/>
00213    </xsd:extension>
00214    </xsd:simpleContent> 
00215    </xsd:complexType>
00216    *    
00217    * 
00218    * @param unique type identifier 
00219    * @return  type id of the basic type from which this type is derived
00220    *          or  if the typeId is one of the atomic types,the same value is returned
00221    *          If the typeId is a complex type Schema::XSD_INVALID is returned
00222    *
00223    */
00224   int getBasicContentType(int typeId)const;
00225   
00226   /**
00227    * getGroup
00228    * @param unique type identifier
00229    * @return  Group*
00230    */
00231   Group* getGroup(const Qname& name);
00232   
00233   /**
00234    * getAttributeGroup
00235    * @param unique type identifier
00236    * @return  AttributeGroup*
00237    */
00238   AttributeGroup* getAttributeGroup(const Qname& name);
00239  
00240   //@}
00241 
00242   /** @name Methods for handling Imports*/
00243   //@{
00244   /**
00245    *  isImported 
00246    *  true if the schema parser imports a namespace
00247    */
00248   bool isImported(const std::string & ns)const;
00249   const SchemaParser* getImportedSchemaParser(const std::string & ns)const;
00250   /**
00251    * addImport .Instructs the schema parser to import a namespace
00252    * @param namespace of the schema
00253    * @param (optional)schemaLocation .If this is not passed ,schema file is not processed
00254    *                                  but any refernces to the namespace are not flagged as errors
00255    * @return true if the schema was succesfully imported.If location is not passed always returns true
00256    */
00257   bool addImport(std::string ns, std::string location="");
00258   /**
00259    * addImport . imports the namespace of the schemaparser
00260    * @param  SchemaParser instance which has parsed the namespace
00261    * @return true if the schema was succesfully imported .
00262    */
00263   bool addImport(SchemaParser* sp);
00264   /*
00265    * addImports .To add an array of schema parsers for imported schemas
00266    * @param array of schema parsers
00267    * @param number of schema parsers added
00268    */
00269   bool addImports(const std::vector<SchemaParser *>& schemaParsers); //to be removed soon
00270 
00271   //@}
00272 
00273 
00274   /** @name Miscellaneous Methods */
00275   //@{
00276   /**
00277    * finalize : tries to match unresolved types and references with imported schemas
00278    *            you *must* call this to ensure successful type resolution
00279    * @return  true if all type references are resolved ,false otherwise
00280    */
00281   bool finalize(void);
00282 
00283   /** 
00284    * setWarningLevel
00285    * default is 0 .
00286    * 1 is wanrning level 
00287    * 2 is information level //quite verbose
00288    */
00289   void setWarningLevel(int l);
00290   /*
00291    * path to the directory where the config file for handling 
00292    * imports is located
00293    */
00294   void setSchemaPath(const std::string& s);
00295 
00296   /* Set the path to the uri from where references to  imported schemas
00297    * may be resolved. One level up the actual location.
00298    * Example if you set uri as "tempuri.org" ,a reference to imported schema location "x.xsd"
00299    * will be mapped to "tempuri.org/x.xsd"
00300    */ 
00301   void setUri(const std::string& u );
00302   /**
00303    * getTypeName()
00304    * return the type name given the id
00305    */
00306   std::string getTypeName(Schema::Type t)const;
00307   TypesTable *getTypesTable();
00308   const SchemaParser *getImportedSchema(std::string &nameSpace);
00309   std::vector<ImportedSchema> &getImportedSchemas();
00310 
00311   /** getVersion()
00312    * return the schema version
00313    */
00314   std::string getVersion()const;  
00315 
00316   bool getElementQualified() const;
00317   std::string getTnsPrefix( void) const;
00318 
00319 #ifdef LOGGING
00320   /**
00321    * for logging purposes
00322    */
00323   void print(std::ostream &) ;
00324 #endif
00325   //@}
00326 
00327  private:
00328   //This function parses global elements
00329   Element  parseElement(bool & fwdRef);
00330   //This function parses global attributes
00331   Attribute parseAttribute(bool & fwdRef);
00332   void init();
00333 
00334   //This  function parses <annotation> tag
00335   void parseAnnotation();
00336   ComplexType *parseComplexType();
00337   SimpleType *parseSimpleType();
00338 
00339 
00340   Element addAny(ContentModel* cm);
00341   Group parseGroup(ContentModel* cm=0);
00342   Constraint* parseConstraint(Schema::ConstraintType cstr);
00343   AttributeGroup* parseAttributeGroup(ComplexType* cType=0);
00344   Attribute addAnyAttribute(ComplexType * cType);
00345 
00346   void parseRestriction(SimpleType * st,ComplexType * ct=0);
00347   void parseComplexContent(ComplexType * ct);
00348   void parseSimpleContent(ComplexType * ct);
00349 
00350   void parseContent(ContentModel * cm);
00351   bool parseImport(void);
00352   bool parseInclude();
00353   bool parseSchema(std::string tag="schema");
00354   bool parseRedefine();
00355   int checkImport(std::string nsp)const;
00356   void copyImports(SchemaParser * sp);
00357   void resolveForwardElementRefs();
00358   void resolveForwardAttributeRefs();
00359   bool& shouldResolve();
00360   bool makeListFromSoapArray (ComplexType * ct);
00361 
00362   std::string fname_;
00363   std::string tnsUri_;
00364   std::string tnsPrefix_;
00365   std::string version_;
00366   XmlPullParser * xParser_;
00367   bool elementQualified_;
00368   bool attributeQualified_;
00369   bool deleteXmlParser_;
00370   bool resolveFwdRefs_;
00371   TypesTable typesTable_;
00372   std::ifstream xmlStream_;
00373   ElementList lElems_;
00374   AttributeList lAttributes_;
00375   GroupList lGroups_;
00376   AttributeGroupList  lAttributeGroups_;
00377   ConstraintList  constraints_;
00378   QNameList  lForwardElemRefs_;
00379   QNameList lForwardAttributeRefs_;
00380   
00381   std::vector<ImportedSchema> importedSchemas_;  
00382   void error(std::string, int level = 0);
00383   int level_;//warning level
00384   std::ostream & logFile_;
00385   std::string confPath_;
00386   std::string uri_; //The uri to use to resolve imports.1 level up the location of the schema file
00387 };
00388 
00389 
00390 inline
00391 bool &
00392 SchemaParser::shouldResolve()
00393 {
00394   return resolveFwdRefs_;
00395   
00396 }
00397 
00398 inline
00399 const SchemaParser::ElementList&
00400 SchemaParser::getElements() const
00401 {
00402   return lElems_;
00403 }
00404   
00405 inline
00406 const SchemaParser::AttributeList&
00407 SchemaParser::getAttributes() const
00408 {
00409   return lAttributes_;
00410 }
00411 
00412 inline
00413 void
00414 SchemaParser::setWarningLevel(int l)
00415 {
00416   level_ = l;
00417 }
00418 inline
00419 bool 
00420 SchemaParser::isImported(const std::string & ns)const
00421 {
00422   return checkImport(ns) != -1;
00423 }
00424 inline
00425 const SchemaParser*
00426 SchemaParser::getImportedSchemaParser(const std::string & ns)const
00427 {
00428   int i= checkImport(ns);
00429   if (i == -1 )
00430     return 0;
00431 
00432   return importedSchemas_[i].sParser;
00433 }
00434 
00435 inline
00436 void
00437 SchemaParser::setSchemaPath(const std::string& s)
00438 {
00439   confPath_ = s;
00440 }
00441 
00442 inline
00443 void
00444 SchemaParser::setUri(const std::string& s)
00445 {
00446   uri_ = s;
00447 }
00448 
00449 inline
00450 TypesTable*
00451 SchemaParser::getTypesTable() 
00452 {
00453   return &typesTable_;
00454 }
00455 
00456 inline
00457 std::vector<SchemaParser::ImportedSchema>&
00458 SchemaParser::getImportedSchemas() 
00459 {
00460   return importedSchemas_;
00461 }
00462 
00463 inline
00464 std::string
00465 SchemaParser::getVersion()const
00466 {
00467   return version_;
00468 }
00469 
00470 inline
00471 bool
00472 SchemaParser::getElementQualified() const
00473 {
00474   return elementQualified_ ;
00475 }
00476 
00477 inline
00478 
00479 std::string
00480 SchemaParser::getTnsPrefix( void) const
00481 {
00482   return tnsPrefix_;
00483 }
00484 
00485 }
00486 #endif                                            /*  */
00487 
00488 

Generated on Sat May 3 16:28:59 2008 for wsdlpull by  doxygen 1.4.6