KItinerary

extractor-document-dump.cpp
1/*
2 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include <kitinerary_version.h>
8
9#include <KItinerary/ExtractorDocumentNode>
10#include <KItinerary/ExtractorEngine>
11#include <KItinerary/ExtractorResult>
12
13#include <QCommandLineParser>
14#include <QCoreApplication>
15#include <QDebug>
16#include <QFile>
17
18#include <iostream>
19
20using namespace KItinerary;
21
22static void printNode(const ExtractorDocumentNode &node, int indent = 0)
23{
24 if (node.isNull()) {
25 return;
26 }
27 for (int j = 0; j < indent; ++j) { std::cout.write(" ", 1); }
28 std::cout << "MIME type: " << qPrintable(node.mimeType()) << std::endl;
29 for (int j = 0; j < indent; ++j) { std::cout.write(" ", 1); }
30 std::cout << "context time : " << qPrintable(node.contextDateTime().toString(Qt::ISODate)) << std::endl;
31 if (!node.location().isNull()) {
32 for (int j = 0; j < indent; ++j) { std::cout.write(" ", 1); }
33 std::cout << "location: " << qPrintable(node.location().toString()) << std::endl;
34 }
35 for (int j = 0; j < indent; ++j) { std::cout.write(" ", 1); }
36 std::cout << "content: " << node.content().typeName() << std::endl;
37 for (int j = 0; j < indent; ++j) { std::cout.write(" ", 1); }
38 std::cout << "results: " << node.result().size() << std::endl;
39
40 for (const auto &child : node.childNodes()) {
41 printNode(child, indent + 2);
42 }
43}
44
45int main(int argc, char **argv)
46{
47 QCoreApplication::setApplicationName(QStringLiteral("extractor-document-dump"));
48 QCoreApplication::setApplicationVersion(QStringLiteral(KITINERARY_VERSION_STRING));
49 QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org"));
50 QCoreApplication::setOrganizationName(QStringLiteral("KDE"));
51 QCoreApplication app(argc, argv);
52
53 QCommandLineParser parser;
54 parser.setApplicationDescription(QStringLiteral("Dump extractor document node tree."));
55 parser.addHelpOption();
56 parser.addVersionOption();
57 parser.addPositionalArgument(QStringLiteral("input"), QStringLiteral("File to read data from, omit for using stdin."));
58 parser.process(app);
59
60 if (parser.positionalArguments().isEmpty()) {
61 parser.showHelp(1);
62 }
63
64 QFile file(parser.positionalArguments().at(0));
65 if (!file.open(QFile::ReadOnly)) {
66 std::cerr << qPrintable(file.errorString()) << std::endl;
67 return 1;
68 }
69
70 const auto data = file.readAll();
71
72 ExtractorEngine engine;
73 engine.setData(data, file.fileName());
74 engine.extract();
75 printNode(engine.rootDocumentNode());
76
77 return 0;
78}
A node in the extracted document object tree.
QJsonArray result
Result access for QJSEngine.
QString mimeType
The MIME type of this node.
QJSValue content
The decoded content of this node.
QVariantList childNodes
Child nodes, for QJSEngine access.
QDateTime contextDateTime
The best known context date/time at this point in the document tree.
QVariant location
Information about the location of this node in relation to one of its ancestors.
Semantic data extraction engine.
void setData(const QByteArray &data, QStringView fileName={}, QStringView mimeType={})
Set raw data to extract from.
QJsonArray extract()
Perform the actual extraction, and return the JSON-LD data that has been found.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
QCommandLineOption addHelpOption()
void addPositionalArgument(const QString &name, const QString &description, const QString &syntax)
QCommandLineOption addVersionOption()
QStringList positionalArguments() const const
void process(const QCoreApplication &app)
void setApplicationDescription(const QString &description)
void showHelp(int exitCode)
void setApplicationName(const QString &application)
void setApplicationVersion(const QString &version)
void setOrganizationDomain(const QString &orgDomain)
void setOrganizationName(const QString &orgName)
QString toString(QStringView format, QCalendar cal) const const
qsizetype size() const const
const_reference at(qsizetype i) const const
bool isEmpty() const const
bool isNull() const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:14:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.