blob: 11b1e5b787ca050c768ed6a512e9c02afb85c908 [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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include <memory>
21
22#include "android-base/macros.h"
23
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include "ConfigDescription.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "Diagnostics.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080026#include "ResourceTable.h"
27#include "ResourceValues.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028#include "StringPool.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029#include "util/Maybe.h"
30#include "util/StringPiece.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080031#include "xml/XmlPullParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080033namespace aapt {
34
Adam Lesinski9ba47d82015-10-13 11:37:10 -070035struct ParsedResource;
36
37struct ResourceParserOptions {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 /**
39 * Whether the default setting for this parser is to allow translation.
40 */
41 bool translatable = true;
Adam Lesinski979ccb22016-01-11 10:42:19 -080042
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 /**
44 * Whether positional arguments in formatted strings are treated as errors or
45 * warnings.
46 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 bool error_on_positional_arguments = true;
Adam Lesinski9ba47d82015-10-13 11:37:10 -070048};
49
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080050/*
51 * Parses an XML file for resources and adds them to a ResourceTable.
52 */
53class ResourceParser {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 public:
55 ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
56 const ConfigDescription& config,
57 const ResourceParserOptions& options = {});
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 bool Parse(xml::XmlPullParser* parser);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 DISALLOW_COPY_AND_ASSIGN(ResourceParser);
62
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 /*
64 * Parses the XML subtree as a StyleString (flattened XML representation for
65 * strings
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 * with formatting). If successful, `out_style_string`
67 * contains the escaped and whitespace trimmed text, while `out_raw_string`
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 * contains the unescaped text. Returns true on success.
69 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 bool FlattenXmlSubtree(xml::XmlPullParser* parser,
71 std::string* out_raw_string,
72 StyleString* out_style_string);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 /*
75 * Parses the XML subtree and returns an Item.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 * The type of Item that can be parsed is denoted by the `type_mask`.
77 * If `allow_raw_value` is true and the subtree can not be parsed as a regular
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 * Item, then a
79 * RawString is returned. Otherwise this returns false;
80 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 std::unique_ptr<Item> ParseXml(xml::XmlPullParser* parser,
82 const uint32_t type_mask,
83 const bool allow_raw_value);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 bool ParseResources(xml::XmlPullParser* parser);
86 bool ParseResource(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080087
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 bool ParseItem(xml::XmlPullParser* parser, ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 uint32_t format);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 bool ParseString(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080091
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 bool ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource);
93 bool ParsePublicGroup(xml::XmlPullParser* parser,
94 ParsedResource* out_resource);
95 bool ParseSymbolImpl(xml::XmlPullParser* parser,
96 ParsedResource* out_resource);
97 bool ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource);
98 bool ParseAddResource(xml::XmlPullParser* parser,
99 ParsedResource* out_resource);
100 bool ParseAttr(xml::XmlPullParser* parser, ParsedResource* out_resource);
101 bool ParseAttrImpl(xml::XmlPullParser* parser, ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 bool weak);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 Maybe<Attribute::Symbol> ParseEnumOrFlagItem(xml::XmlPullParser* parser,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 const StringPiece& tag);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 bool ParseStyle(xml::XmlPullParser* parser, ParsedResource* out_resource);
106 bool ParseStyleItem(xml::XmlPullParser* parser, Style* style);
107 bool ParseDeclareStyleable(xml::XmlPullParser* parser,
108 ParsedResource* out_resource);
109 bool ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource);
110 bool ParseIntegerArray(xml::XmlPullParser* parser,
111 ParsedResource* out_resource);
112 bool ParseStringArray(xml::XmlPullParser* parser,
113 ParsedResource* out_resource);
114 bool ParseArrayImpl(xml::XmlPullParser* parser, ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 uint32_t typeMask);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 bool ParsePlural(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800117
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 IDiagnostics* diag_;
119 ResourceTable* table_;
120 Source source_;
121 ConfigDescription config_;
122 ResourceParserOptions options_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800123};
124
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800126
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127#endif // AAPT_RESOURCE_PARSER_H