blob: 548f5f9531fd68dc31a1e5503188a51e1c8fe798 [file] [log] [blame]
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef AAPT_RESOURCE_PARSER_H
#define AAPT_RESOURCE_PARSER_H
#include <memory>
#include <optional>
#include "android-base/macros.h"
#include "androidfw/ConfigDescription.h"
#include "androidfw/StringPiece.h"
#include "Diagnostics.h"
#include "ResourceTable.h"
#include "ResourceValues.h"
#include "StringPool.h"
#include "xml/XmlPullParser.h"
namespace aapt {
struct ParsedResource;
struct ResourceParserOptions {
/**
* Whether the default setting for this parser is to allow translation.
*/
bool translatable = true;
/**
* Whether positional arguments in formatted strings are treated as errors or
* warnings.
*/
bool error_on_positional_arguments = true;
/**
* If true, apply the same visibility rules for styleables as are used for
* all other resources. Otherwise, all styleables will be made public.
*/
bool preserve_visibility_of_styleables = false;
// If visibility was forced, we need to use it when creating a new resource and also error if we
// try to parse the <public>, <public-group>, <java-symbol> or <symbol> tags.
std::optional<Visibility::Level> visibility;
};
struct FlattenedXmlSubTree {
std::string raw_value;
StyleString style_string;
std::vector<UntranslatableSection> untranslatable_sections;
xml::IPackageDeclStack* namespace_resolver;
Source source;
};
/*
* Parses an XML file for resources and adds them to a ResourceTable.
*/
class ResourceParser {
public:
ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
const android::ConfigDescription& config,
const ResourceParserOptions& options = {});
bool Parse(xml::XmlPullParser* parser);
static std::unique_ptr<Item> ParseXml(const FlattenedXmlSubTree& xmlsub_tree, uint32_t type_mask,
bool allow_raw_value, ResourceTable& table,
const android::ConfigDescription& config,
IDiagnostics& diag);
private:
DISALLOW_COPY_AND_ASSIGN(ResourceParser);
std::optional<FlattenedXmlSubTree> CreateFlattenSubTree(xml::XmlPullParser* parser);
// Parses the XML subtree as a StyleString (flattened XML representation for strings with
// formatting). If parsing fails, false is returned and the out parameters are left in an
// unspecified state. Otherwise,
// `out_style_string` contains the escaped and whitespace trimmed text.
// `out_raw_string` contains the un-escaped text.
// `out_untranslatable_sections` contains the sections of the string that should not be
// translated.
bool FlattenXmlSubtree(xml::XmlPullParser* parser, std::string* out_raw_string,
StyleString* out_style_string,
std::vector<UntranslatableSection>* out_untranslatable_sections);
/*
* Parses the XML subtree and returns an Item.
* The type of Item that can be parsed is denoted by the `type_mask`.
* If `allow_raw_value` is true and the subtree can not be parsed as a regular
* Item, then a
* RawString is returned. Otherwise this returns false;
*/
std::unique_ptr<Item> ParseXml(xml::XmlPullParser* parser, const uint32_t type_mask,
const bool allow_raw_value);
bool ParseResources(xml::XmlPullParser* parser);
bool ParseResource(xml::XmlPullParser* parser, ParsedResource* out_resource);
bool ParseItem(xml::XmlPullParser* parser, ParsedResource* out_resource, uint32_t format);
bool ParseString(xml::XmlPullParser* parser, ParsedResource* out_resource);
bool ParseMacro(xml::XmlPullParser* parser, ParsedResource* out_resource);
bool ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource);
bool ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource);
bool ParseStagingPublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource);
bool ParseStagingPublicGroupFinal(xml::XmlPullParser* parser, ParsedResource* out_resource);
bool ParseSymbolImpl(xml::XmlPullParser* parser, ParsedResource* out_resource);
bool ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource);
bool ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource);
bool ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource);
bool ParseAttr(xml::XmlPullParser* parser, ParsedResource* out_resource);
bool ParseAttrImpl(xml::XmlPullParser* parser, ParsedResource* out_resource, bool weak);
std::optional<Attribute::Symbol> ParseEnumOrFlagItem(xml::XmlPullParser* parser,
const android::StringPiece& tag);
bool ParseStyle(ResourceType type, xml::XmlPullParser* parser, ParsedResource* out_resource);
bool ParseStyleItem(xml::XmlPullParser* parser, Style* style);
bool ParseDeclareStyleable(xml::XmlPullParser* parser, ParsedResource* out_resource);
bool ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource);
bool ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource);
bool ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource);
bool ParseArrayImpl(xml::XmlPullParser* parser, ParsedResource* out_resource, uint32_t typeMask);
bool ParsePlural(xml::XmlPullParser* parser, ParsedResource* out_resource);
IDiagnostics* diag_;
ResourceTable* table_;
Source source_;
android::ConfigDescription config_;
ResourceParserOptions options_;
};
} // namespace aapt
#endif // AAPT_RESOURCE_PARSER_H