diff options
author | Tor Norbye <tnorbye@google.com> | 2012-11-02 11:36:43 -0700 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2012-11-02 14:19:30 -0700 |
commit | 4059b6808d2567dc90767da13e450d814f82c412 (patch) | |
tree | f3cf02702b1513f1f7bf47c35562bb43b0e180cc /lint/libs | |
parent | 47366b5973be3cf146815c4560a54ca7ef681a9f (diff) | |
download | sdk-4059b6808d2567dc90767da13e450d814f82c412.zip sdk-4059b6808d2567dc90767da13e450d814f82c412.tar.gz sdk-4059b6808d2567dc90767da13e450d814f82c412.tar.bz2 |
39228: Validation for custom namespace in XMLs
Change-Id: Ie71f8a4706b1b91a84903257aca1adda461c277f
Diffstat (limited to 'lint/libs')
3 files changed, 46 insertions, 0 deletions
diff --git a/lint/libs/lint_checks/src/com/android/tools/lint/checks/NamespaceDetector.java b/lint/libs/lint_checks/src/com/android/tools/lint/checks/NamespaceDetector.java index 2d72271..7d4a2f7 100644 --- a/lint/libs/lint_checks/src/com/android/tools/lint/checks/NamespaceDetector.java +++ b/lint/libs/lint_checks/src/com/android/tools/lint/checks/NamespaceDetector.java @@ -127,10 +127,30 @@ public class NamespaceDetector extends LayoutDetector { } mUnusedNamespaces.put(item.getNodeName().substring(XMLNS_PREFIX.length()), attribute); + } else if (!value.startsWith("http://")) { //$NON-NLS-1$ + context.report(TYPO, attribute, context.getLocation(attribute), + "Suspicious namespace: should start with http://", null); + + continue; } String name = attribute.getName(); if (!name.equals(XMLNS_ANDROID) && !name.equals(XMLNS_A)) { + // See if it looks like a typo + int resIndex = value.indexOf("/res/"); //$NON-NLS-1$ + if (resIndex != -1 && value.length() + 5 > URI_PREFIX.length()) { + String urlPrefix = value.substring(0, resIndex + 5); + if (!urlPrefix.equals(URI_PREFIX) && + LintUtils.editDistance(URI_PREFIX, urlPrefix) <= 3) { + String correctUri = URI_PREFIX + value.substring(resIndex + 5); + context.report(TYPO, attribute, context.getLocation(attribute), + String.format( + "Possible typo in URL: was \"%1$s\", should " + + "probably be \"%2$s\"", + value, correctUri), + null); + } + } continue; } diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/NamespaceDetectorTest.java b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/NamespaceDetectorTest.java index 0912733..0017634 100644 --- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/NamespaceDetectorTest.java +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/NamespaceDetectorTest.java @@ -100,6 +100,22 @@ public class NamespaceDetectorTest extends AbstractCheckTest { lintProject("res/layout/wrong_namespace3.xml")); } + public void testTypo4() throws Exception { + assertEquals( + "res/layout/wrong_namespace5.xml:2: Warning: Suspicious namespace: should start with http:// [NamespaceTypo]\n" + + " xmlns:noturi=\"tp://schems.android.com/apk/res/com.my.package\"\n" + + " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" + + "res/layout/wrong_namespace5.xml:3: Warning: Possible typo in URL: was \"http://schems.android.com/apk/res/com.my.package\", should probably be \"http://schemas.android.com/apk/res/com.my.package\" [NamespaceTypo]\n" + + " xmlns:typo1=\"http://schems.android.com/apk/res/com.my.package\"\n" + + " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" + + "res/layout/wrong_namespace5.xml:4: Warning: Possible typo in URL: was \"http://schems.android.comm/apk/res/com.my.package\", should probably be \"http://schemas.android.com/apk/res/com.my.package\" [NamespaceTypo]\n" + + " xmlns:typo2=\"http://schems.android.comm/apk/res/com.my.package\"\n" + + " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" + + "0 errors, 3 warnings\n", + + lintProject("res/layout/wrong_namespace5.xml")); + } + public void testTypoOk() throws Exception { assertEquals( "No warnings.", diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/wrong_namespace5.xml b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/wrong_namespace5.xml new file mode 100644 index 0000000..ce0fc4b --- /dev/null +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/res/layout/wrong_namespace5.xml @@ -0,0 +1,10 @@ +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:noturi="tp://schems.android.com/apk/res/com.my.package" + xmlns:typo1="http://schems.android.com/apk/res/com.my.package" + xmlns:typo2="http://schems.android.comm/apk/res/com.my.package" + xmlns:ok="http://foo.bar/res/unrelated" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" > + +</RelativeLayout> |