XerAttributes.cc 13.1 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
Adam Knapp's avatar
Adam Knapp committed
2
 * Copyright (c) 2000-2021 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
 *
 * Contributors:
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Raduly, Csaba
 *   Szabados, Kristof
13
 *   Szabo, Bence Janos
Elemer Lelik's avatar
Elemer Lelik committed
14
15
 *
 ******************************************************************************/
Elemer Lelik's avatar
Elemer Lelik committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
 * XerAttributes.cpp
 *
 *  Created on: Oct 17, 2008
 *      Author: ecsardu
 */

#include "XerAttributes.hh"
// The above line must be the first in this file
#include "../common/memory.h"

#include "Value.hh"

static const NamespaceRestriction empty_nsr = {
  0, 0, NamespaceRestriction::UNUSED
};

static const XerAttributes::NameChange nochange= { NamespaceSpecification::NO_MANGLING };

XerAttributes::XerAttributes()
Elemer Lelik's avatar
Elemer Lelik committed
36
37
: abstract_(false)
, attribute_(false)
Elemer Lelik's avatar
Elemer Lelik committed
38
39
40
, anyAttributes_(empty_nsr)
, anyElement_(empty_nsr)
, base64_(false)
Elemer Lelik's avatar
Elemer Lelik committed
41
, block_(false)
Elemer Lelik's avatar
Elemer Lelik committed
42
43
, decimal_(false)
, defaultForEmpty_(0)
44
45
, defaultForEmptyIsRef_(false)
, defaultForEmptyRef_(0)
Elemer Lelik's avatar
Elemer Lelik committed
46
47
48
49
, defaultValue_(0)
, element_(false)
, embedValues_(false)
, form_(UNSET)
50
51
, has_fractionDigits_(false)
, fractionDigits_(0)
Elemer Lelik's avatar
Elemer Lelik committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
, hex_(false)
, list_(false)
, name_(nochange)
, namespace_()
//, pi_or_comment_()
, num_text_(0)
, text_(0)
, untagged_(false)
, useNil_(false)
, useNumber_(false)
, useOrder_(false)
, useQName_(false)
, useType_(false)
, useUnion_(false)
, whitespace_(PRESERVE)
67
, xsd_type(XSD_NONE)
Elemer Lelik's avatar
Elemer Lelik committed
68
69
70
{
  //__asm("int3");
  //fprintf(stderr, "XER attributes(%p) new\n", (void*)this);
71
  namespace_.global_variant = false;
Elemer Lelik's avatar
Elemer Lelik committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
}

void FreeNamespaceRestriction(NamespaceRestriction& nsr)
{
  for (size_t i=0; i < nsr.nElements_; ++i) {
    Free(nsr.uris_[i]);
  }
  Free(nsr.uris_);
}

XerAttributes::~XerAttributes()
{
  FreeNamespaceRestriction(anyAttributes_);
  FreeNamespaceRestriction(anyElement_);

  Free(defaultForEmpty_);
  delete defaultValue_;
89
  delete defaultForEmptyRef_;
Elemer Lelik's avatar
Elemer Lelik committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

  FreeNameChange(name_);
  FreeNamespace(namespace_);

  //if (pi_or_comment_.position_ > AFTER_VALUE) {
  //  Free(pi_or_comment_.value_);
  //}

  for (size_t i=0; i < num_text_; ++i) {
    if ((unsigned long)text_[i].keyword
      > (unsigned long)NamespaceSpecification::LOWERCASED) {
      Free(text_[i].uri);
    }
    if (text_[i].prefix > (char*)NamespaceSpecification::ALL) {
      Free(text_[i].prefix);
    }
  }
  Free(text_);
}

void XerAttributes::FreeNamespace(NamespaceSpecification &ns) {
  switch (ns.keyword) {
  case NamespaceSpecification::NO_MANGLING:
  case NamespaceSpecification::CAPITALIZED:
  case NamespaceSpecification::UNCAPITALIZED:
  case NamespaceSpecification::UPPERCASED:
  case NamespaceSpecification::LOWERCASED:
    break; // nothing to do
  default: // real string, must be freed
    Free(ns.uri);
    break;
  }
  ns.uri = 0;
  Free(ns.prefix);
  ns.prefix = 0;
125
  ns.global_variant = false;
Elemer Lelik's avatar
Elemer Lelik committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
}

void XerAttributes::FreeNameChange(XerAttributes::NameChange& n) {
  switch (n.kw_) {
  case NamespaceSpecification::NO_MANGLING:
  case NamespaceSpecification::CAPITALIZED:
  case NamespaceSpecification::UNCAPITALIZED:
  case NamespaceSpecification::UPPERCASED:
  case NamespaceSpecification::LOWERCASED:
    break; // nothing to do
  default: // real string, must be freed
    Free(n.nn_);
    break;
  }
  n.kw_ = NamespaceSpecification::NO_MANGLING;
}


void XerAttributes::print(const char *type_name) const {
  fprintf(stderr, "XER attributes(%p) for %s:\n", (const void*)this, type_name);
  if (empty()) fputs("...Empty...\n", stderr);
  else {
Elemer Lelik's avatar
Elemer Lelik committed
148
    fputs(abstract_ ? "ABSTRACT\n" : "", stderr);
Elemer Lelik's avatar
Elemer Lelik committed
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
174
    fputs(attribute_ ? "ATTRIBUTE\n" : "", stderr);

    if (has_aa(this)) {
      if (anyAttributes_.type_ == NamespaceRestriction::NOTHING) {
        fputs("ANY-ATTRIBUTES\n", stderr);
      }
      else for (size_t i = 0; i < anyAttributes_.nElements_; ++i) {
        fprintf(stderr, "ANY-ATTRIBUTES %s %s\n",
          anyAttributes_.type_ == NamespaceRestriction::FROM ? "EXCEPT" : "FROM",
            (anyAttributes_.uris_[i] && *anyAttributes_.uris_[i]) ?
              anyAttributes_.uris_[i] : "ABSENT");
      }
    }

    if (has_ae(this)) {
      if (anyElement_.type_ == NamespaceRestriction::NOTHING) {
        fputs("ANY-ELEMENT\n", stderr);
      }
      else for (size_t i = 0; i < anyElement_.nElements_; ++i) {
        fprintf(stderr, "ANY-ELEMENT %s %s\n",
          anyElement_.type_ == NamespaceRestriction::FROM ? "EXCEPT" : "FROM",
            (anyElement_.uris_[i] && *anyElement_.uris_[i]) ?
              anyElement_.uris_[i] : "ABSENT");
      }
    }
    fputs(base64_ ? "BASE64\n" : "", stderr);
Elemer Lelik's avatar
Elemer Lelik committed
175
    fputs(block_ ? "BLOCK\n" : "", stderr);
Elemer Lelik's avatar
Elemer Lelik committed
176
177
    fputs(decimal_ ? "DECIMAL\n" : "", stderr);

178
179
180
181
    if (defaultForEmpty_)
      fprintf(stderr, "DEFAULT-FOR-EMPTY '%s' %s\n",
        defaultForEmptyIsRef_ ? defaultForEmptyRef_->get_dispname().c_str() : defaultForEmpty_,
        defaultForEmptyIsRef_ ? "(reference) " : "");
Elemer Lelik's avatar
Elemer Lelik committed
182
183
184
185

    if (element_) fputs("ELEMENT\n", stderr);
    fputs(embedValues_ ? "EMBED-VALUES\n" : "", stderr);
    fputs((form_ & QUALIFIED) ? "FORM AS QUALIFIED\n" : "", stderr);
186
    if (has_fractionDigits_) fprintf(stderr, "FRACTIONDIGITS '%i'\n", fractionDigits_);
Elemer Lelik's avatar
Elemer Lelik committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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
    fputs(hex_ ? "hexBinary" : "", stderr);
    fputs(list_ ? "LIST\n" : "", stderr);

    static const char * xforms[] = {
      "CAPITALIZED", "UNCAPITALIZED", "UPPERCASED", "LOWERCASED"
    };
    switch (name_.kw_) {
    case NamespaceSpecification::NO_MANGLING: // nothing to do
      break;
    default: // a string
      fprintf(stderr, "NAME AS '%s'\n", name_.nn_);
      break;
    case NamespaceSpecification::CAPITALIZED:
    case NamespaceSpecification::UNCAPITALIZED:
    case NamespaceSpecification::LOWERCASED:
    case NamespaceSpecification::UPPERCASED:
      fprintf(stderr, "NAME AS %s\n",
        xforms[name_.kw_ - NamespaceSpecification::CAPITALIZED]);
      break;
    }

    if (namespace_.uri) {
      fprintf(stderr, "NAMESPACE '%s' %s %s\n", namespace_.uri,
        (namespace_.prefix ? "PREFIX" : ""),
        (namespace_.prefix ? namespace_.prefix : ""));
    }

    //if (pi_or_comment_.position_ != NOWHERE) {
    //  fputs("PI-OR-COMMENT\n", stderr);
    //}
    if (num_text_) {
      fputs("TEXT\n", stderr);
      for (size_t t=0; t < num_text_; ++t) {
        const char* who = 0, *action = 0;
        switch ((unsigned long)(text_[t].uri) ) {
        case NamespaceSpecification::LOWERCASED:
          action = "LOWERCASED"; break;
        case NamespaceSpecification::UPPERCASED:
          action = "UPPERCASED"; break;
        case NamespaceSpecification::CAPITALIZED:
          action = "CAPITALIZED"; break;
        case NamespaceSpecification::UNCAPITALIZED:
          action = "UNCAPITALIZED"; break;
        case 0:
          action = "text"; break;
        default:
          action = text_[t].uri; break;
        }

        switch ((unsigned long)text_[t].prefix) {
        case 0: who = ""; break;
        case NamespaceSpecification::ALL: who = "ALL"; break;
        default: who = text_[t].prefix; break;
        }
        fprintf(stderr, "   %s as %s\n", who, action);
      }
    }
    fputs(untagged_ ? "UNTAGGED\n" : "", stderr);
    fputs(useNil_ ? "USE-NIL\n" : "", stderr);
    fputs(useNumber_ ? "USE-NUMBER\n" : "", stderr);
    fputs(useOrder_ ? "USE-ORDER\n" : "", stderr);
    fputs(useQName_ ? "USE-QNAME\n" : "", stderr);
    fputs(useType_ ? "USE-TYPE\n" : "", stderr);
    fputs(useUnion_ ? "USE-UNION\n" : "", stderr);
    if (whitespace_ != PRESERVE) fprintf(stderr, "WHITESPACE %s\n",
      whitespace_ == COLLAPSE ? "COLLAPSE" : "REPLACE");
    fputs(". . . . .\n", stderr);
  }
}

XerAttributes& XerAttributes::operator |= (const XerAttributes& other)
{
  if (other.empty()) FATAL_ERROR("XerAttributes::operator |=");
/*
fprintf(stderr, "@@@ replacing:\n");
print("orig.");
other.print("other");
*/
Elemer Lelik's avatar
Elemer Lelik committed
265
  abstract_ |= other.abstract_;
Elemer Lelik's avatar
Elemer Lelik committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  attribute_ |= other.attribute_;
  if (has_aa(&other)) {
    FreeNamespaceRestriction(anyAttributes_);
    anyAttributes_.nElements_ = other.anyAttributes_.nElements_;
    anyAttributes_.type_      = other.anyAttributes_.type_;
    anyAttributes_.uris_      = (char**)Malloc(anyAttributes_.nElements_
      * sizeof(char*));
    for (size_t i=0; i < anyAttributes_.nElements_; ++i) {
      anyAttributes_.uris_[i] = mcopystr(other.anyAttributes_.uris_[i]);
    }
  }
  if (has_ae(&other)) {
    FreeNamespaceRestriction(anyElement_);
    anyElement_.nElements_ = other.anyElement_.nElements_;
    anyElement_.type_      = other.anyElement_.type_;
    anyElement_.uris_      = (char**)Malloc(anyElement_.nElements_
      * sizeof(char*));
    for (size_t i=0; i < anyElement_.nElements_; ++i) {
      anyElement_.uris_[i] = mcopystr(other.anyElement_.uris_[i]);
    }
  }
  base64_ |= other.base64_;
Elemer Lelik's avatar
Elemer Lelik committed
288
  block_ |= other.block_;
Elemer Lelik's avatar
Elemer Lelik committed
289
290
291
292
293
  decimal_ |= other.decimal_;

  if (other.defaultForEmpty_ != 0) {
    Free(defaultForEmpty_);
    defaultForEmpty_ = mcopystr(other.defaultForEmpty_);
294
295
296
    defaultForEmptyIsRef_ = other.defaultForEmptyIsRef_;
  }
  
297
298
  defaultForEmptyIsRef_ |= other.defaultForEmptyIsRef_;
  if (other.defaultForEmptyRef_ != 0) {
299
300
    Free(defaultForEmptyRef_);
    defaultForEmptyRef_ = other.defaultForEmptyRef_->clone();
Elemer Lelik's avatar
Elemer Lelik committed
301
302
303
304
305
  }

  element_ |= other.element_;
  embedValues_ |= other.embedValues_;
  form_ = other.form_;
BenceJanosSzabo's avatar
BenceJanosSzabo committed
306
307
308
309
  if (other.has_fractionDigits_) {
    has_fractionDigits_ = other.has_fractionDigits_;
    fractionDigits_ = other.fractionDigits_;
  }
Elemer Lelik's avatar
Elemer Lelik committed
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
  hex_  |= other.hex_;
  list_ |= other.list_;
  if (other.name_.kw_ != NamespaceSpecification::NO_MANGLING) {
    FreeNameChange(name_);
    switch (other.name_.kw_) {
    case NamespaceSpecification::NO_MANGLING:
      break; // not possible inside the if
    case NamespaceSpecification::CAPITALIZED:
    case NamespaceSpecification::UNCAPITALIZED:
    case NamespaceSpecification::UPPERCASED:
    case NamespaceSpecification::LOWERCASED:
      name_.kw_ = other.name_.kw_;
      break;
    default: // a real string
      name_.nn_ = mcopystr(other.name_.nn_);
      break;
    }
  }

329
330
331
  if (other.namespace_.uri != 0 &&
      // global namespace variants should no longer overwrite inherited namespace variants (issue #545)
      (namespace_.uri == 0 || !other.namespace_.global_variant)) {
Elemer Lelik's avatar
Elemer Lelik committed
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
    switch (namespace_.keyword) {
    case NamespaceSpecification::NO_MANGLING:
    case NamespaceSpecification::CAPITALIZED:
    case NamespaceSpecification::UNCAPITALIZED:
    case NamespaceSpecification::UPPERCASED:
    case NamespaceSpecification::LOWERCASED:
      break; // nothing to do
    default: // real string, must be freed
      Free(namespace_.uri);
      break;
    }
    switch (other.namespace_.keyword) {
    case NamespaceSpecification::NO_MANGLING:
    case NamespaceSpecification::CAPITALIZED:
    case NamespaceSpecification::UNCAPITALIZED:
    case NamespaceSpecification::UPPERCASED:
    case NamespaceSpecification::LOWERCASED:
      namespace_.uri = other.namespace_.uri;
      break;
    default: // real string
      namespace_.uri = mcopystr(other.namespace_.uri);
      break;
    }
    Free(namespace_.prefix);
    namespace_.prefix = mcopystr(other.namespace_.prefix);
  }
  //pi_or_comment_;

  if (other.num_text_) {
    // Append the other TEXT. No attempt is made to eliminate duplicates.
    // This will be done in Type::chk_xer_text().
    size_t old_num = num_text_;
    num_text_ += other.num_text_;
    text_ = (NamespaceSpecification *)Realloc(
      text_, num_text_ * sizeof(NamespaceSpecification));
    for (size_t t = 0; t < other.num_text_; ++t) {
      switch ((unsigned long)(other.text_[t].uri) ) {
      case NamespaceSpecification::LOWERCASED:
      case NamespaceSpecification::UPPERCASED:
      case NamespaceSpecification::CAPITALIZED:
      case NamespaceSpecification::UNCAPITALIZED:
      case NamespaceSpecification::NO_MANGLING:
        text_[old_num + t].uri = other.text_[t].uri;
        break;
      default:
        text_[old_num + t].uri = mcopystr(other.text_[t].uri);
        break;
      }

      switch ((unsigned long)other.text_[t].prefix) {
      case 0: case NamespaceSpecification::ALL:
        text_[old_num + t].prefix = other.text_[t].prefix;
        break;
      default:
        text_[old_num + t].prefix = mcopystr(other.text_[t].prefix);
        break;
      }
    }
  }
  untagged_ |= other.untagged_;
  useNil_ |= other.useNil_;
  useNumber_ |= other.useNumber_;
  useOrder_ |= other.useOrder_;
  useQName_ |= other.useQName_;
  useType_ |= other.useType_;
  useUnion_ |= other.useUnion_;
  whitespace_ = other.whitespace_;
399
400
401
402

  if (other.xsd_type != XSD_NONE) {
    xsd_type = other.xsd_type;
  }
Elemer Lelik's avatar
Elemer Lelik committed
403
404
405
406
407
  return *this;
}

bool XerAttributes::empty() const
{
Elemer Lelik's avatar
Elemer Lelik committed
408
409
  return !abstract_
  && !attribute_
Elemer Lelik's avatar
Elemer Lelik committed
410
411
412
  && !has_aa(this)
  && !has_ae(this)
  && !base64_
Elemer Lelik's avatar
Elemer Lelik committed
413
  && !block_
Elemer Lelik's avatar
Elemer Lelik committed
414
415
  && !decimal_
  && defaultForEmpty_ == 0
416
417
  && !defaultForEmptyIsRef_
  && !defaultForEmptyRef_
Elemer Lelik's avatar
Elemer Lelik committed
418
419
420
  && !element_
  && !embedValues_
  && !(form_ & LOCALLY_SET)
421
  && !has_fractionDigits_
Elemer Lelik's avatar
Elemer Lelik committed
422
423
424
425
426
427
428
429
430
431
432
433
  && !hex_
  && !list_
  && name_.kw_ == NamespaceSpecification::NO_MANGLING
  && namespace_.uri == 0
  && num_text_ == 0
  && !untagged_
  && !useNil_
  && !useNumber_
  && !useOrder_
  && !useQName_
  && !useType_
  && !useUnion_
434
435
  && whitespace_ == PRESERVE
  && xsd_type == XSD_NONE;
Elemer Lelik's avatar
Elemer Lelik committed
436
437
}