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
14 ******************************************************************************/
15 #include "ImportStatement.hh"
17 #include "GeneralFunctions.hh"
18 #include "TTCN3Module.hh"
19 #include "TTCN3ModuleInventory.hh"
21 ImportStatement::ImportStatement(XMLParser
* a_parser
, TTCN3Module
* a_module
, ConstructType a_construct
)
22 : RootType(a_parser
, a_module
, a_construct
)
24 , from_schemaLocation()
28 void ImportStatement::loadWithValues() {
29 const XMLParser::TagAttributes
& attr
= parser
->getActualTagAttributes();
31 switch (parser
->getActualTagName()) {
33 name
.upload(Mstring("import"));
34 type
.upload(Mstring("import"));
35 from_namespace
= attr
.namespace_
;
36 from_schemaLocation
= attr
.schemaLocation
;
39 name
.upload(Mstring("include"));
40 type
.upload(Mstring("include"));
41 from_namespace
= attr
.namespace_
;
42 from_schemaLocation
= attr
.schemaLocation
;
45 addComment(Mstring("LABEL:"));
48 addComment(Mstring("DEFINITION:"));
56 const Mstring
XMLSchema("http://www.w3.org/2001/XMLSchema");
58 void ImportStatement::referenceResolving(void) {
59 if (from_namespace
== XMLSchema
) {
64 TTCN3ModuleInventory
& modules
= TTCN3ModuleInventory::getInstance();
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
71 // Try the namespace first
72 if (from_namespace
== mod
->Data
->getTargetNamespace()) {
73 source_module
= mod
->Data
;
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
86 if (!source_module
) // still not found
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
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();
101 printWarning(module
->getSchemaname(), getName().convertedValue
,
102 "The \'" + from_schemaLocation
+ "\' specified in the \'schemaLocation\' attribute"
103 " is not resolvable.");
104 modules
.incrNumWarnings();
108 } else module
->addImportedModule(source_module
);
111 void ImportStatement::printToFile(FILE * file
) {
112 if (!visible
) return;
114 if (from_namespace
== module
->getTargetNamespace()) return;
115 // Not include or import in this case: including is automatic because modules have the same targetnamespace
119 switch (getConstruct()) {
123 for (List
<TTCN3Module
*>::iterator wImport
= TTCN3ModuleInventory::getInstance().getWrittenImports().begin(); wImport
; wImport
= wImport
->Next
) {
124 if (wImport
->Data
== source_module
) {
130 fprintf(file
, "import from %s all;\n\n\n", source_module
->getModulename().c_str());
131 TTCN3ModuleInventory::getInstance().getWrittenImports().push_back(source_module
);
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
);
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());