blob: 901a344881fc08d2f5028e762c24402ae8328ed0 [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"
18#include "XmlDom.h"
19
20#include "java/AnnotationProcessor.h"
21#include "java/ManifestClassGenerator.h"
22#include "util/Maybe.h"
23
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
61static bool writeSymbol(IDiagnostics* diag, const Source& source, xml::Element* el,
62 std::ostream* out) {
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
75 *out << "\n";
76
77 if (!util::trimWhitespace(el->comment).empty()) {
78 AnnotationProcessor processor(" ");
79 processor.appendComment(el->comment);
80 *out << processor.buildComment() << "\n";
81 std::string annotations = processor.buildAnnotations();
82 if (!annotations.empty()) {
83 *out << annotations << "\n";
84 }
85 }
86 *out << " public static final String " << result.value() << "=\"" << attr->value << "\";\n";
87 return true;
88}
89
90bool ManifestClassGenerator::generate(IDiagnostics* diag, const StringPiece16& package,
91 XmlResource* res, std::ostream* out) {
92 xml::Element* el = xml::findRootElement(res->root.get());
93 if (!el) {
94 return false;
95 }
96
97 if (el->name != u"manifest" && !el->namespaceUri.empty()) {
98 diag->error(DiagMessage(res->file.source) << "no <manifest> root tag defined");
99 return false;
100 }
101
102 *out << "package " << package << ";\n\n"
103 << "public class Manifest {\n";
104
105 bool error = false;
106 std::vector<xml::Element*> children = el->getChildElements();
107
108
109 // First write out permissions.
110 *out << " public static class permission {\n";
111 for (xml::Element* childEl : children) {
112 if (childEl->namespaceUri.empty() && childEl->name == u"permission") {
113 error |= !writeSymbol(diag, res->file.source, childEl, out);
114 }
115 }
116 *out << " }\n";
117
118 // Next write out permission groups.
119 *out << " public static class permission_group {\n";
120 for (xml::Element* childEl : children) {
121 if (childEl->namespaceUri.empty() && childEl->name == u"permission-group") {
122 error |= !writeSymbol(diag, res->file.source, childEl, out);
123 }
124 }
125 *out << " }\n";
126
127 *out << "}\n";
128 return !error;
129}
130
131} // namespace aapt