From 7167ce41b61d2ba2cdb526777a4233eb84a3b66a Mon Sep 17 00:00:00 2001 From: Unit 193 Date: Sat, 6 Dec 2014 17:33:25 -0500 Subject: Imported Upstream version 2.99.6 --- Plugins/RegExpImport/RegExpImport.pro | 28 ++++ Plugins/RegExpImport/regexpimport.cpp | 209 +++++++++++++++++++++++++++++ Plugins/RegExpImport/regexpimport.h | 52 +++++++ Plugins/RegExpImport/regexpimport.json | 7 + Plugins/RegExpImport/regexpimport.qrc | 5 + Plugins/RegExpImport/regexpimport.ui | 99 ++++++++++++++ Plugins/RegExpImport/regexpimport_global.h | 12 ++ 7 files changed, 412 insertions(+) create mode 100644 Plugins/RegExpImport/RegExpImport.pro create mode 100644 Plugins/RegExpImport/regexpimport.cpp create mode 100644 Plugins/RegExpImport/regexpimport.h create mode 100644 Plugins/RegExpImport/regexpimport.json create mode 100644 Plugins/RegExpImport/regexpimport.qrc create mode 100644 Plugins/RegExpImport/regexpimport.ui create mode 100644 Plugins/RegExpImport/regexpimport_global.h (limited to 'Plugins/RegExpImport') diff --git a/Plugins/RegExpImport/RegExpImport.pro b/Plugins/RegExpImport/RegExpImport.pro new file mode 100644 index 0000000..99c884e --- /dev/null +++ b/Plugins/RegExpImport/RegExpImport.pro @@ -0,0 +1,28 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2014-07-20T12:19:38 +# +#------------------------------------------------- + +include($$PWD/../../SQLiteStudio3/plugins.pri) + +QT -= gui + +TARGET = RegExpImport +TEMPLATE = lib + +DEFINES += REGEXPIMPORT_LIBRARY + +SOURCES += regexpimport.cpp + +HEADERS += regexpimport.h\ + regexpimport_global.h + +OTHER_FILES += \ + regexpimport.json + +FORMS += \ + regexpimport.ui + +RESOURCES += \ + regexpimport.qrc diff --git a/Plugins/RegExpImport/regexpimport.cpp b/Plugins/RegExpImport/regexpimport.cpp new file mode 100644 index 0000000..cd761b1 --- /dev/null +++ b/Plugins/RegExpImport/regexpimport.cpp @@ -0,0 +1,209 @@ +#include "regexpimport.h" +#include "services/notifymanager.h" +#include "common/utils.h" +#include "services/importmanager.h" +#include "sqlitestudio.h" +#include +#include +#include + +RegExpImport::RegExpImport() +{ +} + +bool RegExpImport::init() +{ + Q_INIT_RESOURCE(regexpimport); + return GenericPlugin::init(); +} + +void RegExpImport::deinit() +{ + Q_CLEANUP_RESOURCE(regexpimport); +} + +QString RegExpImport::getDataSourceTypeName() const +{ + return "RegExp"; +} + +ImportManager::StandardConfigFlags RegExpImport::standardOptionsToEnable() const +{ + return ImportManager::CODEC|ImportManager::FILE_NAME; +} + +QString RegExpImport::getFileFilter() const +{ + return tr("Text files (*.txt);;All files (*)"); +} + +bool RegExpImport::beforeImport(const ImportManager::StandardImportConfig& config) +{ + safe_delete(re); + safe_delete(file); + safe_delete(stream); + groups.clear(); + buffer.clear(); + columns.clear(); + + + file = new QFile(config.inputFileName); + if (!file->open(QFile::ReadOnly) || !file->isReadable()) + { + notifyError(tr("Cannot read file %1").arg(config.inputFileName)); + safe_delete(file); + return false; + } + + stream = new QTextStream(file); + stream->setCodec(config.codec.toLatin1().data()); + + + static const QString intColTemplate = QStringLiteral("column%1"); + re = new QRegularExpression(cfg.RegExpImport.Pattern.get()); + QString colName; + if (cfg.RegExpImport.GroupsMode.get() == "all") + { + for (int i = 1; i <= re->captureCount(); i++) + { + groups << i; + colName = intColTemplate.arg(i); + columns << generateUniqueName(colName, columns); + } + } + else + { + QStringList entries = cfg.RegExpImport.CustomGroupList.get().split(QRegularExpression(",\\s*")); + int i; + bool ok; + for (const QString& entry : entries) + { + i = entry.toInt(&ok); + if (ok) + { + groups << i; + colName = intColTemplate.arg(i); + } + else + { + groups << entry; + colName = entry; + } + columns << generateUniqueName(colName, columns); + } + } + return true; +} + +void RegExpImport::afterImport() +{ + safe_delete(re); + safe_delete(file); + safe_delete(stream); + buffer.clear(); + groups.clear(); +} + +QList RegExpImport::getColumns() const +{ + QList columnList; + for (const QString& colName : columns) + columnList << ImportPlugin::ColumnDefinition(colName, QString()); + + return columnList; +} + +QList RegExpImport::next() +{ + QRegularExpressionMatch match = re->match(buffer); + QString line; + while (!match.hasMatch() && !(line = stream->readLine()).isNull()) + { + buffer += line; + match = re->match(buffer); + } + + if (!match.hasMatch()) + return QList(); + + QList values; + for (const QVariant& group : groups) + { + if (group.type() == QVariant::Int) + values << match.captured(group.toInt()); + else + values << match.captured(group.toString()); + } + + buffer = buffer.mid(match.capturedEnd()); + + return values; +} + +CfgMain* RegExpImport::getConfig() +{ + return &cfg; +} + +QString RegExpImport::getImportConfigFormName() const +{ + return "RegExpImportConfig"; +} + +bool RegExpImport::validateOptions() +{ + QString reMsg; + QString pattern = cfg.RegExpImport.Pattern.get(); + bool reOk = true; + if (pattern.isEmpty()) + { + reOk = false; + reMsg = tr("Enter the regular expression pattern."); + } + + QRegularExpression localRe(pattern); + if (reOk) + { + reOk &= localRe.isValid(); + if (!reOk) + reMsg = tr("Invalid pattern: %1").arg(localRe.errorString()); + } + + QString groupMsg; + bool groupsOk = true; + bool isCustom = (cfg.RegExpImport.GroupsMode.get() == "custom"); + if (isCustom && reOk) + { + QStringList entries = cfg.RegExpImport.CustomGroupList.get().split(QRegularExpression(",\\s*")); + QStringList namedCaptureGroups = localRe.namedCaptureGroups(); + int captureCount = localRe.captureCount(); + int i; + bool ok; + for (const QString& entry : entries) + { + i = entry.toInt(&ok); + if (ok) + { + if (i < 0 || i > captureCount) + { + groupMsg = tr("Requested capture index %1 is out of range.").arg(i); + groupsOk = false; + break; + } + } + else if (!namedCaptureGroups.contains(entry)) + { + groupMsg = tr("

Requested capture group name '%1', but it's not defined in the pattern:

%2

") + .arg(entry, pattern.toHtmlEscaped()); + groupsOk = false; + break; + } + } + } + + IMPORT_MANAGER->handleValidationFromPlugin(reOk, cfg.RegExpImport.Pattern, reMsg); + IMPORT_MANAGER->handleValidationFromPlugin(groupsOk, cfg.RegExpImport.CustomGroupList, groupMsg); + IMPORT_MANAGER->updateVisibilityAndEnabled(cfg.RegExpImport.CustomGroupList, true, isCustom); + + return reOk && groupsOk; +} diff --git a/Plugins/RegExpImport/regexpimport.h b/Plugins/RegExpImport/regexpimport.h new file mode 100644 index 0000000..3603401 --- /dev/null +++ b/Plugins/RegExpImport/regexpimport.h @@ -0,0 +1,52 @@ +#ifndef REGEXPIMPORT_H +#define REGEXPIMPORT_H + +#include "regexpimport_global.h" +#include "plugins/genericplugin.h" +#include "plugins/importplugin.h" +#include "config_builder.h" + +class QRegularExpression; +class QFile; +class QTextStream; + +CFG_CATEGORIES(RegExpImportConfig, + CFG_CATEGORY(RegExpImport, + CFG_ENTRY(QString, Pattern, QString()) + CFG_ENTRY(QString, GroupsMode, "all") // all / custom + CFG_ENTRY(QString, CustomGroupList, QString()) + ) +) + +class REGEXPIMPORTSHARED_EXPORT RegExpImport : public GenericPlugin, public ImportPlugin +{ + Q_OBJECT + SQLITESTUDIO_PLUGIN("regexpimport.json") + + public: + RegExpImport(); + + bool init(); + void deinit(); + QString getDataSourceTypeName() const; + ImportManager::StandardConfigFlags standardOptionsToEnable() const; + QString getFileFilter() const; + bool beforeImport(const ImportManager::StandardImportConfig& config); + void afterImport(); + QList getColumns() const; + QList next(); + CfgMain* getConfig(); + QString getImportConfigFormName() const; + bool validateOptions(); + + private: + CFG_LOCAL(RegExpImportConfig, cfg) + QRegularExpression* re = nullptr; + QList groups; + QStringList columns; + QFile* file = nullptr; + QTextStream* stream = nullptr; + QString buffer; +}; + +#endif // REGEXPIMPORT_H diff --git a/Plugins/RegExpImport/regexpimport.json b/Plugins/RegExpImport/regexpimport.json new file mode 100644 index 0000000..8a0f746 --- /dev/null +++ b/Plugins/RegExpImport/regexpimport.json @@ -0,0 +1,7 @@ +{ + "type": "ImportPlugin", + "title": "RegExp import", + "description": "Importing data from text files using regular expression.", + "version": 10000, + "author": "SalSoft" +} diff --git a/Plugins/RegExpImport/regexpimport.qrc b/Plugins/RegExpImport/regexpimport.qrc new file mode 100644 index 0000000..9ece86e --- /dev/null +++ b/Plugins/RegExpImport/regexpimport.qrc @@ -0,0 +1,5 @@ + + + regexpimport.ui + + diff --git a/Plugins/RegExpImport/regexpimport.ui b/Plugins/RegExpImport/regexpimport.ui new file mode 100644 index 0000000..d1287cb --- /dev/null +++ b/Plugins/RegExpImport/regexpimport.ui @@ -0,0 +1,99 @@ + + + RegExpImportConfig + + + + 0 + 0 + 400 + 133 + + + + Form + + + + + + Capture groups + + + + + + Treat all RegExp capture groups as columns + + + all + + + RegExpImport.GroupsMode + + + + + + + Import only following groups: + + + custom + + + RegExpImport.GroupsMode + + + + + + + <p>Enter comma separated list of capture group indexes. The 0 index refers to the entire matched string.</p> +<p>If you used named groups in the pattern, you can use names instead of indexes. You can mix indexes and names in this list.</p> + + + Example: 1, 3, 4 + + + RegExpImport.CustomGroupList + + + + + + + + + + Pattern: + + + + + + + <p>Use Regular Expression groups to enclose parts of the expression that you want to import. If you want to use a group, that you don't want to import, then use "import only following groups" option below. + +You can use named groups and refer to them in group list below. To name a group use: <pre>(?&lt;myGroupName&gt;\s+\d+\s+)</pre></p> + + + Example: (\d+)\s+((\d+)\w+)\s+(\w+) + + + RegExpImport.Pattern + + + + + + + + ConfigRadioButton + QRadioButton +
common/configradiobutton.h
+
+
+ + +
diff --git a/Plugins/RegExpImport/regexpimport_global.h b/Plugins/RegExpImport/regexpimport_global.h new file mode 100644 index 0000000..c09b742 --- /dev/null +++ b/Plugins/RegExpImport/regexpimport_global.h @@ -0,0 +1,12 @@ +#ifndef REGEXPIMPORT_GLOBAL_H +#define REGEXPIMPORT_GLOBAL_H + +#include + +#if defined(REGEXPIMPORT_LIBRARY) +# define REGEXPIMPORTSHARED_EXPORT Q_DECL_EXPORT +#else +# define REGEXPIMPORTSHARED_EXPORT Q_DECL_IMPORT +#endif + +#endif // REGEXPIMPORT_GLOBAL_H -- cgit v1.2.3