Commit | Line | Data |
---|---|---|
d44e3c4f | 1 | /****************************************************************************** |
2 | * Copyright (c) 2000-2016 Ericsson Telecom AB | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * Balasko, Jeno | |
10 | * Baranyi, Botond | |
11 | * Raduly, Csaba | |
12 | * Szabados, Kristof | |
13 | * | |
14 | ******************************************************************************/ | |
970ed795 EL |
15 | /* |
16 | * XerAttributes.cpp | |
17 | * | |
18 | * Created on: Oct 17, 2008 | |
19 | * Author: ecsardu | |
20 | */ | |
21 | ||
22 | #include "XerAttributes.hh" | |
23 | // The above line must be the first in this file | |
24 | #include "../common/memory.h" | |
25 | ||
26 | #include "Value.hh" | |
27 | ||
28 | static const NamespaceRestriction empty_nsr = { | |
29 | 0, 0, NamespaceRestriction::UNUSED | |
30 | }; | |
31 | ||
32 | static const XerAttributes::NameChange nochange= { NamespaceSpecification::NO_MANGLING }; | |
33 | ||
34 | XerAttributes::XerAttributes() | |
51fa56b9 | 35 | : abstract_(false) |
36 | , attribute_(false) | |
970ed795 EL |
37 | , anyAttributes_(empty_nsr) |
38 | , anyElement_(empty_nsr) | |
39 | , base64_(false) | |
51fa56b9 | 40 | , block_(false) |
970ed795 EL |
41 | , decimal_(false) |
42 | , defaultForEmpty_(0) | |
43 | , defaultValue_(0) | |
44 | , element_(false) | |
45 | , embedValues_(false) | |
46 | , form_(UNSET) | |
47 | , hex_(false) | |
48 | , list_(false) | |
49 | , name_(nochange) | |
50 | , namespace_() | |
51 | //, pi_or_comment_() | |
52 | , num_text_(0) | |
53 | , text_(0) | |
54 | , untagged_(false) | |
55 | , useNil_(false) | |
56 | , useNumber_(false) | |
57 | , useOrder_(false) | |
58 | , useQName_(false) | |
59 | , useType_(false) | |
60 | , useUnion_(false) | |
61 | , whitespace_(PRESERVE) | |
62 | { | |
63 | //__asm("int3"); | |
64 | //fprintf(stderr, "XER attributes(%p) new\n", (void*)this); | |
65 | } | |
66 | ||
67 | void FreeNamespaceRestriction(NamespaceRestriction& nsr) | |
68 | { | |
69 | for (size_t i=0; i < nsr.nElements_; ++i) { | |
70 | Free(nsr.uris_[i]); | |
71 | } | |
72 | Free(nsr.uris_); | |
73 | } | |
74 | ||
75 | XerAttributes::~XerAttributes() | |
76 | { | |
77 | FreeNamespaceRestriction(anyAttributes_); | |
78 | FreeNamespaceRestriction(anyElement_); | |
79 | ||
80 | Free(defaultForEmpty_); | |
81 | delete defaultValue_; | |
82 | ||
83 | FreeNameChange(name_); | |
84 | FreeNamespace(namespace_); | |
85 | ||
86 | //if (pi_or_comment_.position_ > AFTER_VALUE) { | |
87 | // Free(pi_or_comment_.value_); | |
88 | //} | |
89 | ||
90 | for (size_t i=0; i < num_text_; ++i) { | |
91 | if ((unsigned long)text_[i].keyword | |
92 | > (unsigned long)NamespaceSpecification::LOWERCASED) { | |
93 | Free(text_[i].uri); | |
94 | } | |
95 | if (text_[i].prefix > (char*)NamespaceSpecification::ALL) { | |
96 | Free(text_[i].prefix); | |
97 | } | |
98 | } | |
99 | Free(text_); | |
100 | } | |
101 | ||
102 | void XerAttributes::FreeNamespace(NamespaceSpecification &ns) { | |
103 | switch (ns.keyword) { | |
104 | case NamespaceSpecification::NO_MANGLING: | |
105 | case NamespaceSpecification::CAPITALIZED: | |
106 | case NamespaceSpecification::UNCAPITALIZED: | |
107 | case NamespaceSpecification::UPPERCASED: | |
108 | case NamespaceSpecification::LOWERCASED: | |
109 | break; // nothing to do | |
110 | default: // real string, must be freed | |
111 | Free(ns.uri); | |
112 | break; | |
113 | } | |
114 | ns.uri = 0; | |
115 | Free(ns.prefix); | |
116 | ns.prefix = 0; | |
117 | } | |
118 | ||
119 | void XerAttributes::FreeNameChange(XerAttributes::NameChange& n) { | |
120 | switch (n.kw_) { | |
121 | case NamespaceSpecification::NO_MANGLING: | |
122 | case NamespaceSpecification::CAPITALIZED: | |
123 | case NamespaceSpecification::UNCAPITALIZED: | |
124 | case NamespaceSpecification::UPPERCASED: | |
125 | case NamespaceSpecification::LOWERCASED: | |
126 | break; // nothing to do | |
127 | default: // real string, must be freed | |
128 | Free(n.nn_); | |
129 | break; | |
130 | } | |
131 | n.kw_ = NamespaceSpecification::NO_MANGLING; | |
132 | } | |
133 | ||
134 | ||
135 | void XerAttributes::print(const char *type_name) const { | |
136 | fprintf(stderr, "XER attributes(%p) for %s:\n", (const void*)this, type_name); | |
137 | if (empty()) fputs("...Empty...\n", stderr); | |
138 | else { | |
51fa56b9 | 139 | fputs(abstract_ ? "ABSTRACT\n" : "", stderr); |
970ed795 EL |
140 | fputs(attribute_ ? "ATTRIBUTE\n" : "", stderr); |
141 | ||
142 | if (has_aa(this)) { | |
143 | if (anyAttributes_.type_ == NamespaceRestriction::NOTHING) { | |
144 | fputs("ANY-ATTRIBUTES\n", stderr); | |
145 | } | |
146 | else for (size_t i = 0; i < anyAttributes_.nElements_; ++i) { | |
147 | fprintf(stderr, "ANY-ATTRIBUTES %s %s\n", | |
148 | anyAttributes_.type_ == NamespaceRestriction::FROM ? "EXCEPT" : "FROM", | |
149 | (anyAttributes_.uris_[i] && *anyAttributes_.uris_[i]) ? | |
150 | anyAttributes_.uris_[i] : "ABSENT"); | |
151 | } | |
152 | } | |
153 | ||
154 | if (has_ae(this)) { | |
155 | if (anyElement_.type_ == NamespaceRestriction::NOTHING) { | |
156 | fputs("ANY-ELEMENT\n", stderr); | |
157 | } | |
158 | else for (size_t i = 0; i < anyElement_.nElements_; ++i) { | |
159 | fprintf(stderr, "ANY-ELEMENT %s %s\n", | |
160 | anyElement_.type_ == NamespaceRestriction::FROM ? "EXCEPT" : "FROM", | |
161 | (anyElement_.uris_[i] && *anyElement_.uris_[i]) ? | |
162 | anyElement_.uris_[i] : "ABSENT"); | |
163 | } | |
164 | } | |
165 | fputs(base64_ ? "BASE64\n" : "", stderr); | |
51fa56b9 | 166 | fputs(block_ ? "BLOCK\n" : "", stderr); |
970ed795 EL |
167 | fputs(decimal_ ? "DECIMAL\n" : "", stderr); |
168 | ||
169 | if (defaultForEmpty_) fprintf(stderr, "DEFAULT-FOR-EMPTY '%s'\n", defaultForEmpty_); | |
170 | ||
171 | if (element_) fputs("ELEMENT\n", stderr); | |
172 | fputs(embedValues_ ? "EMBED-VALUES\n" : "", stderr); | |
173 | fputs((form_ & QUALIFIED) ? "FORM AS QUALIFIED\n" : "", stderr); | |
174 | fputs(hex_ ? "hexBinary" : "", stderr); | |
175 | fputs(list_ ? "LIST\n" : "", stderr); | |
176 | ||
177 | static const char * xforms[] = { | |
178 | "CAPITALIZED", "UNCAPITALIZED", "UPPERCASED", "LOWERCASED" | |
179 | }; | |
180 | switch (name_.kw_) { | |
181 | case NamespaceSpecification::NO_MANGLING: // nothing to do | |
182 | break; | |
183 | default: // a string | |
184 | fprintf(stderr, "NAME AS '%s'\n", name_.nn_); | |
185 | break; | |
186 | case NamespaceSpecification::CAPITALIZED: | |
187 | case NamespaceSpecification::UNCAPITALIZED: | |
188 | case NamespaceSpecification::LOWERCASED: | |
189 | case NamespaceSpecification::UPPERCASED: | |
190 | fprintf(stderr, "NAME AS %s\n", | |
191 | xforms[name_.kw_ - NamespaceSpecification::CAPITALIZED]); | |
192 | break; | |
193 | } | |
194 | ||
195 | if (namespace_.uri) { | |
196 | fprintf(stderr, "NAMESPACE '%s' %s %s\n", namespace_.uri, | |
197 | (namespace_.prefix ? "PREFIX" : ""), | |
198 | (namespace_.prefix ? namespace_.prefix : "")); | |
199 | } | |
200 | ||
201 | //if (pi_or_comment_.position_ != NOWHERE) { | |
202 | // fputs("PI-OR-COMMENT\n", stderr); | |
203 | //} | |
204 | if (num_text_) { | |
205 | fputs("TEXT\n", stderr); | |
206 | for (size_t t=0; t < num_text_; ++t) { | |
207 | const char* who = 0, *action = 0; | |
208 | switch ((unsigned long)(text_[t].uri) ) { | |
209 | case NamespaceSpecification::LOWERCASED: | |
210 | action = "LOWERCASED"; break; | |
211 | case NamespaceSpecification::UPPERCASED: | |
212 | action = "UPPERCASED"; break; | |
213 | case NamespaceSpecification::CAPITALIZED: | |
214 | action = "CAPITALIZED"; break; | |
215 | case NamespaceSpecification::UNCAPITALIZED: | |
216 | action = "UNCAPITALIZED"; break; | |
217 | case 0: | |
218 | action = "text"; break; | |
219 | default: | |
220 | action = text_[t].uri; break; | |
221 | } | |
222 | ||
223 | switch ((unsigned long)text_[t].prefix) { | |
224 | case 0: who = ""; break; | |
225 | case NamespaceSpecification::ALL: who = "ALL"; break; | |
226 | default: who = text_[t].prefix; break; | |
227 | } | |
228 | fprintf(stderr, " %s as %s\n", who, action); | |
229 | } | |
230 | } | |
231 | fputs(untagged_ ? "UNTAGGED\n" : "", stderr); | |
232 | fputs(useNil_ ? "USE-NIL\n" : "", stderr); | |
233 | fputs(useNumber_ ? "USE-NUMBER\n" : "", stderr); | |
234 | fputs(useOrder_ ? "USE-ORDER\n" : "", stderr); | |
235 | fputs(useQName_ ? "USE-QNAME\n" : "", stderr); | |
236 | fputs(useType_ ? "USE-TYPE\n" : "", stderr); | |
237 | fputs(useUnion_ ? "USE-UNION\n" : "", stderr); | |
238 | if (whitespace_ != PRESERVE) fprintf(stderr, "WHITESPACE %s\n", | |
239 | whitespace_ == COLLAPSE ? "COLLAPSE" : "REPLACE"); | |
240 | fputs(". . . . .\n", stderr); | |
241 | } | |
242 | } | |
243 | ||
244 | XerAttributes& XerAttributes::operator |= (const XerAttributes& other) | |
245 | { | |
246 | if (other.empty()) FATAL_ERROR("XerAttributes::operator |="); | |
247 | /* | |
248 | fprintf(stderr, "@@@ replacing:\n"); | |
249 | print("orig."); | |
250 | other.print("other"); | |
251 | */ | |
51fa56b9 | 252 | abstract_ |= other.abstract_; |
970ed795 EL |
253 | attribute_ |= other.attribute_; |
254 | if (has_aa(&other)) { | |
255 | FreeNamespaceRestriction(anyAttributes_); | |
256 | anyAttributes_.nElements_ = other.anyAttributes_.nElements_; | |
257 | anyAttributes_.type_ = other.anyAttributes_.type_; | |
258 | anyAttributes_.uris_ = (char**)Malloc(anyAttributes_.nElements_ | |
259 | * sizeof(char*)); | |
260 | for (size_t i=0; i < anyAttributes_.nElements_; ++i) { | |
261 | anyAttributes_.uris_[i] = mcopystr(other.anyAttributes_.uris_[i]); | |
262 | } | |
263 | } | |
264 | if (has_ae(&other)) { | |
265 | FreeNamespaceRestriction(anyElement_); | |
266 | anyElement_.nElements_ = other.anyElement_.nElements_; | |
267 | anyElement_.type_ = other.anyElement_.type_; | |
268 | anyElement_.uris_ = (char**)Malloc(anyElement_.nElements_ | |
269 | * sizeof(char*)); | |
270 | for (size_t i=0; i < anyElement_.nElements_; ++i) { | |
271 | anyElement_.uris_[i] = mcopystr(other.anyElement_.uris_[i]); | |
272 | } | |
273 | } | |
274 | base64_ |= other.base64_; | |
51fa56b9 | 275 | block_ |= other.block_; |
970ed795 EL |
276 | decimal_ |= other.decimal_; |
277 | ||
278 | if (other.defaultForEmpty_ != 0) { | |
279 | Free(defaultForEmpty_); | |
280 | defaultForEmpty_ = mcopystr(other.defaultForEmpty_); | |
281 | } | |
282 | ||
283 | element_ |= other.element_; | |
284 | embedValues_ |= other.embedValues_; | |
285 | form_ = other.form_; | |
286 | hex_ |= other.hex_; | |
287 | list_ |= other.list_; | |
288 | if (other.name_.kw_ != NamespaceSpecification::NO_MANGLING) { | |
289 | FreeNameChange(name_); | |
290 | switch (other.name_.kw_) { | |
291 | case NamespaceSpecification::NO_MANGLING: | |
292 | break; // not possible inside the if | |
293 | case NamespaceSpecification::CAPITALIZED: | |
294 | case NamespaceSpecification::UNCAPITALIZED: | |
295 | case NamespaceSpecification::UPPERCASED: | |
296 | case NamespaceSpecification::LOWERCASED: | |
297 | name_.kw_ = other.name_.kw_; | |
298 | break; | |
299 | default: // a real string | |
300 | name_.nn_ = mcopystr(other.name_.nn_); | |
301 | break; | |
302 | } | |
303 | } | |
304 | ||
305 | if (other.namespace_.uri != 0) { | |
306 | switch (namespace_.keyword) { | |
307 | case NamespaceSpecification::NO_MANGLING: | |
308 | case NamespaceSpecification::CAPITALIZED: | |
309 | case NamespaceSpecification::UNCAPITALIZED: | |
310 | case NamespaceSpecification::UPPERCASED: | |
311 | case NamespaceSpecification::LOWERCASED: | |
312 | break; // nothing to do | |
313 | default: // real string, must be freed | |
314 | Free(namespace_.uri); | |
315 | break; | |
316 | } | |
317 | switch (other.namespace_.keyword) { | |
318 | case NamespaceSpecification::NO_MANGLING: | |
319 | case NamespaceSpecification::CAPITALIZED: | |
320 | case NamespaceSpecification::UNCAPITALIZED: | |
321 | case NamespaceSpecification::UPPERCASED: | |
322 | case NamespaceSpecification::LOWERCASED: | |
323 | namespace_.uri = other.namespace_.uri; | |
324 | break; | |
325 | default: // real string | |
326 | namespace_.uri = mcopystr(other.namespace_.uri); | |
327 | break; | |
328 | } | |
329 | Free(namespace_.prefix); | |
330 | namespace_.prefix = mcopystr(other.namespace_.prefix); | |
331 | } | |
332 | //pi_or_comment_; | |
333 | ||
334 | if (other.num_text_) { | |
335 | // Append the other TEXT. No attempt is made to eliminate duplicates. | |
336 | // This will be done in Type::chk_xer_text(). | |
337 | size_t old_num = num_text_; | |
338 | num_text_ += other.num_text_; | |
339 | text_ = (NamespaceSpecification *)Realloc( | |
340 | text_, num_text_ * sizeof(NamespaceSpecification)); | |
341 | for (size_t t = 0; t < other.num_text_; ++t) { | |
342 | switch ((unsigned long)(other.text_[t].uri) ) { | |
343 | case NamespaceSpecification::LOWERCASED: | |
344 | case NamespaceSpecification::UPPERCASED: | |
345 | case NamespaceSpecification::CAPITALIZED: | |
346 | case NamespaceSpecification::UNCAPITALIZED: | |
347 | case NamespaceSpecification::NO_MANGLING: | |
348 | text_[old_num + t].uri = other.text_[t].uri; | |
349 | break; | |
350 | default: | |
351 | text_[old_num + t].uri = mcopystr(other.text_[t].uri); | |
352 | break; | |
353 | } | |
354 | ||
355 | switch ((unsigned long)other.text_[t].prefix) { | |
356 | case 0: case NamespaceSpecification::ALL: | |
357 | text_[old_num + t].prefix = other.text_[t].prefix; | |
358 | break; | |
359 | default: | |
360 | text_[old_num + t].prefix = mcopystr(other.text_[t].prefix); | |
361 | break; | |
362 | } | |
363 | } | |
364 | } | |
365 | untagged_ |= other.untagged_; | |
366 | useNil_ |= other.useNil_; | |
367 | useNumber_ |= other.useNumber_; | |
368 | useOrder_ |= other.useOrder_; | |
369 | useQName_ |= other.useQName_; | |
370 | useType_ |= other.useType_; | |
371 | useUnion_ |= other.useUnion_; | |
372 | whitespace_ = other.whitespace_; | |
373 | return *this; | |
374 | } | |
375 | ||
376 | bool XerAttributes::empty() const | |
377 | { | |
51fa56b9 | 378 | return !abstract_ |
379 | && !attribute_ | |
970ed795 EL |
380 | && !has_aa(this) |
381 | && !has_ae(this) | |
382 | && !base64_ | |
51fa56b9 | 383 | && !block_ |
970ed795 EL |
384 | && !decimal_ |
385 | && defaultForEmpty_ == 0 | |
386 | && !element_ | |
387 | && !embedValues_ | |
388 | && !(form_ & LOCALLY_SET) | |
389 | && !hex_ | |
390 | && !list_ | |
391 | && name_.kw_ == NamespaceSpecification::NO_MANGLING | |
392 | && namespace_.uri == 0 | |
393 | && num_text_ == 0 | |
394 | && !untagged_ | |
395 | && !useNil_ | |
396 | && !useNumber_ | |
397 | && !useOrder_ | |
398 | && !useQName_ | |
399 | && !useType_ | |
400 | && !useUnion_ | |
401 | && whitespace_ == PRESERVE; | |
402 | } | |
403 |