blob: 644ed4923c3a9788e0ef1fc33e3c10c19fcac514 [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 {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 /**
37 * Whether the default setting for this parser is to allow translation.
38 */
39 bool translatable = true;
Adam Lesinski979ccb22016-01-11 10:42:19 -080040
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 /**
42 * Whether positional arguments in formatted strings are treated as errors or
43 * warnings.
44 */
45 bool errorOnPositionalArguments = true;
Adam Lesinski9ba47d82015-10-13 11:37:10 -070046};
47
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048/*
49 * Parses an XML file for resources and adds them to a ResourceTable.
50 */
51class ResourceParser {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 public:
53 ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
54 const ConfigDescription& config,
55 const ResourceParserOptions& options = {});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 ResourceParser(const ResourceParser&) = delete; // No copy.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080058
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 bool parse(xml::XmlPullParser* parser);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080060
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 private:
62 /*
63 * Parses the XML subtree as a StyleString (flattened XML representation for
64 * strings
65 * with formatting). If successful, `outStyleString`
66 * contains the escaped and whitespace trimmed text, while `outRawString`
67 * contains the unescaped text. Returns true on success.
68 */
69 bool flattenXmlSubtree(xml::XmlPullParser* parser, std::string* outRawString,
70 StyleString* outStyleString);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080071
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 /*
73 * Parses the XML subtree and returns an Item.
74 * The type of Item that can be parsed is denoted by the `typeMask`.
75 * If `allowRawValue` is true and the subtree can not be parsed as a regular
76 * Item, then a
77 * RawString is returned. Otherwise this returns false;
78 */
79 std::unique_ptr<Item> parseXml(xml::XmlPullParser* parser,
80 const uint32_t typeMask,
81 const bool allowRawValue);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 bool parseResources(xml::XmlPullParser* parser);
84 bool parseResource(xml::XmlPullParser* parser, ParsedResource* outResource);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080085
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 bool parseItem(xml::XmlPullParser* parser, ParsedResource* outResource,
87 uint32_t format);
88 bool parseString(xml::XmlPullParser* parser, ParsedResource* outResource);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080089
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 bool parsePublic(xml::XmlPullParser* parser, ParsedResource* outResource);
91 bool parsePublicGroup(xml::XmlPullParser* parser,
92 ParsedResource* outResource);
93 bool parseSymbolImpl(xml::XmlPullParser* parser, ParsedResource* outResource);
94 bool parseSymbol(xml::XmlPullParser* parser, ParsedResource* outResource);
95 bool parseAddResource(xml::XmlPullParser* parser,
96 ParsedResource* outResource);
97 bool parseAttr(xml::XmlPullParser* parser, ParsedResource* outResource);
98 bool parseAttrImpl(xml::XmlPullParser* parser, ParsedResource* outResource,
99 bool weak);
100 Maybe<Attribute::Symbol> parseEnumOrFlagItem(xml::XmlPullParser* parser,
101 const StringPiece& tag);
102 bool parseStyle(xml::XmlPullParser* parser, ParsedResource* outResource);
103 bool parseStyleItem(xml::XmlPullParser* parser, Style* style);
104 bool parseDeclareStyleable(xml::XmlPullParser* parser,
105 ParsedResource* outResource);
106 bool parseArray(xml::XmlPullParser* parser, ParsedResource* outResource);
107 bool parseIntegerArray(xml::XmlPullParser* parser,
108 ParsedResource* outResource);
109 bool parseStringArray(xml::XmlPullParser* parser,
110 ParsedResource* outResource);
111 bool parseArrayImpl(xml::XmlPullParser* parser, ParsedResource* outResource,
112 uint32_t typeMask);
113 bool parsePlural(xml::XmlPullParser* parser, ParsedResource* outResource);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800114
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 IDiagnostics* mDiag;
116 ResourceTable* mTable;
117 Source mSource;
118 ConfigDescription mConfig;
119 ResourceParserOptions mOptions;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800120};
121
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800123
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124#endif // AAPT_RESOURCE_PARSER_H