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