ManifestClassGenerator.cpp revision 09f4d706d9438980465faabe81ed143fc299343e
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 "java/ManifestClassGenerator.h" 18 19#include <algorithm> 20 21#include "Source.h" 22#include "java/AnnotationProcessor.h" 23#include "java/ClassDefinition.h" 24#include "text/Unicode.h" 25#include "util/Maybe.h" 26#include "xml/XmlDom.h" 27 28using ::android::StringPiece; 29using ::aapt::text::IsJavaIdentifier; 30 31namespace aapt { 32 33static Maybe<StringPiece> ExtractJavaIdentifier(IDiagnostics* diag, const Source& source, 34 const std::string& value) { 35 StringPiece result = value; 36 size_t pos = value.rfind('.'); 37 if (pos != std::string::npos) { 38 result = result.substr(pos + 1); 39 } 40 41 if (result.empty()) { 42 diag->Error(DiagMessage(source) << "empty symbol"); 43 return {}; 44 } 45 46 if (!IsJavaIdentifier(result)) { 47 diag->Error(DiagMessage(source) << "invalid Java identifier '" << result << "'"); 48 return {}; 49 } 50 return result; 51} 52 53static bool WriteSymbol(const Source& source, IDiagnostics* diag, xml::Element* el, 54 ClassDefinition* class_def) { 55 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name"); 56 if (!attr) { 57 diag->Error(DiagMessage(source) << "<" << el->name << "> must define 'android:name'"); 58 return false; 59 } 60 61 Maybe<StringPiece> result = 62 ExtractJavaIdentifier(diag, source.WithLine(el->line_number), attr->value); 63 if (!result) { 64 return false; 65 } 66 67 std::unique_ptr<StringMember> string_member = 68 util::make_unique<StringMember>(result.value(), attr->value); 69 string_member->GetCommentBuilder()->AppendComment(el->comment); 70 71 class_def->AddMember(std::move(string_member)); 72 return true; 73} 74 75std::unique_ptr<ClassDefinition> GenerateManifestClass(IDiagnostics* diag, xml::XmlResource* res) { 76 xml::Element* el = xml::FindRootElement(res->root.get()); 77 if (!el) { 78 diag->Error(DiagMessage(res->file.source) << "no root tag defined"); 79 return {}; 80 } 81 82 if (el->name != "manifest" && !el->namespace_uri.empty()) { 83 diag->Error(DiagMessage(res->file.source) << "no <manifest> root tag defined"); 84 return {}; 85 } 86 87 std::unique_ptr<ClassDefinition> permission_class = 88 util::make_unique<ClassDefinition>("permission", ClassQualifier::kStatic, false); 89 std::unique_ptr<ClassDefinition> permission_group_class = 90 util::make_unique<ClassDefinition>("permission_group", ClassQualifier::kStatic, false); 91 92 bool error = false; 93 std::vector<xml::Element*> children = el->GetChildElements(); 94 for (xml::Element* child_el : children) { 95 if (child_el->namespace_uri.empty()) { 96 if (child_el->name == "permission") { 97 error |= !WriteSymbol(res->file.source, diag, child_el, permission_class.get()); 98 } else if (child_el->name == "permission-group") { 99 error |= !WriteSymbol(res->file.source, diag, child_el, permission_group_class.get()); 100 } 101 } 102 } 103 104 if (error) { 105 return {}; 106 } 107 108 std::unique_ptr<ClassDefinition> manifest_class = 109 util::make_unique<ClassDefinition>("Manifest", ClassQualifier::kNone, false); 110 manifest_class->AddMember(std::move(permission_class)); 111 manifest_class->AddMember(std::move(permission_group_class)); 112 return manifest_class; 113} 114 115} // namespace aapt 116