Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 21 | #include "Diagnostics.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 22 | #include "ResourceTable.h" |
| 23 | #include "ResourceValues.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 24 | #include "StringPool.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 25 | #include "util/Maybe.h" |
| 26 | #include "util/StringPiece.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 27 | #include "xml/XmlPullParser.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 28 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 29 | #include <memory> |
| 30 | |
| 31 | namespace aapt { |
| 32 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 33 | struct ParsedResource; |
| 34 | |
| 35 | struct ResourceParserOptions { |
| 36 | /** |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 37 | * Whether the default setting for this parser is to allow translation. |
| 38 | */ |
| 39 | bool translatable = true; |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Whether positional arguments in formatted strings are treated as errors or warnings. |
| 43 | */ |
| 44 | bool errorOnPositionalArguments = true; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 47 | /* |
| 48 | * Parses an XML file for resources and adds them to a ResourceTable. |
| 49 | */ |
| 50 | class ResourceParser { |
| 51 | public: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 52 | ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source, |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 53 | const ConfigDescription& config, const ResourceParserOptions& options = {}); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 54 | |
| 55 | ResourceParser(const ResourceParser&) = delete; // No copy. |
| 56 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 57 | bool parse(xml::XmlPullParser* parser); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 58 | |
| 59 | private: |
| 60 | /* |
| 61 | * Parses the XML subtree as a StyleString (flattened XML representation for strings |
| 62 | * with formatting). If successful, `outStyleString` |
| 63 | * contains the escaped and whitespace trimmed text, while `outRawString` |
| 64 | * contains the unescaped text. Returns true on success. |
| 65 | */ |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 66 | bool flattenXmlSubtree(xml::XmlPullParser* parser, std::u16string* outRawString, |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 67 | StyleString* outStyleString); |
| 68 | |
| 69 | /* |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 70 | * Parses the XML subtree and returns an Item. |
| 71 | * The type of Item that can be parsed is denoted by the `typeMask`. |
| 72 | * If `allowRawValue` is true and the subtree can not be parsed as a regular Item, then a |
| 73 | * RawString is returned. Otherwise this returns false; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 74 | */ |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 75 | std::unique_ptr<Item> parseXml(xml::XmlPullParser* parser, const uint32_t typeMask, |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 76 | const bool allowRawValue); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 77 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 78 | bool parseResources(xml::XmlPullParser* parser); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 79 | bool parseResource(xml::XmlPullParser* parser, ParsedResource* outResource); |
| 80 | |
| 81 | bool parseItem(xml::XmlPullParser* parser, ParsedResource* outResource, uint32_t format); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 82 | bool parseString(xml::XmlPullParser* parser, ParsedResource* outResource); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 83 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 84 | bool parsePublic(xml::XmlPullParser* parser, ParsedResource* outResource); |
| 85 | bool parsePublicGroup(xml::XmlPullParser* parser, ParsedResource* outResource); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 86 | bool parseSymbolImpl(xml::XmlPullParser* parser, ParsedResource* outResource); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 87 | bool parseSymbol(xml::XmlPullParser* parser, ParsedResource* outResource); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 88 | bool parseAddResource(xml::XmlPullParser* parser, ParsedResource* outResource); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 89 | bool parseAttr(xml::XmlPullParser* parser, ParsedResource* outResource); |
| 90 | bool parseAttrImpl(xml::XmlPullParser* parser, ParsedResource* outResource, bool weak); |
| 91 | Maybe<Attribute::Symbol> parseEnumOrFlagItem(xml::XmlPullParser* parser, |
| 92 | const StringPiece16& tag); |
| 93 | bool parseStyle(xml::XmlPullParser* parser, ParsedResource* outResource); |
| 94 | bool parseStyleItem(xml::XmlPullParser* parser, Style* style); |
| 95 | bool parseDeclareStyleable(xml::XmlPullParser* parser, ParsedResource* outResource); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 96 | bool parseArray(xml::XmlPullParser* parser, ParsedResource* outResource); |
| 97 | bool parseIntegerArray(xml::XmlPullParser* parser, ParsedResource* outResource); |
| 98 | bool parseStringArray(xml::XmlPullParser* parser, ParsedResource* outResource); |
| 99 | bool parseArrayImpl(xml::XmlPullParser* parser, ParsedResource* outResource, uint32_t typeMask); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 100 | bool parsePlural(xml::XmlPullParser* parser, ParsedResource* outResource); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 101 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 102 | IDiagnostics* mDiag; |
| 103 | ResourceTable* mTable; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 104 | Source mSource; |
| 105 | ConfigDescription mConfig; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 106 | ResourceParserOptions mOptions; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | } // namespace aapt |
| 110 | |
| 111 | #endif // AAPT_RESOURCE_PARSER_H |