implemented decmatch (artf724241)
[deliverable/titan.core.git] / xsdconvert / ImportStatement.cc
CommitLineData
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 * Godar, Marton
11 * Raduly, Csaba
12 * Szabo, Bence Janos
13 *
14 ******************************************************************************/
970ed795
EL
15#include "ImportStatement.hh"
16
17#include "GeneralFunctions.hh"
18#include "TTCN3Module.hh"
19#include "TTCN3ModuleInventory.hh"
20
970ed795
EL
21ImportStatement::ImportStatement(XMLParser * a_parser, TTCN3Module * a_module, ConstructType a_construct)
22: RootType(a_parser, a_module, a_construct)
23, from_namespace()
24, from_schemaLocation()
3abe9331 25, source_module() {
26}
970ed795 27
3abe9331 28void ImportStatement::loadWithValues() {
970ed795
EL
29 const XMLParser::TagAttributes & attr = parser->getActualTagAttributes();
30
3abe9331 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;
970ed795
EL
53 }
54}
55
56const Mstring XMLSchema("http://www.w3.org/2001/XMLSchema");
57
3abe9331 58void ImportStatement::referenceResolving(void) {
970ed795
EL
59 if (from_namespace == XMLSchema) {
60 visible = false;
61 return;
62 }
63
64 TTCN3ModuleInventory& modules = TTCN3ModuleInventory::getInstance();
65
3abe9331 66 for (List<TTCN3Module*>::iterator mod = modules.getModules().begin(); mod; mod = mod->Next) {
970ed795
EL
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 {
3abe9331 88 if (from_schemaLocation.empty()) {
970ed795
EL
89 printWarning(module->getSchemaname(), getName().convertedValue,
90 "The \'" + from_namespace + "\' specified in the \'namespace\' attribute"
91 " is not resolvable.");
92 modules.incrNumWarnings();
3abe9331 93 } else // schemaLocation is not empty
970ed795
EL
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();
3abe9331 100 } else {
970ed795
EL
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;
3abe9331 108 } else module->addImportedModule(source_module);
970ed795
EL
109}
110
3abe9331 111void ImportStatement::printToFile(FILE * file) {
970ed795
EL
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
3abe9331 119 switch (getConstruct()) {
120 case c_import:
970ed795 121 {
3abe9331 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 }
970ed795 128 }
3abe9331 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;
970ed795 134 }
3abe9331 135 case c_include:
970ed795 136 {
3abe9331 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 }
970ed795 142 }
3abe9331 143 break;
970ed795 144 }
3abe9331 145 default:
146 break;
970ed795
EL
147 }
148}
149
3abe9331 150void ImportStatement::dump(unsigned int depth) const {
970ed795 151 fprintf(stderr, "%*s Import statement at %p, ns='%s' loc='%s'\n", depth * 2, "",
3abe9331 152 (const void*) this, from_namespace.c_str(), from_schemaLocation.c_str());
970ed795
EL
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.061876 seconds and 5 git commands to generate.