1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2015 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 ///////////////////////////////////////////////////////////////////////////////
11 * Created on: Oct 17, 2008
15 #include "XerAttributes.hh"
16 // The above line must be the first in this file
17 #include "../common/memory.h"
21 static const NamespaceRestriction empty_nsr
= {
22 0, 0, NamespaceRestriction::UNUSED
25 static const XerAttributes::NameChange nochange
= { NamespaceSpecification::NO_MANGLING
};
27 XerAttributes::XerAttributes()
30 , anyAttributes_(empty_nsr
)
31 , anyElement_(empty_nsr
)
54 , whitespace_(PRESERVE
)
57 //fprintf(stderr, "XER attributes(%p) new\n", (void*)this);
60 void FreeNamespaceRestriction(NamespaceRestriction
& nsr
)
62 for (size_t i
=0; i
< nsr
.nElements_
; ++i
) {
68 XerAttributes::~XerAttributes()
70 FreeNamespaceRestriction(anyAttributes_
);
71 FreeNamespaceRestriction(anyElement_
);
73 Free(defaultForEmpty_
);
76 FreeNameChange(name_
);
77 FreeNamespace(namespace_
);
79 //if (pi_or_comment_.position_ > AFTER_VALUE) {
80 // Free(pi_or_comment_.value_);
83 for (size_t i
=0; i
< num_text_
; ++i
) {
84 if ((unsigned long)text_
[i
].keyword
85 > (unsigned long)NamespaceSpecification::LOWERCASED
) {
88 if (text_
[i
].prefix
> (char*)NamespaceSpecification::ALL
) {
89 Free(text_
[i
].prefix
);
95 void XerAttributes::FreeNamespace(NamespaceSpecification
&ns
) {
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
112 void XerAttributes::FreeNameChange(XerAttributes::NameChange
& n
) {
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
124 n
.kw_
= NamespaceSpecification::NO_MANGLING
;
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
);
132 fputs(abstract_
? "ABSTRACT\n" : "", stderr
);
133 fputs(attribute_
? "ATTRIBUTE\n" : "", stderr
);
136 if (anyAttributes_
.type_
== NamespaceRestriction::NOTHING
) {
137 fputs("ANY-ATTRIBUTES\n", stderr
);
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");
148 if (anyElement_
.type_
== NamespaceRestriction::NOTHING
) {
149 fputs("ANY-ELEMENT\n", stderr
);
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");
158 fputs(base64_
? "BASE64\n" : "", stderr
);
159 fputs(block_
? "BLOCK\n" : "", stderr
);
160 fputs(decimal_
? "DECIMAL\n" : "", stderr
);
162 if (defaultForEmpty_
) fprintf(stderr
, "DEFAULT-FOR-EMPTY '%s'\n", defaultForEmpty_
);
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
);
170 static const char * xforms
[] = {
171 "CAPITALIZED", "UNCAPITALIZED", "UPPERCASED", "LOWERCASED"
174 case NamespaceSpecification::NO_MANGLING
: // nothing to do
177 fprintf(stderr
, "NAME AS '%s'\n", name_
.nn_
);
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
]);
188 if (namespace_
.uri
) {
189 fprintf(stderr
, "NAMESPACE '%s' %s %s\n", namespace_
.uri
,
190 (namespace_
.prefix
? "PREFIX" : ""),
191 (namespace_
.prefix
? namespace_
.prefix
: ""));
194 //if (pi_or_comment_.position_ != NOWHERE) {
195 // fputs("PI-OR-COMMENT\n", stderr);
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;
211 action
= "text"; break;
213 action
= text_
[t
].uri
; break;
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;
221 fprintf(stderr
, " %s as %s\n", who
, action
);
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
);
237 XerAttributes
& XerAttributes::operator |= (const XerAttributes
& other
)
239 if (other
.empty()) FATAL_ERROR("XerAttributes::operator |=");
241 fprintf(stderr, "@@@ replacing:\n");
243 other.print("other");
245 abstract_
|= other
.abstract_
;
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_
253 for (size_t i
=0; i
< anyAttributes_
.nElements_
; ++i
) {
254 anyAttributes_
.uris_
[i
] = mcopystr(other
.anyAttributes_
.uris_
[i
]);
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_
263 for (size_t i
=0; i
< anyElement_
.nElements_
; ++i
) {
264 anyElement_
.uris_
[i
] = mcopystr(other
.anyElement_
.uris_
[i
]);
267 base64_
|= other
.base64_
;
268 block_
|= other
.block_
;
269 decimal_
|= other
.decimal_
;
271 if (other
.defaultForEmpty_
!= 0) {
272 Free(defaultForEmpty_
);
273 defaultForEmpty_
= mcopystr(other
.defaultForEmpty_
);
276 element_
|= other
.element_
;
277 embedValues_
|= other
.embedValues_
;
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_
;
292 default: // a real string
293 name_
.nn_
= mcopystr(other
.name_
.nn_
);
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
);
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
;
318 default: // real string
319 namespace_
.uri
= mcopystr(other
.namespace_
.uri
);
322 Free(namespace_
.prefix
);
323 namespace_
.prefix
= mcopystr(other
.namespace_
.prefix
);
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
;
344 text_
[old_num
+ t
].uri
= mcopystr(other
.text_
[t
].uri
);
348 switch ((unsigned long)other
.text_
[t
].prefix
) {
349 case 0: case NamespaceSpecification::ALL
:
350 text_
[old_num
+ t
].prefix
= other
.text_
[t
].prefix
;
353 text_
[old_num
+ t
].prefix
= mcopystr(other
.text_
[t
].prefix
);
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_
;
369 bool XerAttributes::empty() const
378 && defaultForEmpty_
== 0
381 && !(form_
& LOCALLY_SET
)
384 && name_
.kw_
== NamespaceSpecification::NO_MANGLING
385 && namespace_
.uri
== 0
394 && whitespace_
== PRESERVE
;