本文整理汇总了Java中com.android.tools.lint.detector.api.XmlContext.report方法的典型用法代码示例。如果您正苦于以下问题:Java XmlContext.report方法的具体用法?Java XmlContext.report怎么用?Java XmlContext.report使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.android.tools.lint.detector.api.XmlContext
的用法示例。
在下文中一共展示了XmlContext.report方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: visitAttribute
import com.android.tools.lint.detector.api.XmlContext; //导入方法依赖的package包/类
@Override
public void visitAttribute(@NonNull XmlContext context, @NonNull Attr attribute) {
// 判断资源文件夹是否存在
Project project = context.getProject();
List<File> resourceFolder = project.getResourceFolders();
if (resourceFolder.isEmpty()) {
return;
}
// 获取项目资源文件夹路径
String resourcePath = resourceFolder.get(0).getAbsolutePath();
// 获取 drawable 名字
String drawableName = attribute.getValue().replace("@drawable/", "");
try {
// 若 drawable 为 Vector Drawable,则文件后缀为 xml,根据 resource 路径,drawable 名字,文件后缀拼接出完整路径
FileInputStream fileInputStream = new FileInputStream(resourcePath + "/drawable/" + drawableName + ".xml");
BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream));
String line = reader.readLine();
if (line.contains("vector")) {
// 若文件存在,并且包含首行包含 vector,则为 Vector Drawable,抛出警告
context.report(ISSUE_XML_VECTOR_DRAWABLE, attribute, context.getLocation(attribute), attribute.getValue() + " 为 Vector Drawable,请使用 Vector 属性进行设置,避免 4.0 及以下版本的系统产生 Crash");
}
fileInputStream.close();
} catch (Exception ignored) {
}
}
示例2: visitAttribute
import com.android.tools.lint.detector.api.XmlContext; //导入方法依赖的package包/类
@Override
public void visitAttribute(XmlContext context, Attr attribute) {
super.visitAttribute(context, attribute);
String value = attribute.getValue();
if (Utils.stringIsEmpty(value)) return;
//onClick 属性被赋值
Element element = attribute.getOwnerElement();
boolean has = element.hasAttributeNS(SdkConstants.ANDROID_URI, SdkConstants.ATTR_ID);
if (!has) {
Location location = context.getLocation(attribute);
context.report(ISSUE_MISSING_ID, location, "the view with onClick attribute set should has id attribute");
}
}
示例3: validatePath
import com.android.tools.lint.detector.api.XmlContext; //导入方法依赖的package包/类
@NonNull
private static String validatePath(@NonNull XmlContext context, @NonNull Element element) {
Attr pathNode = element.getAttributeNode(ATTR_PATH);
if (pathNode == null) {
return "";
}
String value = pathNode.getValue();
if (value.contains("//")) {
context.report(ISSUE, element, context.getValueLocation(pathNode),
"Paths are not allowed to contain `//`");
} else if (value.contains("..")) {
context.report(ISSUE, element, context.getValueLocation(pathNode),
"Paths are not allowed to contain `..`");
} else if (value.contains("/")) {
String domain = element.getAttribute(ATTR_DOMAIN);
if (DOMAIN_SHARED_PREF.equals(domain) || DOMAIN_DATABASE.equals(domain)) {
context.report(ISSUE, element, context.getValueLocation(pathNode),
String.format("Subdirectories are not allowed for domain `%1$s`",
domain));
}
}
return value;
}
示例4: visitAttribute
import com.android.tools.lint.detector.api.XmlContext; //导入方法依赖的package包/类
@Override
public void visitAttribute(@NonNull XmlContext context, @NonNull Attr attribute) {
String value = attribute.getValue();
if (!value.isEmpty()) {
// Make sure this is really one of the android: attributes
if (!ANDROID_URI.equals(attribute.getNamespaceURI())) {
return;
}
value = value.substring(5); // ignore "@+id/"
if (!idCorrectFormat(value)) {
context.report(ISSUE, attribute, context.getLocation(attribute),
String.format("The id \"%1$s\", should be written using lowerCamelCase.", value));
}
}
}
示例5: visitElement
import com.android.tools.lint.detector.api.XmlContext; //导入方法依赖的package包/类
@Override
public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
Node parentNode = element.getParentNode();
if (parentNode != null && parentNode.getNodeType() == Node.ELEMENT_NODE) {
Element parent = (Element)parentNode;
Attr width = parent.getAttributeNodeNS(ANDROID_URI, ATTR_LAYOUT_WIDTH);
Attr height = parent.getAttributeNodeNS(ANDROID_URI, ATTR_LAYOUT_HEIGHT);
Attr attr = null;
if (width != null && VALUE_WRAP_CONTENT.equals(width.getValue())) {
attr = width;
}
if (height != null && VALUE_WRAP_CONTENT.equals(height.getValue())) {
attr = height;
}
if (attr != null) {
String message = String.format("Placing a `<WebView>` in a parent element that "
+ "uses a `wrap_content %1$s` can lead to subtle bugs; use `match_parent` "
+ "instead", attr.getLocalName());
Location location = context.getLocation(element);
Location secondary = context.getLocation(attr);
secondary.setMessage("`wrap_content` here may not work well with WebView below");
location.setSecondary(secondary);
context.report(ISSUE, element, location, message);
}
}
}
示例6: visitAttribute
import com.android.tools.lint.detector.api.XmlContext; //导入方法依赖的package包/类
@Override
public void visitAttribute(@NonNull XmlContext context, @NonNull Attr attribute) {
Project mainProject = context.getMainProject();
if (mainProject.isGradleProject()) {
Boolean appCompat = mainProject.dependsOn("com.android.support:appcompat-v7");
if (ANDROID_URI.equals(attribute.getNamespaceURI())) {
if (context.getFolderVersion() >= 14) {
return;
}
if (appCompat == Boolean.TRUE) {
context.report(ISSUE, attribute,
context.getLocation(attribute),
"Should use `app:showAsAction` with the appcompat library with "
+ "`xmlns:app=\"http://schemas.android.com/apk/res-auto\"`");
}
} else {
if (appCompat == Boolean.FALSE) {
context.report(ISSUE, attribute,
context.getLocation(attribute),
"Should use `android:showAsAction` when not using the appcompat library");
}
}
}
}
示例7: beforeCheckFile
import com.android.tools.lint.detector.api.XmlContext; //导入方法依赖的package包/类
@Override
public void beforeCheckFile(@NonNull Context context) {
if (mPrefix != null && context instanceof XmlContext) {
XmlContext xmlContext = (XmlContext) context;
ResourceFolderType folderType = xmlContext.getResourceFolderType();
if (folderType != null && folderType != ResourceFolderType.VALUES) {
String name = LintUtils.getBaseName(context.file.getName());
if (!name.startsWith(mPrefix)) {
// Attempt to report the error on the root tag of the associated
// document to make suppressing the error with a tools:suppress
// attribute etc possible
if (xmlContext.document != null) {
Element root = xmlContext.document.getDocumentElement();
if (root != null) {
xmlContext.report(ISSUE, root, xmlContext.getLocation(root),
getErrorMessage(name));
return;
}
}
context.report(ISSUE, Location.create(context.file),
getErrorMessage(name));
}
}
}
}
示例8: visitElement
import com.android.tools.lint.detector.api.XmlContext; //导入方法依赖的package包/类
@Override
public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
if (!element.hasAttributeNS(ANDROID_URI, ATTR_CONTENT_DESCRIPTION)) {
// Ignore views that are explicitly not important for accessibility
if (VALUE_NO.equals(element.getAttributeNS(ANDROID_URI,
ATTR_IMPORTANT_FOR_ACCESSIBILITY))) {
return;
}
context.report(ISSUE, element, context.getLocation(element),
"[Accessibility] Missing `contentDescription` attribute on image");
} else {
Attr attributeNode = element.getAttributeNodeNS(ANDROID_URI, ATTR_CONTENT_DESCRIPTION);
String attribute = attributeNode.getValue();
if (attribute.isEmpty() || attribute.equals("TODO")) { //$NON-NLS-1$
context.report(ISSUE, attributeNode, context.getLocation(attributeNode),
"[Accessibility] Empty `contentDescription` attribute on image");
}
}
}
示例9: ensureAppNamespace
import com.android.tools.lint.detector.api.XmlContext; //导入方法依赖的package包/类
private static void ensureAppNamespace(XmlContext context, Element element, String name) {
Attr attribute = element.getAttributeNodeNS(ANDROID_URI, name);
if (attribute != null) {
String prefix = getNamespacePrefix(element.getOwnerDocument(), AUTO_URI);
boolean haveNamespace = prefix != null;
if (!haveNamespace) {
prefix = "app";
}
StringBuilder sb = new StringBuilder();
sb.append("Wrong namespace; with v7 `GridLayout` you should use ").append(prefix)
.append(":").append(name);
if (!haveNamespace) {
sb.append(" (and add `xmlns:app=\"").append(AUTO_URI)
.append("\"` to your root element.)");
}
String message = sb.toString();
context.report(ISSUE, attribute, context.getLocation(attribute), message);
}
}
示例10: visitElement
import com.android.tools.lint.detector.api.XmlContext; //导入方法依赖的package包/类
@Override
public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
int childCount = LintUtils.getChildCount(element);
String tagName = element.getTagName();
if (tagName.equals(SCROLL_VIEW) || tagName.equals(HORIZONTAL_SCROLL_VIEW)) {
if (childCount > 1 && getAccurateChildCount(element) > 1) {
context.report(SCROLLVIEW_ISSUE, element,
context.getLocation(element), "A scroll view can have only one child");
}
} else {
// Adapter view
if (childCount > 0 && getAccurateChildCount(element) > 0) {
context.report(ADAPTER_VIEW_ISSUE, element,
context.getLocation(element),
"A list/grid should have no children declared in XML");
}
}
}
示例11: handleVisitIncludeTag
import com.android.tools.lint.detector.api.XmlContext; //导入方法依赖的package包/类
private void handleVisitIncludeTag(XmlContext context, Element element) {
boolean hasAttr = element.hasAttributeNS(SdkConstants.ANDROID_URI,
SdkConstants.ATTR_ID);
if (!hasAttr) {
context.report(ISSUE_INCLUDE, element, context.getLocation(element),
"Missing required attribute 'id'");
}
}
示例12: visitDocument
import com.android.tools.lint.detector.api.XmlContext; //导入方法依赖的package包/类
@Override
public void visitDocument(@NonNull XmlContext context, @NonNull Document document) {
String xml = context.getContents();
if (xml == null) {
return;
}
// AAPT: The prologue must be in the first line
int lineEnd = 0;
int max = xml.length();
for (; lineEnd < max; lineEnd++) {
char c = xml.charAt(lineEnd);
if (c == '\n' || c == '\r') {
break;
}
}
for (int i = 16; i < lineEnd - 5; i++) { // +4: Skip at least <?xml encoding="
if ((xml.charAt(i) == 'u' || xml.charAt(i) == 'U')
&& (xml.charAt(i + 1) == 't' || xml.charAt(i + 1) == 'T')
&& (xml.charAt(i + 2) == 'f' || xml.charAt(i + 2) == 'F')
&& (xml.charAt(i + 3) == '-' || xml.charAt(i + 3) == '_')
&& (xml.charAt(i + 4) == '8')) {
return;
}
}
int encodingIndex = xml.lastIndexOf("encoding", lineEnd); //$NON-NLS-1$
if (encodingIndex != -1) {
Matcher matcher = ENCODING_PATTERN.matcher(xml);
if (matcher.find(encodingIndex)) {
String encoding = matcher.group(1);
Location location = Location.create(context.file, xml,
matcher.start(1), matcher.end(1));
context.report(ISSUE, null, location, String.format(
"%1$s: Not using UTF-8 as the file encoding. This can lead to subtle " +
"bugs with non-ascii characters", encoding));
}
}
}
示例13: visitAttribute
import com.android.tools.lint.detector.api.XmlContext; //导入方法依赖的package包/类
@Override
public void visitAttribute(@NonNull XmlContext context, @NonNull Attr attribute) {
Element element = attribute.getOwnerElement();
if (element.hasAttributeNS(ANDROID_URI, ATTR_HINT)) {
context.report(ISSUE, element, context.getLocation(attribute),
"Do not set both `contentDescription` and `hint`: the `contentDescription` " +
"will mask the `hint`");
}
}
示例14: visitAttribute
import com.android.tools.lint.detector.api.XmlContext; //导入方法依赖的package包/类
@Override
public void visitAttribute(@NonNull XmlContext context, @NonNull Attr attribute) {
String value = attribute.getValue();
if (!value.isEmpty() && (value.charAt(0) != '@' && value.charAt(0) != '?')) {
// Make sure this is really one of the android: attributes
if (!ANDROID_URI.equals(attribute.getNamespaceURI())) {
return;
}
context.report(ISSUE, attribute, context.getLocation(attribute),
String.format("[I18N] Hardcoded string \"%1$s\", should use `@string` resource", value));
}
}
示例15: visitElement
import com.android.tools.lint.detector.api.XmlContext; //导入方法依赖的package包/类
@Override
public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
Attr nameNode = element.getAttributeNodeNS(ANDROID_URI, ATTR_NAME);
if (nameNode != null) {
String permissionName = nameNode.getValue();
if (Arrays.binarySearch(SYSTEM_PERMISSIONS, permissionName) >= 0) {
context.report(ISSUE, element, context.getLocation(nameNode),
"Permission is only granted to system apps");
}
}
}