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 | |
| 34 | /* |
| 35 | * Parses an XML file for resources and adds them to a ResourceTable. |
| 36 | */ |
| 37 | class ResourceParser { |
| 38 | public: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 39 | ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source, |
| 40 | const ConfigDescription& config); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 41 | |
| 42 | ResourceParser(const ResourceParser&) = delete; // No copy. |
| 43 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 44 | bool parse(XmlPullParser* parser); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 45 | |
| 46 | private: |
| 47 | /* |
| 48 | * Parses the XML subtree as a StyleString (flattened XML representation for strings |
| 49 | * with formatting). If successful, `outStyleString` |
| 50 | * contains the escaped and whitespace trimmed text, while `outRawString` |
| 51 | * contains the unescaped text. Returns true on success. |
| 52 | */ |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 53 | bool flattenXmlSubtree(XmlPullParser* parser, std::u16string* outRawString, |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 54 | StyleString* outStyleString); |
| 55 | |
| 56 | /* |
| 57 | * Parses the XML subtree and converts it to an Item. The type of Item that can be |
| 58 | * parsed is denoted by the `typeMask`. If `allowRawValue` is true and the subtree |
| 59 | * can not be parsed as a regular Item, then a RawString is returned. Otherwise |
| 60 | * this returns nullptr. |
| 61 | */ |
| 62 | std::unique_ptr<Item> parseXml(XmlPullParser* parser, uint32_t typeMask, bool allowRawValue); |
| 63 | |
| 64 | bool parseResources(XmlPullParser* parser); |
| 65 | bool parseString(XmlPullParser* parser, const ResourceNameRef& resourceName); |
| 66 | bool parseColor(XmlPullParser* parser, const ResourceNameRef& resourceName); |
| 67 | bool parsePrimitive(XmlPullParser* parser, const ResourceNameRef& resourceName); |
| 68 | bool parsePublic(XmlPullParser* parser, const StringPiece16& name); |
| 69 | bool parseAttr(XmlPullParser* parser, const ResourceNameRef& resourceName); |
| 70 | std::unique_ptr<Attribute> parseAttrImpl(XmlPullParser* parser, |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 71 | ResourceName* resourceName, |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 72 | bool weak); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 73 | Maybe<Attribute::Symbol> parseEnumOrFlagItem(XmlPullParser* parser, const StringPiece16& tag); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 74 | bool parseStyle(XmlPullParser* parser, const ResourceNameRef& resourceName); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 75 | bool parseStyleItem(XmlPullParser* parser, Style* style); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 76 | bool parseDeclareStyleable(XmlPullParser* parser, const ResourceNameRef& resourceName); |
| 77 | bool parseArray(XmlPullParser* parser, const ResourceNameRef& resourceName, uint32_t typeMask); |
| 78 | bool parsePlural(XmlPullParser* parser, const ResourceNameRef& resourceName); |
| 79 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 80 | IDiagnostics* mDiag; |
| 81 | ResourceTable* mTable; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 82 | Source mSource; |
| 83 | ConfigDescription mConfig; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | } // namespace aapt |
| 87 | |
| 88 | #endif // AAPT_RESOURCE_PARSER_H |