c1174dfff03e729f06400baa2941c75570cb884b
[deliverable/titan.core.git] / xsdconvert / AttributeType.cc
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 #include "AttributeType.hh"
9
10 AttributeType::AttributeType(ComplexType * a_complexType)
11 : SimpleType(a_complexType->getParser(), a_complexType->getModule(), c_unknown)
12 , isAnyAttr(false)
13 , useVal(optional)
14 , nameSpace(empty_string)
15 , used(false)
16 , origModule(a_complexType->getModule()) {
17 parent = a_complexType;
18 }
19
20 AttributeType::AttributeType(const AttributeType & other)
21 : SimpleType(other)
22 , isAnyAttr(other.isAnyAttr)
23 , useVal(other.useVal)
24 , nameSpace(other.nameSpace)
25 , used(other.used)
26 , origModule(other.getModule()) {
27 }
28
29 AttributeType::~AttributeType() {
30
31 }
32
33 void AttributeType::modifyValues() {
34 if (parser->getActualTagName() == n_attribute || parser->getActualTagName() == n_anyAttribute) {
35 ((ComplexType*) parent)->modifyAttributeParent();
36 }
37 }
38
39 void AttributeType::setTypeOfField(const Mstring& in) {
40 type.upload(in);
41 }
42
43 void AttributeType::setNameOfField(const Mstring& in) {
44 name.upload(in);
45 }
46
47 void AttributeType::setToAnyAttribute() {
48 isAnyAttr = true;
49 }
50
51 void AttributeType::setFieldPath(const Mstring path) {
52 if (path.empty()) {
53 actualPath = getName().convertedValue;
54 } else {
55 if ((parent->getMinOccurs() != 1 || parent->getMaxOccurs() != 1) && parent->getName().list_extension) {
56 actualPath = path + Mstring("[-].") + getName().convertedValue;
57 } else {
58 actualPath = path + Mstring(".") + getName().convertedValue;
59 }
60 }
61 }
62
63 void AttributeType::collectVariants(List<Mstring>& container) {
64
65 if (variant.empty() && hidden_variant.empty()) {
66 return;
67 }
68
69 if (!isVisible()) {
70 return;
71 }
72
73 for (List<Mstring>::iterator var2 = variant.end(); var2; var2 = var2->Prev) {
74 container.push_back(Mstring("variant (") + actualPath + Mstring(") ") + Mstring(var2->Data.c_str()) + Mstring(";\n"));
75 }
76 for (List<Mstring>::iterator hidden_var = hidden_variant.end(); hidden_var; hidden_var = hidden_var->Prev) {
77 container.push_back(Mstring("//variant (") + actualPath + Mstring(") ") + Mstring(hidden_var->Data.c_str()) + Mstring(";\n"));
78 }
79 }
80
81 void AttributeType::nameConversion_names(QualifiedNames& used_ns) {
82 //Do not convert invisible field names
83 if (!visible || useVal == prohibited) {
84 return;
85 }
86 Mstring res, var(module->getTargetNamespace());
87 QualifiedNames used_names = TTCN3ModuleInventory::getInstance().getTypenames();
88 for (QualifiedNames::iterator n = used_ns.begin(); n; n = n->Next) {
89 used_names.push_back(n->Data);
90 }
91 QualifiedName q;
92 if(!used_names.empty()){
93 q = used_names.back();
94 }else {
95 q = QualifiedName(empty_string, empty_string);
96 }
97 XSDName2TTCN3Name(name.convertedValue, used_names, field_name, res, var);
98 name.convertedValue = res;
99 addVariant(V_onlyValue, var);
100 if (q.name != used_names.back().name) {
101 //If the name is converted then push to the used names list
102 used_ns.push_back(used_names.back());
103 }
104
105 for (List<SimpleType*>::iterator st = nameDepList.begin(); st; st = st->Next) {
106 st->Data->setTypeValue(res);
107 }
108 }
109
110 void AttributeType::applyUseAttribute() {
111 if (isAnyAttr) {
112 return;
113 }
114 switch (useVal) {
115 case optional:
116 minOccurs = 0;
117 maxOccurs = 1;
118 break;
119 case required:
120 minOccurs = 1;
121 maxOccurs = 1;
122 break;
123 case prohibited:
124 minOccurs = 0;
125 maxOccurs = 0;
126 setInvisible();
127 break;
128 }
129 isOptional = isOptional || (minOccurs == 0 && maxOccurs == 1);
130 }
131
132 void AttributeType::applyNamespaceAttribute(VariantMode varLabel) {
133 List<Mstring> namespaces;
134 if (!nameSpace.empty()) {
135 expstring_t valueToSplitIntoTokens = mcopystr(nameSpace.c_str());
136 char * token;
137 token = strtok(valueToSplitIntoTokens, " ");
138 while (token != NULL) {
139 namespaces.push_back(Mstring(token));
140 token = strtok(NULL, " ");
141 }
142 Free(valueToSplitIntoTokens);
143 }
144
145 Mstring any_ns;
146 bool first = true;
147 // Note: libxml2 will verify the namespace list according to the schema
148 // of XML Schema. It is either ##any, ##other, ##local, ##targetNamespace,
149 // or a list of (namespace reference | ##local | ##targetNamespace).
150 for (List<Mstring>::iterator ns = namespaces.begin(); ns; ns = ns->Next) {
151 static const Mstring xxany("##any"), xxother("##other"), xxlocal("##local"),
152 xxtargetNamespace("##targetNamespace");
153 if (!first) any_ns += ", ";
154
155 if (ns->Data == xxany) {
156 }// this must be the only element, nothing to add
157 else if (ns->Data == xxother) { // this must be the only element
158 if(first){ any_ns += " except "; }
159 any_ns += "unqualified";
160 if (module->getTargetNamespace() != "NoTargetNamespace") {
161 any_ns += ", \'";
162 any_ns += parent->getModule()->getTargetNamespace();
163 any_ns += '\'';
164 }
165 }// The three cases below can happen multiple times
166 else {
167 if (first) any_ns += " from ";
168 // else a comma was already added
169 if (ns->Data == xxtargetNamespace) {
170 any_ns += '\'';
171 any_ns += parent->getModule()->getTargetNamespace();
172 any_ns += '\'';
173 } else if (ns->Data == xxlocal) {
174 any_ns += "unqualified";
175 } else {
176 any_ns += '\'';
177 any_ns += ns->Data;
178 any_ns += '\'';
179 }
180 }
181 if(!first || ns->Data != xxany){
182 first = false;
183 }
184 }
185
186 addVariant(varLabel, any_ns, true);
187 }
188
189 void AttributeType::applyMinMaxOccursAttribute(unsigned long long min, unsigned long long max) {
190 minOccurs = min;
191 maxOccurs = max;
192 }
193
194 void AttributeType::dump(unsigned int depth) const {
195 fprintf(stderr, "%*s %sField '%s' -> '%s' at %p\n", depth * 2, "", isVisible() ? "" : "(hidden)",
196 name.originalValueWoPrefix.c_str(), name.convertedValue.c_str(), (const void*) this);
197 fprintf(stderr, "%*s %s Type: \n", depth * 2, "", type.convertedValue.c_str());
198 fprintf(stderr, "%*s type %s \n", (depth + 1) * 2, "", type.convertedValue.c_str());
199 fprintf(stderr, "%*s (%llu .. %llu)\n", (depth + 1) * 2, "", minOccurs, maxOccurs);
200 fprintf(stderr, "%*s %d variants: ", (depth + 1) * 2, "", (int) variant.size());
201 for (List<Mstring>::iterator var = variant.begin(); var; var = var->Next) {
202 fprintf(stderr, "%s, ", var->Data.c_str());
203 }
204 fprintf(stderr, "\n%*s path =/%s/", (depth + 1) * 2, "", actualPath.c_str());
205 }
206
207 void AttributeType::printToFile(FILE* file, unsigned level) {
208 if (!isVisible()) {
209 return;
210 }
211 printComment(file, level);
212 indent(file, level);
213 if(enumeration.modified && hasVariant(Mstring("\"list\""))){
214 printMinOccursMaxOccurs(file, false);
215 fprintf(file, "enumerated {\n");
216 enumeration.sortFacets();
217 enumeration.printToFile(file);
218 indent(file, level);
219 fprintf(file, "\n} %s", name.convertedValue.c_str());
220 } else if (enumeration.modified) {
221 if (isFloatType(builtInBase)) {
222 fprintf(file, "%s %s (", type.convertedValue.c_str(), name.convertedValue.c_str());
223 enumeration.sortFacets();
224 enumeration.printToFile(file);
225 fputc(')', file);
226 } else {
227 fprintf(file, "enumerated {\n");
228 enumeration.sortFacets();
229 enumeration.printToFile(file);
230 fprintf(file, "\n");
231 indent(file, level);
232 fprintf(file, "} %s", name.convertedValue.c_str());
233 }
234 }else {
235 printMinOccursMaxOccurs(file, false);
236 int multiplicity = multi(module, getReference(), this);
237 if ((multiplicity > 1) && getReference().get_ref()) {
238 fprintf(file, "%s.", getReference().get_ref()->getModule()->getModulename().c_str());
239 }
240 fprintf(file, "%s %s", type.convertedValue.c_str(), name.convertedValue.c_str());
241 getPattern().printToFile(file);
242 getValue().printToFile(file);
243 getLength().printToFile(file);
244 }
245 if (isOptional || isAnyAttr) {
246 fprintf(file, " optional");
247 }
248 }
This page took 0.041254 seconds and 5 git commands to generate.