Merge pull request #61 from BenceJanosSzabo/master
[deliverable/titan.core.git] / xsdconvert / ImportStatement.cc
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 * Godar, Marton
11 * Raduly, Csaba
12 * Szabo, Bence Janos
13 *
14 ******************************************************************************/
15 #include "ImportStatement.hh"
16
17 #include "GeneralFunctions.hh"
18 #include "TTCN3Module.hh"
19 #include "TTCN3ModuleInventory.hh"
20
21 ImportStatement::ImportStatement(XMLParser * a_parser, TTCN3Module * a_module, ConstructType a_construct)
22 : RootType(a_parser, a_module, a_construct)
23 , from_namespace()
24 , from_schemaLocation()
25 , source_module() {
26 }
27
28 void ImportStatement::loadWithValues() {
29 const XMLParser::TagAttributes & attr = parser->getActualTagAttributes();
30
31 switch (parser->getActualTagName()) {
32 case n_import:
33 name.upload(Mstring("import"));
34 type.upload(Mstring("import"));
35 from_namespace = attr.namespace_;
36 from_schemaLocation = attr.schemaLocation;
37 break;
38 case n_include:
39 name.upload(Mstring("include"));
40 type.upload(Mstring("include"));
41 from_namespace = attr.namespace_;
42 from_schemaLocation = attr.schemaLocation;
43 break;
44 case n_label:
45 addComment(Mstring("LABEL:"));
46 break;
47 case n_definition:
48 addComment(Mstring("DEFINITION:"));
49 break;
50
51 default:
52 break;
53 }
54 }
55
56 const Mstring XMLSchema("http://www.w3.org/2001/XMLSchema");
57
58 void ImportStatement::referenceResolving(void) {
59 if (from_namespace == XMLSchema) {
60 visible = false;
61 return;
62 }
63
64 TTCN3ModuleInventory& modules = TTCN3ModuleInventory::getInstance();
65
66 for (List<TTCN3Module*>::iterator mod = modules.getModules().begin(); mod; mod = mod->Next) {
67 if (module == mod->Data) {
68 // it's the module that *contains* the import statement
69 continue;
70 }
71 // Try the namespace first
72 if (from_namespace == mod->Data->getTargetNamespace()) {
73 source_module = mod->Data;
74 break;
75 }
76 // Fallback: try the schemaLocation attribute
77 if (!from_schemaLocation.empty()) {
78 if (from_schemaLocation == mod->Data->getSchemaname()) {
79 source_module = mod->Data;
80 from_namespace = mod->Data->getTargetNamespace();
81 // do not break; give a chance to other modules to match the namespace
82 }
83 }
84 }
85
86 if (!source_module) // still not found
87 {
88 if (from_schemaLocation.empty()) {
89 printWarning(module->getSchemaname(), getName().convertedValue,
90 "The \'" + from_namespace + "\' specified in the \'namespace\' attribute"
91 " is not resolvable.");
92 modules.incrNumWarnings();
93 } else // schemaLocation is not empty
94 {
95 if (from_schemaLocation.isFound("http://") || from_schemaLocation.isFound("urn:")) {
96 printWarning(module->getSchemaname(), getName().convertedValue,
97 "It is not supported using a URI (\'" + from_schemaLocation +
98 "\') in the \'schemalocation\' attribute to get access to a file.");
99 modules.incrNumWarnings();
100 } else {
101 printWarning(module->getSchemaname(), getName().convertedValue,
102 "The \'" + from_schemaLocation + "\' specified in the \'schemaLocation\' attribute"
103 " is not resolvable.");
104 modules.incrNumWarnings();
105 }
106 }
107 visible = false;
108 } else module->addImportedModule(source_module);
109 }
110
111 void ImportStatement::printToFile(FILE * file) {
112 if (!visible) return;
113
114 if (from_namespace == module->getTargetNamespace()) return;
115 // Not include or import in this case: including is automatic because modules have the same targetnamespace
116
117 printComment(file);
118
119 switch (getConstruct()) {
120 case c_import:
121 {
122 bool found = false;
123 for (List<TTCN3Module*>::iterator wImport = TTCN3ModuleInventory::getInstance().getWrittenImports().begin(); wImport; wImport = wImport->Next) {
124 if (wImport->Data == source_module) {
125 found = true;
126 break;
127 }
128 }
129 if (!found) {
130 fprintf(file, "import from %s all;\n\n\n", source_module->getModulename().c_str());
131 TTCN3ModuleInventory::getInstance().getWrittenImports().push_back(source_module);
132 }
133 break;
134 }
135 case c_include:
136 {
137 for (List<TTCN3Module*>::iterator mod = TTCN3ModuleInventory::getInstance().getModules().begin(); mod; mod = mod->Next) {
138 if (mod->Data->getSchemaname() == from_schemaLocation) {
139 mod->Data->generate_TTCN3_types(file);
140 break;
141 }
142 }
143 break;
144 }
145 default:
146 break;
147 }
148 }
149
150 void ImportStatement::dump(unsigned int depth) const {
151 fprintf(stderr, "%*s Import statement at %p, ns='%s' loc='%s'\n", depth * 2, "",
152 (const void*) this, from_namespace.c_str(), from_schemaLocation.c_str());
153 fprintf(stderr, "%*s import from %s into %s\n", depth * 2 + 2, "",
154 (source_module ? source_module->getModulename().c_str() : "**unknown**"),
155 module->getModulename().c_str());
156 }
157
This page took 0.034945 seconds and 5 git commands to generate.