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