7#include "extractordocumentnodefactory.h"
8#include "extractordocumentnode.h"
9#include "extractordocumentprocessor.h"
12#include "processors/binarydocumentprocessor.h"
13#include "processors/dosipasdocumentprocessor.h"
14#include "processors/eradocumentprocessor.h"
15#include "processors/externalprocessor.h"
16#include "processors/htmldocumentprocessor.h"
17#include "processors/httpresponseprocessor.h"
18#include "processors/iatabcbpdocumentprocessor.h"
19#include "processors/icaldocumentprocessor.h"
20#include "processors/imagedocumentprocessor.h"
21#include "processors/jsonlddocumentprocessor.h"
22#include "processors/mimedocumentprocessor.h"
23#include "processors/pdfdocumentprocessor.h"
24#include "processors/pkpassdocumentprocessor.h"
25#include "processors/plistdocumentprocessor.h"
26#include "processors/textdocumentprocessor.h"
27#include "processors/uic9183documentprocessor.h"
28#include "processors/vdvdocumentprocessor.h"
31#include <QMimeDatabase>
37 MaxDocumentSize = 10000000,
41class ExtractorDocumentNodeFactoryStatic {
43 ExtractorDocumentNodeFactoryStatic();
45 void registerProcessor(std::unique_ptr<ExtractorDocumentProcessor> &&processor, QStringView canonicalMimeType,
46 std::initializer_list<QStringView> aliasMimeTypes = {}, QStringView fallbackMimeType = {});
49 inline void registerProcessor(QStringView canonicalMimeType, std::initializer_list<QStringView> aliasMimeTypes = {}, QStringView fallbackMimeType = {})
51 registerProcessor(std::make_unique<T>(), canonicalMimeType, aliasMimeTypes, fallbackMimeType);
54 void registerBuiltIn();
55 QStringView resolveAlias(QStringView mimeType)
const;
57 struct ProcessorData {
59 const ExtractorDocumentProcessor* processor;
61 std::vector<ProcessorData> m_probeProcessors;
62 std::vector<ProcessorData> m_fallbackProbeProcessors;
63 std::vector<ProcessorData> m_mimetypeProcessorMap;
64 QHash<QString, QString> m_aliasMap;
67 std::vector<std::unique_ptr<ExtractorDocumentProcessor>> processorPool;
69 static void insertProcessor(
const ExtractorDocumentProcessor *proc, QStringView mimeType, std::vector<ProcessorData> &procMap);
72class ExtractorDocumentNodeFactoryPrivate {
74 ExtractorDocumentNodeFactoryStatic *s;
75 std::unique_ptr<ExtractorDocumentProcessor> interceptProcessor;
79ExtractorDocumentNodeFactoryStatic::ExtractorDocumentNodeFactoryStatic()
84void ExtractorDocumentNodeFactoryStatic::insertProcessor(
const ExtractorDocumentProcessor *proc, QStringView mimeType, std::vector<ProcessorData> &procMap)
90 const auto it = std::lower_bound(procMap.begin(), procMap.end(), mimeType, [](
const auto &proc,
auto mt) {
91 return proc.mimeType < mt;
93 if (it != procMap.end() && (*it).mimeType == mimeType) {
94 qCWarning(Log) <<
"Document processor already registered for mimetype:" <<
mimeType;
98 procMap.insert(it, {
mimeType.toString(), proc });
101void ExtractorDocumentNodeFactoryStatic::registerProcessor(std::unique_ptr<ExtractorDocumentProcessor> &&processor, QStringView canonicalMimeType,
102 std::initializer_list<QStringView> aliasMimeTypes, QStringView fallbackMimeType)
104 insertProcessor(processor.get(), canonicalMimeType, m_probeProcessors);
105 insertProcessor(processor.get(), canonicalMimeType, m_mimetypeProcessorMap);
106 for (
const auto mt : aliasMimeTypes) {
107 m_aliasMap.insert(mt.toString(), canonicalMimeType.
isEmpty() ? fallbackMimeType.
toString() : canonicalMimeType.
toString());
109 if (!fallbackMimeType.
isEmpty()) {
110 m_fallbackProbeProcessors.push_back({ fallbackMimeType.
toString(), processor.get() });
112 insertProcessor(processor.get(), fallbackMimeType, m_mimetypeProcessorMap);
113 processorPool.push_back(std::move(processor));
116void ExtractorDocumentNodeFactoryStatic::registerBuiltIn()
118 registerProcessor<PdfDocumentProcessor>(u
"application/pdf");
119 registerProcessor<PkPassDocumentProcessor>(u
"application/vnd.apple.pkpass");
120 registerProcessor<IcalEventProcessor>(u
"internal/event");
121 registerProcessor<ImageDocumentProcessor>(u
"internal/qimage", {u
"image/png", u
"image/jpeg", u
"image/gif"});
122 registerProcessor<ElbDocumentProcessor>(u
"internal/era-elb");
123 registerProcessor<SsbDocumentProcessor>(u
"internal/era-ssb");
124 registerProcessor<IataBcbpDocumentProcessor>(u
"internal/iata-bcbp");
125 registerProcessor<Uic9183DocumentProcessor>(u
"internal/uic9183");
126 registerProcessor<DosipasDocumentProcessor>(u
"internal/uic-dosipas");
127 registerProcessor<VdvDocumentProcessor>(u
"internal/vdv");
128 registerProcessor<IcalCalendarProcessor>(u
"text/calendar");
129 registerProcessor<PListDocumentProcessor>(u
"application/x-plist");
130 registerProcessor<HttpResponseProcessor>(u
"internal/http-response");
131 registerProcessor<HarDocumentProcessor>(u
"internal/har-archive");
135 registerProcessor<JsonLdDocumentProcessor>({}, {u
"application/json"}, u
"application/ld+json");
136 registerProcessor<MimeDocumentProcessor>({}, {u
"application/mbox"}, u
"message/rfc822");
137 registerProcessor<HtmlDocumentProcessor>({}, {u
"application/xhtml+xml"}, u
"text/html");
138 registerProcessor<TextDocumentProcessor>({}, {}, u
"text/plain");
139 registerProcessor<BinaryDocumentProcessor>({}, {}, u
"application/octet-stream");
142QStringView ExtractorDocumentNodeFactoryStatic::resolveAlias(QStringView mimeType)
const
144 const auto it = m_aliasMap.find(
mimeType.toString());
145 if (it != m_aliasMap.end()) {
152ExtractorDocumentNodeFactory::ExtractorDocumentNodeFactory()
153 : d(std::make_unique<ExtractorDocumentNodeFactoryPrivate>())
155 static ExtractorDocumentNodeFactoryStatic s_factory;
159ExtractorDocumentNodeFactory::~ExtractorDocumentNodeFactory() =
default;
163 if (data.
size() <= MinDocumentSize || data.
size() > MaxDocumentSize) {
167 if (d->interceptProcessor && d->interceptProcessor->canHandleData(data, fileName)) {
168 auto node = d->interceptProcessor->createNodeFromData(data);
169 if (node.mimeType().isEmpty()) {
170 node.setMimeType(QStringLiteral(
"internal/external-process"));
172 node.setProcessor(d->interceptProcessor.get());
184 mimeType = d->s->resolveAlias(autoDetectedMimeType);
187 for (
const auto &p : d->s->m_probeProcessors) {
188 if (p.processor->canHandleData(data, fileName) || (!mimeType.
isEmpty() && p.mimeType == mimeType)) {
189 auto node = p.processor->createNodeFromData(data);
190 if (node.content().isNull()) {
194 node.setMimeType(p.mimeType);
195 node.setProcessor(p.processor);
201 for (
const auto &p : d->s->m_fallbackProbeProcessors) {
202 if (p.processor->canHandleData(data, fileName)) {
203 auto node = p.processor->createNodeFromData(data);
204 if (node.content().isNull()) {
208 node.setMimeType(p.mimeType);
209 node.setProcessor(p.processor);
218 mimeType = d->s->resolveAlias(mimeType);
219 const auto it = std::lower_bound(d->s->m_mimetypeProcessorMap.begin(), d->s->m_mimetypeProcessorMap.end(), mimeType, [](
const auto &proc,
auto mt) {
220 return proc.mimeType < mt;
222 if (it == d->s->m_mimetypeProcessorMap.end() || (*it).mimeType != mimeType) {
223 qCDebug(
Log) <<
"No document processor found for mimetype" << mimeType;
227 auto node = (*it).processor->createNodeFromData(data);
228 node.setMimeType((*it).mimeType);
229 node.setProcessor((*it).processor);
235 mimeType = d->s->resolveAlias(mimeType);
236 const auto it = std::lower_bound(d->s->m_mimetypeProcessorMap.begin(), d->s->m_mimetypeProcessorMap.end(), mimeType, [](
const auto &proc,
auto mt) {
237 return proc.mimeType < mt;
239 if (it == d->s->m_mimetypeProcessorMap.end() || (*it).mimeType != mimeType) {
240 qCDebug(
Log) <<
"No document processor found for mimetype" << mimeType;
244 auto node = (*it).processor->createNodeFromContent(decodedData);
245 node.setMimeType((*it).mimeType);
246 node.setProcessor((*it).processor);
251 std::initializer_list<QStringView> aliasMimeTypes)
253 d->s->registerProcessor(std::move(processor), mimeType, aliasMimeTypes);
258 if (separateProcess && !d->interceptProcessor) {
259 d->interceptProcessor = std::make_unique<ExternalProcessor>();
260 }
else if (!separateProcess && d->interceptProcessor) {
261 d->interceptProcessor.reset();
KCALUTILS_EXPORT QString mimeType()
Classes for reservation/travel data models, data extraction and data augmentation.
qsizetype size() const const
QMimeType mimeTypeForData(QIODevice *device) const const
QMimeType mimeTypeForFileNameAndData(const QString &fileName, QIODevice *device) const const
bool isEmpty() const const
bool isEmpty() const const
QString toString() const const