本文整理汇总了Java中com.android.tools.lint.detector.api.XmlContext类的典型用法代码示例。如果您正苦于以下问题:Java XmlContext类的具体用法?Java XmlContext怎么用?Java XmlContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
XmlContext类属于com.android.tools.lint.detector.api包,在下文中一共展示了XmlContext类的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: checkCustomNamespace
import com.android.tools.lint.detector.api.XmlContext; //导入依赖的package包/类
private static void checkCustomNamespace(XmlContext context, Element element) {
NamedNodeMap attributes = element.getAttributes();
for (int i = 0, n = attributes.getLength(); i < n; i++) {
Attr attribute = (Attr) attributes.item(i);
if (attribute.getName().startsWith(XMLNS_PREFIX)) {
String uri = attribute.getValue();
if (uri != null && !uri.isEmpty() && uri.startsWith(URI_PREFIX)
&& !uri.equals(ANDROID_URI)) {
if (context.getProject().isGradleProject()) {
context.report(RES_AUTO, attribute, context.getValueLocation(attribute),
"In Gradle projects, always use `" + AUTO_URI + "` for custom " +
"attributes");
} else {
context.report(CUSTOM_VIEW, attribute, context.getValueLocation(attribute),
"When using a custom namespace attribute in a library project, " +
"use the namespace `\"" + AUTO_URI + "\"` instead.");
}
}
}
}
}
示例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: replaceUrlWithValue
import com.android.tools.lint.detector.api.XmlContext; //导入依赖的package包/类
private static String replaceUrlWithValue(@NonNull XmlContext context,
@NonNull String str) {
Project project = context.getProject();
LintClient client = context.getClient();
if (!client.supportsProjectResources()) {
return str;
}
ResourceUrl style = ResourceUrl.parse(str);
if (style == null || style.type != ResourceType.STRING || style.framework) {
return str;
}
AbstractResourceRepository resources = client.getProjectResources(project, true);
if (resources == null) {
return str;
}
List<ResourceItem> items = resources.getResourceItem(ResourceType.STRING, style.name);
if (items == null || items.isEmpty()) {
return str;
}
ResourceValue resourceValue = items.get(0).getResourceValue(false);
if (resourceValue == null) {
return str;
}
return resourceValue.getValue() == null ? str : resourceValue.getValue();
}
示例6: isAlreadyWarnedDrawableFile
import com.android.tools.lint.detector.api.XmlContext; //导入依赖的package包/类
/**
* Returns true if this attribute is in a drawable document with one of the
* root tags that require API 21
*/
private static boolean isAlreadyWarnedDrawableFile(@NonNull XmlContext context,
@NonNull Attr attribute, int attributeApiLevel) {
// Don't complain if it's in a drawable file where we've already
// flagged the root drawable type as being unsupported
if (context.getResourceFolderType() == ResourceFolderType.DRAWABLE
&& attributeApiLevel == 21) {
String root = attribute.getOwnerDocument().getDocumentElement().getTagName();
if (TAG_RIPPLE.equals(root)
|| TAG_VECTOR.equals(root)
|| TAG_ANIMATED_VECTOR.equals(root)
|| TAG_ANIMATED_SELECTOR.equals(root)) {
return true;
}
}
return false;
}
示例7: checkElement
import com.android.tools.lint.detector.api.XmlContext; //导入依赖的package包/类
/** Checks whether the given element is the given tag, and if so, whether it satisfied
* the minimum version that the given tag is supported in */
private void checkElement(@NonNull XmlContext context, @NonNull Element element,
@NonNull String tag, int api, @NonNull Issue issue) {
if (tag.equals(element.getTagName())) {
int minSdk = getMinSdk(context);
if (api > minSdk && api > context.getFolderVersion()
&& api > getLocalMinSdk(element)) {
Location location = context.getLocation(element);
String message;
if (issue == UNSUPPORTED) {
message = String.format(
"`<%1$s>` requires API level %2$d (current min is %3$d)", tag, api,
minSdk);
} else {
assert issue == UNUSED : issue;
message = String.format(
"`<%1$s>` is only used in API level %2$d and higher "
+ "(current min is %3$d)", tag, api, minSdk);
}
context.report(issue, element, location, message);
}
}
}
示例8: visitElement
import com.android.tools.lint.detector.api.XmlContext; //导入依赖的package包/类
@Override
public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
// NOTE: This should NOT be checking *minSdkVersion*, but *targetSdkVersion*
// or even buildTarget instead. However, there's a risk that this will flag
// way too much and make the rule annoying until API 17 support becomes
// more widespread, so for now limit the check to those projects *really*
// working with 17. When API 17 reaches a certain amount of adoption, change
// this to flag all apps supporting 17, including those supporting earlier
// versions as well.
if (context.getMainProject().getMinSdk() < 17) {
return;
}
if (mTextFields == null) {
mTextFields = new ArrayList<Element>();
}
mTextFields.add(element);
}
示例9: checkRepeatedWords
import com.android.tools.lint.detector.api.XmlContext; //导入依赖的package包/类
private static void checkRepeatedWords(XmlContext context, Element element, Node node,
String text, int lastWordBegin, int lastWordEnd, int begin, int end) {
if (lastWordBegin != -1 && end - begin == lastWordEnd - lastWordBegin
&& end - begin > 1) {
// See whether we have a repeated word
boolean different = false;
for (int i = lastWordBegin, j = begin; i < lastWordEnd; i++, j++) {
if (text.charAt(i) != text.charAt(j)) {
different = true;
break;
}
}
if (!different && onlySpace(text, lastWordEnd, begin) && isTranslatable(element)) {
reportRepeatedWord(context, node, text, lastWordBegin, begin, end);
}
}
}
示例10: testLineEndings
import com.android.tools.lint.detector.api.XmlContext; //导入依赖的package包/类
public void testLineEndings() throws Exception {
// Test for http://code.google.com/p/android/issues/detail?id=22925
String xml =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
"<LinearLayout>\r\n" +
"\r" +
"<LinearLayout></LinearLayout>\r\n" +
"</LinearLayout>\r\n";
LintCliXmlParser parser = new LintCliXmlParser();
File file = File.createTempFile("parsertest2", ".xml");
//noinspection IOResourceOpenedButNotSafelyClosed
Writer fw = new BufferedWriter(new FileWriter(file));
fw.write(xml);
fw.close();
LintClient client = new TestClient();
LintDriver driver = new LintDriver(new BuiltinIssueRegistry(), client);
Project project = Project.create(client, file.getParentFile(), file.getParentFile());
XmlContext context = new XmlContext(driver, project, null, file, null, parser);
Document document = parser.parseXml(context);
assertNotNull(document);
//noinspection ResultOfMethodCallIgnored
file.delete();
}
示例11: 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");
}
}
}
}
示例12: visitElement
import com.android.tools.lint.detector.api.XmlContext; //导入依赖的package包/类
@Override
public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
String fqcn = getFqcn(context, element);
String tag = element.getTagName();
String frameworkClass = tagToClass(tag);
if (frameworkClass != null) {
String signature = ClassContext.getInternalName(fqcn);
if (mManifestRegistrations == null) {
mManifestRegistrations = ArrayListMultimap.create(4, 8);
}
mManifestRegistrations.put(frameworkClass, signature);
if (signature.indexOf('$') != -1) {
// The internal name contains a $ which means it's an inner class.
// The conversion from fqcn to internal name is a bit ambiguous:
// "a.b.C.D" usually means "inner class D in class C in package a.b".
// However, it can (see issue 31592) also mean class D in package "a.b.C".
// Place *both* of these possibilities in the registered map, since this
// is only used to check that an activity is registered, not the other way
// (so it's okay to have entries there that do not correspond to real classes).
signature = signature.replace('$', '/');
mManifestRegistrations.put(frameworkClass, signature);
}
}
}
示例13: 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));
}
}
}
}
示例14: visitElement
import com.android.tools.lint.detector.api.XmlContext; //导入依赖的package包/类
@Override
public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
if (mPrefix == null || context.getResourceFolderType() != ResourceFolderType.VALUES) {
return;
}
for (Element item : LintUtils.getChildren(element)) {
Attr nameAttribute = item.getAttributeNode(ATTR_NAME);
if (nameAttribute != null) {
String name = nameAttribute.getValue();
if (!name.startsWith(mPrefix)) {
String message = getErrorMessage(name);
context.report(ISSUE, nameAttribute, context.getLocation(nameAttribute),
message);
}
}
}
}
示例15: visitElement
import com.android.tools.lint.detector.api.XmlContext; //导入依赖的package包/类
@Override
public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
if (context.getResourceFolderType() != ResourceFolderType.VALUES) {
return;
}
assert element.getTagName().equals(TAG_STYLE);
NodeList itemNodes = element.getChildNodes();
for (int j = 0, nodeCount = itemNodes.getLength(); j < nodeCount; j++) {
Node item = itemNodes.item(j);
if (item.getNodeType() == Node.ELEMENT_NODE &&
TAG_ITEM.equals(item.getNodeName())) {
Element itemElement = (Element) item;
NodeList childNodes = item.getChildNodes();
for (int i = 0, n = childNodes.getLength(); i < n; i++) {
Node child = childNodes.item(i);
if (child.getNodeType() != Node.TEXT_NODE) {
return;
}
checkStyleItem(context, itemElement, child);
}
}
}
}