1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2014 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()
29 , anyAttributes_(empty_nsr
)
30 , anyElement_(empty_nsr
)
52 , whitespace_(PRESERVE
)
55 //fprintf(stderr, "XER attributes(%p) new\n", (void*)this);
58 void FreeNamespaceRestriction(NamespaceRestriction
& nsr
)
60 for (size_t i
=0; i
< nsr
.nElements_
; ++i
) {
66 XerAttributes::~XerAttributes()
68 FreeNamespaceRestriction(anyAttributes_
);
69 FreeNamespaceRestriction(anyElement_
);
71 Free(defaultForEmpty_
);
74 FreeNameChange(name_
);
75 FreeNamespace(namespace_
);
77 //if (pi_or_comment_.position_ > AFTER_VALUE) {
78 // Free(pi_or_comment_.value_);
81 for (size_t i
=0; i
< num_text_
; ++i
) {
82 if ((unsigned long)text_
[i
].keyword
83 > (unsigned long)NamespaceSpecification::LOWERCASED
) {
86 if (text_
[i
].prefix
> (char*)NamespaceSpecification::ALL
) {
87 Free(text_
[i
].prefix
);
93 void XerAttributes::FreeNamespace(NamespaceSpecification
&ns
) {
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
110 void XerAttributes::FreeNameChange(XerAttributes::NameChange
& n
) {
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
122 n
.kw_
= NamespaceSpecification::NO_MANGLING
;
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
);
130 fputs(attribute_
? "ATTRIBUTE\n" : "", stderr
);
133 if (anyAttributes_
.type_
== NamespaceRestriction::NOTHING
) {
134 fputs("ANY-ATTRIBUTES\n", stderr
);
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");
145 if (anyElement_
.type_
== NamespaceRestriction::NOTHING
) {
146 fputs("ANY-ELEMENT\n", stderr
);
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");
155 fputs(base64_
? "BASE64\n" : "", stderr
);
156 fputs(decimal_
? "DECIMAL\n" : "", stderr
);
158 if (defaultForEmpty_
) fprintf(stderr
, "DEFAULT-FOR-EMPTY '%s'\n", defaultForEmpty_
);
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
);
166 static const char * xforms
[] = {
167 "CAPITALIZED", "UNCAPITALIZED", "UPPERCASED", "LOWERCASED"
170 case NamespaceSpecification::NO_MANGLING
: // nothing to do
173 fprintf(stderr
, "NAME AS '%s'\n", name_
.nn_
);
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
]);
184 if (namespace_
.uri
) {
185 fprintf(stderr
, "NAMESPACE '%s' %s %s\n", namespace_
.uri
,
186 (namespace_
.prefix
? "PREFIX" : ""),
187 (namespace_
.prefix
? namespace_
.prefix
: ""));
190 //if (pi_or_comment_.position_ != NOWHERE) {
191 // fputs("PI-OR-COMMENT\n", stderr);
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;
207 action
= "text"; break;
209 action
= text_
[t
].uri
; break;
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;
217 fprintf(stderr
, " %s as %s\n", who
, action
);
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
);
233 XerAttributes
& XerAttributes::operator |= (const XerAttributes
& other
)
235 if (other
.empty()) FATAL_ERROR("XerAttributes::operator |=");
237 fprintf(stderr, "@@@ replacing:\n");
239 other.print("other");
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_
248 for (size_t i
=0; i
< anyAttributes_
.nElements_
; ++i
) {
249 anyAttributes_
.uris_
[i
] = mcopystr(other
.anyAttributes_
.uris_
[i
]);
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_
258 for (size_t i
=0; i
< anyElement_
.nElements_
; ++i
) {
259 anyElement_
.uris_
[i
] = mcopystr(other
.anyElement_
.uris_
[i
]);
262 base64_
|= other
.base64_
;
263 decimal_
|= other
.decimal_
;
265 if (other
.defaultForEmpty_
!= 0) {
266 Free(defaultForEmpty_
);
267 defaultForEmpty_
= mcopystr(other
.defaultForEmpty_
);
270 element_
|= other
.element_
;
271 embedValues_
|= other
.embedValues_
;
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_
;
286 default: // a real string
287 name_
.nn_
= mcopystr(other
.name_
.nn_
);
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
);
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
;
312 default: // real string
313 namespace_
.uri
= mcopystr(other
.namespace_
.uri
);
316 Free(namespace_
.prefix
);
317 namespace_
.prefix
= mcopystr(other
.namespace_
.prefix
);
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
;
338 text_
[old_num
+ t
].uri
= mcopystr(other
.text_
[t
].uri
);
342 switch ((unsigned long)other
.text_
[t
].prefix
) {
343 case 0: case NamespaceSpecification::ALL
:
344 text_
[old_num
+ t
].prefix
= other
.text_
[t
].prefix
;
347 text_
[old_num
+ t
].prefix
= mcopystr(other
.text_
[t
].prefix
);
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_
;
363 bool XerAttributes::empty() const
370 && defaultForEmpty_
== 0
373 && !(form_
& LOCALLY_SET
)
376 && name_
.kw_
== NamespaceSpecification::NO_MANGLING
377 && namespace_
.uri
== 0
386 && whitespace_
== PRESERVE
;