File: ImportStatement.cc

package info (click to toggle)
eclipse-titan 8.2.0-1
  • links: PTS
  • area: main
  • in suites: bookworm, sid
  • size: 103,544 kB
  • sloc: cpp: 271,008; ansic: 33,683; yacc: 23,419; makefile: 15,483; lex: 9,204; java: 4,848; perl: 4,555; sh: 2,242; xml: 1,378; javascript: 85; awk: 48; php: 32; python: 13
file content (164 lines) | stat: -rw-r--r-- 5,517 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/******************************************************************************
 * Copyright (c) 2000-2021 Ericsson Telecom AB
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
 *
 * Contributors:
 *   Balasko, Jeno
 *   Godar, Marton
 *   Raduly, Csaba
 *   Szabo, Bence Janos
 *
 ******************************************************************************/
#include "ImportStatement.hh"

#include "GeneralFunctions.hh"
#include "TTCN3Module.hh"
#include "TTCN3ModuleInventory.hh"

ImportStatement::ImportStatement(XMLParser * a_parser, TTCN3Module * a_module, ConstructType a_construct)
: RootType(a_parser, a_module, a_construct)
, from_namespace()
, from_schemaLocation()
, source_module() {
}

void ImportStatement::loadWithValues() {
  const XMLParser::TagAttributes & attr = parser->getActualTagAttributes();

  switch (parser->getActualTagName()) {
    case n_import:
      name.upload(Mstring("import"));
      type.upload(Mstring("import"), false);
      from_namespace = attr.namespace_;
      from_schemaLocation = attr.schemaLocation;
      break;
    case n_include:
      name.upload(Mstring("include"));
      type.upload(Mstring("include"), false);
      from_namespace = attr.namespace_;
      from_schemaLocation = attr.schemaLocation;
      break;
    case n_label:
      addComment(Mstring("LABEL:"));
      break;
    case n_definition:
      addComment(Mstring("DEFINITION:"));
      break;

    default:
      break;
  }
}

void ImportStatement::referenceResolving(void) {
  if (from_namespace == XMLSchema) {
    visible = false;
    return;
  }

  TTCN3ModuleInventory& modules = TTCN3ModuleInventory::getInstance();

  for (List<TTCN3Module*>::iterator mod = modules.getModules().begin(); mod; mod = mod->Next) {
    if (module == mod->Data) {
      // it's the module that *contains* the import statement
      continue;
    }
    // Try the namespace first
    if (from_namespace == mod->Data->getTargetNamespace()) {
      source_module = mod->Data;
      break;
    }
    // Fallback: try the schemaLocation attribute
    if (!from_schemaLocation.empty()) {
      if (from_schemaLocation == mod->Data->getSchemaname()) {
        source_module = mod->Data;
        from_namespace = mod->Data->getTargetNamespace();
        // do not break; give a chance to other modules to match the namespace
      }
    }
  }

  if (!source_module) // still not found
  {
    if (from_schemaLocation.empty()) {
      printWarning(module->getSchemaname(), getName().convertedValue,
        "The \'" + from_namespace + "\' specified in the \'namespace\' attribute"
        " is not resolvable.");
      modules.incrNumWarnings();
    } else // schemaLocation is not empty
    {
      if (from_schemaLocation.isFound("http://") || from_schemaLocation.isFound("urn:")) {
        printWarning(module->getSchemaname(), getName().convertedValue,
          "It is not supported using a URI (\'" + from_schemaLocation +
          "\') in the \'schemalocation\' attribute to get access to a file.");
        modules.incrNumWarnings();
      } else {
        printWarning(module->getSchemaname(), getName().convertedValue,
          "The \'" + from_schemaLocation + "\' specified in the \'schemaLocation\' attribute"
          " is not resolvable.");
        modules.incrNumWarnings();
      }
    }
    visible = false;
  } else {
    // Add the xml namespace to the importing module's declared namespaces
    if (from_namespace == "http://www.w3.org/XML/1998/namespace") {
      NamespaceType tmp_ns_pair;
      tmp_ns_pair.uri = from_namespace;
      tmp_ns_pair.prefix = "xml";
      module->getDeclaredNamespaces().push_back(tmp_ns_pair);
    }
    module->addImportedModule(source_module);
  }
}

void ImportStatement::printToFile(FILE * file) {
  if (!visible) return;

  if (from_namespace == module->getTargetNamespace()) return;
  // Not include or import in this case: including is automatic because modules have the same targetnamespace

  printComment(file);

  switch (getConstruct()) {
    case c_import:
    {
      bool found = false;
      for (List<TTCN3Module*>::iterator wImport = TTCN3ModuleInventory::getInstance().getWrittenImports().begin(); wImport; wImport = wImport->Next) {
        if (wImport->Data == source_module) {
          found = true;
          break;
        }
      }
      if (!found) {
        fprintf(file, "import from %s all;\n\n\n", source_module->getModulename().c_str());
        TTCN3ModuleInventory::getInstance().getWrittenImports().push_back(source_module);
      }
      break;
    }
    case c_include:
    {
      for (List<TTCN3Module*>::iterator mod = TTCN3ModuleInventory::getInstance().getModules().begin(); mod; mod = mod->Next) {
        if (mod->Data->getSchemaname() == from_schemaLocation) {
          mod->Data->generate_TTCN3_types(file);
          break;
        }
      }
      break;
    }
    default:
      break;
  }
}

void ImportStatement::dump(unsigned int depth) const {
  fprintf(stderr, "%*s Import statement at %p, ns='%s' loc='%s'\n", depth * 2, "",
    (const void*) this, from_namespace.c_str(), from_schemaLocation.c_str());
  fprintf(stderr, "%*s import from %s into %s\n", depth * 2 + 2, "",
    (source_module ? source_module->getModulename().c_str() : "**unknown**"),
    module->getModulename().c_str());
}