aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QPDF_Dictionary.cc
blob: 1301d46fcfdbc38664f40bcf536da57f8137a415 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <qpdf/QPDF_Dictionary.hh>

#include <qpdf/QPDF_Null.hh>
#include <qpdf/QPDF_Name.hh>

QPDF_Dictionary::QPDF_Dictionary(
    std::map<std::string, QPDFObjectHandle> const& items) :
    items(items)
{
}

QPDF_Dictionary::~QPDF_Dictionary()
{
}

void
QPDF_Dictionary::releaseResolved()
{
    for (std::map<std::string, QPDFObjectHandle>::iterator iter =
	     this->items.begin();
	 iter != this->items.end(); ++iter)
    {
	QPDFObjectHandle::ReleaseResolver::releaseResolved((*iter).second);
    }
}

std::string
QPDF_Dictionary::unparse()
{
    std::string result = "<< ";
    for (std::map<std::string, QPDFObjectHandle>::iterator iter =
	     this->items.begin();
	 iter != this->items.end(); ++iter)
    {
	result += QPDF_Name::normalizeName((*iter).first) +
	    " " + (*iter).second.unparse() + " ";
    }
    result += ">>";
    return result;
}

JSON
QPDF_Dictionary::getJSON()
{
    JSON j = JSON::makeDictionary();
    for (std::map<std::string, QPDFObjectHandle>::iterator iter =
	     this->items.begin();
	 iter != this->items.end(); ++iter)
    {
        j.addDictionaryMember(QPDF_Name::normalizeName((*iter).first),
                              (*iter).second.getJSON());
    }
    return j;
}

QPDFObject::object_type_e
QPDF_Dictionary::getTypeCode() const
{
    return QPDFObject::ot_dictionary;
}

char const*
QPDF_Dictionary::getTypeName() const
{
    return "dictionary";
}

void
QPDF_Dictionary::setDescription(QPDF* qpdf, std::string const& description)
{
    this->QPDFObject::setDescription(qpdf, description);
}

bool
QPDF_Dictionary::hasKey(std::string const& key)
{
    return ((this->items.count(key) > 0) &&
	    (! this->items[key].isNull()));
}

QPDFObjectHandle
QPDF_Dictionary::getKey(std::string const& key)
{
    // PDF spec says fetching a non-existent key from a dictionary
    // returns the null object.
    if (this->items.count(key))
    {
	// May be a null object
	return (*(this->items.find(key))).second;
    }
    else
    {
        QPDFObjectHandle null = QPDFObjectHandle::newNull();
        QPDF* qpdf = 0;
        std::string description;
        if (getDescription(qpdf, description))
        {
            null.setObjectDescription(
                qpdf, description + " -> dictionary key " + key);
        }
	return null;
    }
}

std::set<std::string>
QPDF_Dictionary::getKeys()
{
    std::set<std::string> result;
    for (std::map<std::string, QPDFObjectHandle>::const_iterator iter =
	     this->items.begin();
	 iter != this->items.end(); ++iter)
    {
	if (hasKey((*iter).first))
	{
	    result.insert((*iter).first);
	}
    }
    return result;
}

std::map<std::string, QPDFObjectHandle> const&
QPDF_Dictionary::getAsMap() const
{
    return this->items;
}

void
QPDF_Dictionary::replaceKey(std::string const& key,
			    QPDFObjectHandle value)
{
    // 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);
}

void
QPDF_Dictionary::replaceOrRemoveKey(std::string const& key,
				    QPDFObjectHandle value)
{
    if (value.isNull())
    {
	removeKey(key);
    }
    else
    {
	replaceKey(key, value);
    }
}