ComplexType.cc 68 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
///////////////////////////////////////////////////////////////////////////////
Elemer Lelik's avatar
Elemer Lelik committed
2
// Copyright (c) 2000-2015 Ericsson Telecom AB
Elemer Lelik's avatar
Elemer Lelik committed
3
4
5
6
7
8
9
10
11
12
13
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
///////////////////////////////////////////////////////////////////////////////
#include "ComplexType.hh"

#include "GeneralFunctions.hh"
#include "XMLParser.hh"
#include "TTCN3Module.hh"
#include "TTCN3ModuleInventory.hh"
Elemer Lelik's avatar
Elemer Lelik committed
14
#include "Annotation.hh"
Elemer Lelik's avatar
Elemer Lelik committed
15
16
17
18

#include <assert.h>

ComplexType::ComplexType(XMLParser * a_parser, TTCN3Module * a_module, ConstructType a_construct)
Elemer Lelik's avatar
Elemer Lelik committed
19
20
21
22
23
: SimpleType(a_parser, a_module, a_construct)
, top(true)
, nillable(false)
, enumerated(false)
, embed(false)
Elemer Lelik's avatar
Elemer Lelik committed
24
, with_union(false)
Elemer Lelik's avatar
Elemer Lelik committed
25
26
27
28
29
30
31
32
33
34
35
, first_child(false)
, fromAll(false)
, max_alt(0)
, skipback(0)
, lastType()
, actualPath(empty_string)
, actfield(this)
, nameDep(NULL)
, nillable_field(NULL)
, basefield(NULL)
, cmode(CT_undefined_mode)
Elemer Lelik's avatar
Elemer Lelik committed
36
, resolved(No)
Elemer Lelik's avatar
Elemer Lelik committed
37
38
39
40
41
, complexfields()
, attribfields()
, enumfields()
, tagNames() {
  xsdtype = n_complexType;
Elemer Lelik's avatar
Elemer Lelik committed
42
43
}

Elemer Lelik's avatar
Elemer Lelik committed
44
45
46
47
48
49
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
50
, with_union(other.with_union)
Elemer Lelik's avatar
Elemer Lelik committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
, first_child(other.first_child)
, fromAll(other.fromAll)
, max_alt(other.max_alt)
, skipback(other.skipback)
, lastType(other.lastType)
, actualPath(other.actualPath)
, actfield(this)
, nameDep(other.nameDep)
, nillable_field(NULL)
, basefield(NULL)
, cmode(other.cmode)
, resolved(other.resolved) {
  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
77
  }
Elemer Lelik's avatar
Elemer Lelik committed
78
79
80
81
82
83
84
85
86

  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
87
88
89
  }
}

Elemer Lelik's avatar
Elemer Lelik committed
90
91
92
93
94
95
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
96
, with_union(false)
Elemer Lelik's avatar
Elemer Lelik committed
97
98
99
100
101
102
103
104
105
106
107
, first_child(false)
, fromAll(false)
, max_alt(0)
, skipback(0)
, lastType()
, actualPath(empty_string)
, actfield(this)
, nameDep(NULL)
, nillable_field(NULL)
, basefield(NULL)
, cmode(CT_undefined_mode)
Elemer Lelik's avatar
Elemer Lelik committed
108
, resolved(No)
Elemer Lelik's avatar
Elemer Lelik committed
109
110
111
112
113
114
115
116
, complexfields()
, attribfields()
, enumfields()
, tagNames() {
  xsdtype = n_complexType;
  parent = other;
  outside_reference = ReferenceData();
}
Elemer Lelik's avatar
Elemer Lelik committed
117

Elemer Lelik's avatar
Elemer Lelik committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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)
, max_alt(0)
, skipback(0)
, lastType()
, actualPath(empty_string)
, actfield(this)
, nameDep(NULL)
, nillable_field(NULL)
, basefield(NULL)
, cmode(CT_simpletype_mode)
, resolved(No)
, complexfields()
, attribfields()
, enumfields()
, tagNames() {

  if(c != fromTagSubstition){
    module->replaceLastMainType(this);
    module->setActualXsdConstruct(c_complexType);
Elemer Lelik's avatar
Elemer Lelik committed
145
  }
Elemer Lelik's avatar
Elemer Lelik committed
146
  construct = c_complexType;
Elemer Lelik's avatar
Elemer Lelik committed
147

Elemer Lelik's avatar
Elemer Lelik committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
  switch (c) {
    case fromTagUnion:
      type.upload(Mstring("union"));
      with_union = true;
      xsdtype = n_union;
      break;
    case fromTagNillable:
      addVariant(V_useNil);
      type.upload(Mstring("record"));
      break;
    case fromTagComplexType:
      type.upload(Mstring("record"));
      xsdtype = n_complexType;
      break;
    case fromTagSubstition:
      type.upload(Mstring("union"));
      name.upload(getName().originalValueWoPrefix + Mstring("_group"));
      xsdtype = n_union;
      subsGroup = this;
      variant.clear();
      enumeration.modified = false;
      value.modified = false;
      pattern.modified = false;
      length.modified = false;
      whitespace.modified = false;
      break;
Elemer Lelik's avatar
Elemer Lelik committed
174
175
176
  }
}

Elemer Lelik's avatar
Elemer Lelik committed
177
178
179
180
181
182
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
183

Elemer Lelik's avatar
Elemer Lelik committed
184
185
186
187
188
  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
189
190
}

Elemer Lelik's avatar
Elemer Lelik committed
191
192
193
194
195
196
197
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
198
  const XMLParser::TagAttributes & atts = parser->getActualTagAttributes();
Elemer Lelik's avatar
Elemer Lelik committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  
  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);
        rec->type.upload(Mstring("record"));
        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;
        }
        type.upload(Mstring("record"));
        xsdtype = n_sequence;
        setMinMaxOccurs(atts.minOccurs, atts.maxOccurs);
      }
Elemer Lelik's avatar
Elemer Lelik committed
221
      break;
Elemer Lelik's avatar
Elemer Lelik committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
    case n_choice:
      if (!top || xsdtype != n_group) {
        //Create new union field
        ComplexType * choice = new ComplexType(this);
        choice->type.upload(Mstring("union"));
        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;
        type.upload(Mstring("union"));
      }
      break;
    case n_all:
Elemer Lelik's avatar
Elemer Lelik committed
239
    {
Elemer Lelik's avatar
Elemer Lelik committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
      //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
      if (parent != NULL && parent->with_union) {
        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);
        complexfields.push_back(f);
        basefield = f;
        actfield = f;
      } 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);
        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
296
    {
Elemer Lelik's avatar
Elemer Lelik committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
      if (atts.nillable) {
        if(cmode == CT_simpletype_mode){
          //If a simple top level element is nillable
          ComplexType * nilrec = new ComplexType(this);
          if (atts.type.empty()) {
            nilrec->type.upload(Mstring("record"));
          } else {
            nilrec->type.upload(atts.type);
          }
          nilrec->name.upload(Mstring("content"));
          nilrec->isOptional = true;
          nilrec->nillable = true;
          setMinMaxOccurs(atts.minOccurs, atts.maxOccurs);
          complexfields.push_back(nilrec);
          type.upload(Mstring("record"));
          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);
          if (atts.type.empty()) {
            nilrec->type.upload(Mstring("record"));
          } else {
            nilrec->type.upload(atts.type);
          }
          record->name.upload(atts.name);
          record->type.upload(Mstring("record"));
          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
337
        }
Elemer Lelik's avatar
Elemer Lelik committed
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
      }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
355
        }
Elemer Lelik's avatar
Elemer Lelik committed
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
        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
379
380
381
382
        }
      }
      break;
    }
Elemer Lelik's avatar
Elemer Lelik committed
383
    case n_attribute:
Elemer Lelik's avatar
Elemer Lelik committed
384
    {
Elemer Lelik's avatar
Elemer Lelik committed
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
      AttributeType * attribute = new AttributeType(this);
      attribute->addVariant(V_attribute);
      attribute->applyMinMaxOccursAttribute(0, 1);
      attribute->setXsdtype(n_attribute);
      attribute->applyDefaultAttribute(atts.default_);
      attribute->applyFixedAttribute(atts.fixed);
      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
400
      }
Elemer Lelik's avatar
Elemer Lelik committed
401
402
403
404
405
406
407
408
409
410
411
412
413
      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
414
      }
Elemer Lelik's avatar
Elemer Lelik committed
415
      break;
Elemer Lelik's avatar
Elemer Lelik committed
416
    }
Elemer Lelik's avatar
Elemer Lelik committed
417
418
419
420
421
422
423
424
425
426
    case n_any:
    {
      ComplexType * any = new ComplexType(this);
      any->name.upload(Mstring("elem"));
      any->type.upload(Mstring("xsd:string"));
      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
427
    }
Elemer Lelik's avatar
Elemer Lelik committed
428
    case n_anyAttribute:
Elemer Lelik's avatar
Elemer Lelik committed
429
    {
Elemer Lelik's avatar
Elemer Lelik committed
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
      AttributeType * anyattr = new AttributeType(this);
      anyattr->setXsdtype(n_anyAttribute);
      anyattr->setNameOfField(Mstring("attr"));
      anyattr->setTypeValue(Mstring("xsd:string"));
      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
450
      }
Elemer Lelik's avatar
Elemer Lelik committed
451
      break;
Elemer Lelik's avatar
Elemer Lelik committed
452
    }
Elemer Lelik's avatar
Elemer Lelik committed
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
    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
483
    {
Elemer Lelik's avatar
Elemer Lelik committed
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
      with_union = true;
      xsdtype = n_union;
      type.upload(Mstring("union"));
      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
510
      }
Elemer Lelik's avatar
Elemer Lelik committed
511
      break;
Elemer Lelik's avatar
Elemer Lelik committed
512
    }
Elemer Lelik's avatar
Elemer Lelik committed
513
514
    case n_simpleType:
    case n_simpleContent:
Elemer Lelik's avatar
Elemer Lelik committed
515
    {
Elemer Lelik's avatar
Elemer Lelik committed
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
      xsdtype = parser->getActualTagName();
      cmode = CT_simpletype_mode;
      Mstring fieldname;
      if (with_union) {
        if (max_alt == 0) {
          fieldname = Mstring("alt_");
        } else {
          fieldname = mprintf("alt_%d", max_alt);
        }
        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
532
      }
Elemer Lelik's avatar
Elemer Lelik committed
533
      break;
Elemer Lelik's avatar
Elemer Lelik committed
534
    }
Elemer Lelik's avatar
Elemer Lelik committed
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
    case n_complexType:
      name.upload(atts.name);
      type.upload(Mstring("record"));
      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"));
        mixed->type.upload(Mstring("xsd:string"));
        mixed->setMinMaxOccurs(0, ULLONG_MAX, false);
        mixed->embed = true;
        complexfields.push_back(mixed);
        addVariant(V_embedValues);
      }
      break;
    case n_list:
    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
578
579
580
581
  }
}

// called from endelementHandler
Elemer Lelik's avatar
Elemer Lelik committed
582
583
584
585
586
587
588
void ComplexType::modifyValues() {
  if (this != actfield) {
    actfield->modifyValues();
    return;
  }
  if (xsdtype == n_sequence) {
    skipback = skipback - 1;
Elemer Lelik's avatar
Elemer Lelik committed
589
590
  }

Elemer Lelik's avatar
Elemer Lelik committed
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
  if ((xsdtype == n_element || 
       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 || 
       xsdtype == n_simpleType || 
       xsdtype == n_simpleContent ||
       (xsdtype == n_sequence && skipback < 0)
      ) 
      && parent != NULL) {
    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
615
616
    }
  }
Elemer Lelik's avatar
Elemer Lelik committed
617
618
619
620
621
622
623
}

void ComplexType::referenceResolving() {
  if (resolved != No) return; // nothing to do
  if(this == subsGroup){
    resolved = Yes;
    return;
Elemer Lelik's avatar
Elemer Lelik committed
624
  }
Elemer Lelik's avatar
Elemer Lelik committed
625
626
627
628
  resolved = InProgress;
  for (List<ComplexType*>::iterator ct = complexfields.begin(); ct; ct = ct->Next) {
    // Referenece resolving of ComplexTypes
    ct->Data->referenceResolving();
Elemer Lelik's avatar
Elemer Lelik committed
629
  }
Elemer Lelik's avatar
Elemer Lelik committed
630
631
632
  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
633
  }
Elemer Lelik's avatar
Elemer Lelik committed
634
635
636
637
638
  
  reference_resolving_funtion();
  
  if(!substitionGroup.empty()){
    addToSubstitutions();
Elemer Lelik's avatar
Elemer Lelik committed
639
  }
Elemer Lelik's avatar
Elemer Lelik committed
640
  resolved = Yes;
Elemer Lelik's avatar
Elemer Lelik committed
641
642
}

Elemer Lelik's avatar
Elemer Lelik committed
643
644
645
646
void ComplexType::reference_resolving_funtion() {
  //Every child element references are resolved here.
  if (outside_reference.empty() && basefield == NULL) {
    return;
Elemer Lelik's avatar
Elemer Lelik committed
647
648
  }

Elemer Lelik's avatar
Elemer Lelik committed
649
650
651
652
653
654
655
656
  SimpleType * st = (SimpleType*) TTCN3ModuleInventory::getInstance().lookup(this, want_BOTH);
  if (st == NULL && basefield == NULL) {
    printError(module->getSchemaname(), name.convertedValue,
      "Reference for a non-defined type: " + getReference().repr());
    TTCN3ModuleInventory::getInstance().incrNumErrors();
    outside_reference.set_resolved(NULL);
    return;
  }
Elemer Lelik's avatar
Elemer Lelik committed
657

Elemer Lelik's avatar
Elemer Lelik committed
658
  resolveAttributeGroup(st);
Elemer Lelik's avatar
Elemer Lelik committed
659

Elemer Lelik's avatar
Elemer Lelik committed
660
  resolveGroup(st);
Elemer Lelik's avatar
Elemer Lelik committed
661

Elemer Lelik's avatar
Elemer Lelik committed
662
  resolveElement(st);
Elemer Lelik's avatar
Elemer Lelik committed
663

Elemer Lelik's avatar
Elemer Lelik committed
664
  resolveSimpleTypeExtension();
Elemer Lelik's avatar
Elemer Lelik committed
665

Elemer Lelik's avatar
Elemer Lelik committed
666
  resolveSimpleTypeRestriction();
Elemer Lelik's avatar
Elemer Lelik committed
667

Elemer Lelik's avatar
Elemer Lelik committed
668
  resolveComplexTypeExtension();
Elemer Lelik's avatar
Elemer Lelik committed
669

Elemer Lelik's avatar
Elemer Lelik committed
670
  resolveComplexTypeRestriction();
Elemer Lelik's avatar
Elemer Lelik committed
671

Elemer Lelik's avatar
Elemer Lelik committed
672
  resolveUnion(st);
Elemer Lelik's avatar
Elemer Lelik committed
673
674
675

}

Elemer Lelik's avatar
Elemer Lelik committed
676
677
void ComplexType::setParent(ComplexType * par, SimpleType * child) {
  child->parent = par;
Elemer Lelik's avatar
Elemer Lelik committed
678
679
}

Elemer Lelik's avatar
Elemer Lelik committed
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
void ComplexType::applyReference(const SimpleType & other, const bool on_attributes) {
  type.convertedValue = other.getType().convertedValue;
  type.originalValueWoPrefix = other.getType().convertedValue;

  if (other.getMinOccurs() > minOccurs || other.getMaxOccurs() < maxOccurs) {
    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.",
        minOccurs,
        maxOccurs,
        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
699
700
      TTCN3ModuleInventory::getInstance().incrNumErrors();
    }
Elemer Lelik's avatar
Elemer Lelik committed
701
702
703
  } else {
    minOccurs = llmax(minOccurs, other.getMinOccurs());
    maxOccurs = llmin(maxOccurs, other.getMaxOccurs());
Elemer Lelik's avatar
Elemer Lelik committed
704
705
  }

Elemer Lelik's avatar
Elemer Lelik committed
706
707
708
709
710
  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
711
712
713
        break;
      }
    }
Elemer Lelik's avatar
Elemer Lelik committed
714
715
716
    if (!found) {
      variant.push_back(var->Data);
      variant_ref.push_back(var->Data);
Elemer Lelik's avatar
Elemer Lelik committed
717
718
719
    }
  }

Elemer Lelik's avatar
Elemer Lelik committed
720
  builtInBase = other.getBuiltInBase();
Elemer Lelik's avatar
Elemer Lelik committed
721

Elemer Lelik's avatar
Elemer Lelik committed
722
723
724
725
726
  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
727
728
}

Elemer Lelik's avatar
Elemer Lelik committed
729
730
731
732
733
734
735
736
737
738
739
740
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
741
742
743
  }
}

Elemer Lelik's avatar
Elemer Lelik committed
744
void ComplexType::nameConversion_names(const List<NamespaceType> &) {
Elemer Lelik's avatar
Elemer Lelik committed
745
746
747
748
  Mstring res, var(module->getTargetNamespace());
  XSDName2TTCN3Name(name.convertedValue, TTCN3ModuleInventory::getInstance().getTypenames(), type_name, res, var);
  name.convertedValue = res;
  bool found = false;
Elemer Lelik's avatar
Elemer Lelik committed
749
  for (List<Mstring>::iterator vari = variant.begin(); vari; vari = vari->Next) {
Elemer Lelik's avatar
Elemer Lelik committed
750
751
752
753
754
755
756
757
    if (vari->Data == "\"untagged\"") {
      found = true;
      break;
    }
  }
  if (!found) {
    addVariant(V_onlyValue, var);
  }
Elemer Lelik's avatar
Elemer Lelik committed
758
759
  for (List<SimpleType*>::iterator dep = nameDepList.begin(); dep; dep = dep->Next) {
    dep->Data->setTypeValue(res);
Elemer Lelik's avatar
Elemer Lelik committed
760
761
762
  }
}

Elemer Lelik's avatar
Elemer Lelik committed
763
764
765
766
767
768
769
770
771
772
773
774
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
775
776
777
778

  if (type.convertedValue == "record" ||
    type.convertedValue == "set" ||
    type.convertedValue == "union" ||
Elemer Lelik's avatar
Elemer Lelik committed
779
    type.convertedValue == "enumerated") {
Elemer Lelik's avatar
Elemer Lelik committed
780
    return;
Elemer Lelik's avatar
Elemer Lelik committed
781
  }
Elemer Lelik's avatar
Elemer Lelik committed
782
783

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

Elemer Lelik's avatar
Elemer Lelik committed
786
  for (List<NamespaceType>::iterator namesp = ns.begin(); namesp; namesp = namesp->Next) {
Elemer Lelik's avatar
Elemer Lelik committed
787
788
789
790
791
792
    if (prefix == namesp->Data.prefix) {
      uri = namesp->Data.uri;
      break;
    }
  }

Elemer Lelik's avatar
Elemer Lelik committed
793
  QualifiedName in(uri, typeValue); // ns uri + original name
Elemer Lelik's avatar
Elemer Lelik committed
794
795
796

  // Check all known types
  QualifiedNames::iterator origTN = TTCN3ModuleInventory::getInstance().getTypenames().begin();
Elemer Lelik's avatar
Elemer Lelik committed
797
  for (; origTN; origTN = origTN->Next) {
Elemer Lelik's avatar
Elemer Lelik committed
798
799
    if (origTN->Data == in) {
      QualifiedName tmp_name(module->getTargetNamespace(), name.convertedValue);
Elemer Lelik's avatar
Elemer Lelik committed
800
      if (origTN->Data != tmp_name){
Elemer Lelik's avatar
Elemer Lelik committed
801
        break;
Elemer Lelik's avatar
Elemer Lelik committed
802
      }
Elemer Lelik's avatar
Elemer Lelik committed
803
804
805
806
807
    }
  }

  if (origTN != NULL) {
    setTypeValue(origTN->Data.name);
Elemer Lelik's avatar
Elemer Lelik committed
808
  } else {
Elemer Lelik's avatar
Elemer Lelik committed
809
    Mstring res, var;
Elemer Lelik's avatar
Elemer Lelik committed
810
    XSDName2TTCN3Name(typeValue, TTCN3ModuleInventory::getInstance().getTypenames(), type_reference_name, res, var, type.no_replace);
Elemer Lelik's avatar
Elemer Lelik committed
811
812
813
814
    setTypeValue(res);
  }
}

Elemer Lelik's avatar
Elemer Lelik committed
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
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
831
832

    Mstring prefix = field->Data->getType().convertedValue.getPrefix(':');
Elemer Lelik's avatar
Elemer Lelik committed
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
    Mstring typeValue = field->Data->getType().convertedValue.getValueWithoutPrefix(':');

    Mstring res, var;
    var = getModule()->getTargetNamespace();
    XSDName2TTCN3Name(typeValue, TTCN3ModuleInventory::getInstance().getTypenames(), type_reference_name, res, var);

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

    if (field->Data->getName().list_extension) {
      field->Data->useNameListProperty();
      XSDName2TTCN3Name(field->Data->getName().convertedValue,
        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
860
861
      }

Elemer Lelik's avatar
Elemer Lelik committed
862
863
864
865
866
867
868
869
870

      if (!found_in_variant) {
        field->Data->addVariant(V_untagged, empty_string, true);
      }
    } else {
      XSDName2TTCN3Name(field->Data->getName().convertedValue,
        used_field_names, field_name, res, var);
      field->Data->setNameValue(res);
      field->Data->addVariant(V_onlyValue, var);
Elemer Lelik's avatar
Elemer Lelik committed
871
    }
Elemer Lelik's avatar
Elemer Lelik committed
872
873
874
875
876
877
878
879
880
881

  }
}

void ComplexType::setFieldPaths(Mstring path) {
  if (path.empty()) {
    if (!top) {
      Mstring field_prefix = empty_string;
      if(parent->minOccurs == 0 && parent->maxOccurs == ULLONG_MAX){
        field_prefix = "[-].";
Elemer Lelik's avatar
Elemer Lelik committed
882
      }
Elemer Lelik's avatar
Elemer Lelik committed
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
      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);
  }
}

void ComplexType::finalModification2() {  
  //Call SimpleType finalModification
  SimpleType::finalModification();

  //Set isOptional field
  isOptional = isOptional || (minOccurs == 0 && maxOccurs == 1);
  
  //
  List<Mstring> enumNames;
  for (List<ComplexType*>::iterator field = complexfields.begin(), nextField; field; field = nextField) {
    nextField = field->Next;
    //Remove invisible fields
    if ((field->Data->minOccurs == 0 && field->Data->maxOccurs == 0) || !field->Data->isVisible()) {
      delete field->Data;
      field->Data = NULL;
      complexfields.remove(field);
    } else {
      //Recursive call
      field->Data->finalModification2();
      //collect <xsd:all> elements
      if (field->Data->fromAll) {
        enumNames.push_back(field->Data->getName().convertedValue);
Elemer Lelik's avatar
Elemer Lelik committed
927
928
      }
    }
Elemer Lelik's avatar
Elemer Lelik committed
929
  }
Elemer Lelik's avatar
Elemer Lelik committed
930

Elemer Lelik's avatar
Elemer Lelik committed
931
932
  ComplexType * embedField = NULL;
  ComplexType * enumField = NULL;
Elemer Lelik's avatar
Elemer Lelik committed
933

Elemer Lelik's avatar
Elemer Lelik committed
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
  //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
951

Elemer Lelik's avatar
Elemer Lelik committed
952
953
954
955
956
957
  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
958
    }
Elemer Lelik's avatar
Elemer Lelik committed
959
  }
Elemer Lelik's avatar
Elemer Lelik committed
960

Elemer Lelik's avatar
Elemer Lelik committed
961
962
963
964
  if (embedField != NULL) {
    //Insert the embed field to the front
    complexfields.push_front(embedField);
  }
Elemer Lelik's avatar
Elemer Lelik committed
965

Elemer Lelik's avatar
Elemer Lelik committed
966
967
968
969
970
971
972
973
  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 {
          field->Data->name.upload(Mstring(mprintf("alt_%d", number)));
Elemer Lelik's avatar
Elemer Lelik committed
974
        }
Elemer Lelik's avatar
Elemer Lelik committed
975
        number++;
Elemer Lelik's avatar
Elemer Lelik committed
976
      }
Elemer Lelik's avatar
Elemer Lelik committed
977
978
    }
  }
Elemer Lelik's avatar
Elemer Lelik committed
979

Elemer Lelik's avatar
Elemer Lelik committed
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
  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 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::finalModification();
    }
  }
  
  //Push anyattribute to the front
  if (anyAttr != NULL) {
    anyAttr->applyNamespaceAttribute(V_anyAttributes);
    attribfields.push_back(anyAttr);
  }

  //Substitution group ordering
  if(subsGroup == this){ //We are a generated substitution group
    //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() {
  finalModification2();
  setFieldPaths(empty_string);
  List<Mstring> container;
  collectVariants(container);
  variant.clear();
  variant = container;
}

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());
    }
    fprintf(file, "\n{\n");

    if (attribfields.empty() && complexfields.empty()) {
      fprintf(file, "\n");
    }

    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
1059
        }
Elemer Lelik's avatar
Elemer Lelik committed
1060
1061
1062
        delete c->Data;
        c->Data = NULL;
        complexfields.remove(c);
Elemer Lelik's avatar
Elemer Lelik committed
1063
      }
Elemer Lelik's avatar
Elemer Lelik committed
1064
1065
1066
1067
1068
1069
1070
1071
    }

    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
1072
1073
      }
    }
Elemer Lelik's avatar
Elemer Lelik committed
1074
1075
1076
1077
1078
1079
1080

    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
1081
1082
      }
    }
Elemer Lelik's avatar
Elemer Lelik committed
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
  } 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());
        }
        if (field_is_record || field_is_union) {
          printMinOccursMaxOccurs(file, with_union, !first_child || parent->getXsdtype() != n_choice);
          fprintf(file, "%s {\n", getType().convertedValue.c_str());
          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
1141

Elemer Lelik's avatar
Elemer Lelik committed
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
          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");
            }
          }
        } else {
          printMinOccursMaxOccurs(file, with_union, !first_child);
          fprintf(file, "%s ", getType().convertedValue.c_str());
          if (getName().convertedValue == Mstring("order") && getType().convertedValue == Mstring("enumerated")) {
            fprintf(file, "{\n");
            for (List<Mstring>::iterator e = enumfields.begin(); e; e = e->Next) {
              indent(file, level + 1);
              fprintf(file, "%s", e->Data.c_str());
              if (e->Next != NULL) {
                fprintf(file, ",\n");
              } else {
                fprintf(file, "\n");
              }
            }
            indent(file, level);
            fprintf(file, "} ");
          }
        }
Elemer Lelik's avatar
Elemer Lelik committed
1168
      }
Elemer Lelik's avatar
Elemer Lelik committed
1169
1170
1171
      if (field_is_record || field_is_union) {
        indent(file, level);
        fprintf(file, "} ");
Elemer Lelik's avatar
Elemer Lelik committed
1172
      }
Elemer Lelik's avatar
Elemer Lelik committed
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187

      fprintf(file, "%s", getName().convertedValue.c_str());
      getPattern().printToFile(file);
      getValue().printToFile(file);
      getLength().printToFile(file);
      if (!with_union && isOptional) {
        fprintf(file, " optional");
      }
    }
  }

  if (top) {
    fprintf(file, "}");
    if(mode == listMode){
      fprintf(file, " %s", name.convertedValue.c_str());
Elemer Lelik's avatar
Elemer Lelik committed
1188
    }
Elemer Lelik's avatar
Elemer Lelik committed
1189
1190
    printVariant(file);
    fprintf(file, ";\n\n\n");
Elemer Lelik's avatar
Elemer Lelik committed
1191
1192
1193
  }
}

Elemer Lelik's avatar
Elemer Lelik committed
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
void ComplexType::collectVariants(List<Mstring>& container) {

  if (e_flag_used || !isVisible()) {
    return;
  }

  if (top) {
    bool useUnionVariantWhenMainTypeIsRecordOf = false;
    for (List<Mstring>::iterator var = variant.end(); var; var = var->Prev) {
      if ((minOccurs != 1 || maxOccurs != 1) && (var->Data == "\"useUnion\"")) { // main type is a record of
        useUnionVariantWhenMainTypeIsRecordOf = true; // TR HL15893
      } else {
        container.push_back(Mstring("variant ") + Mstring(var->Data.c_str()) + Mstring(";\n"));
      }
    }
    if (useUnionVariantWhenMainTypeIsRecordOf) {
      container.push_back(Mstring("variant ([-]) \"useUnion\";\n"));
    }
  }

  //Collect variants of attributes
  for (List<AttributeType*>::iterator field = attribfields.begin(); field; field = field->Next) {
    field->Data->collectVariants(container);
  }

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

Elemer Lelik's avatar
Elemer Lelik committed
1221
1222
1223
    if (!field->Data->isVisible()) {
      continue;
    }
Elemer Lelik's avatar
Elemer Lelik committed
1224
1225
1226
1227

    if (field->Data->getVariant().empty() && field->Data->getHiddenVariant().empty() &&
        field->Data->complexfields.empty() && field->Data->attribfields.empty() &&
        field->Data->enumeration.variants.empty()) {
Elemer Lelik's avatar
Elemer Lelik committed
1228
1229
1230
      continue;
    }

Elemer Lelik's avatar
Elemer Lelik committed
1231
    bool already_used = false;
Elemer Lelik's avatar
Elemer Lelik committed
1232

Elemer Lelik's avatar
Elemer Lelik committed
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
    for (List<Mstring>::iterator var2 = field->Data->getVariant().end(); var2; var2 = var2->Prev) {
      if (var2->Data == "\"untagged\"" && !already_used) {
        container.push_back(Mstring("variant (") + field->Data->actualPath + Mstring(") ") + Mstring(var2->Data.c_str()) + Mstring(";\n"));
        already_used = true;
      } else {
        if ((field->Data->getMinOccurs() != 1 || field->Data->getMaxOccurs() != 1) &&
          (field->Data->getName().list_extension || var2->Data == "\"useUnion\"")) {
          container.push_back(Mstring("variant (") + field->Data->actualPath + Mstring("[-]) ") + Mstring(var2->Data.c_str()) + Mstring(";\n"));
        } else if (var2->Data != "\"untagged\"") {
          container.push_back(Mstring("variant (") + field->Data->actualPath + Mstring(") ") + Mstring(var2->Data.c_str()) + Mstring(";\n"));
        }
      }
Elemer Lelik's avatar
Elemer Lelik committed
1245
    }
Elemer Lelik's avatar
Elemer Lelik committed
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
    for (List<Mstring>::iterator hidden_var = field->Data->getHiddenVariant().end();
      hidden_var; hidden_var = hidden_var->Prev) {
      if ((field->Data->getMinOccurs() != 1 || field->Data->getMaxOccurs() != 1) &&
        field->Data->getName().list_extension) {
        container.push_back(Mstring("//variant (") + field->Data->actualPath + Mstring("[-]) ") + Mstring(hidden_var->Data.c_str()) + Mstring(";\n"));
      } else {
        container.push_back(Mstring("//variant (") + field->Data->actualPath + Mstring(") ") + Mstring(hidden_var->Data.c_str()) + Mstring(";\n"));
      }
    }

    if(field->Data->enumeration.modified){
      Mstring path = empty_string;
      if(field->Data->getMinOccurs() != 1 && field->Data->getMaxOccurs() != 1){
        path = field->Data->actualPath + Mstring("[-]");
      }else {
        path = field->Data->actualPath;
      }
      for(List<Mstring>::iterator var = field->Data->enumeration.variants.end(); var; var = var->Prev){
        if(var->Data.empty()) continue;
        container.push_back("variant (" + path + ") " + var->Data + ";\n");
      }
Elemer Lelik's avatar
Elemer Lelik committed
1267
    }
Elemer Lelik's avatar
Elemer Lelik committed
1268
1269
    //Recursive call
    field->Data->collectVariants(container);
Elemer Lelik's avatar
Elemer Lelik committed
1270
1271
1272
  }
}

Elemer Lelik's avatar
Elemer Lelik committed
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
void ComplexType::printVariant(FILE * file) {
  if (e_flag_used) {
    return;
  }

  bool foundAtLeastOneVariant = false;
  bool foundAtLeastOneHiddenVariant = false;

  if (!variant.empty()) {
    for (List<Mstring>::iterator var = variant.begin(); var; var = var->Next) {
      if (foundAtLeastOneVariant && foundAtLeastOneHiddenVariant) {
        break;
      }
      if (var->Data[0] != '/') {
        foundAtLeastOneVariant = true;
      } else {
        foundAtLeastOneHiddenVariant = true;
      }
    }
  }
Elemer Lelik's avatar
Elemer Lelik committed
1293

Elemer Lelik's avatar
Elemer Lelik committed
1294
1295
1296
  if (!foundAtLeastOneVariant && !foundAtLeastOneHiddenVariant) {
    return;
  }
Elemer Lelik's avatar
Elemer Lelik committed
1297

Elemer Lelik's avatar
Elemer Lelik committed
1298
1299
1300
1301
1302
1303
  if (!foundAtLeastOneVariant) {
    //No other variants, only commented, so the 'with' must be commented also.
    fprintf(file, ";\n//with {\n");
  } else {
    fprintf(file, "\nwith {\n");
  }
Elemer Lelik's avatar
Elemer Lelik committed
1304

Elemer Lelik's avatar
Elemer Lelik committed
1305
1306
  for (List<Mstring>::iterator var = variant.begin(); var; var = var->Next) {
    fprintf(file, "%s", var->Data.c_str());
Elemer Lelik's avatar
Elemer Lelik committed
1307
1308
  }

Elemer Lelik's avatar
Elemer Lelik committed
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
  if (!foundAtLeastOneVariant) {
    fprintf(file, "//");
  }
  fprintf(file, "}");
}

void ComplexType::dump(unsigned int depth) const {
  fprintf(stderr, "%*s %sComplexType at %p | Top:%s\n", depth * 2, "", isVisible() ? "" : "(hidden)", (const void*) this, top ? "true" : "false");
  if (parent != NULL) {
    fprintf(stderr, "%*s parent: %p | parent xsdtype: %i | my xsdtype: %i\n", depth * 2, "", (const void*) parent, parent->getXsdtype(), xsdtype);
  } else {
    fprintf(stderr, "%*s parent: %p | parent xsdtype: %s | my xsdtype: %i\n", depth * 2, "", "NULL", "NULL", xsdtype);
  }
  fprintf(stderr, "%*s name='%s' -> '%s', type='%s' %d complexfields | %s | %s | %s\n", depth * 2, "",
    name.originalValueWoPrefix.c_str(), name.convertedValue.c_str(), type.convertedValue.c_str(), (int) complexfields.size(),
    outside_reference.empty() ? "" : outside_reference.get_val().c_str(), mode == restrictionMode ? "restriction" : "",
    mode == extensionMode ? "extension" : "");
  for (List<ComplexType*>::iterator field = complexfields.begin(); field; field = field->Next) {
    field->Data->dump(depth + 1);
  }
  fprintf(stderr, "%*s %d attribields\n", depth * 2, "", (int) attribfields.size());
  for (List<AttributeType*>::iterator field = attribfields.begin(); field; field = field->Next) {
    field->Data->dump(depth + 1);
  }
  fprintf(stderr, "%*s %d enumfields\n", depth * 2, "", (int) enumfields.size());
  for (List<Mstring>::iterator field = enumfields.begin(); field; field = field->Next) {
    fprintf(stderr, "%*s enum: %s\n", depth * 2 + depth, "", field->Data.c_str());
  }
  fprintf(stderr, "%*s (%llu .. %llu) | Optional:%s | List:%s\n", (depth + 1) * 2, "", minOccurs, maxOccurs, isOptional ? "true" : "false", name.list_extension ? "true" : "false");
  fprintf(stderr, "%*s %d variants: ", (depth + 1) * 2, "", (int) variant.size());
  for (List<Mstring>::iterator var = variant.begin(); var; var = var->Next) {
    fprintf(stderr, "%s, ", var->Data.c_str());
  }
  fprintf(stderr, "%*s pattern:%s | length:%i \n ", (depth + 1) * 2, "", this->pattern.facet.c_str(), (int) (this->length.facet_maxLength));
  fprintf(stderr, "%*s enum: %i \n", (depth + 1)*2, "", (int) this->enumeration.facets.size());
  fprintf(stderr, "\n");
}

void ComplexType::setMinMaxOccurs(const unsigned long long min, const unsigned long long max, const bool generate_list_postfix) {

  if (min != 1 || max != 1) {
    if (xsdtype == n_choice) {
      minOccurs = min;
      maxOccurs = max;
      addVariant(V_untagged);
      first_child = false;
    } else if (xsdtype == n_sequence) {
      ComplexType * rec = new ComplexType(this);
      rec->type.upload(Mstring("record"));
      rec->name.upload(Mstring("sequence"));
      rec->setXsdtype(n_sequence);
      rec->addVariant(V_untagged);
      rec->addVariant(V_untagged);
      rec->minOccurs = min;
      rec->maxOccurs = max;
      complexfields.push_back(rec);
      actfield = rec;
      if ((rec->minOccurs == 0 && rec->maxOccurs > 1) || rec->minOccurs > 0) {
        rec->name.list_extension = true;
      }
    } else {
      minOccurs = min;
      maxOccurs = max;
      if ((minOccurs == 0 && maxOccurs > 1) || minOccurs > 0) {
        if (generate_list_postfix) {
          name.list_extension = true;
        }
      }
      if (parent != NULL && parent->getXsdtype() == n_choice) {
        name.list_extension = true;
        if ((parent != NULL && parent->getXsdtype() == n_choice)) {
          if (parent->first_child == false && minOccurs == 0) {
            parent->first_child = true;
            with_union = true;
            first_child = false;
          } else {
            with_union = true;
            first_child = true;
          }
        }
      }
Elemer Lelik's avatar
Elemer Lelik committed
1390
    }
Elemer Lelik's avatar
Elemer Lelik committed
1391
  }
Elemer Lelik's avatar
Elemer Lelik committed
1392

Elemer Lelik's avatar
Elemer Lelik committed
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
  if (maxOccurs > 1 && generate_list_postfix) {
    name.list_extension = true;
  }
}

void ComplexType::applyNamespaceAttribute(VariantMode varLabel, const Mstring& ns_list) {
  List<Mstring> namespaces;
  if (!ns_list.empty()) {
    expstring_t valueToSplitIntoTokens = mcopystr(ns_list.c_str());
    char * token;
    token = strtok(valueToSplitIntoTokens, " ");
    while (token != NULL) {
      namespaces.push_back(Mstring(token));
      token = strtok(NULL, " ");
    }
    Free(valueToSplitIntoTokens);
  }

  Mstring any_ns;
  bool first = true;
  // Note: libxml2 will verify the namespace list according to the schema
  // of XML Schema. It is either ##any, ##other, ##local, ##targetNamespace,
  // or a list of (namespace reference | ##local | ##targetNamespace).
  for (List<Mstring>::iterator ns = namespaces.begin(); ns; ns = ns->Next) {
    static const Mstring xxany("##any"), xxother("##other"), xxlocal("##local"),
      xxtargetNamespace("##targetNamespace");
    if (!first) any_ns += ',';

    if (ns->Data == xxany) {
    }// this must be the only element, nothing to add
    else if (ns->Data == xxother) { // this must be the only element
      any_ns += " except unqualified";
      if (module->getTargetNamespace() != "NoTargetNamespace") {
        any_ns += ", \'";
        any_ns += parent->getModule()->getTargetNamespace();
        any_ns += '\'';
Elemer Lelik's avatar
Elemer Lelik committed
1429
      }
Elemer Lelik's avatar
Elemer Lelik committed
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
    }// The three cases below can happen multiple times
    else {
      if (first) any_ns += " from ";
      // else a comma was already added
      if (ns->Data == xxtargetNamespace) {
        any_ns += '\'';
        any_ns += parent->getModule()->getTargetNamespace();
        any_ns += '\'';
      } else if (ns->Data == xxlocal) {
        any_ns += "unqualified";
      } else {
        any_ns += '\'';
        any_ns += ns->Data;
        any_ns += '\'';
Elemer Lelik's avatar
Elemer Lelik committed
1444
1445
1446
      }
    }

Elemer Lelik's avatar
Elemer Lelik committed
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
    first = false;
  }

  addVariant(varLabel, any_ns, true);
}

void ComplexType::addComment(const Mstring& text) {
  if (this == actfield) {
    if (lastType == n_attribute) { // ez nem teljesen jo, stb, tobb lehetoseg, es rossy sorrend
      if (!attribfields.empty()) {
        attribfields.back()->addComment(text);
      }
    } else {
      if (actfield->getName().convertedValue == Mstring("base") && parent != NULL) {
        parent->getComment().push_back(Mstring("/* " + text + " */\n"));
      } else {
        comment.push_back(Mstring("/* " + text + " */\n"));
      }
    }
  } else {
    actfield->addComment(text);
    return;
  }
}
Elemer Lelik's avatar
Elemer Lelik committed
1471

Elemer Lelik's avatar
Elemer Lelik committed
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
//Attribute extension logic when extending complextypes
void ComplexType::applyAttributeExtension(ComplexType * found_CT, AttributeType * anyAttrib /* = NULL */) {
  for (List<AttributeType*>::iterator attr = found_CT->attribfields.begin(); attr; attr = attr->Next) {
    bool l = false;
    if (anyAttrib != NULL && attr->Data->isAnyAttribute()) {
      anyAttrib->addNameSpaceAttribute(attr->Data->getNameSpaceAttribute());
      l = true;
    } else {
      for (List<AttributeType*>::iterator attr2 = attribfields.begin(); attr2; attr2 = attr2->Next) {
        if (attr->Data->getName().convertedValue == attr2->Data->getName().convertedValue &&
          attr->Data->getType().convertedValue == attr2->Data->getType().convertedValue) {
          if (attr->Data->getUseVal() == optional) {
            attr2->Data->setUseVal(optional);
          }
          l = true;
Elemer Lelik's avatar
Elemer Lelik committed
1487
1488
1489
1490
          break;
        }
      }
    }
Elemer Lelik's avatar
Elemer Lelik committed
1491
1492
1493
1494
1495
1496
1497
    if (!l) {
      AttributeType * newAttrib = new AttributeType(*attr->Data);
      attribfields.push_back(newAttrib);
      setParent(this, newAttrib);
    }
  }
}
Elemer Lelik's avatar
Elemer Lelik committed
1498

Elemer Lelik's avatar
Elemer Lelik committed
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
//Attribute restriction logic when restricting complextypes
void ComplexType::applyAttributeRestriction(ComplexType * found_CT) {
  for (List<AttributeType*>::iterator attr = attribfields.begin(), nextAttr; attr; attr = nextAttr) {
    nextAttr = attr->Next;
    bool l = false;
    for (List<AttributeType*>::iterator attr2 = found_CT->attribfields.begin(); attr2; attr2 = attr2->Next) {
      if (attr->Data->getName().convertedValue == attr2->Data->getName().convertedValue &&
        attr->Data->getType().convertedValue == attr2->Data->getType().convertedValue) {
        l = true;
        break;
      }
    }
    if (!l) {
      delete attr->Data;
      attr->Data = NULL;
      attribfields.remove(attr);
    }
  }
  size_t size = found_CT->attribfields.size();
  size_t size2 = attribfields.size();
  size_t i = 0;
  List<AttributeType*>::iterator attr = found_CT->attribfields.begin();
  for (; i < size; attr = attr->Next, i = i + 1) {
    bool l = false;
    size_t j = 0;
    List<AttributeType*>::iterator attr2 = attribfields.begin();
    for (; j < size2; attr2 = attr2->Next, j = j + 1) {
      if (attr->Data->getName().convertedValue == attr2->Data->getName().convertedValue &&
        attr->Data->getType().convertedValue == attr2->Data->getType().convertedValue && !attr2->Data->getUsed()) {
        l = true;
        attr2->Data->setUsed(true);
        break;
      }
    }
    if (!l) {
      AttributeType * newAttrib = new AttributeType(*attr->Data);
      attribfields.push_back(newAttrib);
      setParent(this, newAttrib);
    }
  }
}
Elemer Lelik's avatar
Elemer Lelik committed
1540

Elemer Lelik's avatar
Elemer Lelik committed
1541
1542
1543
1544
1545
1546
void ComplexType::addNameSpaceAsVariant(RootType * root, RootType * other) {
  if (other->getModule()->getTargetNamespace() != root->getModule()->getTargetNamespace() &&
    other->getModule()->getTargetNamespace() != "NoTargetNamespace") {
    root->addVariant(V_namespaceAs, other->getModule()->getTargetNamespace());
  }
}
Elemer Lelik's avatar
Elemer Lelik committed
1547

Elemer Lelik's avatar
Elemer Lelik committed
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
void ComplexType::resolveAttribute(AttributeType* attr) {
  if (attr->getXsdtype() == n_attribute && !attr->getReference().empty()) {
    SimpleType * st = (SimpleType*) TTCN3ModuleInventory::getInstance().lookup(attr, want_BOTH);
    if (st != NULL) {
      if (attr->isFromRef()) {
        addNameSpaceAsVariant(attr, st);
        attr->setTypeOfField(st->getName().convertedValue);
        attr->setNameOfField(st->getName().originalValueWoPrefix);
        attr->setOrigModule(st->getModule());
      } else {
        attr->setTypeOfField(st->getName().convertedValue);
        if (st->getType().convertedValue == "record" || st->getType().convertedValue == "union") {
          st->addToNameDepList(attr);
Elemer Lelik's avatar
Elemer Lelik committed
1561
1562
        }
      }
Elemer Lelik's avatar
Elemer Lelik committed
1563
1564
1565
1566
    } else {
      printError(module->getSchemaname(), name.convertedValue,
        "Reference for a non-defined type: " + attr->getReference().repr());
      TTCN3ModuleInventory::getInstance().incrNumErrors();
Elemer Lelik's avatar
Elemer Lelik committed
1567
    }
Elemer Lelik's avatar
Elemer Lelik committed
1568
1569
  }
}
Elemer Lelik's avatar
Elemer Lelik committed
1570

Elemer Lelik's avatar
Elemer Lelik committed
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
void ComplexType::resolveAttributeGroup(SimpleType * st) {
  if (xsdtype == n_attributeGroup && !outside_reference.empty()) {
    ComplexType * ct = (ComplexType*) st;
    if(ct->resolved == No){
      ct->referenceResolving();
    }
    outside_reference.set_resolved(ct);
    setInvisible();
    bool addNameSpaceas = false;
    if (ct->getModule()->getTargetNamespace() != module->getTargetNamespace() &&
      ct->getModule()->getTargetNamespace() != "NoTargetNamespace") {
      addNameSpaceas = true;
    }
    ComplexType * par;
    if(parent->nillable && parent->parent != NULL){
      par = parent->parent;
    }else {
      par = parent;
    }
    List<AttributeType*>::iterator anyAttrib = par->attribfields.begin();
    for (; anyAttrib; anyAttrib = anyAttrib->Next) {
      if (anyAttrib->Data->isAnyAttribute()) {
        break;
      }
Elemer Lelik's avatar
Elemer Lelik committed
1595
    }
Elemer Lelik's avatar
Elemer Lelik committed
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
    for (List<AttributeType*>::iterator attr = ct->attribfields.begin(); attr; attr = attr->Next) {
      AttributeType * attrib = new AttributeType(*attr->Data);
      attr->Data->setOrigModule(ct->getModule());
      if (addNameSpaceas) {
        attrib->addVariant(V_namespaceAs, ct->getModule()->getTargetNamespace());
      }
      if (anyAttrib != NULL && attr->Data->isAnyAttribute()) {
        anyAttrib->Data->addNameSpaceAttribute(attr->Data->getNameSpaceAttribute());
      } else {
        //Nillable attribute placement is hard...
        if (parent->nillable && parent->parent != NULL) {
          parent->parent->attribfields.push_back(attrib);
          attrib->parent = parent->parent;
          setParent(parent->parent, attrib);
        } else if (parent->nillable && !parent->complexfields.empty()) {
          parent->complexfields.back()->attribfields.push_back(attrib);
          attrib->parent = parent->complexfields.back();
        } else if (parent->parent != NULL && (parent->parent->mode == extensionMode || parent->parent->mode == restrictionMode)) {
          parent->parent->attribfields.push_back(attrib);
          setParent(parent->parent, attrib);
        } else {
          parent->attribfields.push_back(attrib);
          setParent(parent, attrib);
Elemer Lelik's avatar
Elemer Lelik committed
1619
1620
        }
      }
Elemer Lelik's avatar
Elemer Lelik committed
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
    }
  }
}

void ComplexType::resolveGroup(SimpleType *st) {
  if (xsdtype == n_group && !outside_reference.empty()) {
    ComplexType * ct = (ComplexType*) st;
    outside_reference.set_resolved(ct);
    setInvisible();
    if(ct->resolved == No){
      ct->referenceResolving();
    }
    //Decide if namespaceas variant needs to be added
    bool addNameSpaceas = false;
    if (ct->getModule()->getTargetNamespace() != module->getTargetNamespace() &&
      ct->getModule()->getTargetNamespace() != "NoTargetNamespace") {
      addNameSpaceas = true;
    }
    if (ct->getXsdtype() == n_sequence && minOccurs == 1 && maxOccurs == 1 && (parent->getXsdtype() == n_complexType || parent->getXsdtype() == n_sequence)) {
      for (List<ComplexType*>::iterator c = ct->complexfields.begin(); c; c = c->Next) {
        ComplexType * newField = new ComplexType(*c->Data);
        parent->complexfields.push_back(newField);
        setParent(parent, newField);
        parent->complexfields.back()->setModule(getModule());
        if (addNameSpaceas) {
          parent->complexfields.back()->addVariant(V_namespaceAs, ct->getModule()->getTargetNamespace());
Elemer Lelik's avatar
Elemer Lelik committed
1647
        }
Elemer Lelik's avatar
Elemer Lelik committed
1648
1649
1650
1651
1652
1653
1654
      }
    } else if (ct->getXsdtype() == n_all) {
      //If the parent optional, then every field is optional
      for (List<ComplexType*>::iterator c = ct->complexfields.begin(); c; c = c->