KItinerary

extractordocumentnode.cpp
1/*
2 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "extractordocumentnode.h"
8#include "extractordocumentprocessor.h"
9#include "extractorfilter.h"
10#include "extractorresult.h"
11
12#include <QJSEngine>
13#include <QJSValue>
14
15#include <cassert>
16
17using namespace KItinerary;
18
19namespace KItinerary {
20class ExtractorDocumentNodePrivate
21{
22public:
23 std::weak_ptr<ExtractorDocumentNodePrivate> parent;
24 std::vector<ExtractorDocumentNode> childNodes;
25 QString mimeType;
26 QVariant content;
27 QDateTime contextDateTime;
28 const ExtractorDocumentProcessor *processor;
29 ExtractorResult result;
30 QVariant location;
31 QJSEngine *m_jsEngine = nullptr;
32 QString usedExtractor;
33
34 QJSEngine *jsEngine() const;
35};
36}
37
38QJSEngine* ExtractorDocumentNodePrivate::jsEngine() const
39{
40 if (m_jsEngine) {
41 return m_jsEngine;
42 }
43 const auto p = parent.lock();
44 return p ? p->jsEngine() : nullptr;
45}
46
48 : d(std::make_shared<ExtractorDocumentNodePrivate>())
49{
50}
51
52ExtractorDocumentNode::ExtractorDocumentNode(const std::shared_ptr<ExtractorDocumentNodePrivate> &dd)
53 : d(dd ? dd : std::make_shared<ExtractorDocumentNodePrivate>())
54{
55}
56
59
60ExtractorDocumentNode::~ExtractorDocumentNode()
61{
62 if (d && d.use_count() == 1 && d->processor) {
63 d->processor->destroyNode(*this);
64 }
65}
66
67ExtractorDocumentNode& ExtractorDocumentNode::operator=(const ExtractorDocumentNode &other)
68{
69 if (d && d.use_count() == 1 && d->processor) {
70 d->processor->destroyNode(*this);
71 }
72 d = other.d;
73 return *this;
74}
75
76ExtractorDocumentNode& ExtractorDocumentNode::operator=(ExtractorDocumentNode &&other)
77{
78 if (d && d.use_count() == 1 && d->processor) {
79 d->processor->destroyNode(*this);
80 }
81 d = std::move(other.d);
82 return *this;
83}
84
85bool ExtractorDocumentNode::operator==(const ExtractorDocumentNode &other) const
86{
87 return d.get() == other.d.get();
88}
89
90bool ExtractorDocumentNode::isNull() const
91{
92 return d->content.isNull() || !d->processor || d->mimeType.isEmpty();
93}
94
96{
97 return ExtractorDocumentNode(d->parent.lock());
98}
99
100void ExtractorDocumentNode::setParent(const ExtractorDocumentNode &parent)
101{
102 d->parent = parent.d;
103}
104
106{
107 return d->mimeType;
108}
109
110void ExtractorDocumentNode::setMimeType(const QString &mimeType)
111{
112 d->mimeType = mimeType;
113}
114
116{
117 return d->content;
118}
119
121{
122 d->content = content;
123}
124
125const ExtractorDocumentProcessor* ExtractorDocumentNode::processor() const
126{
127 return d->processor;
128}
129
130void ExtractorDocumentNode::setProcessor(const ExtractorDocumentProcessor *processor)
131{
132 assert(!d->processor);
133 d->processor = processor;
134}
135
136const std::vector<ExtractorDocumentNode>& ExtractorDocumentNode::childNodes() const
137{
138 return d->childNodes;
139}
140
142{
143 if (child.isNull()) {
144 return;
145 }
146 child.setParent(*this);
147 d->childNodes.push_back(child);
148}
149
151{
152 return d->result;
153}
154
156{
157 d->result.append(std::move(result));
158}
159
161{
162 d->result = std::move(result);
163}
164
166{
167 if (!d->contextDateTime.isValid() && !d->parent.expired()) {
168 return parent().contextDateTime();
169 }
170 return d->contextDateTime;
171}
172
174{
175 d->contextDateTime = contextDateTime;
176}
177
179{
180 if (d->location.isNull() && !d->parent.expired()) {
181 return parent().location();
182 }
183 return d->location;
184}
185
187{
188 d->location = location;
189}
190
191QJsonArray ExtractorDocumentNode::jsonLdResult() const
192{
193 return d->result.jsonLdResult();
194}
195
196QVariantList ExtractorDocumentNode::childNodesVariant() const
197{
198 QVariantList l;
199 l.reserve(d->childNodes.size());
200 std::transform(d->childNodes.begin(), d->childNodes.end(), std::back_inserter(l), [](const auto &c) { return QVariant::fromValue(c); });
201 return l;
202}
203
204QJSValue ExtractorDocumentNode::contentJsValue() const
205{
206 if (!d || !d->processor) {
207 return {};
208 }
209 if (auto jsEngine = d->jsEngine()) {
210 return d->processor->contentToScriptValue(*this, jsEngine);
211 }
212 return {};
213}
214
215void ExtractorDocumentNode::setScriptEngine(QJSEngine* jsEngine) const
216{
217 if (!d->parent.expired()) {
218 parent().setScriptEngine(jsEngine);
219 } else {
220 d->m_jsEngine = jsEngine;
221 }
222}
223
224QVariantList ExtractorDocumentNode::findChildNodes(const QJSValue &jsFilter) const
225{
226 const auto filter = ExtractorFilter::fromJSValue(jsFilter);
227 std::vector<ExtractorDocumentNode> matches;
228 filter.allMatches(*this, matches);
229
230 QVariantList l;
231 l.reserve(matches.size());
232 std::transform(matches.begin(), matches.end(), std::back_inserter(l), [](const auto &c) { return QVariant::fromValue(c); });
233 return l;
234}
235
237{
238 return d->usedExtractor;
239}
240
241void ExtractorDocumentNode::setUsedExtractor(const QString &usedExtractor)
242{
243 d->usedExtractor = usedExtractor;
244}
245
246#include "moc_extractordocumentnode.cpp"
A node in the extracted document object tree.
QJsonArray result
Result access for QJSEngine.
Q_INVOKABLE QVariantList findChildNodes(const QJSValue &jsFilter) const
JS API for finding child nodes given an KItinerary::ExtractorFilter.
void setResult(ExtractorResult &&result)
Replace the existing results by result.
void appendChild(ExtractorDocumentNode &child)
Add another child node.
void setContextDateTime(const QDateTime &contextDateTime)
Set the context date/time.
QString mimeType
The MIME type of this node.
QJSValue content
The decoded content of this node.
void addResult(ExtractorResult &&result)
Add additional results from an extraction step.
QVariantList childNodes
Child nodes, for QJSEngine access.
QDateTime contextDateTime
The best known context date/time at this point in the document tree.
QVariant location
Information about the location of this node in relation to one of its ancestors.
QString usedExtractor() const
Extractor used for the result of this node, if any.
void setContent(const QVariant &content)
Set decoded content.
void setLocation(const QVariant &location)
Set the location information.
KItinerary::ExtractorDocumentNode parent
The parent node, or a null node if this is the root node.
Abstract base class of a document type processor.
Generic extraction result.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.