src/schemaparser/TypeContainer.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 //This class is a recursive storage container for any xml data type
00021 //It stores the actual values occuring in an XML Schema Instance ,
00022 //for a given type defined in the SchemaParser
00023 
00024 #ifndef _TYPECONTAINERH
00025 #define  _TYPECONTAINERH
00026 #include <map>
00027 
00028 #include "xmlpull/wsdlpull_export.h"
00029 #include "xmlpull/XmlUtils.h"
00030 #include "schemaparser/SchemaParser.h"
00031 
00032 
00033 namespace Schema {
00034 class TypeContainer;
00035 
00036 
00037 typedef struct
00038 {
00039   std::vector<TypeContainer *>tc;
00040   int count;//Count maintains the last accessed container
00041   int num;//num is the number of occurrences of this child
00042 } Containers;
00043 
00044 class WSDLPULL_EXPORT TypeContainer
00045 {
00046  public:
00047   TypeContainer(int  typeId,const SchemaParser * sp);
00048   TypeContainer(ContentModel* cm,
00049                 const SchemaParser * sp);
00050   ~TypeContainer();
00051   TypeContainer *getAttributeContainer(std::string attName, 
00052                                        bool create=false);
00053   TypeContainer *getBaseTypeContainer(bool create=false);
00054 
00055   TypeContainer *getChildContainer(std::string elemName, 
00056                                    bool create = false);
00057 
00058   TypeContainer * getChildContainer(ContentModel* cm,
00059                                     bool create=false);
00060 
00061   //If this is a container for simple type this method 
00062   //returns a void* to the type .The actual type  must be found using getTypeId()
00063   void *getValue();
00064 
00065   //Set position markers for this TypeContainer and all its' children to the start
00066   //TODO add an iterator interface to typecontainer
00067   void rewind();
00068 
00069   //This method searches the xml instance for an element whose name is specified
00070   //and is a simple type.If the return value is non null then type has the actual
00071   //type of the returned value which can be used for type casting
00072   void * getValue(const std::string & name,Schema::Type & type);
00073 
00074   const SchemaParser * schemaParser()const;
00075   bool isValueValid()const;
00076   //return the type which the container instanciates
00077   //The typeId is 0 if this is a container for an anonymous content model
00078   int getTypeId()const;
00079   //return the content model which the container instanciates
00080   //The content model is null if this is a container instanciates a schema defined type
00081   ContentModel* getContentModel()const;
00082 
00083   //Various set methods
00084   void setValue(const std::string & sValue,bool valid=true);
00085   void setValue(int iValue,bool valid=true);
00086   void setValue(char  cValue,bool valid=true);
00087   void setValue(long lValue,bool valid=true);
00088   void setValue(unsigned long long ulValue,bool valid=true);
00089   void setValue(float fValue,bool valid=true);
00090   void setValue(double dbValue,bool valid=true);
00091   void setValue(bool bValue,bool valid=true);
00092   void setValue(Qname & qnValue,bool valid=true);
00093 
00094   //if the container actually stores atomic data,return its type
00095   void setValAsString(const std::string &v);//set a value without validating
00096   void print(std::ostream & os); 
00097   friend  std::ostream &operator<<(std::ostream &os, TypeContainer &tc);
00098   static bool printTypeNames_;
00099  private:
00100   //The unique id of the type for which this holds data
00101   Schema::Type typeId_; 
00102   ContentModel* cm_;
00103   std::map < std::string, Containers *> particleContainers_;//Type containers for particles
00104   std::map<ContentModel*,TypeContainer* >cmContainers_;//Type container for the content models
00105   std::map < std::string, TypeContainer *> attributeContainers_;//TypeContainers for attributes
00106   const SchemaParser *sParser_;
00107   TypeContainer * baseContainer_;
00108 
00109   union
00110   {
00111     std::string *sValue;
00112     int *iValue;
00113     unsigned int *uiValue;
00114     long *lValue;
00115     unsigned long *ulValue;
00116     short *shValue;
00117     unsigned short *usValue;
00118     float *fValue;
00119     double *dbValue;
00120     bool *bValue;
00121     char *cValue;
00122 
00123     //              Date *dValue;
00124     //              DateTime *dtValue;
00125     //              Time *tValue;
00126     Qname *qnValue;
00127 
00128     //      Uri *uriValue ;
00129   } Value;
00130   bool isValueValid_;//is the atomic date type valid
00131   std::string strVal;
00132   std::vector<TypeContainer*> tcTable;
00133 
00134   void deleteValue();
00135   void printComplexType (std::ostream & os);
00136   void printSimpleType (std::ostream & os);
00137   void printContentModel(std::ostream & os);
00138 
00139   //Set position markers for this TypeContainer to the start
00140   void rewindParticleContainers(std::map < std::string, Containers *> &particleContainers);
00141 };
00142 
00143 inline
00144 void
00145 TypeContainer::setValue(const std::string & sValue,bool valid)
00146 {
00147   deleteValue();
00148   Value.sValue = new std::string(sValue);
00149   isValueValid_=valid;
00150 }
00151 
00152 inline
00153 void
00154 TypeContainer::setValue(int iValue,bool valid)
00155 {
00156   deleteValue();
00157   Value.iValue = new int (iValue);
00158   isValueValid_=valid;
00159 }
00160 
00161 inline
00162 void 
00163 TypeContainer::setValue(char cValue,bool valid)
00164 {
00165   deleteValue();
00166   Value.cValue = new char (cValue);
00167   isValueValid_=valid;
00168 }
00169 
00170 inline
00171 void 
00172 TypeContainer::setValue(long lValue,bool valid)
00173 {
00174   deleteValue();
00175   Value.lValue = new long (lValue);
00176   isValueValid_=valid;
00177 }
00178 
00179 inline
00180 void 
00181 TypeContainer::setValue(unsigned long long ulValue,bool valid)
00182 {
00183   deleteValue();
00184   Value.ulValue = new unsigned long (ulValue);
00185   isValueValid_=valid;
00186 }
00187 
00188 inline
00189 void 
00190 TypeContainer::setValue(float fValue,bool valid)
00191 {
00192   deleteValue();
00193   Value.fValue = new float;
00194   *(Value.fValue) = fValue;
00195   isValueValid_=valid;
00196 }
00197 
00198 inline
00199 void 
00200 TypeContainer::setValue(double dbValue,bool valid)
00201 {
00202   deleteValue();
00203   Value.dbValue = new double;
00204   *(Value.dbValue) = dbValue;
00205   isValueValid_=valid;
00206 }
00207 
00208 inline
00209 void 
00210 TypeContainer::setValue(bool bValue,bool valid)
00211 {
00212   deleteValue();
00213   Value.bValue = new bool;
00214   *(Value.bValue) = bValue;
00215   isValueValid_=valid;
00216 }
00217 
00218 inline
00219 void 
00220 TypeContainer::setValue(Qname & qnValue,bool valid)
00221 {
00222   deleteValue();
00223   Value.qnValue = new Qname(qnValue);
00224   isValueValid_=valid;
00225 }
00226 
00227 inline
00228 int 
00229 TypeContainer::getTypeId()const
00230 {
00231   return typeId_;
00232 }
00233 
00234 inline 
00235 ContentModel* 
00236 TypeContainer::getContentModel()const
00237 {
00238   return cm_;
00239 }
00240 
00241 inline
00242 void
00243 TypeContainer::setValAsString(const std::string&v)
00244 {
00245   strVal=v;
00246 }
00247 /*
00248 inline
00249 void
00250 TypeContainer::setValidity(bool b )
00251 {
00252   isValueValid_ = b;
00253 }
00254 */
00255 
00256 
00257 inline
00258 bool
00259 TypeContainer::isValueValid()const
00260 {
00261   return isValueValid_;
00262 }
00263 }
00264 #endif                                            /*  */

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