KItinerary

extractordocumentnode.cpp
1 /*
2  SPDX-FileCopyrightText: 2021 Volker Krause <[email protected]>
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 
17 using namespace KItinerary;
18 
19 namespace KItinerary {
20 class ExtractorDocumentNodePrivate
21 {
22 public:
23  std::weak_ptr<ExtractorDocumentNodePrivate> parent;
24  std::vector<ExtractorDocumentNode> childNodes;
26  QVariant content;
27  QDateTime contextDateTime;
28  const ExtractorDocumentProcessor *processor;
29  ExtractorResult result;
31  QJSEngine *m_jsEngine = nullptr;
32 
33  QJSEngine *jsEngine() const;
34 };
35 }
36 
37 QJSEngine* ExtractorDocumentNodePrivate::jsEngine() const
38 {
39  if (m_jsEngine) {
40  return m_jsEngine;
41  }
42  const auto p = parent.lock();
43  return p ? p->jsEngine() : nullptr;
44 }
45 
47  : d(std::make_shared<ExtractorDocumentNodePrivate>())
48 {
49 }
50 
51 ExtractorDocumentNode::ExtractorDocumentNode(const std::shared_ptr<ExtractorDocumentNodePrivate> &dd)
52  : d(dd ? dd : std::make_shared<ExtractorDocumentNodePrivate>())
53 {
54 }
55 
58 
59 ExtractorDocumentNode::~ExtractorDocumentNode()
60 {
61  if (d && d.use_count() == 1 && d->processor) {
62  d->processor->destroyNode(*this);
63  }
64 }
65 
66 ExtractorDocumentNode& ExtractorDocumentNode::operator=(const ExtractorDocumentNode &other)
67 {
68  if (d && d.use_count() == 1 && d->processor) {
69  d->processor->destroyNode(*this);
70  }
71  d = other.d;
72  return *this;
73 }
74 
75 ExtractorDocumentNode& ExtractorDocumentNode::operator=(ExtractorDocumentNode &&other)
76 {
77  if (d && d.use_count() == 1 && d->processor) {
78  d->processor->destroyNode(*this);
79  }
80  d = std::move(other.d);
81  return *this;
82 }
83 
84 bool ExtractorDocumentNode::operator==(const ExtractorDocumentNode &other) const
85 {
86  return d.get() == other.d.get();
87 }
88 
89 bool ExtractorDocumentNode::isNull() const
90 {
91  return d->content.isNull() || !d->processor || d->mimeType.isEmpty();
92 }
93 
95 {
96  return ExtractorDocumentNode(d->parent.lock());
97 }
98 
99 void ExtractorDocumentNode::setParent(const ExtractorDocumentNode &parent)
100 {
101  d->parent = parent.d;
102 }
103 
105 {
106  return d->mimeType;
107 }
108 
109 void ExtractorDocumentNode::setMimeType(const QString &mimeType)
110 {
111  d->mimeType = mimeType;
112 }
113 
115 {
116  return d->content;
117 }
118 
120 {
121  d->content = content;
122 }
123 
124 const ExtractorDocumentProcessor* ExtractorDocumentNode::processor() const
125 {
126  return d->processor;
127 }
128 
129 void ExtractorDocumentNode::setProcessor(const ExtractorDocumentProcessor *processor)
130 {
131  assert(!d->processor);
132  d->processor = processor;
133 }
134 
135 const std::vector<ExtractorDocumentNode>& ExtractorDocumentNode::childNodes() const
136 {
137  return d->childNodes;
138 }
139 
141 {
142  if (child.isNull()) {
143  return;
144  }
145  child.setParent(*this);
146  d->childNodes.push_back(child);
147 }
148 
150 {
151  return d->result;
152 }
153 
155 {
156  d->result.append(std::move(result));
157 }
158 
160 {
161  d->result = std::move(result);
162 }
163 
165 {
166  if (!d->contextDateTime.isValid() && !d->parent.expired()) {
167  return parent().contextDateTime();
168  }
169  return d->contextDateTime;
170 }
171 
173 {
174  d->contextDateTime = contextDateTime;
175 }
176 
178 {
179  if (d->location.isNull() && !d->parent.expired()) {
180  return parent().location();
181  }
182  return d->location;
183 }
184 
186 {
187  d->location = location;
188 }
189 
190 QJsonArray ExtractorDocumentNode::jsonLdResult() const
191 {
192  return d->result.jsonLdResult();
193 }
194 
195 QVariantList ExtractorDocumentNode::childNodesVariant() const
196 {
197  QVariantList l;
198  l.reserve(d->childNodes.size());
199  std::transform(d->childNodes.begin(), d->childNodes.end(), std::back_inserter(l), [](const auto &c) { return QVariant::fromValue(c); });
200  return l;
201 }
202 
203 QJSValue ExtractorDocumentNode::contentJsValue() const
204 {
205  if (!d || !d->processor) {
206  return {};
207  }
208  if (auto jsEngine = d->jsEngine()) {
209  return d->processor->contentToScriptValue(*this, jsEngine);
210  }
211  return {};
212 }
213 
214 void ExtractorDocumentNode::setScriptEngine(QJSEngine* jsEngine) const
215 {
216  if (!d->parent.expired()) {
217  parent().setScriptEngine(jsEngine);
218  } else {
219  d->m_jsEngine = jsEngine;
220  }
221 }
222 
223 QVariantList ExtractorDocumentNode::findChildNodes(const QJSValue &jsFilter) const
224 {
225  const auto filter = ExtractorFilter::fromJSValue(jsFilter);
226  std::vector<ExtractorDocumentNode> matches;
227  filter.allMatches(*this, matches);
228 
229  QVariantList l;
230  l.reserve(matches.size());
231  std::transform(matches.begin(), matches.end(), std::back_inserter(l), [](const auto &c) { return QVariant::fromValue(c); });
232  return l;
233 }
234 
235 #include "moc_extractordocumentnode.cpp"
A node in the extracted document object tree.
void setContextDateTime(const QDateTime &contextDateTime)
Set the context date/time.
QVariant location(const QVariant &res)
Returns the location of a non-transport reservation.
Generic extraction result.
void appendChild(ExtractorDocumentNode &child)
Add another child node.
KCALUTILS_EXPORT QString mimeType()
Abstract base class of a document type processor.
QVariantList childNodes
Child nodes, for QJSEngine access.
QDateTime contextDateTime
The best known context date/time at this point in the document tree.
void addResult(ExtractorResult &&result)
Add additional results from an extraction step.
KItinerary::ExtractorDocumentNode parent
The parent node, or a null node if this is the root node.
QJsonArray result
Result access for QJSEngine.
void setLocation(const QVariant &location)
Set the location information.
QVariant location
Information about the location of this node in relation to one of its ancestors.
void setResult(ExtractorResult &&result)
Replace the existing results by result.
QJSValue content
The decoded content of this node.
void setContent(const QVariant &content)
Set decoded content.
Q_INVOKABLE QVariantList findChildNodes(const QJSValue &jsFilter) const
JS API for finding child nodes given an KItinerary::ExtractorFilter.
QString mimeType
The MIME type of this node.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Oct 6 2022 03:57:55 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.