blob: 52194bd02f990d746564fb8fd49081a6cfee2919 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef AAPT_RESOURCE_PARSER_H
18#define AAPT_RESOURCE_PARSER_H
19
20#include "ConfigDescription.h"
21#include "Logger.h"
22#include "ResourceTable.h"
23#include "ResourceValues.h"
24#include "StringPiece.h"
25#include "StringPool.h"
26#include "XmlPullParser.h"
27
28#include <istream>
29#include <memory>
30
31namespace aapt {
32
33/*
34 * Parses an XML file for resources and adds them to a ResourceTable.
35 */
36class ResourceParser {
37public:
38 /*
39 * Extracts the package, type, and name from a string of the format:
40 *
41 * [package:]type/name
42 *
43 * where the package can be empty. Validation must be performed on each
44 * individual extracted piece to verify that the pieces are valid.
45 */
46 static void extractResourceName(const StringPiece16& str, StringPiece16* outPackage,
47 StringPiece16* outType, StringPiece16* outEntry);
48
49 /*
50 * Returns true if the string was parsed as a reference (@[+][package:]type/name), with
51 * `outReference` set to the parsed reference.
52 *
53 * If '+' was present in the reference, `outCreate` is set to true.
54 * If '*' was present in the reference, `outPrivate` is set to true.
55 */
56 static bool tryParseReference(const StringPiece16& str, ResourceNameRef* outReference,
57 bool* outCreate, bool* outPrivate);
58
59 /*
60 * Returns true if the string was parsed as an attribute reference (?[package:]type/name),
61 * with `outReference` set to the parsed reference.
62 */
63 static bool tryParseAttributeReference(const StringPiece16& str,
64 ResourceNameRef* outReference);
65
66 /*
Adam Lesinski769de982015-04-10 19:43:55 -070067 * Returns true if the string `str` was parsed as a valid reference to a style.
68 * The format for a style parent is slightly more flexible than a normal reference:
69 *
70 * @[package:]style/<entry> or
71 * ?[package:]style/<entry> or
72 * <package>:[style/]<entry>
73 */
74 static bool parseStyleParentReference(const StringPiece16& str, Reference* outReference,
75 std::string* outError);
76
77 /*
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078 * Returns a Reference object if the string was parsed as a resource or attribute reference,
79 * ( @[+][package:]type/name | ?[package:]type/name )
80 * assigning defaultPackage if the package was not present in the string, and setting
81 * outCreate to true if the '+' was present in the string.
82 */
83 static std::unique_ptr<Reference> tryParseReference(const StringPiece16& str,
84 const StringPiece16& defaultPackage,
85 bool* outCreate);
86
87 /*
88 * Returns a BinaryPrimitve object representing @null or @empty if the string was parsed
89 * as one.
90 */
91 static std::unique_ptr<BinaryPrimitive> tryParseNullOrEmpty(const StringPiece16& str);
92
93 /*
94 * Returns a BinaryPrimitve object representing a color if the string was parsed
95 * as one.
96 */
97 static std::unique_ptr<BinaryPrimitive> tryParseColor(const StringPiece16& str);
98
99 /*
100 * Returns a BinaryPrimitve object representing a boolean if the string was parsed
101 * as one.
102 */
103 static std::unique_ptr<BinaryPrimitive> tryParseBool(const StringPiece16& str);
104
105 /*
106 * Returns a BinaryPrimitve object representing an integer if the string was parsed
107 * as one.
108 */
109 static std::unique_ptr<BinaryPrimitive> tryParseInt(const StringPiece16& str);
110
111 /*
112 * Returns a BinaryPrimitve object representing a floating point number
113 * (float, dimension, etc) if the string was parsed as one.
114 */
115 static std::unique_ptr<BinaryPrimitive> tryParseFloat(const StringPiece16& str);
116
117 /*
118 * Returns a BinaryPrimitve object representing an enum symbol if the string was parsed
119 * as one.
120 */
121 static std::unique_ptr<BinaryPrimitive> tryParseEnumSymbol(const Attribute& enumAttr,
122 const StringPiece16& str);
123
124 /*
125 * Returns a BinaryPrimitve object representing a flag symbol if the string was parsed
126 * as one.
127 */
128 static std::unique_ptr<BinaryPrimitive> tryParseFlagSymbol(const Attribute& enumAttr,
129 const StringPiece16& str);
130
131 /*
132 * Try to convert a string to an Item for the given attribute. The attribute will
133 * restrict what values the string can be converted to.
134 * The defaultPackage is used when the string is a reference with no defined package.
135 * The callback function onCreateReference is called when the parsed item is a
136 * reference to an ID that must be created (@+id/foo).
137 */
138 static std::unique_ptr<Item> parseItemForAttribute(
139 const StringPiece16& value, const Attribute& attr, const StringPiece16& defaultPackage,
140 std::function<void(const ResourceName&)> onCreateReference = {});
141
142 static std::unique_ptr<Item> parseItemForAttribute(
143 const StringPiece16& value, uint32_t typeMask, const StringPiece16& defaultPackage,
144 std::function<void(const ResourceName&)> onCreateReference = {});
145
146 static uint32_t androidTypeToAttributeTypeMask(uint16_t type);
147
148 ResourceParser(const std::shared_ptr<ResourceTable>& table, const Source& source,
149 const ConfigDescription& config, const std::shared_ptr<XmlPullParser>& parser);
150
151 ResourceParser(const ResourceParser&) = delete; // No copy.
152
153 bool parse();
154
155private:
156 /*
157 * Parses the XML subtree as a StyleString (flattened XML representation for strings
158 * with formatting). If successful, `outStyleString`
159 * contains the escaped and whitespace trimmed text, while `outRawString`
160 * contains the unescaped text. Returns true on success.
161 */
162 bool flattenXmlSubtree(XmlPullParser* parser, std::u16string* outRawString,\
163 StyleString* outStyleString);
164
165 /*
166 * Parses the XML subtree and converts it to an Item. The type of Item that can be
167 * parsed is denoted by the `typeMask`. If `allowRawValue` is true and the subtree
168 * can not be parsed as a regular Item, then a RawString is returned. Otherwise
169 * this returns nullptr.
170 */
171 std::unique_ptr<Item> parseXml(XmlPullParser* parser, uint32_t typeMask, bool allowRawValue);
172
173 bool parseResources(XmlPullParser* parser);
174 bool parseString(XmlPullParser* parser, const ResourceNameRef& resourceName);
175 bool parseColor(XmlPullParser* parser, const ResourceNameRef& resourceName);
176 bool parsePrimitive(XmlPullParser* parser, const ResourceNameRef& resourceName);
177 bool parsePublic(XmlPullParser* parser, const StringPiece16& name);
178 bool parseAttr(XmlPullParser* parser, const ResourceNameRef& resourceName);
179 std::unique_ptr<Attribute> parseAttrImpl(XmlPullParser* parser,
Adam Lesinski769de982015-04-10 19:43:55 -0700180 ResourceName* resourceName,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800181 bool weak);
182 bool parseEnumOrFlagItem(XmlPullParser* parser, const StringPiece16& tag,
183 Attribute::Symbol* outSymbol);
184 bool parseStyle(XmlPullParser* parser, const ResourceNameRef& resourceName);
185 bool parseUntypedItem(XmlPullParser* parser, Style& style);
186 bool parseDeclareStyleable(XmlPullParser* parser, const ResourceNameRef& resourceName);
187 bool parseArray(XmlPullParser* parser, const ResourceNameRef& resourceName, uint32_t typeMask);
188 bool parsePlural(XmlPullParser* parser, const ResourceNameRef& resourceName);
189
190 std::shared_ptr<ResourceTable> mTable;
191 Source mSource;
192 ConfigDescription mConfig;
193 SourceLogger mLogger;
194 std::shared_ptr<XmlPullParser> mParser;
195};
196
197} // namespace aapt
198
199#endif // AAPT_RESOURCE_PARSER_H