KItinerary

plistdocumentprocessor.cpp
1/*
2 SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
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
15using namespace KItinerary;
16
17bool 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
29static void searchSchemaOrgRecursive(const QJsonValue &val, QJsonArray &result)
30{
31 if (val.isObject()) {
32 const auto obj = val.toObject();
33 if (obj.contains(QLatin1StringView("@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}
ExtractorDocumentNode createNode(const QByteArray &data, QStringView fileName={}, QStringView mimeType={}) const
Create a new document node from data.
A node in the extracted document object tree.
void appendChild(ExtractorDocumentNode &child)
Add another child node.
QJSValue content
The decoded content of this node.
void setContent(const QVariant &content)
Set decoded content.
Semantic data extraction engine.
const ExtractorDocumentNodeFactory * documentNodeFactory() const
Factory for creating new document nodes.
bool canHandleData(const QByteArray &encodedData, QStringView fileName) const override
Fast check whether the given encoded data can possibly be processed by this instance.
void expandNode(ExtractorDocumentNode &node, const ExtractorEngine *engine) const override
Create child nodes for node, as far as that's necessary for this document type.
ExtractorDocumentNode createNodeFromData(const QByteArray &encodedData) const override
Create a document node from raw data.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
bool isEmpty() const const
void push_back(const QJsonValue &value)
bool isArray() const const
bool isObject() const const
QJsonArray toArray() const const
QJsonObject toObject() const const
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:52:37 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.