XML export tool finished

This commit is contained in:
Jakub Melka 2020-10-01 13:27:45 +02:00
parent a6163978bc
commit 7bded071ea
3 changed files with 60 additions and 10 deletions

View File

@ -152,7 +152,9 @@ void PDFToolAbstractApplication::initializeCommandLineParser(QCommandLineParser*
if (optionFlags.testFlag(XmlExport))
{
parser->addOption(QCommandLineOption("xml-export-streams", "Export streams as hexadecimally encoded data. By default, stream data are not exported."));
parser->addOption(QCommandLineOption("xml-export-streams-as-text", "Export streams as text, if possible. This flag enforces exporting stream data (possibly as hexadecimal strings)."));
parser->addOption(QCommandLineOption("xml-export-streams-as-text", "Export streams as text, if possible."));
parser->addOption(QCommandLineOption("xml-use-indent", "Use automatic indent when writing output xml file."));
parser->addOption(QCommandLineOption("xml-always-binary", "Do not try to attempt transform strings to text."));
}
}
@ -227,6 +229,14 @@ PDFToolOptions PDFToolAbstractApplication::getOptions(QCommandLineParser* parser
}
}
if (optionFlags.testFlag(XmlExport))
{
options.xmlExportStreams = parser->isSet("xml-export-streams");
options.xmlExportStreamsAsText = parser->isSet("xml-export-streams-as-text");
options.xmlUseIndent = parser->isSet("xml-use-indent");
options.xmlAlwaysBinaryStrings = parser->isSet("xml-always-binary");
}
return options;
}

View File

@ -57,10 +57,10 @@ struct PDFToolOptions
Qt::DateFormat verificationDateFormat = Qt::DefaultLocaleShortDate;
// For option 'XMLExport'
bool xmlExportStreams = false; dodelat optiony
bool xmlExportStreams = false;
bool xmlExportStreamsAsText = false;
bool xmlUseIndent = true; dodelat
bool xmlAlwaysBinary = false; dodelat
bool xmlUseIndent = false;
bool xmlAlwaysBinaryStrings = false;
};
/// Base class for all applications

View File

@ -18,6 +18,7 @@
#include "pdftoolxml.h"
#include "pdfvisitor.h"
#include "pdfencoding.h"
#include "pdfexception.h"
#include <QXmlStreamWriter>
@ -29,8 +30,10 @@ static PDFToolXmlApplication s_xmlApplication;
class PDFXmlExportVisitor : public pdf::PDFAbstractVisitor
{
public:
PDFXmlExportVisitor(QXmlStreamWriter* writer) :
m_writer(writer)
PDFXmlExportVisitor(QXmlStreamWriter* writer, const pdf::PDFDocument* document, const PDFToolOptions* options) :
m_writer(writer),
m_options(options),
m_document(document)
{
}
@ -50,6 +53,8 @@ private:
void writeTextOrBinary(const QByteArray& stream, QString name);
QXmlStreamWriter* m_writer;
const PDFToolOptions* m_options;
const pdf::PDFDocument* m_document;
};
void PDFXmlExportVisitor::visitNull()
@ -109,7 +114,32 @@ void PDFXmlExportVisitor::visitStream(const pdf::PDFStream* stream)
{
m_writer->writeStartElement("stream");
visitDictionary(stream->getDictionary());
dodelat export dat
if (m_options->xmlExportStreams)
{
m_writer->writeTextElement("data", QString::fromLatin1(stream->getContent()->toHex()).toUpper());
}
if (m_options->xmlExportStreamsAsText)
{
try
{
// Attempt to decode the stream. Exception can be thrown.
QByteArray decodedData = m_document->getDecodedStream(stream);
bool isBinary = true;
QString text = pdf::PDFEncoding::convertSmartFromByteStringToUnicode(decodedData, &isBinary);
if (!isBinary)
{
m_writer->writeTextElement("text", text);
}
}
catch (pdf::PDFException)
{
// Do nothing
}
}
m_writer->writeEndElement();
}
@ -124,9 +154,19 @@ void PDFXmlExportVisitor::visitReference(const pdf::PDFObjectReference reference
void PDFXmlExportVisitor::writeTextOrBinary(const QByteArray& stream, QString name)
{
bool isBinary = false;
bool isBinary = true;
m_writer->writeStartElement(name);
QString text = pdf::PDFEncoding::convertSmartFromByteStringToUnicode(stream, &isBinary);
QString text;
if (!m_options->xmlAlwaysBinaryStrings)
{
text = pdf::PDFEncoding::convertSmartFromByteStringToUnicode(stream, &isBinary);
}
else
{
text = QString::fromLatin1(stream.toHex()).toUpper();
}
m_writer->writeAttribute("form", isBinary ? "binary" : "text");
m_writer->writeCharacters(text);
m_writer->writeEndElement();
@ -175,7 +215,7 @@ int PDFToolXmlApplication::execute(const PDFToolOptions& options)
writer.writeComment(comment);
writer.writeStartElement("document");
PDFXmlExportVisitor visitor(&writer);
PDFXmlExportVisitor visitor(&writer, &document, &options);
writer.writeStartElement("trailer");
document.getStorage().getTrailerDictionary().accept(&visitor);
writer.writeEndElement();