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

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