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"
10 #include "GeneralFunctions.hh"
11 #include "TTCN3Module.hh"
12 #include "TTCN3ModuleInventory.hh"
14 ImportStatement::ImportStatement(XMLParser
* a_parser
, TTCN3Module
* a_module
, ConstructType a_construct
)
15 : RootType(a_parser
, a_module
, a_construct
)
17 , from_schemaLocation()
21 void ImportStatement::loadWithValues() {
22 const XMLParser::TagAttributes
& attr
= parser
->getActualTagAttributes();
24 switch (parser
->getActualTagName()) {
26 name
.upload(Mstring("import"));
27 type
.upload(Mstring("import"));
28 from_namespace
= attr
.namespace_
;
29 from_schemaLocation
= attr
.schemaLocation
;
32 name
.upload(Mstring("include"));
33 type
.upload(Mstring("include"));
34 from_namespace
= attr
.namespace_
;
35 from_schemaLocation
= attr
.schemaLocation
;
38 addComment(Mstring("LABEL:"));
41 addComment(Mstring("DEFINITION:"));
49 const Mstring
XMLSchema("http://www.w3.org/2001/XMLSchema");
51 void ImportStatement::referenceResolving(void) {
52 if (from_namespace
== XMLSchema
) {
57 TTCN3ModuleInventory
& modules
= TTCN3ModuleInventory::getInstance();
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
64 // Try the namespace first
65 if (from_namespace
== mod
->Data
->getTargetNamespace()) {
66 source_module
= mod
->Data
;
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
79 if (!source_module
) // still not found
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
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();
94 printWarning(module
->getSchemaname(), getName().convertedValue
,
95 "The \'" + from_schemaLocation
+ "\' specified in the \'schemaLocation\' attribute"
96 " is not resolvable.");
97 modules
.incrNumWarnings();
101 } else module
->addImportedModule(source_module
);
104 void ImportStatement::printToFile(FILE * file
) {
105 if (!visible
) return;
107 if (from_namespace
== module
->getTargetNamespace()) return;
108 // Not include or import in this case: including is automatic because modules have the same targetnamespace
112 switch (getConstruct()) {
116 for (List
<TTCN3Module
*>::iterator wImport
= TTCN3ModuleInventory::getInstance().getWrittenImports().begin(); wImport
; wImport
= wImport
->Next
) {
117 if (wImport
->Data
== source_module
) {
123 fprintf(file
, "import from %s all;\n\n\n", source_module
->getModulename().c_str());
124 TTCN3ModuleInventory::getInstance().getWrittenImports().push_back(source_module
);
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
);
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());