KItinerary

extractordocumentnode.h
1/*
2 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#pragma once
8
9#include "kitinerary_export.h"
10
11#include <QDateTime>
12#include <QJsonArray>
13#include <QMetaType>
14#include <QVariant>
15
16#include <memory>
17#include <type_traits>
18
19class QJSEngine;
20class QJSValue;
21
22namespace KItinerary {
23
24///@cond internal
25namespace Internal {
26template <typename T>
27struct OwnedPtr {
28 inline OwnedPtr() = default;
29 inline OwnedPtr(T* _ptr) : ptr(_ptr) {}
30 inline operator T*() const { return ptr; }
31 inline T* operator->() const { return ptr; }
32 T *ptr = nullptr;
33};
34}
35///@endcond
36
37class ExtractorDocumentNodePrivate;
38class ExtractorDocumentProcessor;
39class ExtractorResult;
40class ExtractorScriptEngine;
41
42/** A node in the extracted document object tree.
43 * Essentially this models a tree of variants representing the input document,
44 * Each node being associated with and managed by the KItinerary::ExtractorDocumentProcessor
45 * for its corresponding type.
46 * Each nodes also carries the result of data extraction on itself and/or its children.
47 * This is meant for consumption in both C++ and JS code.
48 */
49class KITINERARY_EXPORT ExtractorDocumentNode
50{
51 Q_GADGET
52 Q_PROPERTY(bool isNull READ isNull)
53
54 /** The parent node, or a null node if this is the root node. */
55 Q_PROPERTY(KItinerary::ExtractorDocumentNode parent READ parent)
56 /** Child nodes, for QJSEngine access. */
57 Q_PROPERTY(QVariantList childNodes READ childNodesVariant)
58
59 /** The MIME type of this node. */
60 Q_PROPERTY(QString mimeType READ mimeType)
61 /** The decoded content of this node.
62 * The exact type in here depends on the MIME type, adapted for QJSEngine consumption.
63 */
64 Q_PROPERTY(QJSValue content READ contentJsValue)
65 /** The best known context date/time at this point in the document tree.
66 * If not set on this node, the context date/time of the parent node is returned.
67 */
68 Q_PROPERTY(QDateTime contextDateTime READ contextDateTime)
69 /** Result access for QJSEngine. */
70 Q_PROPERTY(QJsonArray result READ jsonLdResult)
71 /** Information about the location of this node in relation to one of its
72 * ancestors.
73 * The exact meaning of this depends on the type of the node, one example
74 * would be a page number an image is found on in a PDF document.
75 */
76 Q_PROPERTY(QVariant location READ location)
77
78public:
79 /** Creates a null node.
80 * @see KItinerary::ExtractorDocumentNodeFactory on how to create proper instances.
81 */
86 ExtractorDocumentNode& operator=(const ExtractorDocumentNode &other);
88
89 bool operator==(const ExtractorDocumentNode &other) const;
90
91 /** Returns @c true if this is a null instance. */
92 bool isNull() const;
93
94 ExtractorDocumentNode parent() const;
95 ///@cond internal
96 void setParent(const ExtractorDocumentNode &parent);
97 ///@endcond
98
99 /** The MIME type of this node. */
100 QString mimeType() const;
101 ///@cond internal
102 void setMimeType(const QString &mimeType);
103 ///@endcond
104
105 /** Returns the decoded content of this node.
106 * The content of the QVariant depends on the MIME type.
107 */
108 QVariant content() const;
109 /** Set decoded content.
110 * Only to be used from KItinerary::ExtractorDocumentProcessor::createNodeFromData.
111 */
112 void setContent(const QVariant &content);
113
114 /** Checks if the content of this node is of type @p T. */
115 template <typename T>
116 inline bool isA() const
117 {
118 return content().userType() == qMetaTypeId<T>();
119 }
120
121 /** Returns the content of this node converted to type @p T. */
122 template <typename T>
123 inline typename std::enable_if<!std::is_pointer<T>::value || !QMetaTypeId2<Internal::OwnedPtr<typename std::remove_pointer<T>::type>>::Defined, T>::type
124 content() const
125 {
126 return content().value<T>();
127 }
128 template <typename T>
129 inline typename std::enable_if<std::is_pointer<T>::value && QMetaTypeId2<Internal::OwnedPtr<typename std::remove_pointer<T>::type>>::Defined, T>::type
130 content() const
131 {
132 if (isA<T>()) {
133 return content().value<T>();
134 }
135 return content().value<Internal::OwnedPtr<typename std::remove_pointer<T>::type>>();
136 }
137
138 template <typename T>
139 inline void setContent(const T& value)
140 {
141 setContent(QVariant::fromValue(value));
142 }
143
144 /** The best known context date/time at this point in the document tree. */
145 QDateTime contextDateTime() const;
146 /** Set the context date/time.
147 * Only use this from KItinerary::ExtractorDocumentProcessor.
148 */
149 void setContextDateTime(const QDateTime &contextDateTime);
150
151
152 /* Information about the location of this node in relation to one of its ancestors. */
153 QVariant location() const;
154 /** Set the location information.
155 * Only use this from KItinerary::ExtractorDocumentProcessor.
156 */
157 void setLocation(const QVariant &location);
158
159 ///@cond internal
160 const ExtractorDocumentProcessor* processor() const;
161 void setProcessor(const ExtractorDocumentProcessor *processor);
162 ///@endcond
163
164 /** The child nodes of this node. */
165 const std::vector<ExtractorDocumentNode>& childNodes() const;
166 /** Add another child node.
167 * Do not use this outside of KItinerary::ExtractorDocumentProcessor::expandNode().
168 */
169 void appendChild(ExtractorDocumentNode &child);
170
171 /** JS API for finding child nodes given an KItinerary::ExtractorFilter. */
172 Q_INVOKABLE QVariantList findChildNodes(const QJSValue &jsFilter) const;
173
174 /** Returns the results that have accumulated so far from this node or its children. */
175 ExtractorResult result() const;
176 /** Add additional results from an extraction step. */
177 void addResult(ExtractorResult &&result);
178 /** Replace the existing results by @p result. */
179 void setResult(ExtractorResult &&result);
180
181 /** Extractor used for the result of this node, if any.
182 * @internal for development tooling only
183 */
184 QString usedExtractor() const;
185 void setUsedExtractor(const QString &usedExtractor);
186
187private:
188 explicit ExtractorDocumentNode(const std::shared_ptr<ExtractorDocumentNodePrivate> &dd);
189 QJsonArray jsonLdResult() const;
190 QVariantList childNodesVariant() const;
191 QJSValue contentJsValue() const;
192 std::shared_ptr<ExtractorDocumentNodePrivate> d;
193
194 friend class ExtractorScriptEngine;
195 void setScriptEngine(QJSEngine *jsEngine) const;
196};
197
198}
199
200Q_DECLARE_SMART_POINTER_METATYPE(KItinerary::Internal::OwnedPtr)
A node in the extracted document object tree.
std::enable_if<!std::is_pointer< T >::value||!QMetaTypeId2< Internal::OwnedPtr< typenamestd::remove_pointer< T >::type > >::Defined, T >::type content() const
Returns the content of this node converted to type T.
QVariant location(const QVariant &res)
Returns the location of a non-transport reservation.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
QVariant fromValue(T &&value)
int userType() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:00:33 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.