File indexing completed on 2024-11-24 04:46:23

0001 /*
0002     SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <kitinerary_version.h>
0008 
0009 #include <KItinerary/ExtractorDocumentNode>
0010 #include <KItinerary/ExtractorEngine>
0011 #include <KItinerary/ExtractorResult>
0012 
0013 #include <QCommandLineParser>
0014 #include <QCoreApplication>
0015 #include <QDebug>
0016 #include <QFile>
0017 
0018 #include <iostream>
0019 
0020 using namespace KItinerary;
0021 
0022 static void printNode(const ExtractorDocumentNode &node, int indent = 0)
0023 {
0024     if (node.isNull()) {
0025         return;
0026     }
0027     for (int j = 0; j < indent; ++j) { std::cout.write(" ", 1); }
0028     std::cout << "MIME type: " << qPrintable(node.mimeType()) << std::endl;
0029     for (int j = 0; j < indent; ++j) { std::cout.write(" ", 1); }
0030     std::cout << "context time : " << qPrintable(node.contextDateTime().toString(Qt::ISODate)) << std::endl;
0031     if (!node.location().isNull()) {
0032         for (int j = 0; j < indent; ++j) { std::cout.write(" ", 1); }
0033         std::cout << "location: " << qPrintable(node.location().toString()) << std::endl;
0034     }
0035     for (int j = 0; j < indent; ++j) { std::cout.write(" ", 1); }
0036     std::cout << "content: " << node.content().typeName() << std::endl;
0037     for (int j = 0; j < indent; ++j) { std::cout.write(" ", 1); }
0038     std::cout << "results: " << node.result().size() << std::endl;
0039 
0040     for (const auto &child : node.childNodes()) {
0041         printNode(child, indent + 2);
0042     }
0043 }
0044 
0045 int main(int argc, char **argv)
0046 {
0047     QCoreApplication::setApplicationName(QStringLiteral("extractor-document-dump"));
0048     QCoreApplication::setApplicationVersion(QStringLiteral(KITINERARY_VERSION_STRING));
0049     QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org"));
0050     QCoreApplication::setOrganizationName(QStringLiteral("KDE"));
0051     QCoreApplication app(argc, argv);
0052 
0053     QCommandLineParser parser;
0054     parser.setApplicationDescription(QStringLiteral("Dump extractor document node tree."));
0055     parser.addHelpOption();
0056     parser.addVersionOption();
0057     parser.addPositionalArgument(QStringLiteral("input"), QStringLiteral("File to read data from, omit for using stdin."));
0058     parser.process(app);
0059 
0060     if (parser.positionalArguments().isEmpty()) {
0061         parser.showHelp(1);
0062     }
0063 
0064     QFile file(parser.positionalArguments().at(0));
0065     if (!file.open(QFile::ReadOnly)) {
0066         std::cerr << qPrintable(file.errorString()) << std::endl;
0067         return 1;
0068     }
0069 
0070     const auto data = file.readAll();
0071 
0072     ExtractorEngine engine;
0073     engine.setData(data, file.fileName());
0074     engine.extract();
0075     printNode(engine.rootDocumentNode());
0076 
0077     return 0;
0078 }