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_XML_FLATTENER_H |
| 18 | #define AAPT_XML_FLATTENER_H |
| 19 | |
| 20 | #include "BigBuffer.h" |
| 21 | #include "Maybe.h" |
| 22 | #include "Resolver.h" |
| 23 | #include "Source.h" |
| 24 | #include "XmlPullParser.h" |
| 25 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame^] | 26 | #include <string> |
| 27 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 28 | namespace aapt { |
| 29 | |
| 30 | /** |
| 31 | * Flattens an XML file into a binary representation parseable by |
| 32 | * the Android resource system. References to resources are checked |
| 33 | * and string values are transformed to typed data where possible. |
| 34 | */ |
| 35 | class XmlFlattener { |
| 36 | public: |
| 37 | struct Options { |
| 38 | /** |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame^] | 39 | * The package to use when a reference has no package specified |
| 40 | * (or a namespace URI equals "http://schemas.android.com/apk/res-auto"). |
| 41 | */ |
| 42 | std::u16string defaultPackage; |
| 43 | |
| 44 | /** |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 45 | * If set, tells the XmlFlattener to strip out |
| 46 | * attributes that have been introduced after |
| 47 | * max SDK. |
| 48 | */ |
| 49 | Maybe<size_t> maxSdkAttribute; |
| 50 | }; |
| 51 | |
| 52 | /** |
| 53 | * Creates a flattener with a Resolver to resolve references |
| 54 | * and attributes. |
| 55 | */ |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 56 | XmlFlattener(const std::shared_ptr<ResourceTable>& table, |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame^] | 57 | const std::shared_ptr<IResolver>& resolver); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 58 | |
| 59 | XmlFlattener(const XmlFlattener&) = delete; // Not copyable. |
| 60 | |
| 61 | /** |
| 62 | * Flatten an XML file, reading from the XML parser and writing to the |
| 63 | * BigBuffer. The source object is mainly for logging errors. If the |
| 64 | * function succeeds, returns the smallest SDK version of an attribute that |
| 65 | * was stripped out. If no attributes were stripped out, the return value |
| 66 | * is 0. |
| 67 | */ |
| 68 | Maybe<size_t> flatten(const Source& source, const std::shared_ptr<XmlPullParser>& parser, |
| 69 | BigBuffer* outBuffer, Options options); |
| 70 | |
| 71 | private: |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 72 | std::shared_ptr<ResourceTable> mTable; |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame^] | 73 | std::shared_ptr<IResolver> mResolver; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | } // namespace aapt |
| 77 | |
| 78 | #endif // AAPT_XML_FLATTENER_H |