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