blob: 06b2581ceaffe2ee4f8ae19710d8d65009f62614 [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;
Adam Lesinski9f222042015-11-04 13:51:45 -080043
44 /**
45 * Whether the default setting for this parser is to allow translation.
46 */
47 bool translatable = 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 {
54public:
Adam Lesinski1ab598f2015-08-14 14:26:04 -070055 ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -070056 const ConfigDescription& config, const ResourceParserOptions& options = {});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080057
58 ResourceParser(const ResourceParser&) = delete; // No copy.
59
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060 bool parse(XmlPullParser* parser);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080061
62private:
63 /*
64 * Parses the XML subtree as a StyleString (flattened XML representation for 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 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -070069 bool flattenXmlSubtree(XmlPullParser* parser, std::u16string* outRawString,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070 StyleString* outStyleString);
71
72 /*
Adam Lesinskie78fd612015-10-22 12:48:43 -070073 * 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 Item, then a
76 * RawString is returned. Otherwise this returns false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077 */
Adam Lesinskie78fd612015-10-22 12:48:43 -070078 std::unique_ptr<Item> parseXml(XmlPullParser* parser, const uint32_t typeMask,
79 const bool allowRawValue);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080
81 bool parseResources(XmlPullParser* parser);
Adam Lesinski9ba47d82015-10-13 11:37:10 -070082 bool parseString(XmlPullParser* parser, ParsedResource* outResource);
83 bool parseColor(XmlPullParser* parser, ParsedResource* outResource);
84 bool parsePrimitive(XmlPullParser* parser, ParsedResource* outResource);
85 bool parsePublic(XmlPullParser* parser, ParsedResource* outResource);
Adam Lesinski9e10ac72015-10-16 14:37:48 -070086 bool parseSymbol(XmlPullParser* parser, ParsedResource* outResource);
Adam Lesinski9ba47d82015-10-13 11:37:10 -070087 bool parseAttr(XmlPullParser* parser, ParsedResource* outResource);
88 bool parseAttrImpl(XmlPullParser* parser, ParsedResource* outResource, bool weak);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070089 Maybe<Attribute::Symbol> parseEnumOrFlagItem(XmlPullParser* parser, const StringPiece16& tag);
Adam Lesinski9ba47d82015-10-13 11:37:10 -070090 bool parseStyle(XmlPullParser* parser, ParsedResource* outResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070091 bool parseStyleItem(XmlPullParser* parser, Style* style);
Adam Lesinski9ba47d82015-10-13 11:37:10 -070092 bool parseDeclareStyleable(XmlPullParser* parser, ParsedResource* outResource);
93 bool parseArray(XmlPullParser* parser, ParsedResource* outResource, uint32_t typeMask);
94 bool parsePlural(XmlPullParser* parser, ParsedResource* outResource);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080095
Adam Lesinski1ab598f2015-08-14 14:26:04 -070096 IDiagnostics* mDiag;
97 ResourceTable* mTable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098 Source mSource;
99 ConfigDescription mConfig;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700100 ResourceParserOptions mOptions;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101};
102
103} // namespace aapt
104
105#endif // AAPT_RESOURCE_PARSER_H