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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
#include "regexpimport.h"
#include "services/notifymanager.h"
#include "common/utils.h"
#include "services/importmanager.h"
#include "sqlitestudio.h"
#include <QRegularExpression>
#include <QFile>
#include <QTextStream>
RegExpImport::RegExpImport()
{
}
bool RegExpImport::init()
{
SQLS_INIT_RESOURCE(regexpimport);
return GenericPlugin::init();
}
void RegExpImport::deinit()
{
SQLS_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<ImportPlugin::ColumnDefinition> RegExpImport::getColumns() const
{
QList<ImportPlugin::ColumnDefinition> columnList;
for (const QString& colName : columns)
columnList << ImportPlugin::ColumnDefinition(colName, QString());
return columnList;
}
QList<QVariant> 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<QVariant>();
QList<QVariant> 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("<p>Requested capture group name '%1', but it's not defined in the pattern: <pre>%2</pre></p>")
.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;
}
|