2015-08-14 14:26:04 -07:00
|
|
|
/*
|
|
|
|
* 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 "Diagnostics.h"
|
|
|
|
#include "ResourceUtils.h"
|
|
|
|
#include "SdkConstants.h"
|
|
|
|
#include "link/Linkers.h"
|
2015-11-16 17:35:44 -08:00
|
|
|
#include "link/ReferenceLinker.h"
|
2015-08-14 14:26:04 -07:00
|
|
|
#include "process/IResourceTableConsumer.h"
|
|
|
|
#include "process/SymbolTable.h"
|
|
|
|
#include "util/Util.h"
|
2015-11-16 17:35:44 -08:00
|
|
|
#include "xml/XmlDom.h"
|
2015-08-14 14:26:04 -07:00
|
|
|
|
|
|
|
namespace aapt {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-11-16 17:35:44 -08:00
|
|
|
/**
|
|
|
|
* Visits all references (including parents of styles, references in styles, arrays, etc) and
|
|
|
|
* links their symbolic name to their Resource ID, performing mangling and package aliasing
|
|
|
|
* as needed.
|
|
|
|
*/
|
|
|
|
class ReferenceVisitor : public ValueVisitor {
|
|
|
|
public:
|
|
|
|
using ValueVisitor::visit;
|
|
|
|
|
2016-02-18 18:33:06 -08:00
|
|
|
ReferenceVisitor(IAaptContext* context, SymbolTable* symbols, xml::IPackageDeclStack* decls,
|
2015-11-16 17:35:44 -08:00
|
|
|
CallSite* callSite) :
|
|
|
|
mContext(context), mSymbols(symbols), mDecls(decls), mCallSite(callSite),
|
|
|
|
mError(false) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void visit(Reference* ref) override {
|
|
|
|
if (!ReferenceLinker::linkReference(ref, mContext, mSymbols, mDecls, mCallSite)) {
|
|
|
|
mError = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasError() const {
|
|
|
|
return mError;
|
|
|
|
}
|
2016-02-18 18:33:06 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
IAaptContext* mContext;
|
|
|
|
SymbolTable* mSymbols;
|
|
|
|
xml::IPackageDeclStack* mDecls;
|
|
|
|
CallSite* mCallSite;
|
|
|
|
bool mError;
|
2015-11-16 17:35:44 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Visits each xml Element and compiles the attributes within.
|
|
|
|
*/
|
|
|
|
class XmlVisitor : public xml::PackageAwareVisitor {
|
2015-08-14 14:26:04 -07:00
|
|
|
public:
|
|
|
|
using xml::PackageAwareVisitor::visit;
|
|
|
|
|
2016-02-18 18:33:06 -08:00
|
|
|
XmlVisitor(IAaptContext* context, SymbolTable* symbols, const Source& source,
|
2015-11-16 17:35:44 -08:00
|
|
|
std::set<int>* sdkLevelsFound, CallSite* callSite) :
|
2015-11-16 11:59:14 -08:00
|
|
|
mContext(context), mSymbols(symbols), mSource(source), mSdkLevelsFound(sdkLevelsFound),
|
2015-11-16 17:35:44 -08:00
|
|
|
mCallSite(callSite), mReferenceVisitor(context, symbols, this, callSite) {
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(xml::Element* el) override {
|
2015-11-16 11:59:14 -08:00
|
|
|
const Source source = mSource.withLine(el->lineNumber);
|
2015-08-14 14:26:04 -07:00
|
|
|
for (xml::Attribute& attr : el->attributes) {
|
2015-11-16 17:35:44 -08:00
|
|
|
Maybe<xml::ExtractedPackage> maybePackage =
|
|
|
|
xml::extractPackageFromNamespace(attr.namespaceUri);
|
2015-08-14 14:26:04 -07:00
|
|
|
if (maybePackage) {
|
|
|
|
// There is a valid package name for this attribute. We will look this up.
|
2015-11-16 17:35:44 -08:00
|
|
|
StringPiece16 package = maybePackage.value().package;
|
2015-08-14 14:26:04 -07:00
|
|
|
if (package.empty()) {
|
|
|
|
// Empty package means the 'current' or 'local' package.
|
|
|
|
package = mContext->getCompilationPackage();
|
|
|
|
}
|
|
|
|
|
2015-11-16 17:35:44 -08:00
|
|
|
Reference attrRef(ResourceNameRef(package, ResourceType::kAttr, attr.name));
|
|
|
|
attrRef.privateReference = maybePackage.value().privateNamespace;
|
|
|
|
|
|
|
|
std::string errStr;
|
|
|
|
attr.compiledAttribute = ReferenceLinker::compileXmlAttribute(
|
|
|
|
attrRef, mContext->getNameMangler(), mSymbols, mCallSite, &errStr);
|
2015-08-14 14:26:04 -07:00
|
|
|
|
|
|
|
// Convert the string value into a compiled Value if this is a valid attribute.
|
|
|
|
if (attr.compiledAttribute) {
|
2016-02-18 18:33:06 -08:00
|
|
|
if (attr.compiledAttribute.value().id) {
|
|
|
|
// Record all SDK levels from which the attributes were defined.
|
|
|
|
const size_t sdkLevel = findAttributeSdkLevel(
|
|
|
|
attr.compiledAttribute.value().id.value());
|
|
|
|
if (sdkLevel > 1) {
|
|
|
|
mSdkLevelsFound->insert(sdkLevel);
|
|
|
|
}
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const Attribute* attribute = &attr.compiledAttribute.value().attribute;
|
|
|
|
attr.compiledValue = ResourceUtils::parseItemForAttribute(attr.value,
|
|
|
|
attribute);
|
|
|
|
if (!attr.compiledValue &&
|
|
|
|
!(attribute->typeMask & android::ResTable_map::TYPE_STRING)) {
|
|
|
|
// We won't be able to encode this as a string.
|
|
|
|
mContext->getDiagnostics()->error(
|
2015-11-16 11:59:14 -08:00
|
|
|
DiagMessage(source) << "'" << attr.value << "' "
|
|
|
|
<< "is incompatible with attribute "
|
|
|
|
<< package << ":" << attr.name << " "
|
|
|
|
<< *attribute);
|
2015-08-14 14:26:04 -07:00
|
|
|
mError = true;
|
|
|
|
}
|
2016-02-18 18:33:06 -08:00
|
|
|
|
2015-08-14 14:26:04 -07:00
|
|
|
} else {
|
2015-11-16 11:59:14 -08:00
|
|
|
mContext->getDiagnostics()->error(DiagMessage(source)
|
|
|
|
<< "attribute '" << package << ":"
|
2015-11-16 17:35:44 -08:00
|
|
|
<< attr.name << "' " << errStr);
|
2015-08-14 14:26:04 -07:00
|
|
|
mError = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We still encode references.
|
|
|
|
attr.compiledValue = ResourceUtils::tryParseReference(attr.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attr.compiledValue) {
|
|
|
|
// With a compiledValue, we must resolve the reference and assign it an ID.
|
2015-11-16 11:59:14 -08:00
|
|
|
attr.compiledValue->setSource(source);
|
2015-11-16 17:35:44 -08:00
|
|
|
attr.compiledValue->accept(&mReferenceVisitor);
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the super implementation.
|
|
|
|
xml::PackageAwareVisitor::visit(el);
|
|
|
|
}
|
|
|
|
|
2015-11-16 17:35:44 -08:00
|
|
|
bool hasError() {
|
|
|
|
return mError || mReferenceVisitor.hasError();
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
2016-02-18 18:33:06 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
IAaptContext* mContext;
|
|
|
|
SymbolTable* mSymbols;
|
|
|
|
Source mSource;
|
|
|
|
std::set<int>* mSdkLevelsFound;
|
|
|
|
CallSite* mCallSite;
|
|
|
|
ReferenceVisitor mReferenceVisitor;
|
|
|
|
bool mError = false;
|
2015-08-14 14:26:04 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2015-11-16 17:35:44 -08:00
|
|
|
bool XmlReferenceLinker::consume(IAaptContext* context, xml::XmlResource* resource) {
|
2015-08-14 14:26:04 -07:00
|
|
|
mSdkLevelsFound.clear();
|
2015-11-16 17:35:44 -08:00
|
|
|
CallSite callSite = { resource->file.name };
|
|
|
|
XmlVisitor visitor(context, context->getExternalSymbols(), resource->file.source,
|
|
|
|
&mSdkLevelsFound, &callSite);
|
2015-08-14 14:26:04 -07:00
|
|
|
if (resource->root) {
|
|
|
|
resource->root->accept(&visitor);
|
|
|
|
return !visitor.hasError();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace aapt
|