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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
/*
* 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 "BindingXmlPullParser.h"
#include "Util.h"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
namespace aapt {
constexpr const char16_t* kBindingNamespaceUri = u"http://schemas.android.com/apk/binding";
constexpr const char16_t* kAndroidNamespaceUri = u"http://schemas.android.com/apk/res/android";
constexpr const char16_t* kVariableTagName = u"variable";
constexpr const char* kBindingTagPrefix = "android:binding_";
BindingXmlPullParser::BindingXmlPullParser(const std::shared_ptr<XmlPullParser>& parser) :
mParser(parser), mOverride(false), mNextTagId(0) {
}
bool BindingXmlPullParser::readVariableDeclaration() {
VarDecl var;
const auto endAttrIter = mParser->endAttributes();
for (auto attrIter = mParser->beginAttributes(); attrIter != endAttrIter; ++attrIter) {
if (!attrIter->namespaceUri.empty()) {
continue;
}
if (attrIter->name == u"name") {
var.name = util::utf16ToUtf8(attrIter->value);
} else if (attrIter->name == u"type") {
var.type = util::utf16ToUtf8(attrIter->value);
}
}
XmlPullParser::skipCurrentElement(mParser.get());
if (var.name.empty()) {
mLastError = "variable declaration missing name";
return false;
}
if (var.type.empty()) {
mLastError = "variable declaration missing type";
return false;
}
mVarDecls.push_back(std::move(var));
return true;
}
bool BindingXmlPullParser::readExpressions() {
mOverride = true;
std::vector<XmlPullParser::Attribute> expressions;
std::string idValue;
const auto endAttrIter = mParser->endAttributes();
for (auto attr = mParser->beginAttributes(); attr != endAttrIter; ++attr) {
if (attr->namespaceUri == kAndroidNamespaceUri && attr->name == u"id") {
idValue = util::utf16ToUtf8(attr->value);
} else {
StringPiece16 value = util::trimWhitespace(attr->value);
if (util::stringStartsWith<char16_t>(value, u"@{") &&
util::stringEndsWith<char16_t>(value, u"}")) {
// This is attribute's value is an expression of the form
// @{expression}. We need to capture the expression inside.
expressions.push_back(XmlPullParser::Attribute{
attr->namespaceUri,
attr->name,
value.substr(2, value.size() - 3).toString()
});
} else {
// This is a normal attribute, use as is.
mAttributes.emplace_back(*attr);
}
}
}
// Check if we have any expressions.
if (!expressions.empty()) {
// We have expressions, so let's assign the target a tag number
// and add it to our targets list.
int32_t targetId = mNextTagId++;
mTargets.push_back(Target{
util::utf16ToUtf8(mParser->getElementName()),
idValue,
targetId,
std::move(expressions)
});
std::stringstream numGen;
numGen << kBindingTagPrefix << targetId;
mAttributes.push_back(XmlPullParser::Attribute{
std::u16string(kAndroidNamespaceUri),
std::u16string(u"tag"),
util::utf8ToUtf16(numGen.str())
});
}
return true;
}
XmlPullParser::Event BindingXmlPullParser::next() {
// Clear old state in preparation for the next event.
mOverride = false;
mAttributes.clear();
while (true) {
Event event = mParser->next();
if (event == Event::kStartElement) {
if (mParser->getElementNamespace().empty() &&
mParser->getElementName() == kVariableTagName) {
// This is a variable tag. Record data from it, and
// then discard the entire element.
if (!readVariableDeclaration()) {
// mLastError is set, so getEvent will return kBadDocument.
return getEvent();
}
continue;
} else {
// Check for expressions of the form @{} in attribute text.
const auto endAttrIter = mParser->endAttributes();
for (auto attr = mParser->beginAttributes(); attr != endAttrIter; ++attr) {
StringPiece16 value = util::trimWhitespace(attr->value);
if (util::stringStartsWith<char16_t>(value, u"@{") &&
util::stringEndsWith<char16_t>(value, u"}")) {
if (!readExpressions()) {
return getEvent();
}
break;
}
}
}
} else if (event == Event::kStartNamespace || event == Event::kEndNamespace) {
if (mParser->getNamespaceUri() == kBindingNamespaceUri) {
// Skip binding namespace tags.
continue;
}
}
return event;
}
return Event::kBadDocument;
}
bool BindingXmlPullParser::writeToFile(std::ostream& out) const {
out << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
out << "<Layout directory=\"\" layout=\"\" layoutId=\"\">\n";
// Write the variables.
out << " <Variables>\n";
for (const VarDecl& v : mVarDecls) {
out << " <entries name=\"" << v.name << "\" type=\"" << v.type << "\"/>\n";
}
out << " </Variables>\n";
// Write the imports.
std::stringstream tagGen;
// Write the targets.
out << " <Targets>\n";
for (const Target& t : mTargets) {
tagGen.str({});
tagGen << kBindingTagPrefix << t.tagId;
out << " <Target boundClass=\"" << t.className << "\" id=\"" << t.id
<< "\" tag=\"" << tagGen.str() << "\">\n";
out << " <Expressions>\n";
for (const XmlPullParser::Attribute& a : t.expressions) {
out << " <Expression attribute=\"" << a.namespaceUri << ":" << a.name
<< "\" text=\"" << a.value << "\"/>\n";
}
out << " </Expressions>\n";
out << " </Target>\n";
}
out << " </Targets>\n";
out << "</Layout>\n";
return bool(out);
}
XmlPullParser::const_iterator BindingXmlPullParser::beginAttributes() const {
if (mOverride) {
return mAttributes.begin();
}
return mParser->beginAttributes();
}
XmlPullParser::const_iterator BindingXmlPullParser::endAttributes() const {
if (mOverride) {
return mAttributes.end();
}
return mParser->endAttributes();
}
size_t BindingXmlPullParser::getAttributeCount() const {
if (mOverride) {
return mAttributes.size();
}
return mParser->getAttributeCount();
}
XmlPullParser::Event BindingXmlPullParser::getEvent() const {
if (!mLastError.empty()) {
return Event::kBadDocument;
}
return mParser->getEvent();
}
const std::string& BindingXmlPullParser::getLastError() const {
if (!mLastError.empty()) {
return mLastError;
}
return mParser->getLastError();
}
const std::u16string& BindingXmlPullParser::getComment() const {
return mParser->getComment();
}
size_t BindingXmlPullParser::getLineNumber() const {
return mParser->getLineNumber();
}
size_t BindingXmlPullParser::getDepth() const {
return mParser->getDepth();
}
const std::u16string& BindingXmlPullParser::getText() const {
return mParser->getText();
}
const std::u16string& BindingXmlPullParser::getNamespacePrefix() const {
return mParser->getNamespacePrefix();
}
const std::u16string& BindingXmlPullParser::getNamespaceUri() const {
return mParser->getNamespaceUri();
}
bool BindingXmlPullParser::applyPackageAlias(std::u16string* package,
const std::u16string& defaultPackage) const {
return mParser->applyPackageAlias(package, defaultPackage);
}
const std::u16string& BindingXmlPullParser::getElementNamespace() const {
return mParser->getElementNamespace();
}
const std::u16string& BindingXmlPullParser::getElementName() const {
return mParser->getElementName();
}
} // namespace aapt
|