SimpleType.cpp

Go to the documentation of this file.
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 
00022 #include "schemaparser/SimpleType.h"
00023 
00024 //TODO .Need to add checks for validating the occurence of consecutive facets_
00025 namespace Schema {
00026 
00027 SimpleType::SimpleType(const std::string&  ns)
00028   :XSDType(ns),
00029    isList_(false),
00030    isUnion_(false),
00031    uTypes_(0)
00032 {
00033   setContentModel(Schema::Simple);
00034   facetId_.clear();
00035 
00036   validFacets_ = new int[XSD_ANYURI + 1];
00037 
00038   //This table maintains a flag which indicates what facets_ are valid  for a simple type
00039   validFacets_[XSD_STRING] = validFacets_[XSD_NMTOKEN] = validFacets_[XSD_NMTOKENS] =
00040     validFacets_[XSD_TOKEN] =
00041     LENGTH | MINLEN | MAXLEN | ENUM | WSP | PATTERN;
00042   validFacets_[XSD_INTEGER] = validFacets_[XSD_INT] = validFacets_[XSD_UINT] =
00043     validFacets_[XSD_BYTE] = validFacets_[XSD_POSINT] =
00044     ENUM | WSP | MAXEX | MINEX | MAXINC | MININC | TOTALDIGITS | FRAC |
00045     PATTERN;
00046   validFacets_[XSD_LONG] = validFacets_[XSD_ULONG] = validFacets_[XSD_DECIMAL] =
00047     validFacets_[XSD_INT];
00048   validFacets_[XSD_SHORT] = validFacets_[XSD_USHORT] = validFacets_[XSD_INT];
00049   validFacets_[XSD_FLOAT] = validFacets_[XSD_DOUBLE] =
00050     ENUM | WSP | MAXEX | MINEX | MAXINC | MININC | PATTERN;
00051   validFacets_[XSD_BOOLEAN] = WSP | PATTERN;
00052   validFacets_[XSD_TIME] = validFacets_[XSD_DATETIME] = validFacets_[XSD_DATE] =
00053     ENUM | WSP | MAXEX | MINEX | MAXINC | MININC | PATTERN;
00054   validFacets_[XSD_QNAME] = validFacets_[XSD_NCNAME] = validFacets_[XSD_ANYURI] =
00055     LENGTH | MINLEN | MAXLEN | ENUM | WSP | PATTERN;
00056   validFacets_[XSD_ANY] = validFacets_[XSD_ANYTYPE] = NONE | PATTERN;
00057   validFacets_[XSD_BASE64BIN] = validFacets_[XSD_STRING];
00058 
00059   //initialize the union??TODO
00060   facetValue_.numEnums = 0;
00061 
00062   facets_["length"] = LENGTH;
00063   facets_["minLength"]= MINLEN;
00064   facets_["maxLength"]= MAXLEN;
00065   facets_["enumeration"]= ENUM;
00066   facets_["whiteSpace"]= WSP;
00067   facets_["pattern"]= PATTERN;
00068   facets_["maxInclusive"]= MAXINC;
00069   facets_["maxExclusive"]= MAXEX;
00070   facets_[ "minInclusive"]= MININC;
00071   facets_["minExclusive"]= MINEX;
00072   facets_["totalDigits"]= TOTALDIGITS;
00073   facets_["fractionDigits"]= FRAC;
00074 }
00075 
00076 SimpleType::~SimpleType() {
00077   delete[] validFacets_;
00078 }
00079 
00080 //This function also sets the facet if valid
00081 bool 
00082 SimpleType::isvalidFacet(std::string facet)
00083 {
00084   if (getBaseTypeId() == 0){
00085     error("isValidFacet:Unknown base type");
00086     return false;
00087   }
00088   int facetType=facets_[facet];
00089   if (validFacets_[getBaseTypeId()] | facetType)
00090     return true;
00091   else
00092     return false;
00093 }
00094 
00095 
00096 //TODO ..need to add checks to see which facets_ can appear together.
00097 void 
00098 SimpleType::setFacetValue(std::string facet,
00099                           std::string val)
00100 {
00101   int num = -1;
00102   int facetType=facets_[facet];
00103   switch (facetType)
00104     {
00105     case ENUM:
00106       facetValue_.numEnums++;
00107       enumValues_.push_back(val);
00108       break;
00109     case PATTERN:
00110       facetValue_.pattern = (const char*) val.c_str();
00111       break;
00112     case WSP:
00113       if (val == "preserve")
00114         facetValue_.wsp = PRESERVE;
00115 
00116       else if (val == "collapse")
00117         facetValue_.wsp = COLLAPSE;
00118 
00119       else if (val == "replace")
00120         facetValue_.wsp = REPLACE;
00121 
00122       else
00123         error("Invalid facet value for whitespace");
00124       break;
00125       //all other facet values are numbers
00126     default:
00127       num = XmlUtils::parseInt(val);
00128       break;
00129     }
00130 
00131   switch (facetType)
00132     {
00133     case MAXEX:
00134       facetValue_.valRange.maxex = num;
00135       break;
00136     case MAXINC:
00137       facetValue_.valRange.maxinc = num;
00138       break;
00139     case MININC:
00140       facetValue_.valRange.mininc = num;
00141       break;
00142     case MINEX:
00143       facetValue_.valRange.minex = num;
00144       break;
00145     case LENGTH:
00146       facetValue_.length = num;
00147       break;
00148     case MINLEN:
00149       facetValue_.lenRange.minlen = num;
00150       break;
00151     case MAXLEN:
00152       facetValue_.lenRange.maxlen = num;
00153       break;
00154     case TOTALDIGITS:
00155       facetValue_.tot = num;
00156       break;
00157     case FRAC:
00158       facetValue_.frac = num;
00159       break;
00160     default:
00161       break;
00162     }
00163 
00164   int numFacets_=facetId_.size();
00165   if(!(numFacets_!=0 && (facetId_[numFacets_ - 1] == ENUM)))
00166     facetId_.push_back(facetType);
00167   //In case of  enum the new enum value does not define a new facet
00168 }
00169 
00170 
00171 bool
00172 SimpleType::isValidInt(int val)const
00173 {
00174   if (!(getBaseTypeId() == Schema::XSD_INT ||
00175         getBaseTypeId() == Schema::XSD_INTEGER))
00176     return false;
00177   int numdigits = 1, tmp = val;
00178   bool valid = true;
00179   while ((tmp = tmp / 10))
00180     numdigits++;
00181   for (size_t i = 0; i < facetId_.size() && valid; i++)
00182     {
00183       switch (facetId_[i])
00184         {
00185         case MAXEX:
00186           if (val < facetValue_.valRange.maxex)
00187             valid = true;
00188           else
00189             valid = false;
00190           break;
00191         case MAXINC:
00192           if (val <= facetValue_.valRange.maxinc)
00193             valid = true;
00194 
00195           else
00196             valid = false;
00197           break;
00198         case MININC:
00199           if (val >= facetValue_.valRange.mininc)
00200             valid = true;
00201 
00202           else
00203             valid = false;
00204           break;
00205         case MINEX:
00206           if (val > facetValue_.valRange.minex)
00207             valid = true;
00208 
00209           else
00210             valid = false;
00211           break;
00212         case LENGTH:
00213           if (numdigits == facetValue_.length)
00214             valid = true;
00215 
00216           else
00217             valid = false;
00218           break;
00219         case MINLEN:
00220           if (numdigits >= facetValue_.lenRange.minlen)
00221             valid = true;
00222 
00223           else
00224             valid = false;
00225           break;
00226         case MAXLEN:
00227           if (numdigits <= facetValue_.lenRange.maxlen)
00228             valid = true;
00229 
00230           else
00231             valid = false;
00232           break;
00233         default:
00234           valid = false;
00235         }
00236     }
00237   return valid;
00238 }
00239 
00240 
00241 bool
00242 SimpleType::isValidFloat(float val)const
00243 {
00244 
00245   //TODO
00246   return true;
00247 }
00248 
00249 
00250 bool
00251 SimpleType::isValidString(std::string val)const
00252 {
00253   int strlen = val.length();
00254   bool valid = true;
00255   std::list < std::string >::const_iterator pEnums;
00256   for (size_t i = 0; i < facetId_.size(); i++)
00257     {
00258       switch (facetId_[i])
00259         {
00260         case LENGTH:
00261           if (strlen == facetValue_.length)
00262             valid = true;
00263 
00264           else
00265             valid = false;
00266           break;
00267         case MINLEN:
00268           if (strlen >= facetValue_.lenRange.minlen)
00269             valid = true;
00270 
00271           else
00272             valid = false;
00273           break;
00274         case MAXLEN:
00275           if (strlen <= facetValue_.lenRange.maxlen)
00276             valid = true;
00277 
00278           else
00279             valid = false;
00280           break;
00281         case ENUM:
00282           {
00283             valid=false;
00284             for (pEnums = enumValues_.begin();
00285                  pEnums != enumValues_.end();
00286                  pEnums++){
00287 
00288               if (*pEnums == val)
00289                 valid = true;
00290             }
00291             break;
00292           }
00293         case PATTERN:
00294           valid = true;                     //TODO
00295           break;
00296         default:
00297           valid = true;
00298         }
00299     }
00300   return valid;
00301 }
00302 
00303 
00304 /*This function returns the facet values of the requested facet type
00305   if the type is restricted by it.
00306   For enum it returns(via val*) a ptr to std::list<std::string>.
00307   For all other facets_ related to length,its returns(via val*) an int pointer
00308   For inclusive and exclusive facets it returns (via val*) ptr to the value
00309   for example
00310   int * minex=0;
00311   simpleType.getFacetValue(SimpleType::MAXINC,minex);
00312  */
00313 
00314 //TODO add cases to return pointers to other facet values
00315 bool 
00316 SimpleType::getFacetValue(int facet, void *& val)
00317 {
00318   val = 0;
00319   bool isFacetPresent = false;
00320   for (size_t i = 0;
00321        i < facetId_.size() && !isFacetPresent;
00322        i++)
00323     isFacetPresent = (facetId_[i] == facet);
00324 
00325   if (!isFacetPresent)
00326     return false;
00327   switch (facet)
00328     {
00329     case ENUM:
00330       val = (void *) &(enumValues_);
00331       return true;
00332       break;
00333         case MAXINC:
00334                 val =  &(facetValue_.valRange.maxinc);
00335                 return true;
00336                 break;
00337         case MAXEX:
00338                 val = (void*) &(facetValue_.valRange.maxex);
00339                 return true;
00340                 break;
00341         case MININC:
00342                 val = (void*) &(facetValue_.valRange.mininc);
00343                 return true;
00344                 break;
00345         case MINEX:
00346                 val = (void*) &(facetValue_.valRange.minex);
00347                 return true;
00348                 break;
00349         case LENGTH:
00350                 val = (void*) &(facetValue_.length);
00351                 return true;
00352                 break;
00353         case MINLEN:
00354                 val = (void*) &(facetValue_.lenRange.minlen);
00355                 return true;
00356                 break;
00357         case MAXLEN:
00358                 val = (void*) &(facetValue_.lenRange.minlen);
00359                 return true;
00360                 break;
00361         case PATTERN:
00362         val = (void*) &(facetValue_.pattern);
00363                 return true;
00364                 break;
00365         case TOTALDIGITS:
00366                 val = (void*) &(facetValue_.tot);
00367                 return true;
00368                 break;
00369         case FRAC:
00370 
00371     default:
00372       val = 0;
00373       return false;
00374     }
00375 }
00376 
00377 
00378 void SimpleType::error(std::string msg)
00379 {
00380   msg += "SimpleType::error()";
00381   SchemaParserException spe(msg);
00382   throw spe;
00383   return;
00384 }
00385 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by  doxygen 1.6.2