mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-01-27 15:49:19 +01:00
Finished tool for JavaScript scanning
This commit is contained in:
parent
9a85ba458f
commit
71cf448cb3
@ -188,6 +188,12 @@ PDFJavaScriptScanner::Entries PDFJavaScriptScanner::scan(const std::vector<PDFIn
|
||||
}
|
||||
}
|
||||
|
||||
qSort(result);
|
||||
if (options.testFlag(Optimize))
|
||||
{
|
||||
result.erase(std::unique(result.begin(), result.end()), result.end());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -42,6 +42,13 @@ struct PDFJavaScriptEntry
|
||||
|
||||
}
|
||||
|
||||
bool operator <(const PDFJavaScriptEntry& other) const
|
||||
{
|
||||
return std::tie(type, pageIndex, javaScript) < std::tie(other.type, other.pageIndex, other.javaScript);
|
||||
}
|
||||
|
||||
bool operator==(const PDFJavaScriptEntry&) const = default;
|
||||
|
||||
Type type = Type::Invalid;
|
||||
PDFInteger pageIndex = -1;
|
||||
QString javaScript;
|
||||
@ -63,6 +70,7 @@ public:
|
||||
ScanNamed = 0x0008, ///< Scan named javascript in catalog
|
||||
ScanForm = 0x0010, ///< Scan javascript in form actions
|
||||
ScanPage = 0x0020, ///< Scan javascript in page annotations
|
||||
Optimize = 0x0040, ///< Remove redundant entries
|
||||
|
||||
ScanMask = ScanDocument | ScanNamed | ScanForm | ScanPage
|
||||
};
|
||||
|
@ -63,7 +63,87 @@ int PDFToolInfoJavaScriptApplication::execute(const PDFToolOptions& options)
|
||||
}
|
||||
|
||||
pdf::PDFJavaScriptScanner scanner(&document);
|
||||
pdf::PDFJavaScriptScanner::Entries javascripts = scanner.scan(pages, pdf::PDFJavaScriptScanner::ScanMask);
|
||||
pdf::PDFJavaScriptScanner::Entries javascripts = scanner.scan(pages, pdf::PDFJavaScriptScanner::ScanMask | pdf::PDFJavaScriptScanner::Optimize);
|
||||
|
||||
QLocale locale;
|
||||
|
||||
PDFOutputFormatter formatter(options.outputStyle, options.outputCodec);
|
||||
formatter.beginDocument("info-javascripts", PDFToolTranslationContext::tr("JavaScript used in document %1").arg(options.document));
|
||||
formatter.endl();
|
||||
|
||||
formatter.beginTable("overview", PDFToolTranslationContext::tr("JavaScript Usage Overview"));
|
||||
|
||||
formatter.beginTableHeaderRow("header");
|
||||
formatter.writeTableHeaderColumn("no", PDFToolTranslationContext::tr("No."), Qt::AlignLeft);
|
||||
formatter.writeTableHeaderColumn("context", PDFToolTranslationContext::tr("Context"), Qt::AlignLeft);
|
||||
formatter.writeTableHeaderColumn("page-number", PDFToolTranslationContext::tr("Page Number"), Qt::AlignLeft);
|
||||
formatter.writeTableHeaderColumn("code-size", PDFToolTranslationContext::tr("Code Size"), Qt::AlignLeft);
|
||||
formatter.writeTableHeaderColumn("code-snippet", PDFToolTranslationContext::tr("Code Snippet"), Qt::AlignLeft);
|
||||
formatter.endTableHeaderRow();
|
||||
|
||||
int ref = 1;
|
||||
for (const pdf::PDFJavaScriptEntry& entry : javascripts)
|
||||
{
|
||||
formatter.beginTableRow("javascript", ref);
|
||||
|
||||
QString contextText;
|
||||
switch (entry.type)
|
||||
{
|
||||
case pdf::PDFJavaScriptEntry::Type::Document:
|
||||
contextText = PDFToolTranslationContext::tr("Document");
|
||||
break;
|
||||
|
||||
case pdf::PDFJavaScriptEntry::Type::Named:
|
||||
contextText = PDFToolTranslationContext::tr("Named");
|
||||
break;
|
||||
|
||||
case pdf::PDFJavaScriptEntry::Type::Form:
|
||||
contextText = PDFToolTranslationContext::tr("Form");
|
||||
break;
|
||||
|
||||
case pdf::PDFJavaScriptEntry::Type::Page:
|
||||
contextText = PDFToolTranslationContext::tr("Page");
|
||||
break;
|
||||
|
||||
case pdf::PDFJavaScriptEntry::Type::Annotation:
|
||||
contextText = PDFToolTranslationContext::tr("Annotation");
|
||||
break;
|
||||
|
||||
default:
|
||||
Q_ASSERT(false);
|
||||
break;
|
||||
}
|
||||
|
||||
formatter.writeTableColumn("no", locale.toString(ref), Qt::AlignRight);
|
||||
formatter.writeTableColumn("context", contextText);
|
||||
formatter.writeTableColumn("page-number", (entry.pageIndex != -1) ? locale.toString(entry.pageIndex + 1) : QString());
|
||||
formatter.writeTableColumn("code-size", locale.toString(entry.javaScript.size()));
|
||||
formatter.writeTableColumn("code-snippet", entry.javaScript.left(64));
|
||||
|
||||
formatter.endTableRow();
|
||||
++ref;
|
||||
}
|
||||
|
||||
formatter.endTable();
|
||||
|
||||
formatter.endl();
|
||||
|
||||
formatter.beginHeader("details", PDFToolTranslationContext::tr("Details"));
|
||||
|
||||
ref = 1;
|
||||
for (const pdf::PDFJavaScriptEntry& entry : javascripts)
|
||||
{
|
||||
formatter.endl();
|
||||
formatter.beginHeader("javascript", PDFToolTranslationContext::tr("JavaScript #%1").arg(ref), ref);
|
||||
formatter.writeText("code", entry.javaScript);
|
||||
formatter.endHeader();
|
||||
++ref;
|
||||
}
|
||||
|
||||
formatter.endHeader();
|
||||
|
||||
formatter.endDocument();
|
||||
PDFConsole::writeText(formatter.getString(), options.outputCodec);
|
||||
|
||||
return ExitSuccess;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user