2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef AAPT_RESOURCE_PARSER_H
18 #define AAPT_RESOURCE_PARSER_H
22 #include "android-base/macros.h"
23 #include "androidfw/StringPiece.h"
25 #include "ConfigDescription.h"
26 #include "Diagnostics.h"
27 #include "ResourceTable.h"
28 #include "ResourceValues.h"
29 #include "StringPool.h"
30 #include "util/Maybe.h"
31 #include "xml/XmlPullParser.h"
35 struct ParsedResource;
37 struct ResourceParserOptions {
39 * Whether the default setting for this parser is to allow translation.
41 bool translatable = true;
44 * Whether positional arguments in formatted strings are treated as errors or
47 bool error_on_positional_arguments = true;
51 * Parses an XML file for resources and adds them to a ResourceTable.
53 class ResourceParser {
55 ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
56 const ConfigDescription& config,
57 const ResourceParserOptions& options = {});
58 bool Parse(xml::XmlPullParser* parser);
61 DISALLOW_COPY_AND_ASSIGN(ResourceParser);
63 // Parses the XML subtree as a StyleString (flattened XML representation for strings with
64 // formatting). If parsing fails, false is returned and the out parameters are left in an
65 // unspecified state. Otherwise,
66 // `out_style_string` contains the escaped and whitespace trimmed text.
67 // `out_raw_string` contains the un-escaped text.
68 // `out_untranslatable_sections` contains the sections of the string that should not be
70 bool FlattenXmlSubtree(xml::XmlPullParser* parser, std::string* out_raw_string,
71 StyleString* out_style_string,
72 std::vector<UntranslatableSection>* out_untranslatable_sections);
75 * Parses the XML subtree and returns an Item.
76 * 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
79 * RawString is returned. Otherwise this returns false;
81 std::unique_ptr<Item> ParseXml(xml::XmlPullParser* parser,
82 const uint32_t type_mask,
83 const bool allow_raw_value);
85 bool ParseResources(xml::XmlPullParser* parser);
86 bool ParseResource(xml::XmlPullParser* parser, ParsedResource* out_resource);
88 bool ParseItem(xml::XmlPullParser* parser, ParsedResource* out_resource,
90 bool ParseString(xml::XmlPullParser* parser, ParsedResource* out_resource);
92 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,
103 Maybe<Attribute::Symbol> ParseEnumOrFlagItem(xml::XmlPullParser* parser,
104 const android::StringPiece& tag);
105 bool ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
106 ParsedResource* out_resource);
107 bool ParseStyleItem(xml::XmlPullParser* parser, Style* style);
108 bool ParseDeclareStyleable(xml::XmlPullParser* parser,
109 ParsedResource* out_resource);
110 bool ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource);
111 bool ParseIntegerArray(xml::XmlPullParser* parser,
112 ParsedResource* out_resource);
113 bool ParseStringArray(xml::XmlPullParser* parser,
114 ParsedResource* out_resource);
115 bool ParseArrayImpl(xml::XmlPullParser* parser, ParsedResource* out_resource,
117 bool ParsePlural(xml::XmlPullParser* parser, ParsedResource* out_resource);
120 ResourceTable* table_;
122 ConfigDescription config_;
123 ResourceParserOptions options_;
128 #endif // AAPT_RESOURCE_PARSER_H