blob: 514e55852407f4aff37debd24a60ecda6f5645d3 [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"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021#include "Diagnostics.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080022#include "ResourceTable.h"
23#include "ResourceValues.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include "StringPool.h"
25#include "XmlPullParser.h"
26
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027#include "util/Maybe.h"
28#include "util/StringPiece.h"
29
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080030#include <memory>
31
32namespace aapt {
33
34/*
35 * Parses an XML file for resources and adds them to a ResourceTable.
36 */
37class ResourceParser {
38public:
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039 ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
40 const ConfigDescription& config);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080041
42 ResourceParser(const ResourceParser&) = delete; // No copy.
43
Adam Lesinski1ab598f2015-08-14 14:26:04 -070044 bool parse(XmlPullParser* parser);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080045
46private:
47 /*
48 * Parses the XML subtree as a StyleString (flattened XML representation for strings
49 * with formatting). If successful, `outStyleString`
50 * contains the escaped and whitespace trimmed text, while `outRawString`
51 * contains the unescaped text. Returns true on success.
52 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053 bool flattenXmlSubtree(XmlPullParser* parser, std::u16string* outRawString,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080054 StyleString* outStyleString);
55
56 /*
57 * Parses the XML subtree and converts it to an Item. The type of Item that can be
58 * parsed is denoted by the `typeMask`. If `allowRawValue` is true and the subtree
59 * can not be parsed as a regular Item, then a RawString is returned. Otherwise
60 * this returns nullptr.
61 */
62 std::unique_ptr<Item> parseXml(XmlPullParser* parser, uint32_t typeMask, bool allowRawValue);
63
64 bool parseResources(XmlPullParser* parser);
65 bool parseString(XmlPullParser* parser, const ResourceNameRef& resourceName);
66 bool parseColor(XmlPullParser* parser, const ResourceNameRef& resourceName);
67 bool parsePrimitive(XmlPullParser* parser, const ResourceNameRef& resourceName);
68 bool parsePublic(XmlPullParser* parser, const StringPiece16& name);
69 bool parseAttr(XmlPullParser* parser, const ResourceNameRef& resourceName);
70 std::unique_ptr<Attribute> parseAttrImpl(XmlPullParser* parser,
Adam Lesinski769de982015-04-10 19:43:55 -070071 ResourceName* resourceName,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080072 bool weak);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070073 Maybe<Attribute::Symbol> parseEnumOrFlagItem(XmlPullParser* parser, const StringPiece16& tag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080074 bool parseStyle(XmlPullParser* parser, const ResourceNameRef& resourceName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075 bool parseStyleItem(XmlPullParser* parser, Style* style);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076 bool parseDeclareStyleable(XmlPullParser* parser, const ResourceNameRef& resourceName);
77 bool parseArray(XmlPullParser* parser, const ResourceNameRef& resourceName, uint32_t typeMask);
78 bool parsePlural(XmlPullParser* parser, const ResourceNameRef& resourceName);
79
Adam Lesinski1ab598f2015-08-14 14:26:04 -070080 IDiagnostics* mDiag;
81 ResourceTable* mTable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082 Source mSource;
83 ConfigDescription mConfig;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084};
85
86} // namespace aapt
87
88#endif // AAPT_RESOURCE_PARSER_H