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