KItinerary

plistdocumentprocessor.cpp
1 /*
2  SPDX-FileCopyrightText: 2023 Volker Krause <[email protected]>
3  SPDX-License-Identifier: LGPL-2.0-or-later
4 */
5 
6 #include "plistdocumentprocessor.h"
7 
8 #include <plist/plistreader_p.h>
9 
10 #include <KItinerary/ExtractorEngine>
11 #include <KItinerary/ExtractorDocumentNodeFactory>
12 
13 #include <QJsonObject>
14 
15 using namespace KItinerary;
16 
17 bool PListDocumentProcessor::canHandleData(const QByteArray &encodedData, [[maybe_unused]] QStringView fileName) const
18 {
19  return PListReader::maybePList(encodedData);
20 }
21 
23 {
25  node.setContent(QVariant::fromValue(PListReader(encodedData)));
26  return node;
27 }
28 
29 static void searchSchemaOrgRecursive(const QJsonValue &val, QJsonArray &result)
30 {
31  if (val.isObject()) {
32  const auto obj = val.toObject();
33  if (obj.contains(QLatin1String("@type"))) {
34  result.push_back(obj);
35  return;
36  }
37 
38  for (auto it = obj.begin(); it != obj.end(); ++it) {
39  if (it.value().isObject() || it.value().isArray()) {
40  searchSchemaOrgRecursive(it.value(), result);
41  }
42  }
43  }
44 
45  if (val.isArray()) {
46  const auto a = val.toArray();
47  for (const auto &v : a) {
48  if (v.isObject() || v.isArray()) {
49  searchSchemaOrgRecursive(v, result);
50  }
51  }
52  }
53 }
54 
56 {
57  const auto plist = node.content<PListReader>();
58  const auto nsKeyedArchive = plist.unpackKeyedArchive();
59  if (!nsKeyedArchive.isObject()) {
60  return;
61  }
62 
63  // search for schema.org JSON-LD sub-trees in this
64  QJsonArray childData;
65  searchSchemaOrgRecursive(nsKeyedArchive, childData);
66  if (childData.isEmpty()) {
67  auto child = engine->documentNodeFactory()->createNode(QVariant::fromValue(QJsonArray({nsKeyedArchive})), u"application/json");
68  node.appendChild(child);
69  } else {
70  auto child = engine->documentNodeFactory()->createNode(childData, u"application/json");
71  node.appendChild(child);
72  }
73 }
QJsonObject toObject() const const
A node in the extracted document object tree.
ExtractorDocumentNode createNodeFromData(const QByteArray &encodedData) const override
Create a document node from raw data.
QVariant fromValue(const T &value)
bool isObject() const const
void appendChild(ExtractorDocumentNode &child)
Add another child node.
bool canHandleData(const QByteArray &encodedData, QStringView fileName) const override
Fast check whether the given encoded data can possibly be processed by this instance.
ExtractorDocumentNode createNode(const QByteArray &data, QStringView fileName={}, QStringView mimeType={}) const
Create a new document node from data.
void expandNode(ExtractorDocumentNode &node, const ExtractorEngine *engine) const override
Create child nodes for node, as far as that's necessary for this document type.
QJsonArray toArray() const const
Semantic data extraction engine.
bool isEmpty() const const
QJSValue content
The decoded content of this node.
void setContent(const QVariant &content)
Set decoded content.
void push_back(const QJsonValue &value)
bool isArray() const const
const ExtractorDocumentNodeFactory * documentNodeFactory() const
Factory for creating new document nodes.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Nov 29 2023 03:58:18 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.