wsdlpull  1.23
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TypesTable.cpp
Go to the documentation of this file.
1 /*
2  * wsdlpull - A C++ parser for WSDL (Web services description language)
3  * Copyright (C) 2005-2007 Vivek Krishna
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the Free
17  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 /*
21  * This class stores the list of parsed types
22  */
23 
25 
26 using namespace std;
27 namespace Schema {
28 
29 TypesTable::TypesTable()
30 {
31  currentId = Schema::XSD_ANYURI + 1;
32  numTypes = 0;
33  typesArray = new XSDType *[nSize = 10];
34 
35  //map of simple types
36  basicTypes["string"] = Schema::XSD_STRING;
37  basicTypes["integer"] = Schema::XSD_INTEGER;
38  basicTypes["int"] = Schema::XSD_INT;
39  basicTypes["byte"] = Schema::XSD_BYTE;
40  basicTypes["positiveInteger"] = Schema::XSD_POSINT;
41  basicTypes["unsignedInt"] = Schema::XSD_UINT;
42  basicTypes["long"] = Schema::XSD_LONG;
43  basicTypes["unsignedLong"] = Schema::XSD_ULONG;
44  basicTypes["short"] = Schema::XSD_SHORT;
45  basicTypes["unsignedShort"] = Schema::XSD_USHORT;
46  basicTypes["decimal"] = Schema::XSD_DECIMAL;
47  basicTypes["float"] = Schema::XSD_FLOAT;
48  basicTypes["double"] = Schema::XSD_DOUBLE;
49  basicTypes["boolean"] = Schema::XSD_BOOLEAN;
50  basicTypes["time"] = Schema::XSD_TIME;
51  basicTypes["dateTime"] = Schema::XSD_DATETIME;
52  basicTypes["date"] = Schema::XSD_DATE;
53  basicTypes["token"] = Schema::XSD_TOKEN;
54  basicTypes["QName"] = Schema::XSD_QNAME;
55  basicTypes["NCName"] = Schema::XSD_NCNAME;
56  basicTypes["NMTOKEN"] = Schema::XSD_NMTOKEN;
57  basicTypes["NMTOKENS"] = Schema::XSD_NMTOKENS;
58  basicTypes["base64Binary"] = Schema::XSD_BASE64BIN;
59  basicTypes["hexBinary"] = Schema::XSD_HEXBIN;
60  basicTypes["anyType"] = Schema::XSD_ANYTYPE;
61  basicTypes["any"] = Schema::XSD_ANY;
62  basicTypes["anyURI"] = Schema::XSD_ANYURI;
63 }
64 
65 
66 TypesTable::~TypesTable()
67 {
68  clean();
69 }
70 
71 
72 std::string
73 TypesTable::getAtomicTypeName(Schema::Type t)const
74 {
75 
76  for(
77  std::map<std::string,int>::const_iterator it =
78  basicTypes.begin();
79  it != basicTypes.end();
80  it++){
81 
82  if (it->second == t)
83  return it->first;
84  }
85  return "";
86 }
87 
88 void
89 TypesTable::clean()
90 {
91  for (map < string, int >::iterator it = Id.begin(); it != Id.end();
92  ++it)
93  delete getTypePtr(it->second);
94  numTypes = 0;
95  if (typesArray)
96  {
97  delete[] typesArray;
98  typesArray = 0;
99  }
100 }
101 
102 
103 int TypesTable::addType(XSDType * type)
104 {
105  Qname qn = type->getQname();
106  string type_name(qn.getLocalName());
107  int i = 0;
108 
109  // string ns(qn.getNamespace());
110  if (type_name.empty())
111  {
112 
113  //create an anonymous type name
114  ostringstream tmp_name_str;
115  tmp_name_str << "type" << numTypes;
116  type_name = tmp_name_str.str();
117  type->setName(type_name);
118  type->setAnonymous(true); //auto generated name
119  }
120  ensureCapacity();
121 
122  // std::cout<<type_name<<" ";
123 
124  //add the typename and its id to the map
125  if ((i = Id[type_name]) != 0)
126  {
127 
128  //this type was refernced earlier.
129  typesArray[i - (Schema::XSD_ANYURI + 1)] = type;
130  type->setTypeId(i);
131  // cout<<i<<endl;
132  return i;
133  }
134 
135  else
136  {
137  Id[type_name] = currentId;
138  type->setTypeId(currentId);
139  typesArray[numTypes] = type;
140  currentId++;
141  numTypes++;
142  // cout<<type->getName()<<" "<<currentId -1<<" "<<numTypes<<" "<<this<<endl;
143  return currentId - 1;
144  }
145 }
146 
147 
148 
149 
150 //get the type id of a type
151 int TypesTable::getTypeId(const Qname & name, bool create)
152 {
153  int typeId;
154  if (name.getNamespace() == Schema::SchemaUri)
155  {
156 
157  //This is one of the basic types
158  typeId = basicTypes[name.getLocalName()];
159  if(typeId==0) //if this is a basic type which is not mapped,treat as string
160  typeId=Schema::XSD_STRING;
161  }
162 
163  else if (name.getNamespace() == m_tnsUri )
164  typeId = Id[name.getLocalName()];
165  else if (name.getNamespace().empty()){
166 
167  typeId = basicTypes[name.getLocalName()];
168  if(typeId==0)
169  typeId = Id[name.getLocalName()];
170  if(typeId==0)
171  typeId = Schema::XSD_INVALID;
172  }
173  else
174  return Schema::XSD_INVALID; //the Type does not belong to this schema
175  if (typeId == 0 && create)
176  {
177 
178  //handle forward reference
179  //create an id and return its value
180  Id[name.getLocalName()] = currentId;
181  ensureCapacity();
182  typesArray[numTypes] = 0;
183  currentId++;
184  numTypes++;
185  typeId = currentId - 1;
186  //std::cout<<typeId<<" "<<name<<endl;
187 
188  }
189  return typeId;
190 }
191 
192 //for types present in an imported schema we cant use the type id
193 //since its specific to the imported schema
194 //we need to keep a local type id
195 int
196 TypesTable::addExternalTypeId(const Qname & type, const XSDType * pType)
197 {
198  for (unsigned int i = 0; i < extRefs_.size(); i++)
199  if (extRefs_[i].qname == type)
200  return extRefs_[i].localTypeId;
201 
202  extRefs er;
203  er.qname = (pType)?pType->getQname():type;
204  er.localTypeId = currentId;
205  extRefs_.push_back(er);
206  ensureCapacity();
207  typesArray[numTypes] = const_cast<XSDType*>(pType);
208  numTypes++;
209  return currentId++;
210 
211 }
212 
213 //adds a type into a type table for a given type id
214 //used for types present in imported schemas but referenced in current schema
215 int TypesTable::addExtType(XSDType * type, int localId)
216 {
217  int index = localId - Schema::XSD_ANYURI - 1;
218  if (index >= numTypes)
219  return 0;
220  typesArray[index] = type;
221  return localId;
222 }
223 
224 //increase the array storage if necessary
225 void TypesTable::ensureCapacity()
226 {
227  if (numTypes >= nSize)
228  {
229  XSDType **tempArray = new XSDType *[numTypes + 5];
230  for (int ind = 0; ind < nSize; ind++)
231  tempArray[ind] = typesArray[ind];
232  delete[] typesArray;
233  typesArray = tempArray;
234  nSize = numTypes + 5;
235  }
236 }
237 
238 
239 #if 0
240 
241 /*
242  Takes a XSD type and an accessor name to determine if the
243  accessor is a descendant of the given type
244  If found ,returns the schematype Id of the found element
245  and populates an array of indices denoting the path
246  from the parent to accessor .
247  otherwise return 0(invalid type)
248 
249 */
250 int TypesTable::getCompleteXpath(int elemId, string & childName,
251  int *xPath, int limits, int &offset)
252 {
253  int childId = 0, type = 0, i = 0, childIndex;
254  bool found = false;
255  if (xPath == 0 || limits == 0)
256  return 0;
257  XSDType *pType = getTypePtr(elemId);
258  if (pType == 0)
259  return 0;
260  if (pType->isSimple())
261  return 0;
262  ComplexType *ct = (ComplexType *) pType;
263  for (i = 0; i < ct->getNumChildren(); i++)
264  if (ct->getChildName(i) == childName)
265  {
266  childId = ct->getChildType(i);
267  childIndex = i;
268  }
269  if (childId == 0)
270  {
271 
272 /*
273  childName is not a child of elemId,
274  so call this method recursively
275 */
276  for (int i = 0; i < ct->getNumChildren() && !childId; i++)
277  {
278  if (childId =
279  getCompleteXpath(ct->getChildType(i), childName, xPath + 1,
280  limits - 1, ++offset))
281  xPath[0] = i;
282 
283  else
284  offset--;
285  }
286  }
287 
288  else
289  {
290  xPath[0] = childIndex;
291  offset++;
292  }
293  return childId;
294 }
295 #endif
296 
297 
298 bool
299 TypesTable::detectUndefinedTypes(void)
300 {
301  for (int i = 0; i < numTypes; i++)
302  if (typesArray[i] == 0)
303  return true;
304  return false;
305 }
306 
307 
308 void
309 TypesTable::resolveForwardElementRefs(const string & name, Element & e)
310 {
311  for (int i = 0; i < numTypes; i++)
312  if (typesArray[i] != 0)
313  {
314  if (!typesArray[i]->isSimple())
315  {
316  ComplexType *ct = (ComplexType *) typesArray[i];
317  ct->matchElementRef(name, e);
318  }
319  }
320 }
321 
322 
323 void
324 TypesTable::resolveForwardAttributeRefs(const string & name, Attribute & a)
325 {
326  for (int i = 0; i < numTypes; i++)
327  if (typesArray[i] != 0)
328  {
329  if (!typesArray[i]->isSimple())
330  {
331  ComplexType *ct = (ComplexType *) typesArray[i];
332  ct->matchAttributeRef(name, a);
333  }
334  }
335 }
336 
337 
338 void
339 TypesTable::printUndefinedTypes(ostream & out)
340 {
341  for (map < string, int >::iterator it = Id.begin(); it != Id.end();
342  ++it)
343  {
344  if (getTypePtr(it->second) == 0)
345  out << "Could not find {"<<m_tnsUri << "}:" << it->first << std::endl;
346  }
347 }
348 
349 XSDType *
350 TypesTable::getTypePtr(int id) const
351 {
352 
353  // this is a basic XSD type
354  if (id < Schema::XSD_ANYURI + 1 || id > Schema::XSD_ANYURI + numTypes) {
355 
356  // if (id > Schema::XSD_ANYURI + numTypes)
357  // std::cout<<id<<" "<<this<<" "<<numTypes<<std::endl;
358  return 0;
359  }
360  return typesArray[id - (Schema::XSD_ANYURI + 1)];
361 }
362 
363 }
364 
virtual void setName(std::string)
Definition: XSDType.h:222
Qname getQname() const
Definition: XSDType.h:155
Definition: Qname.h:30
std::string getNamespace(void) const
Definition: Qname.h:90
std::string getLocalName(void) const
Definition: Qname.h:76
virtual void setAnonymous(bool)
Definition: XSDType.h:215
void matchAttributeRef(const std::string &name, Attribute &a)
Definition: ComplexType.cpp:69
Type
Definition: Schema.h:59
void matchElementRef(const std::string &name, Element &e)
Definition: ComplexType.cpp:61
const std::string SchemaUri
Definition: Schema.h:92
virtual void setTypeId(int)
Definition: XSDType.h:199