use of com.android.tools.lint.checks.ResourceUsageModel.Resource in project bazel by bazelbuild.
the class ResourceUsageAnalyzer method createStubIds.
/**
* Write stub values for IDs to values.xml to match those available in public.xml.
*/
private void createStubIds(File values, Map<File, String> rewritten, File publicXml) throws IOException, ParserConfigurationException, SAXException {
if (values.exists()) {
String xml = rewritten.get(values);
if (xml == null) {
xml = Files.toString(values, UTF_8);
}
List<String> stubbed = Lists.newArrayList();
Document document = XmlUtils.parseDocument(xml, true);
Element root = document.getDocumentElement();
for (Resource resource : model.getResources()) {
boolean inPublicXml = resource.declarations != null && resource.declarations.contains(publicXml);
NodeList existing = null;
try {
XPathExpression expr = XPathFactory.newInstance().newXPath().compile(String.format("//item[@type=\"id\"][@name=\"%s\"]", resource.name));
existing = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
} catch (XPathException e) {
// Failed to retrieve any existing declarations for resource.
}
if (resource.type == ResourceType.ID && inPublicXml && (existing == null || existing.getLength() == 0)) {
Element item = document.createElement(TAG_ITEM);
item.setAttribute(ATTR_TYPE, resource.type.getName());
item.setAttribute(ATTR_NAME, resource.name);
root.appendChild(item);
stubbed.add(resource.getUrl());
}
}
logger.fine("Created " + stubbed.size() + " stub IDs for:\n " + Joiner.on(", ").join(stubbed));
String formatted = XmlPrettyPrinter.prettyPrint(document, xml.endsWith("\n"));
rewritten.put(values, formatted);
}
}
Aggregations