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" |
| 25 | #include "XmlPullParser.h" |
| 26 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 27 | #include "util/Maybe.h" |
| 28 | #include "util/StringPiece.h" |
| 29 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 30 | #include <memory> |
| 31 | |
| 32 | namespace aapt { |
| 33 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 34 | struct ParsedResource; |
| 35 | |
| 36 | struct 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 Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Whether the default setting for this parser is to allow translation. |
| 46 | */ |
| 47 | bool translatable = true; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 50 | /* |
| 51 | * Parses an XML file for resources and adds them to a ResourceTable. |
| 52 | */ |
| 53 | class ResourceParser { |
| 54 | public: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 55 | ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source, |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 56 | const ConfigDescription& config, const ResourceParserOptions& options = {}); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 57 | |
| 58 | ResourceParser(const ResourceParser&) = delete; // No copy. |
| 59 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 60 | bool parse(XmlPullParser* parser); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 61 | |
| 62 | private: |
| 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 69 | bool flattenXmlSubtree(XmlPullParser* parser, std::u16string* outRawString, |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 70 | StyleString* outStyleString); |
| 71 | |
| 72 | /* |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 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 Item, then a |
| 76 | * RawString is returned. Otherwise this returns false; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 77 | */ |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 78 | std::unique_ptr<Item> parseXml(XmlPullParser* parser, const uint32_t typeMask, |
| 79 | const bool allowRawValue); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 80 | |
| 81 | bool parseResources(XmlPullParser* parser); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 82 | 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 Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 86 | bool parsePublicGroup(XmlPullParser* parser, ParsedResource* outResource); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 87 | bool parseSymbol(XmlPullParser* parser, ParsedResource* outResource); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 88 | bool parseAttr(XmlPullParser* parser, ParsedResource* outResource); |
| 89 | bool parseAttrImpl(XmlPullParser* parser, ParsedResource* outResource, bool weak); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 90 | Maybe<Attribute::Symbol> parseEnumOrFlagItem(XmlPullParser* parser, const StringPiece16& tag); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 91 | bool parseStyle(XmlPullParser* parser, ParsedResource* outResource); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 92 | bool parseStyleItem(XmlPullParser* parser, Style* style); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 93 | bool parseDeclareStyleable(XmlPullParser* parser, ParsedResource* outResource); |
| 94 | bool parseArray(XmlPullParser* parser, ParsedResource* outResource, uint32_t typeMask); |
| 95 | bool parsePlural(XmlPullParser* parser, ParsedResource* outResource); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 96 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 97 | IDiagnostics* mDiag; |
| 98 | ResourceTable* mTable; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 99 | Source mSource; |
| 100 | ConfigDescription mConfig; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 101 | ResourceParserOptions mOptions; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | } // namespace aapt |
| 105 | |
| 106 | #endif // AAPT_RESOURCE_PARSER_H |