diff --git a/autotests/samplefiles/test_with_container.svg b/autotests/samplefiles/test_with_container.svg
new file mode 100644
index 0000000..35657b2
--- /dev/null
+++ b/autotests/samplefiles/test_with_container.svg
@@ -0,0 +1,9 @@
+
+
+
diff --git a/autotests/xmlextractortest.cpp b/autotests/xmlextractortest.cpp
index b912472..80891e8 100644
--- a/autotests/xmlextractortest.cpp
+++ b/autotests/xmlextractortest.cpp
@@ -1,117 +1,137 @@
/*
Copyright (C) 2018 Stefan Brüns
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see .
*/
#include "xmlextractortest.h"
#include
#include "simpleextractionresult.h"
#include "indexerextractortestsconfig.h"
#include "extractors/xmlextractor.h"
using namespace KFileMetaData;
XmlExtractorTests::XmlExtractorTests(QObject* parent) :
QObject(parent)
{
}
QString XmlExtractorTests::testFilePath(const QString& fileName) const
{
return QLatin1String(INDEXER_TESTS_SAMPLE_FILES_PATH) + QLatin1Char('/') + fileName;
}
void XmlExtractorTests::benchMarkXmlExtractor()
{
XmlExtractor plugin(this);
// generate a test file with varying number of words per line
QTemporaryFile file(QStringLiteral("XXXXXX.xml"));
QVERIFY(file.open());
int count = 0;
file.write("\n");
QByteArray chunk("foo bar ");
for (int line = 0; line < 10000; ++line) {
// staircase pattern, 0, 1, 2, ... 98, 0, 0, 1 ... chunks per line
for (int i = 0; i < line % 100; ++i) {
count++;
file.write(chunk);
}
file.write("\n");
}
file.write("\n");
file.close();
SimpleExtractionResult result(file.fileName(), QStringLiteral("application/xml"));
plugin.extract(&result);
QString content = QStringLiteral("foo bar\n");
content.replace(QLatin1Char('\n'), QLatin1Char(' '));
QCOMPARE(result.text().leftRef(8), content.leftRef(8));
QCOMPARE(result.text().size(), 8 * count);
QBENCHMARK {
plugin.extract(&result);
}
}
void XmlExtractorTests::testXmlExtractor()
{
XmlExtractor plugin{this};
SimpleExtractionResult result(testFilePath(QStringLiteral("test_with_metadata.svg")),
QStringLiteral("image/svg"),
ExtractionResult::ExtractEverything);
plugin.extract(&result);
QString content = QStringLiteral("Some text\n");
QCOMPARE(result.types().size(), 1);
QCOMPARE(result.types().at(0), Type::Image);
QCOMPARE(result.properties().size(), 1);
QCOMPARE(result.properties().value(Property::Title).toString(), QStringLiteral("Document Title"));
content.replace(QLatin1Char('\n'), QLatin1Char(' '));
QCOMPARE(result.text(), content);
}
void XmlExtractorTests::testXmlExtractorNoContent()
{
XmlExtractor plugin{this};
SimpleExtractionResult result(testFilePath(QStringLiteral("test_with_metadata.svg")),
QStringLiteral("image/svg"),
ExtractionResult::ExtractMetaData);
plugin.extract(&result);
QCOMPARE(result.types().size(), 1);
QCOMPARE(result.types().at(0), Type::Image);
QCOMPARE(result.properties().size(), 1);
QCOMPARE(result.properties().value(Property::Title).toString(), QStringLiteral("Document Title"));
QVERIFY(result.text().isEmpty());
}
+void XmlExtractorTests::testXmlExtractorContainer()
+{
+ XmlExtractor plugin{this};
+
+ SimpleExtractionResult result(testFilePath(QStringLiteral("test_with_container.svg")),
+ QStringLiteral("image/svg"),
+ ExtractionResult::ExtractEverything);
+ plugin.extract(&result);
+
+ QString content = QStringLiteral("Some text below \n");
+
+ QCOMPARE(result.types().size(), 1);
+ QCOMPARE(result.types().at(0), Type::Image);
+
+ QCOMPARE(result.properties().size(), 0);
+
+ content.replace(QLatin1Char('\n'), QLatin1Char(' '));
+ QCOMPARE(result.text(), content);
+}
+
QTEST_GUILESS_MAIN(XmlExtractorTests)
diff --git a/autotests/xmlextractortest.h b/autotests/xmlextractortest.h
index 4065410..8119aab 100644
--- a/autotests/xmlextractortest.h
+++ b/autotests/xmlextractortest.h
@@ -1,42 +1,43 @@
/*
Copyright (C) 2018 Stefan Brüns
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see .
*/
#ifndef XMLEXTRACTORTESTS_H
#define XMLEXTRACTORTESTS_H
#include
#include
class XmlExtractorTests : public QObject
{
Q_OBJECT
public:
explicit XmlExtractorTests(QObject* parent = nullptr);
private:
QString testFilePath(const QString& fileName) const;
private Q_SLOTS:
void benchMarkXmlExtractor();
void testXmlExtractor();
void testXmlExtractorNoContent();
+ void testXmlExtractorContainer();
};
#endif // XMLEXTRACTORTESTS_H
diff --git a/src/extractors/xmlextractor.cpp b/src/extractors/xmlextractor.cpp
index fccc704..5973eef 100644
--- a/src/extractors/xmlextractor.cpp
+++ b/src/extractors/xmlextractor.cpp
@@ -1,144 +1,145 @@
/*
Copyright (C) 2018 Stefan Brüns
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "xmlextractor.h"
#include "kfilemetadata_debug.h"
#include "dublincoreextractor.h"
#include
#include
#include
#include
namespace {
inline QString dcNS() { return QStringLiteral("http://purl.org/dc/elements/1.1/"); }
inline QString svgNS() { return QStringLiteral("http://www.w3.org/2000/svg"); }
inline QString rdfNS() { return QStringLiteral("http://www.w3.org/1999/02/22-rdf-syntax-ns#"); }
inline QString ccNS() { return QStringLiteral("http://creativecommons.org/ns#"); }
void extractSvgText(KFileMetaData::ExtractionResult* result, const QDomElement &node)
{
if (node.namespaceURI() != svgNS()) {
return;
}
- if (node.localName() == QLatin1String("g")) {
+ if ((node.localName() == QLatin1String("g")) ||
+ (node.localName() == QLatin1String("a"))) {
QDomElement e = node.firstChildElement();
for (; !e.isNull(); e = e.nextSiblingElement()) {
extractSvgText(result, e);
}
} else if (node.localName() == QLatin1String("text")) {
qCDebug(KFILEMETADATA_LOG) << node.text();
result->append(node.text());
}
}
static const QStringList supportedMimeTypes = {
QStringLiteral("application/xml"),
QStringLiteral("image/svg+xml"),
QStringLiteral("image/svg"),
};
}
namespace KFileMetaData
{
XmlExtractor::XmlExtractor(QObject* parent)
: ExtractorPlugin(parent)
{
}
QStringList XmlExtractor::mimetypes() const
{
return supportedMimeTypes;
}
void XmlExtractor::extract(ExtractionResult* result)
{
auto flags = result->inputFlags();
QFile file(result->inputUrl());
if (!file.open(QIODevice::ReadOnly)) {
qCWarning(KFILEMETADATA_LOG) << "Document is not a valid file";
return;
}
if ((result->inputMimetype() == QLatin1String("image/svg")) ||
(result->inputMimetype() == QLatin1String("image/svg+xml"))) {
result->addType(Type::Image);
QDomDocument doc;
const bool processNamespaces = true;
doc.setContent(&file, processNamespaces);
QDomElement svg = doc.firstChildElement();
if (!svg.isNull()
&& svg.localName() == QLatin1String("svg")
&& svg.namespaceURI() == svgNS()) {
QDomElement e = svg.firstChildElement();
for (; !e.isNull(); e = e.nextSiblingElement()) {
if (e.namespaceURI() != svgNS()) {
continue;
}
if (e.localName() == QLatin1String("metadata")) {
auto rdf = e.firstChildElement(QLatin1String("RDF"));
if (rdf.isNull() || rdf.namespaceURI() != rdfNS()) {
continue;
}
auto cc = rdf.firstChildElement(QLatin1String("Work"));
if (cc.isNull() || cc.namespaceURI() != ccNS()) {
continue;
}
DublinCoreExtractor::extract(result, cc);
} else if (e.localName() == QLatin1String("defs")) {
// skip
continue;
} else if (flags & ExtractionResult::ExtractPlainText) {
// extract
extractSvgText(result, e);
}
}
}
} else {
result->addType(Type::Text);
if (flags & ExtractionResult::ExtractPlainText) {
QXmlStreamReader stream(&file);
while (!stream.atEnd()) {
QXmlStreamReader::TokenType token = stream.readNext();
if (token == QXmlStreamReader::Characters) {
QString text = stream.text().trimmed().toString();
if (!text.isEmpty()) {
result->append(text);
}
}
}
}
}
}
} // namespace KFileMetaData