Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7ff3ee19 authored by Adam Lesinski's avatar Adam Lesinski
Browse files

AAPT2: Respect format attribute of <item> tag

An <item> is a general tag that can override certain behavior. For
instance, this is allowed:

    <item name="foo" type="integer" format="float">0.4</item>

Even though without the format attribute, this would be illegal.

Change-Id: I8133ce59e14719a70d7476a1464c3f564c435289
parent 36a832dd
Loading
Loading
Loading
Loading
+322 −276

File changed.

Preview size limit exceeded, changes collapsed.

+8 −3
Original line number Diff line number Diff line
@@ -78,9 +78,11 @@ private:
                                   const bool allowRawValue);

    bool parseResources(xml::XmlPullParser* parser);
    bool parseResource(xml::XmlPullParser* parser, ParsedResource* outResource);

    bool parseItem(xml::XmlPullParser* parser, ParsedResource* outResource, uint32_t format);
    bool parseString(xml::XmlPullParser* parser, ParsedResource* outResource);
    bool parseColor(xml::XmlPullParser* parser, ParsedResource* outResource);
    bool parsePrimitive(xml::XmlPullParser* parser, ParsedResource* outResource);

    bool parsePublic(xml::XmlPullParser* parser, ParsedResource* outResource);
    bool parsePublicGroup(xml::XmlPullParser* parser, ParsedResource* outResource);
    bool parseSymbolImpl(xml::XmlPullParser* parser, ParsedResource* outResource);
@@ -93,7 +95,10 @@ private:
    bool parseStyle(xml::XmlPullParser* parser, ParsedResource* outResource);
    bool parseStyleItem(xml::XmlPullParser* parser, Style* style);
    bool parseDeclareStyleable(xml::XmlPullParser* parser, ParsedResource* outResource);
    bool parseArray(xml::XmlPullParser* parser, ParsedResource* outResource, uint32_t typeMask);
    bool parseArray(xml::XmlPullParser* parser, ParsedResource* outResource);
    bool parseIntegerArray(xml::XmlPullParser* parser, ParsedResource* outResource);
    bool parseStringArray(xml::XmlPullParser* parser, ParsedResource* outResource);
    bool parseArrayImpl(xml::XmlPullParser* parser, ParsedResource* outResource, uint32_t typeMask);
    bool parsePlural(xml::XmlPullParser* parser, ParsedResource* outResource);

    IDiagnostics* mDiag;
+10 −0
Original line number Diff line number Diff line
@@ -575,4 +575,14 @@ TEST_F(ResourceParserTest, AddResourcesElementShouldAddEntryWithUndefinedSymbol)
    EXPECT_EQ(SymbolState::kUndefined, entry->symbolStatus.state);
}

TEST_F(ResourceParserTest, ParseItemElementWithFormat) {
    std::string input = R"EOF(<item name="foo" type="integer" format="float">0.3</item>)EOF";
    ASSERT_TRUE(testParse(input));

    BinaryPrimitive* val = test::getValue<BinaryPrimitive>(&mTable, u"@integer/foo");
    ASSERT_NE(nullptr, val);

    EXPECT_EQ(uint32_t(android::Res_value::TYPE_FLOAT), val->value.dataType);
}

} // namespace aapt
+84 −0
Original line number Diff line number Diff line
/*
 * 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_UTIL_IMMUTABLEMAP_H
#define AAPT_UTIL_IMMUTABLEMAP_H

#include "util/TypeTraits.h"

#include <utility>
#include <vector>

namespace aapt {

template <typename TKey, typename TValue>
class ImmutableMap {
    static_assert(is_comparable<TKey, TKey>::value, "key is not comparable");

private:
    std::vector<std::pair<TKey, TValue>> mData;

    explicit ImmutableMap(std::vector<std::pair<TKey, TValue>> data) : mData(std::move(data)) {
    }

public:
    using const_iterator = typename decltype(mData)::const_iterator;

    ImmutableMap(ImmutableMap&&) = default;
    ImmutableMap& operator=(ImmutableMap&&) = default;

    ImmutableMap(const ImmutableMap&) = delete;
    ImmutableMap& operator=(const ImmutableMap&) = delete;

    static ImmutableMap<TKey, TValue> createPreSorted(
            std::initializer_list<std::pair<TKey, TValue>> list) {
        return ImmutableMap(std::vector<std::pair<TKey, TValue>>(list.begin(), list.end()));
    }

    static ImmutableMap<TKey, TValue> createAndSort(
            std::initializer_list<std::pair<TKey, TValue>> list) {
        std::vector<std::pair<TKey, TValue>> data(list.begin(), list.end());
        std::sort(data.begin(), data.end());
        return ImmutableMap(std::move(data));
    }

    template <typename TKey2,
              typename = typename std::enable_if<is_comparable<TKey, TKey2>::value>::type>
    const_iterator find(const TKey2& key) const {
        auto cmp = [](const std::pair<TKey, TValue>& candidate, const TKey2& target) -> bool {
            return candidate.first < target;
        };

        const_iterator endIter = end();
        auto iter = std::lower_bound(mData.begin(), endIter, key, cmp);
        if (iter == endIter || iter->first == key) {
            return iter;
        }
        return endIter;
    }

    const_iterator begin() const {
        return mData.begin();
    }

    const_iterator end() const {
        return mData.end();
    }
};

} // namespace aapt

#endif /* AAPT_UTIL_IMMUTABLEMAP_H */
+51 −0
Original line number Diff line number Diff line
/*
 * 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_UTIL_TYPETRAITS_H
#define AAPT_UTIL_TYPETRAITS_H

#include <type_traits>

namespace aapt {

#define DEFINE_HAS_BINARY_OP_TRAIT(name, op) \
    template <typename T, typename U> \
    struct name { \
        template <typename V, typename W> \
        static constexpr decltype(std::declval<V>() op std::declval<W>(), bool()) test(int) { \
        return true; \
    } \
    template <typename V, typename W> \
    static constexpr bool test(...) { \
        return false; \
    } \
    static constexpr bool value = test<T, U>(int()); \
}

DEFINE_HAS_BINARY_OP_TRAIT(has_eq_op, ==);
DEFINE_HAS_BINARY_OP_TRAIT(has_lt_op, <);

/**
 * Type trait that checks if two types can be equated (==) and compared (<).
 */
template <typename T, typename U>
struct is_comparable {
    static constexpr bool value = has_eq_op<T, U>::value && has_lt_op<T, U>::value;
};

} // namespace aapt

#endif /* AAPT_UTIL_TYPETRAITS_H */