summaryrefslogtreecommitdiffstats
path: root/tools/aapt/AaptXml.cpp
blob: b04a55d91b9c299d0515f23db01c9d95b1060f02 (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <androidfw/ResourceTypes.h>
#include <utils/String8.h>

#include "AaptXml.h"

using namespace android;

namespace AaptXml {

static String8 getStringAttributeAtIndex(const ResXMLTree& tree, ssize_t attrIndex,
        String8* outError) {
    Res_value value;
    if (tree.getAttributeValue(attrIndex, &value) < 0) {
        if (outError != NULL) {
            *outError = "could not find attribute at index";
        }
        return String8();
    }

    if (value.dataType != Res_value::TYPE_STRING) {
        if (outError != NULL) {
            *outError = "attribute is not a string value";
        }
        return String8();
    }

    size_t len;
    const char16_t* str = tree.getAttributeStringValue(attrIndex, &len);
    return str ? String8(str, len) : String8();
}

static int32_t getIntegerAttributeAtIndex(const ResXMLTree& tree, ssize_t attrIndex,
    int32_t defValue, String8* outError) {
    Res_value value;
    if (tree.getAttributeValue(attrIndex, &value) < 0) {
        if (outError != NULL) {
            *outError = "could not find attribute at index";
        }
        return defValue;
    }

    if (value.dataType < Res_value::TYPE_FIRST_INT
            || value.dataType > Res_value::TYPE_LAST_INT) {
        if (outError != NULL) {
            *outError = "attribute is not an integer value";
        }
        return defValue;
    }
    return value.data;
}


ssize_t indexOfAttribute(const ResXMLTree& tree, uint32_t attrRes) {
    size_t attrCount = tree.getAttributeCount();
    for (size_t i = 0; i < attrCount; i++) {
        if (tree.getAttributeNameResID(i) == attrRes) {
            return (ssize_t)i;
        }
    }
    return -1;
}

String8 getAttribute(const ResXMLTree& tree, const char* ns,
        const char* attr, String8* outError) {
    ssize_t idx = tree.indexOfAttribute(ns, attr);
    if (idx < 0) {
        return String8();
    }
    return getStringAttributeAtIndex(tree, idx, outError);
}

String8 getAttribute(const ResXMLTree& tree, uint32_t attrRes, String8* outError) {
    ssize_t idx = indexOfAttribute(tree, attrRes);
    if (idx < 0) {
        return String8();
    }
    return getStringAttributeAtIndex(tree, idx, outError);
}

String8 getResolvedAttribute(const ResTable& resTable, const ResXMLTree& tree,
        uint32_t attrRes, String8* outError) {
    ssize_t idx = indexOfAttribute(tree, attrRes);
    if (idx < 0) {
        return String8();
    }
    Res_value value;
    if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
        if (value.dataType == Res_value::TYPE_STRING) {
            size_t len;
            const char16_t* str = tree.getAttributeStringValue(idx, &len);
            return str ? String8(str, len) : String8();
        }
        resTable.resolveReference(&value, 0);
        if (value.dataType != Res_value::TYPE_STRING) {
            if (outError != NULL) {
                *outError = "attribute is not a string value";
            }
            return String8();
        }
    }
    size_t len;
    const Res_value* value2 = &value;
    const char16_t* str = resTable.valueToString(value2, 0, NULL, &len);
    return str ? String8(str, len) : String8();
}

int32_t getIntegerAttribute(const ResXMLTree& tree, const char* ns,
        const char* attr, int32_t defValue, String8* outError) {
    ssize_t idx = tree.indexOfAttribute(ns, attr);
    if (idx < 0) {
        return defValue;
    }
    return getIntegerAttributeAtIndex(tree, idx, defValue, outError);
}

int32_t getIntegerAttribute(const ResXMLTree& tree, uint32_t attrRes, int32_t defValue,
        String8* outError) {
    ssize_t idx = indexOfAttribute(tree, attrRes);
    if (idx < 0) {
        return defValue;
    }
    return getIntegerAttributeAtIndex(tree, idx, defValue, outError);
}

int32_t getResolvedIntegerAttribute(const ResTable& resTable, const ResXMLTree& tree,
        uint32_t attrRes, int32_t defValue, String8* outError) {
    ssize_t idx = indexOfAttribute(tree, attrRes);
    if (idx < 0) {
        return defValue;
    }
    Res_value value;
    if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
        if (value.dataType == Res_value::TYPE_REFERENCE) {
            resTable.resolveReference(&value, 0);
        }
        if (value.dataType < Res_value::TYPE_FIRST_INT
                || value.dataType > Res_value::TYPE_LAST_INT) {
            if (outError != NULL) {
                *outError = "attribute is not an integer value";
            }
            return defValue;
        }
    }
    return value.data;
}

void getResolvedResourceAttribute(const ResTable& resTable, const ResXMLTree& tree,
        uint32_t attrRes, Res_value* outValue, String8* outError) {
    ssize_t idx = indexOfAttribute(tree, attrRes);
    if (idx < 0) {
        if (outError != NULL) {
            *outError = "attribute could not be found";
        }
        return;
    }
    if (tree.getAttributeValue(idx, outValue) != NO_ERROR) {
        if (outValue->dataType == Res_value::TYPE_REFERENCE) {
            resTable.resolveReference(outValue, 0);
        }
        // The attribute was found and was resolved if need be.
        return;
    }
    if (outError != NULL) {
        *outError = "error getting resolved resource attribute";
    }
}

} // namespace AaptXml