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 | * | |
13 | ******************************************************************************/ | |
970ed795 EL |
14 | /* |
15 | * XerAttributes.h | |
16 | * | |
17 | * Created on: Oct 17, 2008 | |
18 | * Author: ecsardu | |
19 | */ | |
20 | ||
21 | #ifndef XERATTRIBUTES_H_ | |
22 | #define XERATTRIBUTES_H_ | |
23 | ||
24 | #include <stddef.h> | |
25 | #include <limits.h> | |
26 | #include "../common/memory.h" | |
27 | ||
28 | namespace Common { | |
29 | class Value; | |
30 | } | |
31 | ||
32 | struct NamespaceRestriction // union member, no constructor allowed | |
33 | { | |
34 | size_t nElements_; | |
35 | /** URIs for ANY-ATTRIBUTES and ANY-ELEMENT. NULL is used to represent | |
36 | * ABSENT */ | |
37 | char ** uris_; | |
38 | enum { | |
39 | UNUSED, | |
40 | NOTHING, ///< anyElement | |
41 | FROM, ///< anyElement from ... | |
42 | EXCEPT ///< anyElement except ... | |
43 | } type_; | |
44 | }; | |
45 | // #define ABSENT ((char*)5) just use NULL | |
46 | ||
47 | void FreeNamespaceRestriction(NamespaceRestriction& nsr); | |
48 | ||
49 | ||
50 | struct NamespaceSpecification | |
51 | { | |
52 | /** How to transform the ASN/TTCN name to XML name */ | |
53 | enum NameMangling { | |
54 | NO_MANGLING, ///< no change | |
55 | CAPITALIZED, ///< change the first letter to uppercase | |
56 | UNCAPITALIZED,///< change the first letter to lowercase | |
57 | UPPERCASED, ///< change all letters to uppercase | |
58 | LOWERCASED, ///< change all letters to lowercase | |
59 | // The underlying type of the enum is an integral type that can cover | |
60 | // all the enum values. Using a value which doesn't fit into signed int, | |
61 | // we try to force the underlying type to be _unsigned_ int. | |
62 | FORCE_UNSIGNED = UINT_MAX | |
63 | }; | |
64 | enum { ALL = LOWERCASED+1 }; // should be one bigger than the last of NameMangling | |
65 | ||
66 | /** The URI for the NAMESPACE encoding instruction, or TextToBeUsed for TEXT. | |
67 | * May be a valid pointer or any of the NameMangling enum values, | |
68 | * or NULL for "text". | |
69 | * For NAMESPACE, NULL (NO_MANGLING) means that no NAMESPACE instruction | |
70 | * is assigned directly; UNCAPITALIZED means that an explicit "cancel | |
71 | * all namespaces" instruction was applied: "namespace '' prefix ''" */ | |
72 | union { | |
73 | char * uri; // for NAMESPACE | |
74 | char * new_text; // for TEXT | |
75 | NameMangling keyword; // for TEXT | |
76 | }; | |
77 | /** The prefix for the NAME encoding instruction or the Target for TEXT. | |
78 | * May be XerAttributes::ALL for "text all as ...cased", or 0 for "text" */ | |
79 | union { | |
80 | char * prefix; // for NAMESPACE | |
81 | char * target; // for TEXT | |
82 | }; | |
83 | }; | |
84 | ||
85 | inline void free_name_or_kw(char *s) | |
86 | { | |
87 | if (s > (char*)NamespaceSpecification::ALL) { | |
88 | Free(s); | |
89 | } | |
90 | } | |
91 | ||
92 | /** XER attributes during compilation. | |
93 | * | |
94 | * @todo members are ordered alphabetically, this should be changed to | |
95 | * be more space efficient (pointers, then integers, then booleans) | |
96 | */ | |
97 | class XerAttributes { | |
98 | private: | |
99 | // Compiler-generated copy constructor and assignment NOT safe. | |
100 | // Must disable or implement own. | |
101 | XerAttributes(const XerAttributes&); | |
102 | XerAttributes& operator=(const XerAttributes&); | |
103 | public: | |
104 | //enum PI_Location { NOWHERE, BEFORE_TAG, BEFORE_VALUE, AFTER_TAG, AFTER_VALUE }; | |
105 | enum WhitespaceAction { PRESERVE, REPLACE, COLLAPSE }; | |
106 | enum Form { // a bit mask | |
107 | UNSET = 0, | |
108 | // Global defaults for the module: | |
109 | ELEMENT_DEFAULT_QUALIFIED = 1, | |
110 | ATTRIBUTE_DEFAULT_QUALIFIED = 2, | |
111 | // Locally set values (applied directly to the type or field) | |
112 | UNQUALIFIED = 4, //< "form as unqualified" | |
113 | QUALIFIED = 8, //< "form as qualified" | |
114 | LOCALLY_SET = (QUALIFIED | UNQUALIFIED) | |
115 | }; | |
116 | ||
117 | /** Name change action. | |
118 | * | |
119 | * When importing XSD into ASN.1 or TTCN-3, some names might need to be | |
120 | * changed to protect the guilty (make them valid ASN.1/TTCN.3). | |
121 | * The NAME encoding instruction describes how to get back to the XML | |
122 | * tag name. The original name may be retrieved with one of the "canned" actions | |
123 | * (change the case of the first or all letters) or by storing the original | |
124 | * name in full (usually if characters were appended to remove name clashes). | |
125 | * | |
126 | * This union and the related code makes the assumption that not only | |
127 | * 0, but 1,2,3,4 will not appear as valid pointers. Switch statements | |
128 | * operating on the kw_ member must have cases for the enumeration values, | |
129 | * the default case means that the nn_ member is in fact active | |
130 | * and contains a pointer to the string. Remember to free/alloc new space | |
131 | * in this situation. */ | |
132 | typedef union { | |
133 | NamespaceSpecification::NameMangling kw_; | |
134 | char* nn_; | |
135 | } NameChange; | |
136 | ||
137 | XerAttributes(); | |
138 | ~XerAttributes(); | |
139 | ||
140 | /// If the NameChange structure contains a string, free it. | |
141 | static void FreeNameChange(NameChange& n); | |
142 | /// If the NamespaceSpecification contains a string, free it. | |
143 | static void FreeNamespace(NamespaceSpecification& ns); | |
144 | public: | |
51fa56b9 | 145 | bool abstract_; |
970ed795 EL |
146 | bool attribute_; |
147 | NamespaceRestriction anyAttributes_; | |
148 | NamespaceRestriction anyElement_; | |
149 | /// Base64 encoding for string-like types (XSD:base64Binary) | |
150 | bool base64_; | |
51fa56b9 | 151 | bool block_; |
970ed795 EL |
152 | /// No scientific notation for float |
153 | bool decimal_; | |
154 | /// String parsed out from the encoding attribute | |
155 | char * defaultForEmpty_; | |
156 | /// Value object constructed by Type::chk_xer() from defaultForEmpty_ | |
157 | Common::Value *defaultValue_; | |
158 | /// Global element in XSD | |
159 | bool element_; | |
160 | /// Give values to text nodes. Applied to record containing a record of string | |
161 | bool embedValues_; | |
162 | /// Qualified or unqualified form for local elements and attributes | |
163 | unsigned short form_; | |
164 | /// XSD:hexBinary | |
165 | bool hex_; | |
166 | /// space-separated values for record-of/set-of | |
167 | bool list_; | |
168 | /// How to get back the XML name | |
169 | NameChange name_; | |
170 | NamespaceSpecification namespace_; | |
171 | //struct { PI_Location position_; /*const*/ char * value_; } pi_or_comment_; | |
172 | /// Number of TEXT instructions stored. | |
173 | size_t num_text_; | |
174 | /// Pointer to an array for the TEXT encoding instruction | |
175 | NamespaceSpecification *text_; // re-use of struct | |
176 | /// No XML tag, just the value | |
177 | bool untagged_; | |
178 | bool useNil_; | |
179 | bool useNumber_; | |
180 | bool useOrder_; | |
181 | bool useQName_; | |
182 | bool useType_; | |
183 | bool useUnion_; | |
184 | WhitespaceAction whitespace_; | |
185 | ||
186 | void print(const char *type_name) const; | |
187 | ||
188 | /** Override/merge XER attributes from @p other. | |
189 | * | |
190 | * Boolean attributes (attribute_, base64_, decimal_, embedValues_, | |
191 | * list_, untagged_, useNil_, useNumber_, useOrder_, useQName_, useType_, | |
192 | * useUnion_) are merged. | |
193 | * In case of "Value" attributes (anyAttributes_, anyElement_, name_, | |
194 | * namespace_, text_, whitespace_) the value in @p other overwrites | |
195 | * the value in @p this. | |
196 | * | |
197 | * @param other set of XER attributes | |
198 | * @return the merged object (@p *this) | |
199 | * @pre @a other must not be @t empty() */ | |
200 | XerAttributes& operator |= (const XerAttributes& other); | |
201 | ||
202 | /** Return true if no attribute is set, false otherwise */ | |
203 | bool empty() const; | |
204 | }; | |
205 | ||
206 | inline bool has_ae(const XerAttributes *xa) { | |
207 | return xa->anyElement_.type_ != NamespaceRestriction::UNUSED; | |
208 | } | |
209 | ||
210 | inline bool has_aa(const XerAttributes *xa) { | |
211 | return xa->anyAttributes_.type_ != NamespaceRestriction::UNUSED; | |
212 | } | |
213 | ||
214 | ||
215 | #endif /* XERATTRIBUTES_H_ */ |