ComplexType.cc 87.1 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
balaskoa's avatar
balaskoa committed
2
 * Copyright (c) 2000-2018 Ericsson Telecom AB
Elemer Lelik's avatar
Elemer Lelik committed
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v2.0
Elemer Lelik's avatar
Elemer Lelik committed
5
 * which accompanies this distribution, and is available at
6
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
Elemer Lelik's avatar
Elemer Lelik committed
7
8
9
10
11
12
13
14
15
16
 *
 * Contributors:
 *   Balasko, Jeno
 *   Beres, Szabolcs
 *   Godar, Marton
 *   Ormandi, Matyas
 *   Raduly, Csaba
 *   Szabo, Bence Janos
 *
 ******************************************************************************/
Elemer Lelik's avatar
Elemer Lelik committed
17
18
19
20
21
22
#include "ComplexType.hh"

#include "GeneralFunctions.hh"
#include "XMLParser.hh"
#include "TTCN3Module.hh"
#include "TTCN3ModuleInventory.hh"
Elemer Lelik's avatar
Elemer Lelik committed
23
#include "Annotation.hh"
24
#include "Constant.hh"
25
#include "converter.hh"
Elemer Lelik's avatar
Elemer Lelik committed
26
27
28
29

#include <assert.h>

ComplexType::ComplexType(XMLParser * a_parser, TTCN3Module * a_module, ConstructType a_construct)
Elemer Lelik's avatar
Elemer Lelik committed
30
31
32
33
34
: SimpleType(a_parser, a_module, a_construct)
, top(true)
, nillable(false)
, enumerated(false)
, embed(false)
Elemer Lelik's avatar
Elemer Lelik committed
35
, with_union(false)
Elemer Lelik's avatar
Elemer Lelik committed
36
37
, first_child(false)
, fromAll(false)
38
39
40
, listPrint(false)
, listMinOccurs(1)
, listMaxOccurs(1)
Elemer Lelik's avatar
Elemer Lelik committed
41
42
43
44
45
46
47
48
, max_alt(0)
, skipback(0)
, lastType()
, actualPath(empty_string)
, actfield(this)
, nillable_field(NULL)
, basefield(NULL)
, cmode(CT_undefined_mode)
Elemer Lelik's avatar
Elemer Lelik committed
49
, resolved(No)
Elemer Lelik's avatar
Elemer Lelik committed
50
, parentTypeSubsGroup(NULL)
Elemer Lelik's avatar
Elemer Lelik committed
51
52
53
54
55
, complexfields()
, attribfields()
, enumfields()
, tagNames() {
  xsdtype = n_complexType;
Elemer Lelik's avatar
Elemer Lelik committed
56
57
}

Elemer Lelik's avatar
Elemer Lelik committed
58
59
60
61
62
63
ComplexType::ComplexType(ComplexType & other)
: SimpleType(other)
, top(other.top)
, nillable(other.nillable)
, enumerated(other.enumerated)
, embed(other.embed)
Elemer Lelik's avatar
Elemer Lelik committed
64
, with_union(other.with_union)
Elemer Lelik's avatar
Elemer Lelik committed
65
66
, first_child(other.first_child)
, fromAll(other.fromAll)
67
68
69
, listPrint(other.listPrint)
, listMinOccurs(other.listMinOccurs)
, listMaxOccurs(other.listMaxOccurs)
Elemer Lelik's avatar
Elemer Lelik committed
70
71
72
73
74
75
76
77
, max_alt(other.max_alt)
, skipback(other.skipback)
, lastType(other.lastType)
, actualPath(other.actualPath)
, actfield(this)
, nillable_field(NULL)
, basefield(NULL)
, cmode(other.cmode)
Elemer Lelik's avatar
Elemer Lelik committed
78
79
, resolved(other.resolved)
, parentTypeSubsGroup(other.parentTypeSubsGroup) {
Elemer Lelik's avatar
Elemer Lelik committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  type.originalValueWoPrefix = other.type.originalValueWoPrefix;
  for (List<AttributeType*>::iterator attr = other.attribfields.begin(); attr; attr = attr->Next) {
    attribfields.push_back(new AttributeType(*attr->Data));
    attribfields.back()->parent = this;
  }

  for (List<ComplexType*>::iterator field = other.complexfields.begin(); field; field = field->Next) {
    complexfields.push_back(new ComplexType(*field->Data));
    complexfields.back()->parent = this;
    if(field->Data == other.basefield){
      basefield = complexfields.back();
    }else if(field->Data == other.nillable_field){
      nillable_field = complexfields.back();
    }
Elemer Lelik's avatar
Elemer Lelik committed
94
  }
Elemer Lelik's avatar
Elemer Lelik committed
95
96
97
98
99
100
101
102
103

  if (other.nameDep != NULL) {
    SimpleType* dep = other.nameDep;
    if(dep->getSubstitution() != NULL){
      dep->getSubstitution()->addToNameDepList(this);
      nameDep = dep->getSubstitution();
    }else {
      other.nameDep->addToNameDepList(this);
    }
Elemer Lelik's avatar
Elemer Lelik committed
104
105
106
  }
}

Elemer Lelik's avatar
Elemer Lelik committed
107
108
109
110
111
112
ComplexType::ComplexType(ComplexType * other)
: SimpleType(other->getParser(), other->getModule(), c_unknown)
, top(false)
, nillable(false)
, enumerated(false)
, embed(false)
Elemer Lelik's avatar
Elemer Lelik committed
113
, with_union(false)
Elemer Lelik's avatar
Elemer Lelik committed
114
115
, first_child(false)
, fromAll(false)
116
117
118
, listPrint(false)
, listMinOccurs(1)
, listMaxOccurs(1)
Elemer Lelik's avatar
Elemer Lelik committed
119
120
121
122
123
124
125
126
, max_alt(0)
, skipback(0)
, lastType()
, actualPath(empty_string)
, actfield(this)
, nillable_field(NULL)
, basefield(NULL)
, cmode(CT_undefined_mode)
Elemer Lelik's avatar
Elemer Lelik committed
127
, resolved(No)
Elemer Lelik's avatar
Elemer Lelik committed
128
, parentTypeSubsGroup(NULL)
Elemer Lelik's avatar
Elemer Lelik committed
129
130
131
132
133
134
135
136
, complexfields()
, attribfields()
, enumfields()
, tagNames() {
  xsdtype = n_complexType;
  parent = other;
  outside_reference = ReferenceData();
}
Elemer Lelik's avatar
Elemer Lelik committed
137

Elemer Lelik's avatar
Elemer Lelik committed
138
139
140
141
142
143
144
145
146
ComplexType::ComplexType(const SimpleType & other, CT_fromST c)
: SimpleType(other)
, top(true)
, nillable(false)
, enumerated(false)
, embed(false)
, with_union(false)
, first_child(false)
, fromAll(false)
147
148
149
, listPrint(false)
, listMinOccurs(1)
, listMaxOccurs(1)
Elemer Lelik's avatar
Elemer Lelik committed
150
151
152
153
154
155
156
157
158
, max_alt(0)
, skipback(0)
, lastType()
, actualPath(empty_string)
, actfield(this)
, nillable_field(NULL)
, basefield(NULL)
, cmode(CT_simpletype_mode)
, resolved(No)
Elemer Lelik's avatar
Elemer Lelik committed
159
, parentTypeSubsGroup(NULL)
Elemer Lelik's avatar
Elemer Lelik committed
160
161
162
163
164
, complexfields()
, attribfields()
, enumfields()
, tagNames() {

165
  if(c != fromElementSubstitution && c != fromTypeSubstitution){
Elemer Lelik's avatar
Elemer Lelik committed
166
167
    module->replaceLastMainType(this);
    module->setActualXsdConstruct(c_complexType);
Elemer Lelik's avatar
Elemer Lelik committed
168
  }
Elemer Lelik's avatar
Elemer Lelik committed
169
  construct = c_complexType;
170
  
Elemer Lelik's avatar
Elemer Lelik committed
171
172
  switch (c) {
    case fromTagUnion:
173
      type.upload(Mstring("union"), false);
Elemer Lelik's avatar
Elemer Lelik committed
174
175
176
177
178
      with_union = true;
      xsdtype = n_union;
      break;
    case fromTagNillable:
      addVariant(V_useNil);
179
      type.upload(Mstring("record"), false);
Elemer Lelik's avatar
Elemer Lelik committed
180
181
      break;
    case fromTagComplexType:
182
      type.upload(Mstring("record"), false);
Elemer Lelik's avatar
Elemer Lelik committed
183
184
      xsdtype = n_complexType;
      break;
185
    case fromElementSubstitution:
186
      type.upload(Mstring("union"), false);
Elemer Lelik's avatar
Elemer Lelik committed
187
188
189
190
      name.upload(getName().originalValueWoPrefix + Mstring("_group"));
      xsdtype = n_union;
      subsGroup = this;
      variant.clear();
Elemer Lelik's avatar
Elemer Lelik committed
191
      hidden_variant.clear();
Elemer Lelik's avatar
Elemer Lelik committed
192
193
194
195
196
      enumeration.modified = false;
      value.modified = false;
      pattern.modified = false;
      length.modified = false;
      whitespace.modified = false;
197
      new_construct = c_complexType;
Elemer Lelik's avatar
Elemer Lelik committed
198
      break;
Elemer Lelik's avatar
Elemer Lelik committed
199
    case fromTypeSubstitution:
200
      type.upload(Mstring("union"), false);
Elemer Lelik's avatar
Elemer Lelik committed
201
202
203
204
205
206
207
208
209
210
211
      name.upload(getName().originalValueWoPrefix + Mstring("_derivations"));
      xsdtype = n_union;
      substitutionGroup = empty_string;
      typeSubsGroup = this;
      variant.clear();
      hidden_variant.clear();
      enumeration.modified = false;
      value.modified = false;
      pattern.modified = false;
      length.modified = false;
      whitespace.modified = false;
212
      new_construct = c_complexType;
Elemer Lelik's avatar
Elemer Lelik committed
213
214
215
  }
}

Elemer Lelik's avatar
Elemer Lelik committed
216
217
218
219
220
221
ComplexType::~ComplexType() {
  for (List<ComplexType*>::iterator field = complexfields.begin(); field; field = field->Next) {
    delete field->Data;
    field->Data = NULL;
  }
  complexfields.clear();
Elemer Lelik's avatar
Elemer Lelik committed
222

Elemer Lelik's avatar
Elemer Lelik committed
223
224
225
226
227
  for (List<AttributeType*>::iterator field = attribfields.begin(); field; field = field->Next) {
    delete field->Data;
    field->Data = NULL;
  }
  attribfields.clear();
Elemer Lelik's avatar
Elemer Lelik committed
228
229
}

Elemer Lelik's avatar
Elemer Lelik committed
230
231
232
233
234
235
236
void ComplexType::loadWithValues() {
  //Find the last field where the tag is found
  if (this != actfield) {
    actfield->loadWithValues();
    return;
  }
  
Elemer Lelik's avatar
Elemer Lelik committed
237
  const XMLParser::TagAttributes & atts = parser->getActualTagAttributes();
Elemer Lelik's avatar
Elemer Lelik committed
238
  
239
240
241
  Mstring xsdPrefix = module->getxmlSchemaPrefixes().size() != 0 ?
    (module->getxmlSchemaPrefixes().front() + ':') : empty_string;
  
Elemer Lelik's avatar
Elemer Lelik committed
242
243
244
245
246
  switch (parser->getActualTagName()) {
    case n_sequence:
      if (!top && xsdtype != n_sequence && xsdtype != n_complexType && xsdtype != n_extension && xsdtype != n_restriction && xsdtype != n_element) {
        //Create new record
        ComplexType * rec = new ComplexType(this);
247
        rec->type.upload(Mstring("record"), false);
Elemer Lelik's avatar
Elemer Lelik committed
248
249
250
251
252
253
254
255
256
257
258
        rec->name.upload(Mstring("sequence"));
        rec->addVariant(V_untagged);
        rec->setMinMaxOccurs(atts.minOccurs, atts.maxOccurs);
        rec->setXsdtype(n_sequence);
        complexfields.push_back(rec);
        actfield = rec;
      } else {
        //Do not create new record, it is an embedded sequence
        if (xsdtype == n_sequence && atts.minOccurs == 1 && atts.maxOccurs == 1) {
          skipback += 1;
        }
259
        type.upload(Mstring("record"), false);
Elemer Lelik's avatar
Elemer Lelik committed
260
261
262
        xsdtype = n_sequence;
        setMinMaxOccurs(atts.minOccurs, atts.maxOccurs);
      }
Elemer Lelik's avatar
Elemer Lelik committed
263
      break;
Elemer Lelik's avatar
Elemer Lelik committed
264
265
266
267
    case n_choice:
      if (!top || xsdtype != n_group) {
        //Create new union field
        ComplexType * choice = new ComplexType(this);
268
        choice->type.upload(Mstring("union"), false);
Elemer Lelik's avatar
Elemer Lelik committed
269
270
271
272
273
274
275
276
        choice->name.upload(Mstring("choice"));
        choice->setXsdtype(n_choice);
        choice->addVariant(V_untagged);
        choice->setMinMaxOccurs(atts.minOccurs, atts.maxOccurs);
        actfield = choice;
        complexfields.push_back(choice);
      } else {
        xsdtype = n_choice;
277
        type.upload(Mstring("union"), false);
Elemer Lelik's avatar
Elemer Lelik committed
278
279
280
      }
      break;
    case n_all:
Elemer Lelik's avatar
Elemer Lelik committed
281
    {
Elemer Lelik's avatar
Elemer Lelik committed
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
      //Create the record of enumerated field
      xsdtype = n_all;
      ComplexType * enumField = new ComplexType(this);
      enumField->setTypeValue(Mstring("enumerated"));
      enumField->setNameValue(Mstring("order"));
      enumField->setBuiltInBase(Mstring("string"));
      enumField->enumerated = true;
      enumField->setMinMaxOccurs(0, ULLONG_MAX, false);
      setMinMaxOccurs(atts.minOccurs, atts.maxOccurs);
      addVariant(V_useOrder);
      complexfields.push_back(enumField);
      if (atts.minOccurs == 0) {
        isOptional = true;
      }
      break;
    }
    case n_restriction:
      mode = restrictionMode;
      //If it is an xsd:union then call SimpleType::loadWithValues
301
      if (parent != NULL && parent->with_union && parent->hasVariant(Mstring("useUnion"))) {
Elemer Lelik's avatar
Elemer Lelik committed
302
303
304
305
306
307
308
309
310
311
        SimpleType::loadWithValues();
        break;
      }
      if (cmode == CT_simpletype_mode) {
        //if it is from a SimpleType, then create a base field
        ComplexType * f = new ComplexType(this);
        f->name.upload(Mstring("base"));
        f->type.upload(atts.base);
        f->setReference(atts.base);
        f->addVariant(V_untagged);
312
        f->mode = restrictionMode;
Elemer Lelik's avatar
Elemer Lelik committed
313
314
315
        complexfields.push_back(f);
        basefield = f;
        actfield = f;
316
317
318
319
320
321
        
        // If it is a restriction of a list, then no new basefield will be 
        // present, to we apply the references to the parent.
        if(parent != NULL && parent->inList) {
          parent->applyReference(*f, true);
        }
Elemer Lelik's avatar
Elemer Lelik committed
322
323
324
325
326
327
328
329
330
331
332
333
334
335
      } else if (cmode == CT_complextype_mode) {
        setReference(atts.base);
        xsdtype = n_restriction;
      }
      break;
    case n_extension:
      mode = extensionMode;
      if (cmode == CT_simpletype_mode) {
        //if it is from a SimpleType, then create a base field
        ComplexType * f = new ComplexType(this);
        f->name.upload(Mstring("base"));
        f->type.upload(atts.base);
        f->setReference(atts.base);
        f->addVariant(V_untagged);
336
        f->mode = extensionMode;
Elemer Lelik's avatar
Elemer Lelik committed
337
338
339
340
341
342
343
344
345
        complexfields.push_back(f);
        basefield = f;
        actfield = f;
      } else if (cmode == CT_complextype_mode) {
        setReference(atts.base);
        xsdtype = n_extension;
      }
      break;
    case n_element:
Elemer Lelik's avatar
Elemer Lelik committed
346
    {
Elemer Lelik's avatar
Elemer Lelik committed
347
348
349
350
      if (atts.nillable) {
        if(cmode == CT_simpletype_mode){
          //If a simple top level element is nillable
          ComplexType * nilrec = new ComplexType(this);
351
          nilrec->xsdtype = n_element;
Elemer Lelik's avatar
Elemer Lelik committed
352
          if (atts.type.empty()) {
353
            nilrec->type.upload(Mstring("record"), false);
Elemer Lelik's avatar
Elemer Lelik committed
354
355
          } else {
            nilrec->type.upload(atts.type);
356
            nilrec->setReference(atts.type);
Elemer Lelik's avatar
Elemer Lelik committed
357
358
359
360
361
362
          }
          nilrec->name.upload(Mstring("content"));
          nilrec->isOptional = true;
          nilrec->nillable = true;
          setMinMaxOccurs(atts.minOccurs, atts.maxOccurs);
          complexfields.push_back(nilrec);
363
          type.upload(Mstring("record"), false);
Elemer Lelik's avatar
Elemer Lelik committed
364
365
366
367
368
369
370
          name.upload(atts.name);
          actfield = nilrec;
          nillable_field = nilrec;
        } else {
          //From a complexType element is nillable
          ComplexType * record = new ComplexType(this);
          ComplexType * nilrec = new ComplexType(record);
371
          nilrec->xsdtype = n_element;
Elemer Lelik's avatar
Elemer Lelik committed
372
          if (atts.type.empty()) {
373
            nilrec->type.upload(Mstring("record"), false);
Elemer Lelik's avatar
Elemer Lelik committed
374
375
          } else {
            nilrec->type.upload(atts.type);
376
            nilrec->setReference(atts.type);
Elemer Lelik's avatar
Elemer Lelik committed
377
378
          }
          record->name.upload(atts.name);
379
          record->type.upload(Mstring("record"), false);
Elemer Lelik's avatar
Elemer Lelik committed
380
381
382
383
384
385
386
387
388
389
390
          record->complexfields.push_back(nilrec);
          record->addVariant(V_useNil);
          record->nillable_field = nilrec;
          record->setMinMaxOccurs(atts.minOccurs, atts.maxOccurs);

          nilrec->name.upload(Mstring("content"));
          nilrec->nillable = true;
          nilrec->isOptional = true;
          nilrec->tagNames.push_back(parser->getActualTagName());
          complexfields.push_back(record);
          actfield = nilrec;
Elemer Lelik's avatar
Elemer Lelik committed
391
        }
Elemer Lelik's avatar
Elemer Lelik committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
      }else {
        //It is a simple element
        ComplexType* c = new ComplexType(this);
        c->setXsdtype(n_element);
        c->type.upload(atts.type);
        c->name.upload(atts.name);
        c->setReference(atts.type);
        c->setMinMaxOccurs(atts.minOccurs, atts.maxOccurs);
        c->applyDefaultAttribute(atts.default_);
        c->applyFixedAttribute(atts.fixed);
        c->setElementFormAs(atts.form);
        if (atts.ref.empty()) {
          c->setReference(atts.type);
        } else {
          c->applyRefAttribute(atts.ref);
          c->name.upload(atts.ref.getValueWithoutPrefix(':'));
          c->type.upload(atts.ref);
Elemer Lelik's avatar
Elemer Lelik committed
409
        }
Elemer Lelik's avatar
Elemer Lelik committed
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
        c->applySubstitionGroupAttribute(atts.substitionGroup);
        c->applyBlockAttribute(atts.block);
        actfield = c;

        //Inside all have some special conditions
        if (xsdtype == n_all) {
          if (atts.minOccurs > 1) {
            printError(getModule()->getSchemaname(), name.convertedValue,
              Mstring("Inside <all>, minOccurs must be 0 or 1"));
            TTCN3ModuleInventory::incrNumErrors();
          }
          if (atts.maxOccurs != 1) {
            printError(getModule()->getSchemaname(), name.convertedValue,
              Mstring("Inside <all>, maxOccurs must be 1"));
            TTCN3ModuleInventory::incrNumErrors();
          }
          c->fromAll = true;
          complexfields.push_back(c);
          if (isOptional) {
            c->isOptional = true;
          }
        } else {
          complexfields.push_back(c);
Elemer Lelik's avatar
Elemer Lelik committed
433
434
435
436
        }
      }
      break;
    }
Elemer Lelik's avatar
Elemer Lelik committed
437
    case n_attribute:
Elemer Lelik's avatar
Elemer Lelik committed
438
    {
Elemer Lelik's avatar
Elemer Lelik committed
439
440
441
442
443
444
445
446
447
448
449
450
451
      AttributeType * attribute = new AttributeType(this);
      attribute->addVariant(V_attribute);
      attribute->applyMinMaxOccursAttribute(0, 1);
      attribute->setXsdtype(n_attribute);
      attribute->setUseVal(atts.use);
      attribute->setAttributeFormAs(atts.form);
      lastType = n_attribute;
      if (atts.ref.empty()) {
        attribute->setNameOfField(atts.name);
        attribute->setTypeOfField(atts.type);
        attribute->setReference(atts.type, true);
      } else {
        attribute->applyRefAttribute(atts.ref);
Elemer Lelik's avatar
Elemer Lelik committed
452
      }
453
454
      attribute->applyDefaultAttribute(atts.default_);
      attribute->applyFixedAttribute(atts.fixed);
Elemer Lelik's avatar
Elemer Lelik committed
455
456
457
458
459
460
461
462
463
464
465
466
467
      actfield = attribute;
      
      //In case of nillable parent it is difficult...
      if (nillable && parent != NULL) {
        parent->attribfields.push_back(attribute);
        attribute->parent = parent;
      } else if (nillable && !complexfields.empty() && parent == NULL) {
        complexfields.back()->attribfields.push_back(attribute);
      } else if (parent != NULL && (parent->mode == extensionMode || parent->mode == restrictionMode) && name.convertedValue == Mstring("base")) {
        parent->attribfields.push_back(attribute);
        attribute->parent = parent;
      } else {
        attribfields.push_back(attribute);
Elemer Lelik's avatar
Elemer Lelik committed
468
      }
Elemer Lelik's avatar
Elemer Lelik committed
469
      break;
Elemer Lelik's avatar
Elemer Lelik committed
470
    }
Elemer Lelik's avatar
Elemer Lelik committed
471
472
473
474
    case n_any:
    {
      ComplexType * any = new ComplexType(this);
      any->name.upload(Mstring("elem"));
475
      any->type.upload(xsdPrefix + Mstring("string"), false);
Elemer Lelik's avatar
Elemer Lelik committed
476
477
478
479
480
      any->applyNamespaceAttribute(V_anyElement, atts.namespace_);
      any->setMinMaxOccurs(atts.minOccurs, atts.maxOccurs);
      any->setXsdtype(n_any);
      complexfields.push_back(any);
      break;
Elemer Lelik's avatar
Elemer Lelik committed
481
    }
Elemer Lelik's avatar
Elemer Lelik committed
482
    case n_anyAttribute:
Elemer Lelik's avatar
Elemer Lelik committed
483
    {
Elemer Lelik's avatar
Elemer Lelik committed
484
485
486
      AttributeType * anyattr = new AttributeType(this);
      anyattr->setXsdtype(n_anyAttribute);
      anyattr->setNameOfField(Mstring("attr"));
487
      anyattr->setTypeValue(xsdPrefix + Mstring("string"));
Elemer Lelik's avatar
Elemer Lelik committed
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
      anyattr->setToAnyAttribute();
      anyattr->applyMinMaxOccursAttribute(0, ULLONG_MAX);
      anyattr->addNameSpaceAttribute(atts.namespace_);
      actfield = anyattr;

      //In case of nillable parent it is difficult...
      if (nillable && parent != NULL) {
        parent->attribfields.push_back(anyattr);
        anyattr->parent = parent;
      } else if (nillable && !complexfields.empty() && parent == NULL) {
        complexfields.back()->attribfields.push_back(anyattr);
      } else if (parent != NULL && (parent->mode == extensionMode || parent->mode == restrictionMode) && name.convertedValue == Mstring("base")) {
        parent->attribfields.push_back(anyattr);
        anyattr->parent = parent;
      } else {
        attribfields.push_back(anyattr);
Elemer Lelik's avatar
Elemer Lelik committed
504
      }
Elemer Lelik's avatar
Elemer Lelik committed
505
      break;
Elemer Lelik's avatar
Elemer Lelik committed
506
    }
Elemer Lelik's avatar
Elemer Lelik committed
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
    case n_attributeGroup:
      if (!atts.ref.empty()) {
        ComplexType * g = new ComplexType(this);
        g->setXsdtype(n_attributeGroup);
        g->setReference(atts.ref);
        complexfields.push_back(g);
        actfield = g;
      } else {
        xsdtype = n_attributeGroup;
        name.upload(Mstring(atts.name));
        setInvisible();
      }
      break;
    case n_group:
      if (atts.ref.empty()) {
        //It is a definition
        xsdtype = n_group;
        name.upload(atts.name);
      } else {
        //It is a reference
        ComplexType* group = new ComplexType(this);
        group->setXsdtype(n_group);
        group->name.upload(atts.name);
        group->setReference(Mstring(atts.ref));
        group->setMinMaxOccurs(atts.minOccurs, atts.maxOccurs);
        complexfields.push_back(group);
        actfield = group;
      }
      break;
    case n_union:
Elemer Lelik's avatar
Elemer Lelik committed
537
    {
Elemer Lelik's avatar
Elemer Lelik committed
538
539
      with_union = true;
      xsdtype = n_union;
540
      type.upload(Mstring("union"), false);
Elemer Lelik's avatar
Elemer Lelik committed
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
      addVariant(V_useUnion);
      if (!atts.memberTypes.empty()) {
        List<Mstring> types;
        //Get the union values
        expstring_t valueToSplitIntoTokens = mcopystr(atts.memberTypes.c_str());
        char * token;
        token = strtok(valueToSplitIntoTokens, " ");
        while (token != NULL) {
          types.push_back(Mstring(token));
          token = strtok(NULL, " ");
        }
        Free(valueToSplitIntoTokens);
        
        //Create the union elements and push into the container
        for (List<Mstring>::iterator memberType = types.begin(); memberType; memberType = memberType->Next) {
          Mstring tmp_name = memberType->Data.getValueWithoutPrefix(':');
          ComplexType * f = new ComplexType(this);
          f->name.upload(tmp_name);
          f->type.upload(memberType->Data);
          f->setXsdtype(n_simpleType);
          f->setReference(memberType->Data);
          complexfields.push_back(f);
        }
Elemer Lelik's avatar
Elemer Lelik committed
564
      }
Elemer Lelik's avatar
Elemer Lelik committed
565
      break;
Elemer Lelik's avatar
Elemer Lelik committed
566
    }
Elemer Lelik's avatar
Elemer Lelik committed
567
568
    case n_simpleType:
    case n_simpleContent:
Elemer Lelik's avatar
Elemer Lelik committed
569
    {
Elemer Lelik's avatar
Elemer Lelik committed
570
571
      xsdtype = parser->getActualTagName();
      cmode = CT_simpletype_mode;
572
      if (with_union && hasVariant(Mstring("useUnion"))) {
573
        Mstring fieldname;
Elemer Lelik's avatar
Elemer Lelik committed
574
575
576
        if (max_alt == 0) {
          fieldname = Mstring("alt_");
        } else {
577
578
579
          expstring_t new_name = mprintf("alt_%d", max_alt);
          fieldname = new_name;
          Free(new_name);
Elemer Lelik's avatar
Elemer Lelik committed
580
581
582
583
584
585
586
587
        }
        max_alt++;
        ComplexType * field = new ComplexType(this);
        field->name.upload(fieldname);
        field->setXsdtype(n_simpleType);
        field->addVariant(V_nameAs, empty_string, true);
        complexfields.push_back(field);
        actfield = field;
Elemer Lelik's avatar
Elemer Lelik committed
588
      }
Elemer Lelik's avatar
Elemer Lelik committed
589
      break;
Elemer Lelik's avatar
Elemer Lelik committed
590
    }
Elemer Lelik's avatar
Elemer Lelik committed
591
592
    case n_complexType:
      name.upload(atts.name);
593
      type.upload(Mstring("record"), false);
Elemer Lelik's avatar
Elemer Lelik committed
594
595
596
597
598
599
600
601
602
603
      applyAbstractAttribute(atts.abstract);
      applySubstitionGroupAttribute(atts.substitionGroup);
      applyBlockAttribute(atts.block);
      // fall through
    case n_complexContent:
      tagNames.push_back(parser->getActualTagName());
      cmode = CT_complextype_mode;
      if (atts.mixed) {
        ComplexType * mixed = new ComplexType(this);
        mixed->name.upload(Mstring("embed_values"));
604
        mixed->type.upload(xsdPrefix + Mstring("string"), false);
Elemer Lelik's avatar
Elemer Lelik committed
605
606
607
608
609
610
611
        mixed->setMinMaxOccurs(0, ULLONG_MAX, false);
        mixed->embed = true;
        complexfields.push_back(mixed);
        addVariant(V_embedValues);
      }
      break;
    case n_list:
ebensza's avatar
ebensza committed
612
      if (parent != NULL && parent->basefield == this) {
613
614
615
616
617
618
619
620
621
622
623
624
625
626
        if (parent->getMaxOccurs() == 1) { // optional or minOccurs = maxOccurs = 1
          if (parent->getMinOccurs() == 0) {
            parent->isOptional = true;
            if (parent->parent != NULL && parent->parent->getXsdtype() == n_choice) {
              parent->listPrint = true;
              parent->listMinOccurs = parent->getMinOccurs();
              parent->listMaxOccurs = parent->getMaxOccurs();
            }
          }
        } else if (parent->parent != NULL){
          parent->listPrint = true;
          parent->listMinOccurs = parent->getMinOccurs();
          parent->listMaxOccurs = parent->getMaxOccurs();
        }
ebensza's avatar
ebensza committed
627
        parent->basefield = NULL;
628
        parent->SimpleType::loadWithValues();
ebensza's avatar
ebensza committed
629
        setInvisible();
630
      } else if(parent != NULL) {
631
632
633
634
635
636
637
638
639
640
641
642
643
644
        if (getMaxOccurs() == 1) { // optional or minOccurs = maxOccurs = 1
          if (getMinOccurs() == 0) {
            isOptional = true;
            if (parent->getXsdtype() == n_choice) {
              listPrint = true;
              listMinOccurs = getMinOccurs();
              listMaxOccurs = getMaxOccurs();
            }
          }
        } else {
          listPrint = true;
          listMinOccurs = getMinOccurs();
          listMaxOccurs = getMaxOccurs();
        }
645
        SimpleType::loadWithValues();
ebensza's avatar
ebensza committed
646
      }
647
      break;
Elemer Lelik's avatar
Elemer Lelik committed
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
    case n_length:
    case n_minLength:
    case n_maxLength:
    case n_pattern:
    case n_enumeration:
    case n_whiteSpace:
    case n_minInclusive:
    case n_maxInclusive:
    case n_minExclusive:
    case n_maxExclusive:
    case n_totalDigits:
    case n_fractionDigits:
      SimpleType::loadWithValues();
      break;
    case n_label:
      addComment(Mstring("LABEL:"));
      break;
    case n_definition:
      addComment(Mstring("DEFINITION:"));
      break;
    default:
      break;
Elemer Lelik's avatar
Elemer Lelik committed
670
671
672
673
  }
}

// called from endelementHandler
Elemer Lelik's avatar
Elemer Lelik committed
674
675
676
677
678
679
680
void ComplexType::modifyValues() {
  if (this != actfield) {
    actfield->modifyValues();
    return;
  }
  if (xsdtype == n_sequence) {
    skipback = skipback - 1;
Elemer Lelik's avatar
Elemer Lelik committed
681
682
  }

ebensza's avatar
ebensza committed
683
684
  if ( parent != NULL && 
      (xsdtype == n_element || 
Elemer Lelik's avatar
Elemer Lelik committed
685
686
687
688
689
690
691
692
693
694
       xsdtype == n_complexType || 
       xsdtype == n_complexContent || 
       xsdtype == n_all || 
       xsdtype == n_attribute || 
       xsdtype == n_anyAttribute ||
       xsdtype == n_choice || 
       xsdtype == n_group || 
       xsdtype == n_attributeGroup || 
       xsdtype == n_extension || 
       xsdtype == n_restriction || 
695
       (xsdtype == n_simpleType && !inList) || 
Elemer Lelik's avatar
Elemer Lelik committed
696
697
       xsdtype == n_simpleContent ||
       (xsdtype == n_sequence && skipback < 0)
ebensza's avatar
ebensza committed
698
      )) {
Elemer Lelik's avatar
Elemer Lelik committed
699
700
701
702
703
704
705
706
    if (!tagNames.empty() && tagNames.back() == parser->getParentTagName()) {
      if (nillable && tagNames.back() == n_element) {
        parent->modifyValues();
      }
      tagNames.pop_back();
    } else if (tagNames.empty()) {
      parent->actfield = parent;
      parent->lastType = xsdtype;
Elemer Lelik's avatar
Elemer Lelik committed
707
708
    }
  }
709
710
711
  if (xsdtype == n_simpleType) {
    inList = false;
  }
Elemer Lelik's avatar
Elemer Lelik committed
712
713
}

714
715
716
717
718
719
720
721
722
723
724
void ComplexType::modifyList() {
  if (this != actfield) {
    ((SimpleType*)actfield)->modifyList();
    return;
  }
  if (!inList && mode == listMode && parent != NULL) {
    parent->actfield = parent;
    parent->lastType = xsdtype;
  }
}

Elemer Lelik's avatar
Elemer Lelik committed
725
726
727
728
729
void ComplexType::referenceResolving() {
  if (resolved != No) return; // nothing to do
  if(this == subsGroup){
    resolved = Yes;
    return;
Elemer Lelik's avatar
Elemer Lelik committed
730
  }
Elemer Lelik's avatar
Elemer Lelik committed
731
732
  resolved = InProgress;
  for (List<ComplexType*>::iterator ct = complexfields.begin(); ct; ct = ct->Next) {
733
    // Reference resolving of ComplexTypes
Elemer Lelik's avatar
Elemer Lelik committed
734
    ct->Data->referenceResolving();
Elemer Lelik's avatar
Elemer Lelik committed
735
  }
Elemer Lelik's avatar
Elemer Lelik committed
736
737
738
  for (List<AttributeType*>::iterator attr = attribfields.begin(); attr; attr = attr->Next) {
    //Reference resolving for Attributes
    resolveAttribute(attr->Data);
Elemer Lelik's avatar
Elemer Lelik committed
739
  }
Elemer Lelik's avatar
Elemer Lelik committed
740
741
742
  
  reference_resolving_funtion();
  
Elemer Lelik's avatar
Elemer Lelik committed
743
  if(!substitutionGroup.empty()){
Elemer Lelik's avatar
Elemer Lelik committed
744
    addToSubstitutions();
Elemer Lelik's avatar
Elemer Lelik committed
745
  }
Elemer Lelik's avatar
Elemer Lelik committed
746
  resolved = Yes;
Elemer Lelik's avatar
Elemer Lelik committed
747
748
}

Elemer Lelik's avatar
Elemer Lelik committed
749
750
751
void ComplexType::reference_resolving_funtion() {
  //Every child element references are resolved here.
  if (outside_reference.empty() && basefield == NULL) {
Elemer Lelik's avatar
Elemer Lelik committed
752
753
754
755
756
    //Its not in the resolveElement function because we need the built in type
    //reference too, and then the outside_reference is empty.
    if(xsdtype == n_element){
      collectElementTypes(NULL, NULL);
    }
Elemer Lelik's avatar
Elemer Lelik committed
757
    return;
Elemer Lelik's avatar
Elemer Lelik committed
758
759
  }

760
  resolveAttributeGroup();
Elemer Lelik's avatar
Elemer Lelik committed
761

762
  resolveGroup();
Elemer Lelik's avatar
Elemer Lelik committed
763

764
  resolveElement();
Elemer Lelik's avatar
Elemer Lelik committed
765

Elemer Lelik's avatar
Elemer Lelik committed
766
  resolveSimpleTypeExtension();
Elemer Lelik's avatar
Elemer Lelik committed
767

Elemer Lelik's avatar
Elemer Lelik committed
768
  resolveSimpleTypeRestriction();
Elemer Lelik's avatar
Elemer Lelik committed
769

Elemer Lelik's avatar
Elemer Lelik committed
770
  resolveComplexTypeExtension();
Elemer Lelik's avatar
Elemer Lelik committed
771

Elemer Lelik's avatar
Elemer Lelik committed
772
  resolveComplexTypeRestriction();
Elemer Lelik's avatar
Elemer Lelik committed
773

774
  resolveUnion();
Elemer Lelik's avatar
Elemer Lelik committed
775

Elemer Lelik's avatar
Elemer Lelik committed
776
777
  addToTypeSubstitutions();

Elemer Lelik's avatar
Elemer Lelik committed
778
779
}

Elemer Lelik's avatar
Elemer Lelik committed
780
781
void ComplexType::setParent(ComplexType * par, SimpleType * child) {
  child->parent = par;
Elemer Lelik's avatar
Elemer Lelik committed
782
783
}

Elemer Lelik's avatar
Elemer Lelik committed
784
785
void ComplexType::applyReference(const SimpleType & other, const bool on_attributes) {
  type.convertedValue = other.getType().convertedValue;
Elemer Lelik's avatar
Elemer Lelik committed
786
  type.originalValueWoPrefix = other.getType().convertedValue.getValueWithoutPrefix(':');
Elemer Lelik's avatar
Elemer Lelik committed
787

788
789
  if (other.getMinOccurs() > getMinOccurs() ||
      other.getMaxOccurs() < getMaxOccurs()) {
Elemer Lelik's avatar
Elemer Lelik committed
790
791
792
793
794
795
    if (!on_attributes) {
      expstring_t temp = memptystr();
      temp = mputprintf(
        temp,
        "The occurrence range (%llu .. %llu) of the element (%s) is not compatible "
        "with the occurrence range (%llu .. %llu) of the referenced element.",
796
797
        getMinOccurs(),
        getMaxOccurs(),
Elemer Lelik's avatar
Elemer Lelik committed
798
799
800
801
802
803
        name.originalValueWoPrefix.c_str(),
        other.getMinOccurs(),
        other.getMaxOccurs());
      printError(module->getSchemaname(), parent->getName().originalValueWoPrefix,
        Mstring(temp));
      Free(temp);
Elemer Lelik's avatar
Elemer Lelik committed
804
805
      TTCN3ModuleInventory::getInstance().incrNumErrors();
    }
Elemer Lelik's avatar
Elemer Lelik committed
806
  } else {
807
808
    setMinOccurs(llmax(getMinOccurs(), other.getMinOccurs()));
    setMaxOccurs(llmin(getMaxOccurs(), other.getMaxOccurs()));
Elemer Lelik's avatar
Elemer Lelik committed
809
810
  }

Elemer Lelik's avatar
Elemer Lelik committed
811
812
813
814
815
  for (List<Mstring>::iterator var = other.getVariantRef().begin(); var; var = var->Next) {
    bool found = false;
    for (List<Mstring>::iterator var1 = variant.begin(); var1; var1 = var1->Next) {
      if (var->Data == var1->Data) {
        found = true;
Elemer Lelik's avatar
Elemer Lelik committed
816
817
818
        break;
      }
    }
Elemer Lelik's avatar
Elemer Lelik committed
819
820
821
    if (!found) {
      variant.push_back(var->Data);
      variant_ref.push_back(var->Data);
Elemer Lelik's avatar
Elemer Lelik committed
822
823
824
    }
  }

Elemer Lelik's avatar
Elemer Lelik committed
825
  builtInBase = other.getBuiltInBase();
826
  
Elemer Lelik's avatar
Elemer Lelik committed
827
828
829
830
831
  length.applyReference(other.getLength());
  pattern.applyReference(other.getPattern());
  enumeration.applyReference(other.getEnumeration());
  whitespace.applyReference(other.getWhitespace());
  value.applyReference(other.getValue());
Elemer Lelik's avatar
Elemer Lelik committed
832
833
}

Elemer Lelik's avatar
Elemer Lelik committed
834
835
836
837
838
839
840
841
842
843
844
845
void ComplexType::nameConversion(NameConversionMode conversion_mode, const List<NamespaceType> & ns) {
  if(!visible) return;
  switch (conversion_mode) {
    case nameMode:
      nameConversion_names(ns);
      break;
    case typeMode:
      nameConversion_types(ns);
      break;
    case fieldMode:
      nameConversion_fields(ns);
      break;
Elemer Lelik's avatar
Elemer Lelik committed
846
847
848
  }
}

Elemer Lelik's avatar
Elemer Lelik committed
849
void ComplexType::nameConversion_names(const List<NamespaceType> &) {
Elemer Lelik's avatar
Elemer Lelik committed
850
  Mstring res, var(module->getTargetNamespace());
851
  XSDName2TTCN3Name(name.convertedValue, empty_string, TTCN3ModuleInventory::getInstance().getTypenames(), type_name, res, var);
Elemer Lelik's avatar
Elemer Lelik committed
852
853
  name.convertedValue = res;
  bool found = false;
Elemer Lelik's avatar
Elemer Lelik committed
854
  for (List<Mstring>::iterator vari = variant.begin(); vari; vari = vari->Next) {
Elemer Lelik's avatar
Elemer Lelik committed
855
856
857
858
859
    if (vari->Data == "\"untagged\"") {
      found = true;
      break;
    }
  }
860
861
  // element or type substitution generated types do not need 'name as' variant
  if (!found && subsGroup != this && typeSubsGroup != this) {
Elemer Lelik's avatar
Elemer Lelik committed
862
863
    addVariant(V_onlyValue, var);
  }
864
  for (List<RootType*>::iterator dep = nameDepList.begin(); dep; dep = dep->Next) {
865
    dep->Data->setTypeValueWoPrefix(res);
Elemer Lelik's avatar
Elemer Lelik committed
866
867
868
  }
}

Elemer Lelik's avatar
Elemer Lelik committed
869
870
871
872
873
874
875
876
877
878
879
880
void ComplexType::nameConversion_types(const List<NamespaceType> & ns) {
  attribfields.sort(compareAttributeNameSpaces);
  attribfields.sort(compareAttributeTypes);
  for (List<AttributeType*>::iterator field = attribfields.begin(); field; field = field->Next) {
    field->Data->nameConversion(typeMode, ns);
  }

  for (List<ComplexType*>::iterator field = complexfields.begin(); field; field = field->Next) {
    field->Data->nameConversion_types(ns);
  }

  Mstring prefix, uri, typeValue;
Elemer Lelik's avatar
Elemer Lelik committed
881
882
883
884

  if (type.convertedValue == "record" ||
    type.convertedValue == "set" ||
    type.convertedValue == "union" ||
Elemer Lelik's avatar
Elemer Lelik committed
885
    type.convertedValue == "enumerated") {
Elemer Lelik's avatar
Elemer Lelik committed
886
    return;
Elemer Lelik's avatar
Elemer Lelik committed
887
  }
Elemer Lelik's avatar
Elemer Lelik committed
888
889

  prefix = type.convertedValue.getPrefix(':');
Elemer Lelik's avatar
Elemer Lelik committed
890
  typeValue = type.convertedValue.getValueWithoutPrefix(':');
Elemer Lelik's avatar
Elemer Lelik committed
891

Elemer Lelik's avatar
Elemer Lelik committed
892
  for (List<NamespaceType>::iterator namesp = ns.begin(); namesp; namesp = namesp->Next) {
Elemer Lelik's avatar
Elemer Lelik committed
893
894
895
896
897
898
    if (prefix == namesp->Data.prefix) {
      uri = namesp->Data.uri;
      break;
    }
  }

Elemer Lelik's avatar
Elemer Lelik committed
899
  QualifiedName in(uri, typeValue); // ns uri + original name
Elemer Lelik's avatar
Elemer Lelik committed
900
901
902

  // Check all known types
  QualifiedNames::iterator origTN = TTCN3ModuleInventory::getInstance().getTypenames().begin();
Elemer Lelik's avatar
Elemer Lelik committed
903
  for (; origTN; origTN = origTN->Next) {
Elemer Lelik's avatar
Elemer Lelik committed
904
905
    if (origTN->Data == in) {
      QualifiedName tmp_name(module->getTargetNamespace(), name.convertedValue);
Elemer Lelik's avatar
Elemer Lelik committed
906
      if (origTN->Data != tmp_name){
Elemer Lelik's avatar
Elemer Lelik committed
907
        break;
Elemer Lelik's avatar
Elemer Lelik committed
908
      }
Elemer Lelik's avatar
Elemer Lelik committed
909
910
911
912
913
    }
  }

  if (origTN != NULL) {
    setTypeValue(origTN->Data.name);
Elemer Lelik's avatar
Elemer Lelik committed
914
  } else {
Elemer Lelik's avatar
Elemer Lelik committed
915
    Mstring res, var;
916
    XSDName2TTCN3Name(typeValue, uri, TTCN3ModuleInventory::getInstance().getTypenames(), type_reference_name, res, var, type.no_replace);
Elemer Lelik's avatar
Elemer Lelik committed
917
918
919
920
    setTypeValue(res);
  }
}

Elemer Lelik's avatar
Elemer Lelik committed
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
void ComplexType::nameConversion_fields(const List<NamespaceType> & ns) {
  QualifiedNames used_field_names;
  
  for (List<AttributeType*>::iterator field = attribfields.begin(); field; field = field->Next) {
    field->Data->nameConversion_names(used_field_names);
  }

  for (List<ComplexType*>::iterator field = complexfields.begin(); field; field = field->Next) {
    if (field->Data->getMinOccurs() == 0 && field->Data->getMaxOccurs() == 0) {
      continue;
    }
    if (!field->Data->isVisible()) {
      continue;
    }
    
    field->Data->nameConversion_fields(ns);
Elemer Lelik's avatar
Elemer Lelik committed
937
938

    Mstring prefix = field->Data->getType().convertedValue.getPrefix(':');
Elemer Lelik's avatar
Elemer Lelik committed
939
940
941
942
    Mstring typeValue = field->Data->getType().convertedValue.getValueWithoutPrefix(':');

    Mstring res, var;
    var = getModule()->getTargetNamespace();
943
944
945
946
947
948
949
950
    List<NamespaceType>::iterator declNS;
    for (declNS = module->getDeclaredNamespaces().begin(); declNS; declNS = declNS->Next) {
      if (prefix == declNS->Data.prefix) {
        break;
      }
    }
    XSDName2TTCN3Name(typeValue, declNS ? declNS->Data.uri : empty_string,
      TTCN3ModuleInventory::getInstance().getTypenames(), type_reference_name, res, var);
Elemer Lelik's avatar
Elemer Lelik committed
951
952
953
954
955
956

    field->Data->addVariant(V_onlyValue, var);
    var = getModule()->getTargetNamespace();

    if (field->Data->getName().list_extension) {
      field->Data->useNameListProperty();
957
      XSDName2TTCN3Name(field->Data->getName().convertedValue, empty_string,
Elemer Lelik's avatar
Elemer Lelik committed
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
        used_field_names, field_name, res, var);
      field->Data->setNameValue(res);
      bool found_in_variant = false;
      for (List<Mstring>::iterator vari = field->Data->getVariant().begin(); vari; vari = vari->Next) {
        if (vari->Data == Mstring("\"untagged\"")) {
          found_in_variant = true;
          break;
        }
      }
      if (!field->Data->getName().originalValueWoPrefix.empty() &&
        field->Data->getName().originalValueWoPrefix != "sequence" &&
        field->Data->getName().originalValueWoPrefix != "choice" &&
        field->Data->getName().originalValueWoPrefix != "elem" &&
        !found_in_variant) {
        field->Data->addVariant(V_nameAs, field->Data->getName().originalValueWoPrefix);
Elemer Lelik's avatar
Elemer Lelik committed
973
974
      }

Elemer Lelik's avatar
Elemer Lelik committed
975
976
977
978
979

      if (!found_in_variant) {
        field->Data->addVariant(V_untagged, empty_string, true);
      }
    } else {
980
      XSDName2TTCN3Name(field->Data->getName().convertedValue, empty_string,
Elemer Lelik's avatar
Elemer Lelik committed
981
982
983
        used_field_names, field_name, res, var);
      field->Data->setNameValue(res);
      field->Data->addVariant(V_onlyValue, var);
Elemer Lelik's avatar
Elemer Lelik committed
984
    }
Elemer Lelik's avatar
Elemer Lelik committed
985
986
987
988
989
990
991
992

  }
}

void ComplexType::setFieldPaths(Mstring path) {
  if (path.empty()) {
    if (!top) {
      Mstring field_prefix = empty_string;
993
      if(parent->getMinOccurs() == 0 && parent->getMaxOccurs() == ULLONG_MAX){
Elemer Lelik's avatar
Elemer Lelik committed
994
        field_prefix = "[-].";
Elemer Lelik's avatar
Elemer Lelik committed
995
      }
Elemer Lelik's avatar
Elemer Lelik committed
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
      path = field_prefix + getName().convertedValue;
      actualPath = field_prefix + getName().convertedValue;
    }else {
      actualPath = getName().convertedValue;
    }
  } else if (parent != NULL && (parent->getMinOccurs() != 1 || parent->getMaxOccurs() != 1) &&
             (parent->getName().list_extension || parent->mode == listMode)) {
    path = path + Mstring("[-].") + getName().convertedValue;
    actualPath = path;
  } else {
    path = path + Mstring(".") + getName().convertedValue;
    actualPath = path;
  }

  for (List<ComplexType*>::iterator field = complexfields.begin(); field; field = field->Next) {
    field->Data->setFieldPaths(path);
  }
  for (List<AttributeType*>::iterator attr = attribfields.begin(); attr; attr = attr->Next) {
    attr->Data->setFieldPath(path);
  }
}

1018
void ComplexType::subFinalModification() {  
Elemer Lelik's avatar
Elemer Lelik committed
1019
1020
  //Call SimpleType finalModification
  SimpleType::finalModification();
1021
  
Elemer Lelik's avatar
Elemer Lelik committed
1022
  //Set isOptional field
1023
  isOptional = isOptional || (getMinOccurs() == 0 && getMaxOccurs() == 1);
Elemer Lelik's avatar
Elemer Lelik committed
1024
1025
1026
1027
1028
1029
  
  //
  List<Mstring> enumNames;
  for (List<ComplexType*>::iterator field = complexfields.begin(), nextField; field; field = nextField) {
    nextField = field->Next;
    //Remove invisible fields
1030
    if ((field->Data->getMinOccurs() == 0 && field->Data->getMaxOccurs() == 0) || !field->Data->isVisible()) {
Elemer Lelik's avatar
Elemer Lelik committed
1031
1032
1033
1034
1035
      delete field->Data;
      field->Data = NULL;
      complexfields.remove(field);
    } else {
      //Recursive call
1036
      field->Data->subFinalModification();
Elemer Lelik's avatar
Elemer Lelik committed
1037
1038
1039
      //collect <xsd:all> elements
      if (field->Data->fromAll) {
        enumNames.push_back(field->Data->getName().convertedValue);
Elemer Lelik's avatar
Elemer Lelik committed
1040
1041
      }
    }
Elemer Lelik's avatar
Elemer Lelik committed
1042
  }
Elemer Lelik's avatar
Elemer Lelik committed
1043

Elemer Lelik's avatar
Elemer Lelik committed
1044
1045
  ComplexType * embedField = NULL;
  ComplexType * enumField = NULL;
Elemer Lelik's avatar
Elemer Lelik committed
1046

Elemer Lelik's avatar
Elemer Lelik committed
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
  //Find the embed and order fields, and remove them
  for (List<ComplexType*>::iterator field = complexfields.begin(), nextField; field; field = nextField) {
    nextField = field->Next;
    if (field->Data->embed) {
      embedField = new ComplexType(*field->Data);
      embedField->parent = this;
      delete field->Data;
      field->Data = NULL;
      complexfields.remove(field);
    } else if (field->Data->enumerated) {
      enumField = new ComplexType(*field->Data);
      enumField->parent = this;
      delete field->Data;
      field->Data = NULL;
      complexfields.remove(field);
    }
  }
Elemer Lelik's avatar
Elemer Lelik committed
1064

Elemer Lelik's avatar
Elemer Lelik committed
1065
1066
1067
1068
1069
1070
  if (enumField != NULL) {
    //Insert the order field in the front
    complexfields.push_front(enumField);
    //Push the field names into the order field
    for (List<Mstring>::iterator field = enumNames.begin(); field; field = field->Next) {
      enumField->enumfields.push_back(field->Data);
Elemer Lelik's avatar
Elemer Lelik committed
1071
    }
Elemer Lelik's avatar
Elemer Lelik committed
1072
  }
Elemer Lelik's avatar
Elemer Lelik committed
1073

Elemer Lelik's avatar
Elemer Lelik committed
1074
1075
1076
1077
  if (embedField != NULL) {
    //Insert the embed field to the front
    complexfields.push_front(embedField);
  }
Elemer Lelik's avatar
Elemer Lelik committed
1078

Elemer Lelik's avatar
Elemer Lelik committed
1079
1080
1081
1082
1083
1084
1085
  if (with_union) {
    unsigned number = 0;
    for (List<ComplexType*>::iterator field = complexfields.begin(); field; field = field->Next) {
      if (field->Data->name.convertedValue.foundAt("alt_") == field->Data->name.convertedValue.c_str()) {
        if (number == 0) {
          field->Data->name.upload(Mstring("alt_"));
        } else {
1086
1087
1088
          expstring_t new_name = mprintf("alt_%d", number);
          field->Data->name.upload(Mstring(new_name));
          Free(new_name);
Elemer Lelik's avatar
Elemer Lelik committed
1089
        }
Elemer Lelik's avatar
Elemer Lelik committed
1090
        number++;
Elemer Lelik's avatar
Elemer Lelik committed
1091
      }
Elemer Lelik's avatar
Elemer Lelik committed
1092
1093
    }
  }
Elemer Lelik's avatar
Elemer Lelik committed
1094

Elemer Lelik's avatar
Elemer Lelik committed
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
  AttributeType * anyAttr = NULL;
  for (List<AttributeType*>::iterator field = attribfields.begin(), nextField; field; field = nextField) {
    nextField = field->Next;
    field->Data->applyUseAttribute();
    //Find anyattribute, and remove it
    if (field->Data->isAnyAttribute()) {
      anyAttr = new AttributeType(*field->Data);
      setParent(this, anyAttr);
      delete field->Data;
      field->Data = NULL;
      attribfields.remove(field);
    } else {
      field->Data->SimpleType::finalModification();
    }
  }
  
  //Push anyattribute to the front
  if (anyAttr != NULL) {
    anyAttr->applyNamespaceAttribute(V_anyAttributes);
    attribfields.push_back(anyAttr);
  }

  //Substitution group ordering
Elemer Lelik's avatar
Elemer Lelik committed
1118
  if(subsGroup == this || typeSubsGroup == this){ //We are a generated substitution group
Elemer Lelik's avatar
Elemer Lelik committed
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
    //Substitution group never empty
    ComplexType * front = complexfields.front();
    List<ComplexType*>::iterator it = complexfields.begin();
    complexfields.remove(it);
    complexfields.sort(compareComplexTypeNameSpaces);
    complexfields.sort(compareTypes);
    complexfields.push_front(front);
  }
}

void ComplexType::finalModification() {
1130
  subFinalModification();
Elemer Lelik's avatar
Elemer Lelik committed
1131
  setFieldPaths(empty_string);
1132
1133
1134
1135
}

void ComplexType::finalModification2() {
  subFinalModification2();
Elemer Lelik's avatar
Elemer Lelik committed
1136
1137
1138
1139
1140
1141
  List<Mstring> container;
  collectVariants(container);
  variant.clear();
  variant = container;
}

1142
1143
1144
1145
1146
1147
void ComplexType::subFinalModification2() {
  SimpleType::finalModification2();
  for (List<ComplexType*>::iterator field = complexfields.begin(); field; field = field->Next) {
    //Recursive call
    field->Data->subFinalModification2();
  }
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
  for (List<AttributeType*>::iterator field = attribfields.begin(), nextField; field; field = nextField) {
    nextField = field->Next;
    if (field->Data->getUseVal() == prohibited || !field->Data->isVisible()) {
      //Not visible attribute removed
      delete field->Data;
      field->Data = NULL;
      attribfields.remove(field);
    } else {
      field->Data->SimpleType::finalModification2();
    }
1158
1159
1160
  }
}

Elemer Lelik's avatar
Elemer Lelik committed
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
void ComplexType::printToFile(FILE * file) {
  printToFile(file, 0, false);
}

void ComplexType::printToFile(FILE * file, const unsigned level, const bool is_union) {
  if (!isVisible()) {
    return;
  }
  printComment(file, level);
  if (top) {
    fprintf(file, "type ");
    if(mode == listMode){
      printMinOccursMaxOccurs(file, is_union);
      fprintf(file, "%s", type.convertedValue.c_str());
    }else {
      fprintf(file, "%s %s", type.convertedValue.c_str(), name.convertedValue.c_str());
    }
1178
1179
1180
1181
1182
1183
    if(type.convertedValue == "record" || type.convertedValue == "union"){
      fprintf(file, "\n{\n");
      if (attribfields.empty() && complexfields.empty()) {
        fprintf(file, "\n");
      }
    } 
Elemer Lelik's avatar
Elemer Lelik committed
1184
1185
1186
1187
1188
1189
1190
1191
1192

    for (List<ComplexType*>::iterator c = complexfields.begin(), nextField; c; c = nextField) {
      nextField = c->Next;
      if (c->Data->embed || c->Data->enumerated) {
        c->Data->printToFile(file, level + 1, is_union);
        if (c->Next != NULL || !attribfields.empty()) {
          fprintf(file, ",\n");
        } else {
          fprintf(file, "\n");
Elemer Lelik's avatar
Elemer Lelik committed
1193
        }
Elemer Lelik's avatar
Elemer Lelik committed
1194
1195
1196
        delete c->Data;
        c->Data = NULL;
        complexfields.remove(c);
Elemer Lelik's avatar
Elemer Lelik committed
1197
      }
Elemer Lelik's avatar
Elemer Lelik committed
1198
1199
1200
1201
1202
1203
1204
1205
    }

    for (List<AttributeType*>::iterator f = attribfields.begin(); f; f = f->Next) {
      f->Data->printToFile(file, level + 1);
      if (f->Next != NULL || !complexfields.empty()) {
        fprintf(file, ",\n");
      } else {
        fprintf(file, "\n");
Elemer Lelik's avatar
Elemer Lelik committed
1206
1207
      }
    }
Elemer Lelik's avatar
Elemer Lelik committed
1208
1209
1210
1211
1212
1213
1214

    for (List<ComplexType*>::iterator c = complexfields.begin(); c; c = c->Next) {
      c->Data->printToFile(file, level + 1, is_union);
      if (c->Next != NULL) {
        fprintf(file, ",\n");
      } else {
        fprintf(file, "\n");
Elemer Lelik's avatar
Elemer Lelik committed
1215
1216
      }
    }
Elemer Lelik's avatar
Elemer Lelik committed
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
  } else {
    const bool field_is_record = getType().convertedValue == Mstring("record");
    const bool field_is_union = getType().convertedValue == "union";
    if (complexfields.empty() && attribfields.empty() && (field_is_record || field_is_union)) {
      if (field_is_record) {
        indent(file, level);
        printMinOccursMaxOccurs(file, is_union);
        fprintf(file, "%s {\n", getType().convertedValue.c_str());
        indent(file, level);
        fprintf(file, "} %s", getName().convertedValue.c_str());
        if (isOptional) {
          fprintf(file, " optional");
        }
      } else if (field_is_union) {
        indent(file, level);
        printMinOccursMaxOccurs(file, is_union);
        fprintf(file, "%s {\n", getType().convertedValue.c_str());
        indent(file, level + 1);
        fprintf(file, "record length(0 .. 1) of enumerated { NULL_ } choice\n");
        indent(file, level);
        fprintf(file, "} %s", getName().convertedValue.c_str());
        if (isOptional) {
          fprintf(file, " optional");
        }
      }
    } else {
      indent(file, level);
      if (getEnumeration().modified) {
        if (isFloatType(getBuiltInBase())) {
          fprintf(file, "%s (", type.convertedValue.c_str());
          getEnumeration().sortFacets();
          getEnumeration().printToFile(file);
          fprintf(file, ")");
        } else {
          printMinOccursMaxOccurs(file, with_union);
          fprintf(file, "enumerated {\n");
          //getEnumeration().sortFacets();
          getEnumeration().printToFile(file, level);
          fprintf(file, "\n");
          indent(file, level);
          fprintf(file, "} ");
        }
      } else {
        int multiplicity = multi(module, getReference(), this);
        if ((multiplicity > 1) && getReference().get_ref()) {
          fprintf(file, "%s.", getReference().get_ref()->getModule()->getModulename().c_str());
        }
1264
1265
1266
1267
1268
1269
1270
        if (field_is_record || field_is_union || listPrint) {
          unsigned long long int tempMin = getMinOccurs();
          unsigned long long int tempMax = getMaxOccurs();
          if (listPrint) {
            setMinOccurs(listMinOccurs);
            setMaxOccurs(listMaxOccurs);
          }
Elemer Lelik's avatar
Elemer Lelik committed
1271
          printMinOccursMaxOccurs(file, with_union, !first_child || parent->getXsdtype() != n_choice);
1272
1273
1274
          if (listPrint) {
            setMinOccurs(tempMin);
            setMaxOccurs(tempMax);
Elemer Lelik's avatar
Elemer Lelik committed
1275
          }