blob: 5ccd47f2b6a7eb95ec184fc8e452c9d9d2a1d5d9 [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
Adam Lesinski9ba47d82015-10-13 11:37:10 -070034struct ParsedResource;
35
36struct ResourceParserOptions {
37 /**
38 * Optional product name by which to filter resources.
39 * This is like a preprocessor definition in that we strip out resources
40 * that don't match before we compile them.
41 */
42 Maybe<std::u16string> product;
43};
44
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080045/*
46 * Parses an XML file for resources and adds them to a ResourceTable.
47 */
48class ResourceParser {
49public:
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050 ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -070051 const ConfigDescription& config, const ResourceParserOptions& options = {});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080052
53 ResourceParser(const ResourceParser&) = delete; // No copy.
54
Adam Lesinski1ab598f2015-08-14 14:26:04 -070055 bool parse(XmlPullParser* parser);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056
57private:
58 /*
59 * Parses the XML subtree as a StyleString (flattened XML representation for strings
60 * with formatting). If successful, `outStyleString`
61 * contains the escaped and whitespace trimmed text, while `outRawString`
62 * contains the unescaped text. Returns true on success.
63 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064 bool flattenXmlSubtree(XmlPullParser* parser, std::u16string* outRawString,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080065 StyleString* outStyleString);
66
67 /*
68 * Parses the XML subtree and converts it to an Item. The type of Item that can be
69 * parsed is denoted by the `typeMask`. If `allowRawValue` is true and the subtree
70 * can not be parsed as a regular Item, then a RawString is returned. Otherwise
71 * this returns nullptr.
72 */
73 std::unique_ptr<Item> parseXml(XmlPullParser* parser, uint32_t typeMask, bool allowRawValue);
74
75 bool parseResources(XmlPullParser* parser);
Adam Lesinski9ba47d82015-10-13 11:37:10 -070076 bool parseString(XmlPullParser* parser, ParsedResource* outResource);
77 bool parseColor(XmlPullParser* parser, ParsedResource* outResource);
78 bool parsePrimitive(XmlPullParser* parser, ParsedResource* outResource);
79 bool parsePublic(XmlPullParser* parser, ParsedResource* outResource);
80 bool parseAttr(XmlPullParser* parser, ParsedResource* outResource);
81 bool parseAttrImpl(XmlPullParser* parser, ParsedResource* outResource, bool weak);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070082 Maybe<Attribute::Symbol> parseEnumOrFlagItem(XmlPullParser* parser, const StringPiece16& tag);
Adam Lesinski9ba47d82015-10-13 11:37:10 -070083 bool parseStyle(XmlPullParser* parser, ParsedResource* outResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070084 bool parseStyleItem(XmlPullParser* parser, Style* style);
Adam Lesinski9ba47d82015-10-13 11:37:10 -070085 bool parseDeclareStyleable(XmlPullParser* parser, ParsedResource* outResource);
86 bool parseArray(XmlPullParser* parser, ParsedResource* outResource, uint32_t typeMask);
87 bool parsePlural(XmlPullParser* parser, ParsedResource* outResource);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080088
Adam Lesinski1ab598f2015-08-14 14:26:04 -070089 IDiagnostics* mDiag;
90 ResourceTable* mTable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080091 Source mSource;
92 ConfigDescription mConfig;
Adam Lesinski9ba47d82015-10-13 11:37:10 -070093 ResourceParserOptions mOptions;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080094};
95
96} // namespace aapt
97
98#endif // AAPT_RESOURCE_PARSER_H