Titan Core Initial Contribution
[deliverable/titan.core.git] / xsdconvert / RootType.cc
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 ///////////////////////////////////////////////////////////////////////////////
8 #include "RootType.hh"
9
10 #include "TTCN3Module.hh"
11
12 RootType::RootType(XMLParser * a_parser, TTCN3Module * a_module, ConstructType a_construct)
13 : parser(a_parser)
14 , module(a_module)
15 , name()
16 , type()
17 , id()
18 , minOccurs(1)
19 , maxOccurs(1)
20 , variant()
21 , variant_ref()
22 , comment()
23 , construct(a_construct)
24 , origin(from_unknown)
25 , visible(true)
26 , nameDepList()
27 {
28 switch (a_construct)
29 {
30 case c_schema:
31 case c_annotation:
32 case c_include:
33 case c_import:
34 //redundant: origin = from_unknown;
35 break;
36 case c_unknown: // because when using fields in complextypes we set construct to c_unknown
37 case c_simpleType:
38 origin = from_simpleType;
39 type.upload(Mstring("anySimpleType"));
40 break;
41 case c_element:
42 origin = from_element;
43 type.upload(Mstring("anyType"));
44 addVariant(V_element);
45 break;
46 case c_attribute:
47 origin = from_attribute;
48 type.upload(Mstring("anySimpleType"));
49 addVariant(V_attribute);
50 break;
51 case c_complexType:
52 origin = from_complexType;
53 type.upload(Mstring("record"));
54 break;
55 case c_group:
56 origin = from_group;
57 type.upload(Mstring("record"));
58 addVariant(V_untagged);
59 break;
60 case c_attributeGroup:
61 origin = from_attributeGroup;
62 type.upload(Mstring("record"));
63 addVariant(V_attributeGroup);
64 visible = false;
65 break;
66 }
67 }
68
69 void RootType::addVariant(VariantMode var, const Mstring& var_value, bool into_variant_ref)
70 {
71 Mstring variantstring;
72
73 switch (var)
74 {
75 case V_anyAttributes:
76 variantstring = "\"anyAttributes" + var_value + "\"";
77 break;
78 case V_anyElement:
79 variantstring = "\"anyElement" + var_value + "\"";
80 break;
81 case V_attribute:
82 variantstring = "\"attribute\"";
83 break;
84 case V_attributeFormQualified:
85 variantstring = "\"attributeFormQualified\"";
86 break;
87 case V_attributeGroup:
88 variantstring = "\"attributeGroup\"";
89 break;
90 case V_controlNamespace:
91 variantstring = "\"controlNamespace" + var_value + "\"";
92 break;
93 case V_defaultForEmpty:
94 variantstring = "\"defaultForEmpty as \'" + var_value + "\'\""; // chapter 7.1.5
95 break;
96 case V_element:
97 variantstring = "\"element\"";
98 break;
99 case V_elementFormQualified:
100 variantstring = "\"elementFormQualified\"";
101 break;
102 case V_embedValues:
103 variantstring = "\"embedValues\"";
104 break;
105 case V_formAs:
106 variantstring = "\"form as " + var_value + "\"";
107 break;
108 case V_list:
109 variantstring = "\"list\"";
110 break;
111 case V_nameAs:
112 variantstring = "\"name as \'" + var_value + "\'\"";
113 break;
114 case V_namespaceAs: {
115 Mstring prefix;
116 Mstring uri;
117 for (List<NamespaceType>::iterator namesp = module->getDeclaredNamespaces().begin(); namesp; namesp = namesp->Next) {
118 if (namesp->Data.uri == var_value) {
119 prefix = namesp->Data.prefix;
120 uri = namesp->Data.uri;
121 break;
122 }
123 }
124 if (prefix.empty() || uri.empty()) {
125 break;
126 }
127 variantstring = "\"namespace as \'" + uri + "\' prefix \'" + prefix + "\'\"";
128 break; }
129 case V_onlyValue:
130 variantstring = var_value;
131 break;
132 case V_untagged:
133 variantstring = "\"untagged\"";
134 break;
135 case V_useNil:
136 variantstring = "\"useNil\"";
137 break;
138 case V_useNumber:
139 variantstring = "\"useNumber\"";
140 break;
141 case V_useOrder:
142 variantstring = "\"useOrder\"";
143 break;
144 case V_useUnion:
145 variantstring = "\"useUnion\"";
146 break;
147 case V_whiteSpace:
148 variantstring = "\"whiteSpace " + var_value + "\"";
149 break;
150 }
151
152 if (!variantstring.empty()) {
153 variant.push_back(variantstring);
154 if (into_variant_ref) {
155 variant_ref.push_back(variantstring);
156 }
157 }
158 }
159
160 void RootType::printVariant(FILE * file)
161 {
162 if (!e_flag_used && !variant.empty())
163 {
164 fprintf(file, "\nwith {\n");
165 for (List<Mstring>::iterator var = variant.end(); var; var = var->Prev)
166 {
167 fprintf(file, "variant %s;\n", var->Data.c_str());
168 }
169 fprintf(file, "}");
170 }
171 }
172
173 void RootType::addComment(const Mstring& text)
174 {
175 comment += "/* " + text + " */\n";
176 }
177
178 void RootType::printComment(FILE * file)
179 {
180 if (!c_flag_used)
181 {
182 fprintf(file, "%s", comment.c_str());
183 }
184 }
185
186 void RootType::printMinOccursMaxOccurs(FILE * file, bool inside_union,
187 bool empty_allowed /* = true */) const
188 {
189 unsigned long long tmp_minOccurs = minOccurs;
190 if (minOccurs == 0 && !empty_allowed) tmp_minOccurs = 1ULL;
191
192 if (maxOccurs == 1)
193 {
194 if (minOccurs == 0) {
195 if (inside_union || name.list_extension) {
196 fputs("record length(", file);
197 if (empty_allowed) fputs("0 .. ", file);
198 // else: length(1..1) is shortened to length(1)
199 fputs("1) of ", file);
200 }
201 // else it's optional which is not printed from here
202 }
203 else if (minOccurs == 1) {
204 // min==max==1; do nothing unless...
205 if (name.convertedValue == "embed_values") fputs("record length(1) of ", file);
206 }
207 }
208 else if (maxOccurs == ULLONG_MAX) {
209 if (minOccurs == 0) {
210 fputs("record ", file);
211 if (!empty_allowed) fputs("length(1 .. infinity) ", file);
212 fputs("of ", file);
213 }
214 else fprintf(file, "record length(%llu .. infinity) of ", tmp_minOccurs);
215 }
216 else
217 {
218 fprintf(file, "record length(%llu .. %llu) of ", tmp_minOccurs, maxOccurs);
219 }
220 }
This page took 0.035685 seconds and 6 git commands to generate.