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/XmlExport/XmlExport.pro | 27 ++ Plugins/XmlExport/XmlExport.ui | 151 +++++++++++ Plugins/XmlExport/xmlexport.cpp | 468 +++++++++++++++++++++++++++++++++++ Plugins/XmlExport/xmlexport.h | 75 ++++++ Plugins/XmlExport/xmlexport.json | 7 + Plugins/XmlExport/xmlexport.qrc | 5 + Plugins/XmlExport/xmlexport_global.h | 12 + 7 files changed, 745 insertions(+) create mode 100644 Plugins/XmlExport/XmlExport.pro create mode 100644 Plugins/XmlExport/XmlExport.ui create mode 100644 Plugins/XmlExport/xmlexport.cpp create mode 100644 Plugins/XmlExport/xmlexport.h create mode 100644 Plugins/XmlExport/xmlexport.json create mode 100644 Plugins/XmlExport/xmlexport.qrc create mode 100644 Plugins/XmlExport/xmlexport_global.h (limited to 'Plugins/XmlExport') diff --git a/Plugins/XmlExport/XmlExport.pro b/Plugins/XmlExport/XmlExport.pro new file mode 100644 index 0000000..87e0c7f --- /dev/null +++ b/Plugins/XmlExport/XmlExport.pro @@ -0,0 +1,27 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2014-06-15T05:20:57 +# +#------------------------------------------------- + +include($$PWD/../../SQLiteStudio3/plugins.pri) + +QT -= gui + +TARGET = XmlExport +TEMPLATE = lib + +DEFINES += XMLEXPORT_LIBRARY + +SOURCES += xmlexport.cpp + +HEADERS += xmlexport.h\ + xmlexport_global.h + +FORMS += XmlExport.ui + +OTHER_FILES += \ + xmlexport.json + +RESOURCES += \ + xmlexport.qrc diff --git a/Plugins/XmlExport/XmlExport.ui b/Plugins/XmlExport/XmlExport.ui new file mode 100644 index 0000000..2a236c6 --- /dev/null +++ b/Plugins/XmlExport/XmlExport.ui @@ -0,0 +1,151 @@ + + + XmlExportConfig + + + + 0 + 0 + 325 + 280 + + + + Form + + + + + + Output format + + + + + + Format document (new lines, indentation) + + + true + + + format + + + XmlExport.Format + + + + + + + Compress (everything in one line) + + + compress + + + XmlExport.Format + + + + + + + + + + Special characters escaping + + + + + + <p>Ampersands will be used for shorter values and CDATA will be used for larger values. This applies only to values that require character escaping. Other values will be exported as they are.</p> + + + Use CDATA and ampersands + + + true + + + mixed + + + XmlExport.Escaping + + + + + + + <p>Every value requiring character escepe will be enclosed in CDATA block.</p> + + + Always use CDATA + + + cdata + + + XmlExport.Escaping + + + + + + + <p>Every character that require esceping will be replaced with its ampersand escape sequence. No CDATA blocks will be used.</p> + + + Always use ampersand + + + ampersand + + + XmlExport.Escaping + + + + + + + + + + Define XML namespace + + + true + + + false + + + XmlExport.UseNamespace + + + + + + XmlExport.Namespace + + + + + + + + + + + ConfigRadioButton + QRadioButton +
common/configradiobutton.h
+
+
+ + +
diff --git a/Plugins/XmlExport/xmlexport.cpp b/Plugins/XmlExport/xmlexport.cpp new file mode 100644 index 0000000..746d246 --- /dev/null +++ b/Plugins/XmlExport/xmlexport.cpp @@ -0,0 +1,468 @@ +#include "xmlexport.h" +#include "services/exportmanager.h" +#include "common/unused.h" +#include + +const QString XmlExport::docBegin = QStringLiteral("\n"); + +XmlExport::XmlExport() +{ +} + +QString XmlExport::getFormatName() const +{ + return QStringLiteral("XML"); +} + +ExportManager::StandardConfigFlags XmlExport::standardOptionsToEnable() const +{ + return ExportManager::CODEC; +} + +QString XmlExport::getExportConfigFormName() const +{ + return QStringLiteral("XmlExportConfig"); +} + +CfgMain* XmlExport::getConfig() +{ + return &cfg; +} + +void XmlExport::validateOptions() +{ + bool useNs = cfg.XmlExport.UseNamespace.get(); + EXPORT_MANAGER->updateVisibilityAndEnabled(cfg.XmlExport.Namespace, true, useNs); + + bool nsValid = !useNs || !cfg.XmlExport.Namespace.get().isEmpty(); + EXPORT_MANAGER->handleValidationFromPlugin(nsValid, cfg.XmlExport.Namespace, tr("Enter the namespace to use (for example: http://my.namespace.org")); +} + +QString XmlExport::defaultFileExtension() const +{ + return QStringLiteral("xml"); +} + +bool XmlExport::beforeExportQueryResults(const QString& query, QList& columns, const QHash providedData) +{ + UNUSED(providedData); + + setupConfig(); + + write(docBegin.arg(codecName)); + + writeln(QString("").arg(nsStr)); + incrIndent(); + + writeln(""); + incrIndent(); + writeln(escape(query)); + decrIndent(); + writeln(""); + + QList columnTypes = QueryExecutor::resolveColumnTypes(db, columns, true); + writeln(""); + incrIndent(); + int i = 0; + DataType type; + for (QueryExecutor::ResultColumnPtr col : columns) + { + type = columnTypes[i]; + + writeln(""); + incrIndent(); + writeln(""+ escape(col->displayName) + ""); + writeln(""+ escape(col->column) + ""); + writeln(""+ escape(col->table) + "
"); + writeln(""+ escape(col->database) + ""); + writeln(""+ escape(type.toFullTypeString()) + ""); + decrIndent(); + writeln("
"); + i++; + } + decrIndent(); + writeln("
"); + + writeln(""); + incrIndent(); + return true; +} + +bool XmlExport::exportQueryResultsRow(SqlResultsRowPtr row) +{ + static const QString rowTpl = QStringLiteral("%2"); + static const QString nullTpl = QStringLiteral(""); + + writeln(""); + incrIndent(); + + int i = 0; + for (const QVariant& value : row->valueList()) + { + if (value.isNull()) + writeln(nullTpl.arg(i)); + else + writeln(rowTpl.arg(i).arg(escape(value.toString()))); + } + + decrIndent(); + writeln(""); + return true; +} + +bool XmlExport::afterExportQueryResults() +{ + decrIndent(); + write(""); + decrIndent(); + write(""); + return true; +} + +bool XmlExport::exportTable(const QString& database, const QString& table, const QStringList& columnNames, const QString& ddl, SqliteCreateTablePtr createTable, const QHash providedData) +{ + UNUSED(columnNames); + UNUSED(providedData); + if (isTableExport()) + { + setupConfig(); + write(docBegin.arg(codecName)); + } + + writeln(QString("").arg(isTableExport() ? nsStr : "")); + incrIndent(); + + writeln("" + escape(database) + ""); + writeln("" + escape(table) + ""); + if (!createTable->withOutRowId.isNull()) + writeln(QString("true")); + + writeln("" + escape(ddl) + ""); + + writeln(""); + incrIndent(); + for (SqliteCreateTable::Column* col : createTable->columns) + { + writeln(""); + incrIndent(); + writeln(""+ col->name + ""); + writeln(QString("%1").arg((col->type ? col->type->toDataType().toFullTypeString() : ""))); + if (col->constraints.size() > 0) + { + writeln(""); + incrIndent(); + for (SqliteCreateTable::Column::Constraint* constr : col->constraints) + { + writeln(""); + incrIndent(); + writeln("" + constr->typeString() + ""); + writeln("" + constr->detokenize() + ""); + decrIndent(); + writeln(""); + } + decrIndent(); + writeln(""); + } + decrIndent(); + writeln(""); + } + decrIndent(); + writeln(""); + + if (createTable->constraints.size() > 0) + { + writeln(""); + incrIndent(); + for (SqliteCreateTable::Constraint* constr : createTable->constraints) + { + writeln(""); + incrIndent(); + writeln("" + constr->typeString() + ""); + writeln("" + constr->detokenize() + ""); + decrIndent(); + writeln(""); + } + decrIndent(); + writeln(""); + } + + writeln(""); + incrIndent(); + return true; +} + +bool XmlExport::exportVirtualTable(const QString& database, const QString& table, const QStringList& columnNames, const QString& ddl, SqliteCreateVirtualTablePtr createTable, const QHash providedData) +{ + UNUSED(providedData); + + if (isTableExport()) + { + setupConfig(); + write(docBegin.arg(codecName)); + } + + writeln(QString("").arg(isTableExport() ? nsStr : "")); + incrIndent(); + + writeln("" + escape(database) + ""); + writeln("" + escape(table) + ""); + writeln("true"); + writeln("" + escape(createTable->module) + ""); + + writeln("" + escape(ddl) + ""); + + writeln(""); + incrIndent(); + for (const QString& col : columnNames) + { + writeln(""); + incrIndent(); + writeln(""+ col + ""); + decrIndent(); + writeln(""); + } + decrIndent(); + writeln(""); + + if (createTable->args.size() > 0) + { + writeln(""); + incrIndent(); + for (const QString& arg : createTable->args) + writeln("" + arg + ""); + + decrIndent(); + writeln(""); + } + + writeln(""); + incrIndent(); + return true; +} + +bool XmlExport::exportTableRow(SqlResultsRowPtr data) +{ + return exportQueryResultsRow(data); +} + +bool XmlExport::afterExportTable() +{ + decrIndent(); + writeln(""); + decrIndent(); + writeln(""); + return true; +} + +bool XmlExport::beforeExportDatabase(const QString& database) +{ + setupConfig(); + write(docBegin.arg(codecName)); + + writeln(QString("").arg(nsStr)); + incrIndent(); + writeln("" + escape(database) + ""); + + return true; +} + +bool XmlExport::exportIndex(const QString& database, const QString& name, const QString& ddl, SqliteCreateIndexPtr createIndex) +{ + writeln(""); + incrIndent(); + + writeln("" + escape(database) + ""); + writeln("" + escape(name) + ""); + if (createIndex->uniqueKw) + writeln("true"); + + if (createIndex->where) + writeln("" + createIndex->where->detokenize() + ""); + + writeln("" + escape(ddl) + ""); + + decrIndent(); + writeln(""); + return true; +} + +bool XmlExport::exportTrigger(const QString& database, const QString& name, const QString& ddl, SqliteCreateTriggerPtr createTrigger) +{ + UNUSED(createTrigger); + writeln(""); + incrIndent(); + + writeln("" + escape(database) + ""); + writeln("" + escape(name) + ""); + writeln("" + escape(ddl) + ""); + + QString timing = SqliteCreateTrigger::time(createTrigger->eventTime); + writeln("" + escape(timing) + ""); + + QString event = createTrigger->event ? SqliteCreateTrigger::Event::typeToString(createTrigger->event->type) : ""; + writeln("" + escape(event) + ""); + + QString tag; + if (createTrigger->eventTime == SqliteCreateTrigger::Time::INSTEAD_OF) + tag = ""; + else + tag = ""; + + writeln(tag + escape(createTrigger->table) + tag); + + if (createTrigger->precondition) + writeln("" + escape(createTrigger->precondition->detokenize()) + ""); + + QStringList queryStrings; + for (SqliteQuery* q : createTrigger->queries) + queryStrings << q->detokenize(); + + writeln("" + escape(queryStrings.join("\n")) + ""); + + decrIndent(); + writeln(""); + return true; +} + +bool XmlExport::exportView(const QString& database, const QString& name, const QString& ddl, SqliteCreateViewPtr createView) +{ + UNUSED(createView); + writeln(""); + incrIndent(); + + writeln("" + escape(database) + ""); + writeln("" + escape(name) + ""); + writeln("" + escape(ddl) + ""); + writeln(""); + decrIndent(); + writeln(""); + return true; +} + +bool XmlExport::afterExportDatabase() +{ + decrIndent(); + writeln(""); + return true; +} + +void XmlExport::setupConfig() +{ + codecName = codec->name(); + indentDepth = 0; + newLineStr = ""; + indentStr = ""; + indent = (cfg.XmlExport.Format.get() == "format"); + if (indent) + newLineStr = "\n"; + + nsStr = QString(); + if (cfg.XmlExport.UseNamespace.get()) + nsStr = " xmlns=\"" + cfg.XmlExport.Namespace.get() + "\""; + + if (cfg.XmlExport.Escaping.get() == "ampersand") + { + useAmpersand = true; + useCdata = false; + } + else if (cfg.XmlExport.Escaping.get() == "cdata") + { + useAmpersand = false; + useCdata = true; + } + else + { + useAmpersand = true; + useCdata = true; + } +} + +void XmlExport::incrIndent() +{ + if (indent) + { + indentDepth++; + updateIndent(); + } +} + +void XmlExport::decrIndent() +{ + if (indent) + { + indentDepth--; + updateIndent(); + } +} + +void XmlExport::updateIndent() +{ + indentStr = QString(" ").repeated(indentDepth); +} + +void XmlExport::writeln(const QString& str) +{ + QString newStr; + if (str.contains("\n")) + { + QStringList lines = str.split("\n"); + QMutableStringListIterator it(lines); + while (it.hasNext()) + it.value().prepend(indentStr); + + newStr = lines.join("\n") + newLineStr; + } + else + { + newStr = indentStr + str + newLineStr; + } + GenericExportPlugin::write(newStr); +} + +QString XmlExport::escape(const QString& str) +{ + if (useAmpersand && useCdata) + { + if (str.length() >= minLenghtForCdata) + return escapeCdata(str); + else + return escapeAmpersand(str); + } + else if (useAmpersand) + { + return escapeAmpersand(str); + } + else + { + return escapeCdata(str); + } +} + +QString XmlExport::escapeCdata(const QString& str) +{ + if (str.contains('"') || str.contains('&') || str.contains('<') || str.contains('>')) + return ""; + + return str; +} + +QString XmlExport::escapeAmpersand(const QString& str) +{ + return str.toHtmlEscaped(); +} + +QString XmlExport::toString(bool value) +{ + return value ? "true" : "false"; +} + +bool XmlExport::init() +{ + Q_INIT_RESOURCE(xmlexport); + return GenericExportPlugin::init(); +} + +void XmlExport::deinit() +{ + Q_CLEANUP_RESOURCE(xmlexport); +} diff --git a/Plugins/XmlExport/xmlexport.h b/Plugins/XmlExport/xmlexport.h new file mode 100644 index 0000000..271a687 --- /dev/null +++ b/Plugins/XmlExport/xmlexport.h @@ -0,0 +1,75 @@ +#ifndef XMLEXPORT_H +#define XMLEXPORT_H + +#include "xmlexport_global.h" +#include "plugins/genericexportplugin.h" +#include "config_builder.h" + +CFG_CATEGORIES(XmlExportConfig, + CFG_CATEGORY(XmlExport, + CFG_ENTRY(QString, Format, "format") + CFG_ENTRY(bool, UseNamespace, false) + CFG_ENTRY(QString, Namespace, QString()) + CFG_ENTRY(QString, Escaping, "mixed") + ) +) + +class XMLEXPORTSHARED_EXPORT XmlExport : public GenericExportPlugin +{ + Q_OBJECT + SQLITESTUDIO_PLUGIN("xmlexport.json") + + public: + XmlExport(); + + QString getFormatName() const; + ExportManager::StandardConfigFlags standardOptionsToEnable() const; + QString getExportConfigFormName() const; + CfgMain* getConfig(); + void validateOptions(); + QString defaultFileExtension() const; + bool beforeExportQueryResults(const QString& query, QList& columns, + const QHash providedData); + bool exportQueryResultsRow(SqlResultsRowPtr row); + bool afterExportQueryResults(); + bool exportTable(const QString& database, const QString& table, const QStringList& columnNames, const QString& ddl, SqliteCreateTablePtr createTable, + const QHash providedData); + bool exportVirtualTable(const QString& database, const QString& table, const QStringList& columnNames, const QString& ddl, SqliteCreateVirtualTablePtr createTable, + const QHash providedData); + bool exportTableRow(SqlResultsRowPtr data); + bool afterExportTable(); + bool beforeExportDatabase(const QString& database); + bool exportIndex(const QString& database, const QString& name, const QString& ddl, SqliteCreateIndexPtr createIndex); + bool exportTrigger(const QString& database, const QString& name, const QString& ddl, SqliteCreateTriggerPtr createTrigger); + bool exportView(const QString& database, const QString& name, const QString& ddl, SqliteCreateViewPtr createView); + bool afterExportDatabase(); + bool init(); + void deinit(); + + private: + void setupConfig(); + void incrIndent(); + void decrIndent(); + void updateIndent(); + void writeln(const QString& str); + QString escape(const QString& str); + QString escapeCdata(const QString& str); + QString escapeAmpersand(const QString& str); + + static QString toString(bool value); + + CFG_LOCAL(XmlExportConfig, cfg) + bool indent = false; + int indentDepth = 0; + QString indentStr; + QString newLineStr; + QString nsStr; + QString codecName; + bool useAmpersand = true; + bool useCdata = true; + static const QString docBegin; + + static constexpr int minLenghtForCdata = 100; +}; + +#endif // XMLEXPORT_H diff --git a/Plugins/XmlExport/xmlexport.json b/Plugins/XmlExport/xmlexport.json new file mode 100644 index 0000000..2432cdd --- /dev/null +++ b/Plugins/XmlExport/xmlexport.json @@ -0,0 +1,7 @@ +{ + "type": "ExportPlugin", + "title": "XML export", + "description": "Provides XML format for exporting.", + "version": 10000, + "author": "SalSoft" +} diff --git a/Plugins/XmlExport/xmlexport.qrc b/Plugins/XmlExport/xmlexport.qrc new file mode 100644 index 0000000..d6a77a6 --- /dev/null +++ b/Plugins/XmlExport/xmlexport.qrc @@ -0,0 +1,5 @@ + + + XmlExport.ui + + diff --git a/Plugins/XmlExport/xmlexport_global.h b/Plugins/XmlExport/xmlexport_global.h new file mode 100644 index 0000000..fb1f244 --- /dev/null +++ b/Plugins/XmlExport/xmlexport_global.h @@ -0,0 +1,12 @@ +#ifndef XMLEXPORT_GLOBAL_H +#define XMLEXPORT_GLOBAL_H + +#include + +#if defined(XMLEXPORT_LIBRARY) +# define XMLEXPORTSHARED_EXPORT Q_DECL_EXPORT +#else +# define XMLEXPORTSHARED_EXPORT Q_DECL_IMPORT +#endif + +#endif // XMLEXPORT_GLOBAL_H -- cgit v1.2.3