blob: 51cbbe19384e7671b76ec83ed1200af6d6ac6224 [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"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "util/Maybe.h"
26#include "util/StringPiece.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080027#include "xml/XmlPullParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080029#include <memory>
30
31namespace aapt {
32
Adam Lesinski9ba47d82015-10-13 11:37:10 -070033struct ParsedResource;
34
35struct ResourceParserOptions {
36 /**
Adam Lesinski7751afc2016-01-06 15:45:28 -080037 * Optional product names by which to filter resources.
Adam Lesinski9ba47d82015-10-13 11:37:10 -070038 * This is like a preprocessor definition in that we strip out resources
39 * that don't match before we compile them.
40 */
Adam Lesinski7751afc2016-01-06 15:45:28 -080041 std::vector<std::u16string> products;
Adam Lesinski9f222042015-11-04 13:51:45 -080042
43 /**
44 * Whether the default setting for this parser is to allow translation.
45 */
46 bool translatable = true;
Adam Lesinski979ccb22016-01-11 10:42:19 -080047
48 /**
49 * Whether positional arguments in formatted strings are treated as errors or warnings.
50 */
51 bool errorOnPositionalArguments = true;
Adam Lesinski9ba47d82015-10-13 11:37:10 -070052};
53
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080054/*
55 * Parses an XML file for resources and adds them to a ResourceTable.
56 */
57class ResourceParser {
58public:
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059 ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -070060 const ConfigDescription& config, const ResourceParserOptions& options = {});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080061
62 ResourceParser(const ResourceParser&) = delete; // No copy.
63
Adam Lesinski467f1712015-11-16 17:35:44 -080064 bool parse(xml::XmlPullParser* parser);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080065
66private:
67 /*
68 * Parses the XML subtree as a StyleString (flattened XML representation for strings
69 * with formatting). If successful, `outStyleString`
70 * contains the escaped and whitespace trimmed text, while `outRawString`
71 * contains the unescaped text. Returns true on success.
72 */
Adam Lesinski467f1712015-11-16 17:35:44 -080073 bool flattenXmlSubtree(xml::XmlPullParser* parser, std::u16string* outRawString,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080074 StyleString* outStyleString);
75
76 /*
Adam Lesinskie78fd612015-10-22 12:48:43 -070077 * Parses the XML subtree and returns an Item.
78 * The type of Item that can be parsed is denoted by the `typeMask`.
79 * If `allowRawValue` is true and the subtree can not be parsed as a regular Item, then a
80 * RawString is returned. Otherwise this returns false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080081 */
Adam Lesinski467f1712015-11-16 17:35:44 -080082 std::unique_ptr<Item> parseXml(xml::XmlPullParser* parser, const uint32_t typeMask,
Adam Lesinskie78fd612015-10-22 12:48:43 -070083 const bool allowRawValue);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084
Adam Lesinski467f1712015-11-16 17:35:44 -080085 bool parseResources(xml::XmlPullParser* parser);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080086 bool parseResource(xml::XmlPullParser* parser, ParsedResource* outResource);
87
88 bool parseItem(xml::XmlPullParser* parser, ParsedResource* outResource, uint32_t format);
Adam Lesinski467f1712015-11-16 17:35:44 -080089 bool parseString(xml::XmlPullParser* parser, ParsedResource* outResource);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080090
Adam Lesinski467f1712015-11-16 17:35:44 -080091 bool parsePublic(xml::XmlPullParser* parser, ParsedResource* outResource);
92 bool parsePublicGroup(xml::XmlPullParser* parser, ParsedResource* outResource);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080093 bool parseSymbolImpl(xml::XmlPullParser* parser, ParsedResource* outResource);
Adam Lesinski467f1712015-11-16 17:35:44 -080094 bool parseSymbol(xml::XmlPullParser* parser, ParsedResource* outResource);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080095 bool parseAddResource(xml::XmlPullParser* parser, ParsedResource* outResource);
Adam Lesinski467f1712015-11-16 17:35:44 -080096 bool parseAttr(xml::XmlPullParser* parser, ParsedResource* outResource);
97 bool parseAttrImpl(xml::XmlPullParser* parser, ParsedResource* outResource, bool weak);
98 Maybe<Attribute::Symbol> parseEnumOrFlagItem(xml::XmlPullParser* parser,
99 const StringPiece16& tag);
100 bool parseStyle(xml::XmlPullParser* parser, ParsedResource* outResource);
101 bool parseStyleItem(xml::XmlPullParser* parser, Style* style);
102 bool parseDeclareStyleable(xml::XmlPullParser* parser, ParsedResource* outResource);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800103 bool parseArray(xml::XmlPullParser* parser, ParsedResource* outResource);
104 bool parseIntegerArray(xml::XmlPullParser* parser, ParsedResource* outResource);
105 bool parseStringArray(xml::XmlPullParser* parser, ParsedResource* outResource);
106 bool parseArrayImpl(xml::XmlPullParser* parser, ParsedResource* outResource, uint32_t typeMask);
Adam Lesinski467f1712015-11-16 17:35:44 -0800107 bool parsePlural(xml::XmlPullParser* parser, ParsedResource* outResource);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800108
Adam Lesinski7751afc2016-01-06 15:45:28 -0800109 bool shouldStripResource(const ResourceNameRef& name,
110 const Maybe<std::u16string>& product) const;
111
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700112 IDiagnostics* mDiag;
113 ResourceTable* mTable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800114 Source mSource;
115 ConfigDescription mConfig;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700116 ResourceParserOptions mOptions;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800117};
118
119} // namespace aapt
120
121#endif // AAPT_RESOURCE_PARSER_H