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#include "Source.h"
18#include "java/AnnotationProcessor.h"
19#include "java/ClassDefinition.h"
20#include "java/ManifestClassGenerator.h"
21#include "util/Maybe.h"
22#include "xml/XmlDom.h"
23
24#include <algorithm>
25
26namespace aapt {
27
28static Maybe<StringPiece16> extractJavaIdentifier(IDiagnostics* diag, const Source& source,
29                                                  const StringPiece16& value) {
30    const StringPiece16 sep = u".";
31    auto iter = std::find_end(value.begin(), value.end(), sep.begin(), sep.end());
32
33    StringPiece16 result;
34    if (iter != value.end()) {
35        result.assign(iter + sep.size(), value.end() - (iter + sep.size()));
36    } else {
37        result = value;
38    }
39
40    if (result.empty()) {
41        diag->error(DiagMessage(source) << "empty symbol");
42        return {};
43    }
44
45    iter = util::findNonAlphaNumericAndNotInSet(result, u"_");
46    if (iter != result.end()) {
47        diag->error(DiagMessage(source)
48                    << "invalid character '" << StringPiece16(iter, 1)
49                    << "' in '" << result << "'");
50        return {};
51    }
52
53    if (*result.begin() >= u'0' && *result.begin() <= u'9') {
54        diag->error(DiagMessage(source) << "symbol can not start with a digit");
55        return {};
56    }
57
58    return result;
59}
60
61static bool writeSymbol(const Source& source, IDiagnostics* diag, xml::Element* el,
62                        ClassDefinition* classDef) {
63    xml::Attribute* attr = el->findAttribute(xml::kSchemaAndroid, u"name");
64    if (!attr) {
65        diag->error(DiagMessage(source) << "<" << el->name << "> must define 'android:name'");
66        return false;
67    }
68
69    Maybe<StringPiece16> result = extractJavaIdentifier(diag, source.withLine(el->lineNumber),
70                                                        attr->value);
71    if (!result) {
72        return false;
73    }
74
75    std::unique_ptr<StringMember> stringMember = util::make_unique<StringMember>(
76            util::utf16ToUtf8(result.value()), util::utf16ToUtf8(attr->value));
77    stringMember->getCommentBuilder()->appendComment(el->comment);
78
79    classDef->addMember(std::move(stringMember));
80    return true;
81}
82
83std::unique_ptr<ClassDefinition> generateManifestClass(IDiagnostics* diag, xml::XmlResource* res) {
84    xml::Element* el = xml::findRootElement(res->root.get());
85    if (!el) {
86        diag->error(DiagMessage(res->file.source) << "no root tag defined");
87        return {};
88    }
89
90    if (el->name != u"manifest" && !el->namespaceUri.empty()) {
91        diag->error(DiagMessage(res->file.source) << "no <manifest> root tag defined");
92        return {};
93    }
94
95    std::unique_ptr<ClassDefinition> permissionClass =
96            util::make_unique<ClassDefinition>("permission", ClassQualifier::Static, false);
97    std::unique_ptr<ClassDefinition> permissionGroupClass =
98            util::make_unique<ClassDefinition>("permission_group", ClassQualifier::Static, false);
99
100    bool error = false;
101
102    std::vector<xml::Element*> children = el->getChildElements();
103    for (xml::Element* childEl : children) {
104        if (childEl->namespaceUri.empty()) {
105            if (childEl->name == u"permission") {
106                error |= !writeSymbol(res->file.source, diag, childEl, permissionClass.get());
107            } else if (childEl->name == u"permission-group") {
108                error |= !writeSymbol(res->file.source, diag, childEl, permissionGroupClass.get());
109            }
110        }
111    }
112
113    if (error) {
114        return {};
115    }
116
117    std::unique_ptr<ClassDefinition> manifestClass =
118            util::make_unique<ClassDefinition>("Manifest", ClassQualifier::None, false);
119    manifestClass->addMember(std::move(permissionClass));
120    manifestClass->addMember(std::move(permissionGroupClass));
121    return manifestClass;
122}
123
124} // namespace aapt
125