blob: a9b4c14337fe8ec3113c197ddaabcfa5ba4f070a [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 Lesinskib274e352015-11-06 15:14:35 -080019#include "java/ClassDefinitionWriter.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 Lesinskica5638f2015-10-21 14:42:43 -070028static 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
Adam Lesinskib274e352015-11-06 15:14:35 -080061static bool writeSymbol(IDiagnostics* diag, ClassDefinitionWriter* outClassDef, const Source& source,
62 xml::Element* el) {
Adam Lesinski2ae4a872015-11-02 16:10:55 -080063 xml::Attribute* attr = el->findAttribute(xml::kSchemaAndroid, u"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
69 Maybe<StringPiece16> result = extractJavaIdentifier(diag, source.withLine(el->lineNumber),
70 attr->value);
71 if (!result) {
72 return false;
73 }
74
Adam Lesinskib274e352015-11-06 15:14:35 -080075 AnnotationProcessor processor;
76 processor.appendComment(el->comment);
77 outClassDef->addStringMember(result.value(), &processor, attr->value);
Adam Lesinskica5638f2015-10-21 14:42:43 -070078 return true;
79}
80
81bool ManifestClassGenerator::generate(IDiagnostics* diag, const StringPiece16& package,
Adam Lesinski467f1712015-11-16 17:35:44 -080082 xml::XmlResource* res, std::ostream* out) {
Adam Lesinskica5638f2015-10-21 14:42:43 -070083 xml::Element* el = xml::findRootElement(res->root.get());
84 if (!el) {
85 return false;
86 }
87
88 if (el->name != u"manifest" && !el->namespaceUri.empty()) {
89 diag->error(DiagMessage(res->file.source) << "no <manifest> root tag defined");
90 return false;
91 }
92
93 *out << "package " << package << ";\n\n"
Adam Lesinskib274e352015-11-06 15:14:35 -080094 << "public final class Manifest {\n";
Adam Lesinskica5638f2015-10-21 14:42:43 -070095
96 bool error = false;
97 std::vector<xml::Element*> children = el->getChildElements();
98
Adam Lesinskib274e352015-11-06 15:14:35 -080099 ClassDefinitionWriterOptions classOptions;
100 classOptions.useFinalQualifier = true;
101 classOptions.forceCreationIfEmpty = false;
Adam Lesinskica5638f2015-10-21 14:42:43 -0700102
103 // First write out permissions.
Adam Lesinskib274e352015-11-06 15:14:35 -0800104 ClassDefinitionWriter classDef("permission", classOptions);
Adam Lesinskica5638f2015-10-21 14:42:43 -0700105 for (xml::Element* childEl : children) {
106 if (childEl->namespaceUri.empty() && childEl->name == u"permission") {
Adam Lesinskib274e352015-11-06 15:14:35 -0800107 error |= !writeSymbol(diag, &classDef, res->file.source, childEl);
Adam Lesinskica5638f2015-10-21 14:42:43 -0700108 }
109 }
Adam Lesinskib274e352015-11-06 15:14:35 -0800110 classDef.writeToStream(out, " ");
Adam Lesinskica5638f2015-10-21 14:42:43 -0700111
112 // Next write out permission groups.
Adam Lesinskib274e352015-11-06 15:14:35 -0800113 classDef = ClassDefinitionWriter("permission_group", classOptions);
Adam Lesinskica5638f2015-10-21 14:42:43 -0700114 for (xml::Element* childEl : children) {
115 if (childEl->namespaceUri.empty() && childEl->name == u"permission-group") {
Adam Lesinskib274e352015-11-06 15:14:35 -0800116 error |= !writeSymbol(diag, &classDef, res->file.source, childEl);
Adam Lesinskica5638f2015-10-21 14:42:43 -0700117 }
118 }
Adam Lesinskib274e352015-11-06 15:14:35 -0800119 classDef.writeToStream(out, " ");
Adam Lesinskica5638f2015-10-21 14:42:43 -0700120
121 *out << "}\n";
122 return !error;
123}
124
125} // namespace aapt