1 /*
2 SPDX-License-Identifier: GPL-2.0-or-later
3 SPDX-FileCopyrightText: 2006-2020 Umbrello UML Modeller Authors <umbrello-devel@kde.org>
4 */
5
6 // own header
7 #include "classimport.h"
8
9 // app includes
10 #include "debug_utils.h"
11 #include "folder.h"
12 #include "uml.h"
13 #include "umldoc.h"
14 #include "idlimport.h"
15 #include "pythonimport.h"
16 #include "javaimport.h"
17 #include "adaimport.h"
18 #include "pascalimport.h"
19 #include "sqlimport.h"
20 #include "cppimport.h"
21 #include "csharpimport.h"
22 #include "codeimpthread.h"
23 #ifdef ENABLE_PHP_IMPORT
24 #include "phpimport.h"
25 #endif
26
27 // kde includes
28 #include <KLocalizedString>
29
30 // qt includes
31 #include <QRegExp>
32
33 /**
34 * Factory method.
35 * @param fileName name of imported file
36 * @return the class import object
37 */
createImporterByFileExt(const QString & fileName,CodeImpThread * thread)38 ClassImport *ClassImport::createImporterByFileExt(const QString &fileName, CodeImpThread* thread)
39 {
40 ClassImport *classImporter;
41 if (fileName.endsWith(QLatin1String(".idl")))
42 classImporter = new IDLImport(thread);
43 else if (fileName.contains(QRegExp(QLatin1String("\\.pyw?$"))))
44 classImporter = new PythonImport(thread);
45 else if (fileName.endsWith(QLatin1String(".java")))
46 classImporter = new JavaImport(thread);
47 else if (fileName.contains(QRegExp(QLatin1String("\\.ad[sba]$"))))
48 classImporter = new AdaImport(thread);
49 else if (fileName.endsWith(QLatin1String(".pas")))
50 classImporter = new PascalImport(thread);
51 else if (fileName.endsWith(QLatin1String(".cs")))
52 classImporter = new CSharpImport(thread);
53 else if (fileName.endsWith(QLatin1String(".sql")))
54 classImporter = new SQLImport(thread);
55 #ifdef ENABLE_PHP_IMPORT
56 else if (fileName.endsWith(QLatin1String(".php")))
57 classImporter = new PHPImport(thread);
58 #endif
59 else
60 classImporter = new CppImport(thread); // the default.
61 return classImporter;
62 }
63
ClassImport(CodeImpThread * thread)64 ClassImport::ClassImport(CodeImpThread* thread)
65 : m_thread(thread),
66 m_enabled(true)
67 {
68 }
69
~ClassImport()70 ClassImport::~ClassImport()
71 {
72 }
73
74 /**
75 * Do initializations before importing a single file.
76 * This is called by importFile() before calling parseFile().
77 * @todo check if the default implementation should do anything
78 */
initPerFile()79 void ClassImport::initPerFile()
80 {
81 }
82
83 /**
84 * Import files.
85 * @param fileNames List of files to import.
86 */
importFiles(const QStringList & fileNames)87 bool ClassImport::importFiles(const QStringList& fileNames)
88 {
89 initialize();
90 UMLDoc *umldoc = UMLApp::app()->document();
91 uint processedFilesCount = 0;
92 bool result = true;
93 umldoc->setLoading(true);
94 umldoc->setImporting(true);
95 foreach (const QString& fileName, fileNames) {
96 umldoc->writeToStatusBar(i18n("Importing file: %1 Progress: %2/%3",
97 fileName, processedFilesCount, fileNames.size()));
98 if (!importFile(fileName))
99 result = false;
100 processedFilesCount++;
101 }
102 umldoc->setLoading(false);
103 umldoc->setImporting(false);
104 umldoc->writeToStatusBar(result ? i18nc("ready to status bar", "Ready.") : i18nc("failed to status bar", "Failed."));
105 return result;
106 }
107
108 /**
109 * Import a single file.
110 * @param fileName The file to import.
111 */
importFile(const QString & fileName)112 bool ClassImport::importFile(const QString& fileName)
113 {
114 initPerFile();
115 return parseFile(fileName);
116 }
117
setRootPath(const QString & path)118 void ClassImport::setRootPath(const QString &path)
119 {
120 m_rootPath = path;
121 }
122
123 /**
124 * Write info to a logger or to the debug output.
125 * @param file the name of the parsed file
126 * @param text the text to write
127 */
log(const QString & file,const QString & text)128 void ClassImport::log(const QString& file, const QString& text)
129 {
130 if (m_thread) {
131 m_thread->emitMessageToLog(file, text);
132 }
133 else {
134 uDebug() << file << " - " << text;
135 }
136 }
137
138 /**
139 * Write info to a logger or to the debug output.
140 * @param text the text to write
141 */
log(const QString & text)142 void ClassImport::log(const QString& text)
143 {
144 log(QString(), text);
145 }
146