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 #ifdef LOGGING
00312   /**
00313    * for logging purposes
00314    */
00315   void print(std::ostream &) ;
00316 #endif
00317   //@}
00318 
00319  private:
00320   //This function parses global elements
00321   Element  parseElement(bool & fwdRef);
00322   //This function parses global attributes
00323   Attribute parseAttribute(bool & fwdRef);
00324   void init();
00325 
00326   //This  function parses <annotation> tag
00327   void parseAnnotation();
00328   ComplexType *parseComplexType();
00329   SimpleType *parseSimpleType();
00330 
00331 
00332   Element addAny(ContentModel* cm);
00333   Group parseGroup(ContentModel* cm=0);
00334   Constraint* parseConstraint(Schema::ConstraintType cstr);
00335   AttributeGroup* parseAttributeGroup(ComplexType* cType=0);
00336   Attribute addAnyAttribute(ComplexType * cType);
00337 
00338   void parseRestriction(SimpleType * st,ComplexType * ct=0);
00339   void parseComplexContent(ComplexType * ct);
00340   void parseSimpleContent(ComplexType * ct);
00341 
00342   void parseContent(ContentModel * cm);
00343   bool parseImport(void);
00344   bool parseInclude();
00345   bool parseSchema(std::string tag="schema");
00346   bool parseRedefine();
00347   int checkImport(std::string nsp)const;
00348   void copyImports(SchemaParser * sp);
00349   void resolveForwardElementRefs();
00350   void resolveForwardAttributeRefs();
00351     int  addExternalElement(const std::string & name,const std::string & nspace,int localTypeId);
00352   bool& shouldResolve();
00353   bool makeListFromSoapArray (ComplexType * ct);
00354 
00355   std::string fname_;
00356   std::string tnsUri_;
00357   std::string tnsPrefix_;
00358   XmlPullParser * xParser_;
00359   bool elementQualified_;
00360   bool attributeQualified_;
00361   bool deleteXmlParser_;
00362   bool resolveFwdRefs_;
00363   TypesTable typesTable_;
00364   std::ifstream xmlStream_;
00365   ElementList lElems_;
00366   AttributeList lAttributes_;
00367   GroupList lGroups_;
00368   AttributeGroupList  lAttributeGroups_;
00369   ConstraintList  constraints_;
00370   QNameList  lForwardElemRefs_;
00371   QNameList lForwardAttributeRefs_;
00372   
00373   std::vector<ImportedSchema> importedSchemas_;  
00374   void error(std::string, int level = 0);
00375   int level_;//warning level
00376   std::ostream & logFile_;
00377   std::string confPath_;
00378   std::string uri_; //The uri to use to resolve imports.1 level up the location of the schema file
00379 };
00380 
00381 
00382 inline
00383 bool &
00384 SchemaParser::shouldResolve()
00385 {
00386   return resolveFwdRefs_;
00387   
00388 }
00389 
00390 inline
00391 const SchemaParser::ElementList&
00392 SchemaParser::getElements() const
00393 {
00394   return lElems_;
00395 }
00396   
00397 inline
00398 const SchemaParser::AttributeList&
00399 SchemaParser::getAttributes() const
00400 {
00401   return lAttributes_;
00402 }
00403 
00404 inline
00405 void
00406 SchemaParser::setWarningLevel(int l)
00407 {
00408   level_ = l;
00409 }
00410 inline
00411 bool 
00412 SchemaParser::isImported(const std::string & ns)const
00413 {
00414   return checkImport(ns) != -1;
00415 }
00416 inline
00417 const SchemaParser*
00418 SchemaParser::getImportedSchemaParser(const std::string & ns)const
00419 {
00420   int i= checkImport(ns);
00421   if (i == -1 )
00422     return 0;
00423 
00424   return importedSchemas_[i].sParser;
00425 }
00426 
00427 inline
00428 void
00429 SchemaParser::setSchemaPath(const std::string& s)
00430 {
00431   confPath_ = s;
00432 }
00433 
00434 inline
00435 void
00436 SchemaParser::setUri(const std::string& s)
00437 {
00438   uri_ = s;
00439 }
00440 
00441 inline
00442 TypesTable*
00443 SchemaParser::getTypesTable() 
00444 {
00445   return &typesTable_;
00446 }
00447 
00448 inline
00449 std::vector<SchemaParser::ImportedSchema>&
00450 SchemaParser::getImportedSchemas() 
00451 {
00452   return importedSchemas_;
00453 }
00454 
00455 }
00456 #endif                                            /*  */
00457 
00458 

Generated on Fri Oct 19 19:34:04 2007 for wsdlpull by  doxygen 1.4.6