Skip to content

Instantly share code, notes, and snippets.

@dominicthomas
Created November 26, 2018 12:57
Show Gist options
  • Save dominicthomas/db709288d0c253053a7b8e063d57e0d0 to your computer and use it in GitHub Desktop.
Save dominicthomas/db709288d0c253053a7b8e063d57e0d0 to your computer and use it in GitHub Desktop.
Custom lint class for check for and enforce custom xml headers
public class MissingXmlHeaderDetector extends ResourceXmlDetector {
public static final Issue ISSUE_MISSING_XML_HEADER = Issue.create(
"MissingXmlHeader",
"Flags xml files that don't have a header.",
"An xml file should always have the xml header to declare that it is an xml file despite the file ending.",
Category.CORRECTNESS, 10, Severity.ERROR,
new Implementation(MissingXmlHeaderDetector.class, Scope.RESOURCE_FILE_SCOPE));
@Override
public void visitDocument(XmlContext context, Document document) {
final String content = context.getClient().readFile(context.file).toString();
if (!content.startsWith("<?xml")) {
final LintFix addXmlHeader = fix()
.replace()
.name("Add xml header")
.text(content)
.with("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n$content")
.autoFix()
.build();
context.report(ISSUE_MISSING_XML_HEADER,
document,
Location.create(context.file, content, 0, content.length()),
"Missing an xml header.",
addXmlHeader);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment