1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Source.h"
#include "java/AnnotationProcessor.h"
#include "java/ClassDefinitionWriter.h"
#include "java/ManifestClassGenerator.h"
#include "util/Maybe.h"
#include "xml/XmlDom.h"
#include <algorithm>
namespace aapt {
static Maybe<StringPiece16> extractJavaIdentifier(IDiagnostics* diag, const Source& source,
const StringPiece16& value) {
const StringPiece16 sep = u".";
auto iter = std::find_end(value.begin(), value.end(), sep.begin(), sep.end());
StringPiece16 result;
if (iter != value.end()) {
result.assign(iter + sep.size(), value.end() - (iter + sep.size()));
} else {
result = value;
}
if (result.empty()) {
diag->error(DiagMessage(source) << "empty symbol");
return {};
}
iter = util::findNonAlphaNumericAndNotInSet(result, u"_");
if (iter != result.end()) {
diag->error(DiagMessage(source)
<< "invalid character '" << StringPiece16(iter, 1)
<< "' in '" << result << "'");
return {};
}
if (*result.begin() >= u'0' && *result.begin() <= u'9') {
diag->error(DiagMessage(source) << "symbol can not start with a digit");
return {};
}
return result;
}
static bool writeSymbol(IDiagnostics* diag, ClassDefinitionWriter* outClassDef, const Source& source,
xml::Element* el) {
xml::Attribute* attr = el->findAttribute(xml::kSchemaAndroid, u"name");
if (!attr) {
diag->error(DiagMessage(source) << "<" << el->name << "> must define 'android:name'");
return false;
}
Maybe<StringPiece16> result = extractJavaIdentifier(diag, source.withLine(el->lineNumber),
attr->value);
if (!result) {
return false;
}
AnnotationProcessor processor;
processor.appendComment(el->comment);
outClassDef->addStringMember(result.value(), &processor, attr->value);
return true;
}
bool ManifestClassGenerator::generate(IDiagnostics* diag, const StringPiece16& package,
xml::XmlResource* res, std::ostream* out) {
xml::Element* el = xml::findRootElement(res->root.get());
if (!el) {
return false;
}
if (el->name != u"manifest" && !el->namespaceUri.empty()) {
diag->error(DiagMessage(res->file.source) << "no <manifest> root tag defined");
return false;
}
*out << "package " << package << ";\n\n"
<< "public final class Manifest {\n";
bool error = false;
std::vector<xml::Element*> children = el->getChildElements();
ClassDefinitionWriterOptions classOptions;
classOptions.useFinalQualifier = true;
classOptions.forceCreationIfEmpty = false;
// First write out permissions.
ClassDefinitionWriter classDef("permission", classOptions);
for (xml::Element* childEl : children) {
if (childEl->namespaceUri.empty() && childEl->name == u"permission") {
error |= !writeSymbol(diag, &classDef, res->file.source, childEl);
}
}
classDef.writeToStream(out, " ");
// Next write out permission groups.
classDef = ClassDefinitionWriter("permission_group", classOptions);
for (xml::Element* childEl : children) {
if (childEl->namespaceUri.empty() && childEl->name == u"permission-group") {
error |= !writeSymbol(diag, &classDef, res->file.source, childEl);
}
}
classDef.writeToStream(out, " ");
*out << "}\n";
return !error;
}
} // namespace aapt
|