blob: 5ff11b1303d3296fb065bf64e00a5d3f7a688bec [file] [log] [blame]
Adam Lesinskica5638f2015-10-21 14:42:43 -07001/*
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"
Adam Lesinskica5638f2015-10-21 14:42:43 -070018#include "java/AnnotationProcessor.h"
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070019#include "java/ClassDefinition.h"
Adam Lesinskica5638f2015-10-21 14:42:43 -070020#include "java/ManifestClassGenerator.h"
21#include "util/Maybe.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080022#include "xml/XmlDom.h"
Adam Lesinskica5638f2015-10-21 14:42:43 -070023
24#include <algorithm>
25
26namespace aapt {
27
Adam Lesinskid0f116b2016-07-08 15:00:32 -070028static Maybe<StringPiece> extractJavaIdentifier(IDiagnostics* diag, const Source& source,
29 const StringPiece& value) {
30 const StringPiece sep = ".";
Adam Lesinskica5638f2015-10-21 14:42:43 -070031 auto iter = std::find_end(value.begin(), value.end(), sep.begin(), sep.end());
32
Adam Lesinskid0f116b2016-07-08 15:00:32 -070033 StringPiece result;
Adam Lesinskica5638f2015-10-21 14:42:43 -070034 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
Adam Lesinskid0f116b2016-07-08 15:00:32 -070045 iter = util::findNonAlphaNumericAndNotInSet(result, "_");
Adam Lesinskica5638f2015-10-21 14:42:43 -070046 if (iter != result.end()) {
47 diag->error(DiagMessage(source)
Adam Lesinskid0f116b2016-07-08 15:00:32 -070048 << "invalid character '" << StringPiece(iter, 1)
Adam Lesinskica5638f2015-10-21 14:42:43 -070049 << "' in '" << result << "'");
50 return {};
51 }
52
Adam Lesinskid0f116b2016-07-08 15:00:32 -070053 if (*result.begin() >= '0' && *result.begin() <= '9') {
Adam Lesinskica5638f2015-10-21 14:42:43 -070054 diag->error(DiagMessage(source) << "symbol can not start with a digit");
55 return {};
56 }
57
58 return result;
59}
60
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070061static bool writeSymbol(const Source& source, IDiagnostics* diag, xml::Element* el,
62 ClassDefinition* classDef) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -070063 xml::Attribute* attr = el->findAttribute(xml::kSchemaAndroid, "name");
Adam Lesinskica5638f2015-10-21 14:42:43 -070064 if (!attr) {
65 diag->error(DiagMessage(source) << "<" << el->name << "> must define 'android:name'");
66 return false;
67 }
68
Adam Lesinskid0f116b2016-07-08 15:00:32 -070069 Maybe<StringPiece> result = extractJavaIdentifier(diag, source.withLine(el->lineNumber),
70 attr->value);
Adam Lesinskica5638f2015-10-21 14:42:43 -070071 if (!result) {
72 return false;
73 }
74
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070075 std::unique_ptr<StringMember> stringMember = util::make_unique<StringMember>(
Adam Lesinskid0f116b2016-07-08 15:00:32 -070076 result.value(), attr->value);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070077 stringMember->getCommentBuilder()->appendComment(el->comment);
78
79 classDef->addMember(std::move(stringMember));
Adam Lesinskica5638f2015-10-21 14:42:43 -070080 return true;
81}
82
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070083std::unique_ptr<ClassDefinition> generateManifestClass(IDiagnostics* diag, xml::XmlResource* res) {
Adam Lesinskica5638f2015-10-21 14:42:43 -070084 xml::Element* el = xml::findRootElement(res->root.get());
85 if (!el) {
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070086 diag->error(DiagMessage(res->file.source) << "no root tag defined");
87 return {};
Adam Lesinskica5638f2015-10-21 14:42:43 -070088 }
89
Adam Lesinskid0f116b2016-07-08 15:00:32 -070090 if (el->name != "manifest" && !el->namespaceUri.empty()) {
Adam Lesinskica5638f2015-10-21 14:42:43 -070091 diag->error(DiagMessage(res->file.source) << "no <manifest> root tag defined");
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070092 return {};
Adam Lesinskica5638f2015-10-21 14:42:43 -070093 }
94
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070095 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);
Adam Lesinskica5638f2015-10-21 14:42:43 -070099
100 bool error = false;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700101
Adam Lesinskica5638f2015-10-21 14:42:43 -0700102 std::vector<xml::Element*> children = el->getChildElements();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700103 for (xml::Element* childEl : children) {
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700104 if (childEl->namespaceUri.empty()) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700105 if (childEl->name == "permission") {
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700106 error |= !writeSymbol(res->file.source, diag, childEl, permissionClass.get());
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700107 } else if (childEl->name == "permission-group") {
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700108 error |= !writeSymbol(res->file.source, diag, childEl, permissionGroupClass.get());
109 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700110 }
111 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700112
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700113 if (error) {
114 return {};
Adam Lesinskica5638f2015-10-21 14:42:43 -0700115 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700116
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -0700117 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;
Adam Lesinskica5638f2015-10-21 14:42:43 -0700122}
123
124} // namespace aapt