KDocTools

meinproc.cpp
1
2#include "../config-kdoctools.h"
3#include "docbookxslt.h"
4#include "docbookxslt_p.h"
5#include "loggingcategory.h"
6#include "meinproc_common.h"
7
8#include <QCoreApplication>
9#include <QDir>
10#include <QFile>
11#include <QFileInfo>
12#include <QList>
13#include <QStandardPaths>
14#include <QString>
15
16#include <QUrl>
17
18#include <libexslt/exslt.h>
19#include <libxml/HTMLtree.h>
20#include <libxml/debugXML.h>
21#include <libxml/parserInternals.h>
22#include <libxml/xmlIO.h>
23#include <libxml/xmlmemory.h>
24#include <libxml/xmlversion.h>
25#include <libxslt/transform.h>
26#include <libxslt/xsltInternals.h>
27#include <libxslt/xsltconfig.h>
28#include <libxslt/xsltutils.h>
29
30#include <QCommandLineOption>
31#include <QCommandLineParser>
32#include <qplatformdefs.h>
33#include <string.h>
34
35using namespace KDocTools;
36
37#ifndef _WIN32
38extern "C" int xmlLoadExtDtdDefaultValue;
39#endif
40
41#define DIE(x) \
42 do { \
43 qCCritical(KDocToolsLog) << "Error:" << x; \
44 exit(1); \
45 } while (0)
46
47int main(int argc, char **argv)
48{
49 // xsltSetGenericDebugFunc(stderr, NULL);
50
51 QCoreApplication app(argc, argv);
52 app.setApplicationName(QStringLiteral("meinproc"));
53 app.setApplicationVersion(QStringLiteral("5.0"));
54
55 QCommandLineParser parser;
56 parser.setApplicationDescription(QCoreApplication::translate("main", "KDE Translator for XML"));
57 parser.addHelpOption();
58 parser.addVersionOption();
59 parser.addOption(
60 QCommandLineOption(QStringList() << QStringLiteral("stylesheet"), QCoreApplication::translate("main", "Stylesheet to use"), QStringLiteral("xsl")));
61 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("stdout"), QCoreApplication::translate("main", "Output whole document to stdout")));
62 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("o") << QStringLiteral("output"),
63 QCoreApplication::translate("main", "Output whole document to file"),
64 QStringLiteral("file")));
65 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("check"), QCoreApplication::translate("main", "Check the document for validity")));
66 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("cache"),
67 QCoreApplication::translate("main", "Create a cache file for the document"),
68 QStringLiteral("file")));
69 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("srcdir"),
70 QCoreApplication::translate("main", "Set the srcdir, for kdoctools"),
71 QStringLiteral("dir")));
72 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("param"),
73 QCoreApplication::translate("main", "Parameters to pass to the stylesheet"),
74 QStringLiteral("key=value")));
75 parser.addPositionalArgument(QStringLiteral("xml"), QCoreApplication::translate("main", "The file to transform"));
76 parser.process(app);
77
78 const QStringList filesToRead = parser.positionalArguments();
79 if (filesToRead.count() != 1) {
80 parser.showHelp();
81 return (1);
82 }
83
84 exsltRegisterAll();
85
86 // Need to set SRCDIR before calling setupStandardDirs
87 QString srcdir;
88 if (parser.isSet(QStringLiteral("srcdir"))) {
89 srcdir = QDir(parser.value(QStringLiteral("srcdir"))).absolutePath();
90 }
91 setupStandardDirs(srcdir);
92
93 LIBXML_TEST_VERSION
94
95 const QString checkFilename(filesToRead.first());
96 CheckFileResult ckr = checkFile(checkFilename);
97 switch (ckr) {
98 case CheckFileSuccess:
99 break;
100 case CheckFileDoesNotExist:
101 DIE("File" << checkFilename << "does not exist.");
102 case CheckFileIsNotFile:
103 DIE(checkFilename << "is not a file.");
104 case CheckFileIsNotReadable:
105 DIE("File" << checkFilename << "is not readable.");
106 }
107
108 if (parser.isSet(QStringLiteral("check"))) {
109 QStringList lst = getKDocToolsCatalogs();
110 if (lst.isEmpty()) {
111 DIE("Could not find kdoctools catalogs");
112 }
113 QByteArray catalogs = lst.join(" ").toLocal8Bit();
114 QString exe;
115#if defined(XMLLINT)
116 exe = QStringLiteral(XMLLINT);
117#endif
118 if (!QFileInfo(exe).isExecutable()) {
119 exe = QStandardPaths::findExecutable(QStringLiteral("xmllint"));
120 }
121
122 CheckResult cr = check(checkFilename, exe, catalogs);
123 switch (cr) {
124 case CheckSuccess:
125 break;
126 case CheckNoXmllint:
127 DIE("Could not find xmllint");
128 case CheckNoOut:
129 DIE("`xmllint --noout` outputted text");
130 }
131 }
132
133 QList<const char *> params;
134 {
135 const QStringList paramList = parser.values(QStringLiteral("param"));
136 for (const QString &tuple : paramList) {
137 const int ch = tuple.indexOf(QLatin1Char('='));
138 if (ch == -1) {
139 DIE("Key-Value tuple" << tuple << "lacks a '='!");
140 }
141 params.append(qstrdup(tuple.left(ch).toUtf8().constData()));
142 params.append(qstrdup(tuple.mid(ch + 1).toUtf8().constData()));
143 }
144 }
145 params.append(nullptr);
146
147 QString tss = parser.value(QStringLiteral("stylesheet"));
148 if (tss.isEmpty()) {
149 tss = QStringLiteral("customization/kde-chunk.xsl");
150 }
151
152 QString tssPath = locateFileInDtdResource(tss);
153 if (tssPath.isEmpty()) {
154 DIE("Unable to find the stylesheet named" << tss << "in dtd resources");
155 }
156#ifndef MEINPROC_NO_KARCHIVE
157 const QString cache = parser.value(QStringLiteral("cache"));
158#else
159 if (parser.isSet("cache")) {
160 qCWarning(KDocToolsLog) << QCoreApplication::translate(
161 "main",
162 "The cache option is not available, please re-compile with KArchive support. See MEINPROC_NO_KARCHIVE in KDocTools");
163 }
164#endif
165 const bool usingStdOut = parser.isSet(QStringLiteral("stdout"));
166 const bool usingOutput = parser.isSet(QStringLiteral("output"));
167 const QString outputOption = parser.value(QStringLiteral("output"));
168
169 QString output = transform(checkFilename, tssPath, params);
170 if (output.isEmpty()) {
171 DIE("Unable to parse" << checkFilename);
172 }
173
174#ifndef MEINPROC_NO_KARCHIVE
175 if (!cache.isEmpty()) {
176 if (!saveToCache(output, cache)) {
177 qCWarning(KDocToolsLog) << QCoreApplication::translate("main", "Could not write to cache file %1.").arg(cache);
178 }
179 goto end;
180 }
181#endif
182
183 doOutput(output, usingStdOut, usingOutput, outputOption, true /* replaceCharset */);
184#ifndef MEINPROC_NO_KARCHIVE
185end:
186#endif
187 xmlCleanupParser();
188 xmlMemoryDump();
189 return (0);
190}
Utility methods to generate documentation in various format from DocBook files.
KDOCTOOLS_EXPORT QString transform(const QString &file, const QString &stylesheet, const QList< const char * > &params=QList< const char * >())
Transform and return the content of file with the specified XSLT stylesheet (both already in memory) ...
Definition xslt.cpp:134
KDOCTOOLS_EXPORT QString locateFileInDtdResource(const QString &file, const QStandardPaths::LocateOptions option=QStandardPaths::LocateFile)
Find a specified file amongst the resource shipped with KDocTools.
Definition xslt.cpp:352
KDOCTOOLS_EXPORT bool saveToCache(const QString &contents, const QString &filename)
Save the content (compressed) in the specified filename.
Definition xslt_kde.cpp:8
KDOCTOOLS_EXPORT void setupStandardDirs(const QString &srcdir=QString())
Initialize the XML catalog used by XSLT functions from the standard directories or from the specified...
Definition xslt.cpp:329
const QList< QKeySequence > & end()
QCommandLineOption addHelpOption()
bool addOption(const QCommandLineOption &option)
void addPositionalArgument(const QString &name, const QString &description, const QString &syntax)
QCommandLineOption addVersionOption()
bool isSet(const QCommandLineOption &option) const const
QStringList positionalArguments() const const
void process(const QCoreApplication &app)
void setApplicationDescription(const QString &description)
void showHelp(int exitCode)
QString value(const QCommandLineOption &option) const const
QStringList values(const QCommandLineOption &option) const const
QString translate(const char *context, const char *sourceText, const char *disambiguation, int n)
QString absolutePath() const const
void append(QList< T > &&value)
qsizetype count() const const
T & first()
bool isEmpty() const const
QString findExecutable(const QString &executableName, const QStringList &paths)
QString arg(Args &&... args) const const
bool isEmpty() const const
QByteArray toLocal8Bit() const const
qsizetype indexOf(const QRegularExpression &re, qsizetype from) const const
QString join(QChar separator) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:00 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.