KItinerary

extractordocumentnode.h
1 /*
2  SPDX-FileCopyrightText: 2021 Volker Krause <[email protected]>
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 
19 class QJSEngine;
20 class QJSValue;
21 
22 namespace KItinerary {
23 
24 ///@cond internal
25 namespace Internal {
26 template <typename T>
27 struct 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 
37 class ExtractorDocumentNodePrivate;
38 class ExtractorDocumentProcessor;
39 class ExtractorResult;
40 class 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  */
49 class 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 
78 public:
79  /** Creates a null node.
80  * @see KItinerary::ExtractorDocumentNodeFactory on how to create proper instances.
81  */
86  ExtractorDocumentNode& operator=(const ExtractorDocumentNode &other);
87  ExtractorDocumentNode& operator=(ExtractorDocumentNode &&other);
88 
89  /** Returns @c true if this is a null instance. */
90  bool isNull() const;
91 
92  ExtractorDocumentNode parent() const;
93  ///@cond internal
94  void setParent(const ExtractorDocumentNode &parent);
95  ///@endcond
96 
97  /** The MIME type of this node. */
98  QString mimeType() const;
99  ///@cond internal
100  void setMimeType(const QString &mimeType);
101  ///@endcond
102 
103  /** Returns the decoded content of this node.
104  * The content of the QVariant depends on the MIME type.
105  */
106  QVariant content() const;
107  /** Set decoded content.
108  * Only to be used from KItinerary::ExtractorDocumentProcessor::createNodeFromData.
109  */
110  void setContent(const QVariant &content);
111 
112  /** Checks if the content of this node is of type @p T. */
113  template <typename T>
114  inline bool isA() const
115  {
116  return content().userType() == qMetaTypeId<T>();
117  }
118 
119  /** Returns the content of this node converted to type @p T. */
120  template <typename T>
121  inline typename std::enable_if<!std::is_pointer<T>::value || !QMetaTypeId2<Internal::OwnedPtr<typename std::remove_pointer<T>::type>>::Defined, T>::type
122  content() const
123  {
124  return content().value<T>();
125  }
126  template <typename T>
127  inline typename std::enable_if<std::is_pointer<T>::value && QMetaTypeId2<Internal::OwnedPtr<typename std::remove_pointer<T>::type>>::Defined, T>::type
128  content() const
129  {
130  if (isA<T>()) {
131  return content().value<T>();
132  }
133  return content().value<Internal::OwnedPtr<typename std::remove_pointer<T>::type>>();
134  }
135 
136  template <typename T>
137  inline void setContent(const T& value)
138  {
139  setContent(QVariant::fromValue(value));
140  }
141 
142  /** The best known context date/time at this point in the document tree. */
143  QDateTime contextDateTime() const;
144  /** Set the context date/time.
145  * Only use this from KItinerary::ExtractorDocumentProcessor.
146  */
147  void setContextDateTime(const QDateTime &contextDateTime);
148 
149 
150  /* Information about the location of this node in relation to one of its ancestors. */
151  QVariant location() const;
152  /** Set the location information.
153  * Only use this from KItinerary::ExtractorDocumentProcessor.
154  */
155  void setLocation(const QVariant &location);
156 
157  ///@cond internal
158  const ExtractorDocumentProcessor* processor() const;
159  void setProcessor(const ExtractorDocumentProcessor *processor);
160  ///@endcond
161 
162  /** The child nodes of this node. */
163  const std::vector<ExtractorDocumentNode>& childNodes() const;
164  /** Add another child node.
165  * Do not use this outside of KItinerary::ExtractorDocumentProcessor::expandNode().
166  */
167  void appendChild(ExtractorDocumentNode &child);
168 
169  /** JS API for finding child nodes given an KItinerary::ExtractorFilter. */
170  Q_INVOKABLE QVariantList findChildNodes(const QJSValue &jsFilter) const;
171 
172  /** Returns the results that have accumulated so far from this node or its children. */
173  ExtractorResult result() const;
174  /** Add additional results from an extraction step. */
175  void addResult(ExtractorResult &&result);
176  /** Replace the existing results by @p result. */
177  void setResult(ExtractorResult &&result);
178 
179 private:
180  explicit ExtractorDocumentNode(const std::shared_ptr<ExtractorDocumentNodePrivate> &dd);
181  QJsonArray jsonLdResult() const;
182  QVariantList childNodesVariant() const;
183  QJSValue contentJsValue() const;
184  std::shared_ptr<ExtractorDocumentNodePrivate> d;
185 
186  friend class ExtractorScriptEngine;
187  void setScriptEngine(QJSEngine *jsEngine) const;
188 };
189 
190 }
191 
192 Q_DECLARE_METATYPE(KItinerary::ExtractorDocumentNode)
193 Q_DECLARE_SMART_POINTER_METATYPE(KItinerary::Internal::OwnedPtr)
A node in the extracted document object tree.
QVariant location(const QVariant &res)
Returns the location of a non-transport reservation.
QVariant fromValue(const T &value)
std::enable_if<!std::is_pointer< T >::value||!QMetaTypeId2< Internal::OwnedPtr< typename std::remove_pointer< T >::type > >::Defined, T >::type content() const
Returns the content of this node converted to type T.
int userType() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu May 26 2022 03:54:24 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.