diff options
author | Tor Norbye <tnorbye@google.com> | 2012-11-10 19:04:12 -0800 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2012-11-16 19:43:07 +0100 |
commit | 6b9d0d8ef30b2bd47d81f21f5a36a31485b3fae2 (patch) | |
tree | 4eacdd3932b227990cdb8b254bbc3d811377bf8c /lint/libs | |
parent | 40457035c42951d6c23186ea3277d8e4ce4d2ef4 (diff) | |
download | sdk-6b9d0d8ef30b2bd47d81f21f5a36a31485b3fae2.zip sdk-6b9d0d8ef30b2bd47d81f21f5a36a31485b3fae2.tar.gz sdk-6b9d0d8ef30b2bd47d81f21f5a36a31485b3fae2.tar.bz2 |
Warn about custom namespaces on builtin tags
This CL adds a lint check which flags using unknown namespaces
on non custom views.
Change-Id: I5d53635e5e0ea7be774f90c07103f1defae97980
Diffstat (limited to 'lint/libs')
4 files changed, 44 insertions, 6 deletions
diff --git a/lint/libs/lint_checks/src/com/android/tools/lint/checks/DetectMissingPrefix.java b/lint/libs/lint_checks/src/com/android/tools/lint/checks/DetectMissingPrefix.java index 6ce22f7..71ea704 100644 --- a/lint/libs/lint_checks/src/com/android/tools/lint/checks/DetectMissingPrefix.java +++ b/lint/libs/lint_checks/src/com/android/tools/lint/checks/DetectMissingPrefix.java @@ -17,11 +17,13 @@ package com.android.tools.lint.checks; import static com.android.SdkConstants.ANDROID_PKG_PREFIX; +import static com.android.SdkConstants.ANDROID_URI; import static com.android.SdkConstants.ATTR_CLASS; import static com.android.SdkConstants.ATTR_CORE_APP; import static com.android.SdkConstants.ATTR_LAYOUT; import static com.android.SdkConstants.ATTR_PACKAGE; import static com.android.SdkConstants.ATTR_STYLE; +import static com.android.SdkConstants.TOOLS_URI; import static com.android.SdkConstants.VIEW_TAG; import static com.android.SdkConstants.XMLNS_PREFIX; import static com.android.resources.ResourceFolderType.ANIM; @@ -44,6 +46,7 @@ import com.android.tools.lint.detector.api.XmlContext; import org.w3c.dom.Attr; import org.w3c.dom.Element; +import org.w3c.dom.Node; import java.util.Collection; import java.util.EnumSet; @@ -134,6 +137,21 @@ public class DetectMissingPrefix extends LayoutDetector { context.getLocation(attribute), "Attribute is missing the Android namespace prefix", null); + } else if (!ANDROID_URI.equals(uri) + && !TOOLS_URI.equals(uri) + && context.getResourceFolderType() == ResourceFolderType.LAYOUT + && !isCustomView(attribute.getOwnerElement()) + // TODO: Consider not enforcing that the parent is a custom view + // too, though in that case we should filter out views that are + // layout params for the custom view parent: + // ....&& !attribute.getLocalName().startsWith(ATTR_LAYOUT_RESOURCE_PREFIX) + && attribute.getOwnerElement().getParentNode().getNodeType() == Node.ELEMENT_NODE + && !isCustomView((Element) attribute.getOwnerElement().getParentNode())) { + context.report(MISSING_NAMESPACE, attribute, + context.getLocation(attribute), + String.format("Unexpected namespace prefix \"%1$s\" found for tag %2$s", + attribute.getPrefix(), attribute.getOwnerElement().getTagName()), + null); } } diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/DetectMissingPrefixTest.java b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/DetectMissingPrefixTest.java index 944fe4f..3c03ea2 100644 --- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/DetectMissingPrefixTest.java +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/DetectMissingPrefixTest.java @@ -28,13 +28,15 @@ public class DetectMissingPrefixTest extends AbstractCheckTest { public void test() throws Exception { assertEquals( "res/layout/namespace.xml:2: Error: Attribute is missing the Android namespace prefix [MissingPrefix]\n" + - "<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\" android:id=\"@+id/newlinear\" android:orientation=\"vertical\" android:layout_width=\"match_parent\" android:layout_height=\"match_parent\" orientation=\"true\">\n" + - " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" + + "<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\" xmlns:other=\"http://foo.bar\" android:id=\"@+id/newlinear\" android:orientation=\"vertical\" android:layout_width=\"match_parent\" android:layout_height=\"match_parent\" orientation=\"true\">\n" + + " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" + "res/layout/namespace.xml:3: Error: Attribute is missing the Android namespace prefix [MissingPrefix]\n" + " <Button style=\"@style/setupWizardOuterFrame\" android.text=\"Button\" android:id=\"@+id/button1\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\"></Button>\n" + " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" + - "2 errors, 0 warnings\n" + - "", + "res/layout/namespace.xml:5: Error: Unexpected namespace prefix \"other\" found for tag LinearLayout [MissingPrefix]\n" + + " <LinearLayout other:orientation=\"horizontal\"/>\n" + + " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" + + "3 errors, 0 warnings\n", lintFiles("res/layout/namespace.xml")); } diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/namespace.xml b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/namespace.xml index e8f60bb..a984333 100644 --- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/namespace.xml +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/namespace.xml @@ -1,6 +1,7 @@ <?xml version="1.0" encoding="utf-8"?> -<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/newlinear" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" orientation="true"> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:other="http://foo.bar" android:id="@+id/newlinear" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" orientation="true"> <Button style="@style/setupWizardOuterFrame" android.text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <ImageView android:style="@style/bogus" android:id="@+id/android_logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/android_button" android:focusable="false" android:clickable="false" android:layout_weight="1.0" /> + <LinearLayout other:orientation="horizontal"/> </LinearLayout> diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/namespace2.xml b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/namespace2.xml index a45af90..59001c2 100644 --- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/namespace2.xml +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/namespace2.xml @@ -6,7 +6,24 @@ customprefix:layout_width="match_parent" customprefix:layout_height="match_parent" customprefix:orientation="vertical" - bogus:orientation="bogus" orientation="true"> + <view class="foo.bar.LinearLayout"> + bogus:orientation="bogus" + </view> + + <foo.bar.LinearLayout + customprefix:id="@+id/newlinear2" + customprefix:layout_width="match_parent" + customprefix:layout_height="match_parent" + customprefix:orientation="vertical" + bogus:orientation="bogus" + orientation="true"> + + <view class="foo.bar.LinearLayout"> + bogus:orientation="bogus" + </view> + + </foo.bar.LinearLayout> + </LinearLayout> |