2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDF_Dictionary.hh>
|
|
|
|
|
|
|
|
#include <qpdf/QPDF_Name.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QPDF_Null.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
QPDF_Dictionary::QPDF_Dictionary(
|
|
|
|
std::map<std::string, QPDFObjectHandle> const& items) :
|
|
|
|
items(items)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDF_Dictionary::~QPDF_Dictionary()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-06-06 13:32:08 +00:00
|
|
|
void
|
|
|
|
QPDF_Dictionary::releaseResolved()
|
|
|
|
{
|
|
|
|
for (std::map<std::string, QPDFObjectHandle>::iterator iter =
|
2022-02-08 14:18:08 +00:00
|
|
|
this->items.begin();
|
2022-04-02 21:14:10 +00:00
|
|
|
iter != this->items.end();
|
|
|
|
++iter) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QPDFObjectHandle::ReleaseResolver::releaseResolved((*iter).second);
|
2010-06-06 13:32:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string
|
|
|
|
QPDF_Dictionary::unparse()
|
|
|
|
{
|
|
|
|
std::string result = "<< ";
|
|
|
|
for (std::map<std::string, QPDFObjectHandle>::iterator iter =
|
2022-02-08 14:18:08 +00:00
|
|
|
this->items.begin();
|
2022-04-02 21:14:10 +00:00
|
|
|
iter != this->items.end();
|
|
|
|
++iter) {
|
|
|
|
result += QPDF_Name::normalizeName((*iter).first) + " " +
|
|
|
|
(*iter).second.unparse() + " ";
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
result += ">>";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-12-17 22:40:29 +00:00
|
|
|
JSON
|
|
|
|
QPDF_Dictionary::getJSON()
|
|
|
|
{
|
|
|
|
JSON j = JSON::makeDictionary();
|
|
|
|
for (std::map<std::string, QPDFObjectHandle>::iterator iter =
|
2022-02-08 14:18:08 +00:00
|
|
|
this->items.begin();
|
2022-04-02 21:14:10 +00:00
|
|
|
iter != this->items.end();
|
|
|
|
++iter) {
|
|
|
|
j.addDictionaryMember(
|
|
|
|
QPDF_Name::normalizeName((*iter).first), (*iter).second.getJSON());
|
2018-12-17 22:40:29 +00:00
|
|
|
}
|
|
|
|
return j;
|
|
|
|
}
|
|
|
|
|
2013-01-22 14:57:07 +00:00
|
|
|
QPDFObject::object_type_e
|
|
|
|
QPDF_Dictionary::getTypeCode() const
|
|
|
|
{
|
|
|
|
return QPDFObject::ot_dictionary;
|
|
|
|
}
|
|
|
|
|
|
|
|
char const*
|
|
|
|
QPDF_Dictionary::getTypeName() const
|
|
|
|
{
|
|
|
|
return "dictionary";
|
|
|
|
}
|
|
|
|
|
2018-02-16 22:25:27 +00:00
|
|
|
void
|
|
|
|
QPDF_Dictionary::setDescription(QPDF* qpdf, std::string const& description)
|
|
|
|
{
|
|
|
|
this->QPDFObject::setDescription(qpdf, description);
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
bool
|
|
|
|
QPDF_Dictionary::hasKey(std::string const& key)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
return ((this->items.count(key) > 0) && (!this->items[key].isNull()));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDF_Dictionary::getKey(std::string const& key)
|
|
|
|
{
|
|
|
|
// PDF spec says fetching a non-existent key from a dictionary
|
|
|
|
// returns the null object.
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->items.count(key)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// May be a null object
|
|
|
|
return (*(this->items.find(key))).second;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2018-02-16 22:25:27 +00:00
|
|
|
QPDFObjectHandle null = QPDFObjectHandle::newNull();
|
|
|
|
QPDF* qpdf = 0;
|
|
|
|
std::string description;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (getDescription(qpdf, description)) {
|
2018-02-16 22:25:27 +00:00
|
|
|
null.setObjectDescription(
|
|
|
|
qpdf, description + " -> dictionary key " + key);
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
return null;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<std::string>
|
|
|
|
QPDF_Dictionary::getKeys()
|
|
|
|
{
|
|
|
|
std::set<std::string> result;
|
|
|
|
for (std::map<std::string, QPDFObjectHandle>::const_iterator iter =
|
2022-02-08 14:18:08 +00:00
|
|
|
this->items.begin();
|
2022-04-02 21:14:10 +00:00
|
|
|
iter != this->items.end();
|
|
|
|
++iter) {
|
|
|
|
if (hasKey((*iter).first)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
result.insert((*iter).first);
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-08-10 17:33:58 +00:00
|
|
|
std::map<std::string, QPDFObjectHandle> const&
|
|
|
|
QPDF_Dictionary::getAsMap() const
|
|
|
|
{
|
|
|
|
return this->items;
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDF_Dictionary::replaceKey(std::string const& key, QPDFObjectHandle value)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// add or replace value
|
|
|
|
this->items[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF_Dictionary::removeKey(std::string const& key)
|
|
|
|
{
|
|
|
|
// no-op if key does not exist
|
|
|
|
this->items.erase(key);
|
|
|
|
}
|
2010-08-02 22:17:01 +00:00
|
|
|
|
|
|
|
void
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDF_Dictionary::replaceOrRemoveKey(
|
|
|
|
std::string const& key, QPDFObjectHandle value)
|
2010-08-02 22:17:01 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (value.isNull()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
removeKey(key);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
replaceKey(key, value);
|
2010-08-02 22:17:01 +00:00
|
|
|
}
|
|
|
|
}
|