OSDN Git Service

Modify validator to reject file resource names starting with _
authorTor Norbye <tnorbye@google.com>
Sun, 9 Oct 2011 17:16:16 +0000 (10:16 -0700)
committerTor Norbye <tnorbye@google.com>
Sun, 9 Oct 2011 17:19:02 +0000 (10:19 -0700)
Change-Id: I23cea24d7363597968d6602b5a056a59085b9c6c

eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/resources/ResourceNameValidator.java
eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/resources/ResourceNameValidatorTest.java

index 1838d14..2b07545 100644 (file)
@@ -92,7 +92,7 @@ public class ResourceNameValidator implements IInputValidator {
             // Resource names must be valid Java identifiers, since they will
             // be represented as Java identifiers in the R file:
             if (!Character.isJavaIdentifierStart(newText.charAt(0))) {
-                return "The layout name must begin with a character";
+                return "The resource name must begin with a character";
             }
             for (int i = 1, n = newText.length(); i < n; i++) {
                 char c = newText.charAt(i);
@@ -102,6 +102,12 @@ public class ResourceNameValidator implements IInputValidator {
             }
 
             if (mIsFileType) {
+                char first = newText.charAt(0);
+                if (!(first >= 'a' && first <= 'z')) {
+                    return String.format(
+                            "File-based resource names must start with a lowercase letter.");
+                }
+
                 // AAPT only allows lowercase+digits+_:
                 // "%s: Invalid file name: must contain only [a-z0-9_.]","
                 for (int i = 0, n = newText.length(); i < n; i++) {
index b771667..a5d34ec 100644 (file)
@@ -23,6 +23,7 @@ import java.util.Collections;
 
 import junit.framework.TestCase;
 
+@SuppressWarnings("javadoc")
 public class ResourceNameValidatorTest extends TestCase {
     public void testValidator() throws Exception {
         // Valid
@@ -47,5 +48,13 @@ public class ResourceNameValidatorTest extends TestCase {
                 .isValid("Foo123_$") != null);
         assertTrue(ResourceNameValidator.create(true, ResourceFolderType.LAYOUT)
                 .isValid("foo123_") == null);
+
+        // Can't start with _ in file-based resource names, is okay for value based resources
+        assertTrue(ResourceNameValidator.create(true, ResourceFolderType.VALUES)
+                .isValid("_foo") == null);
+        assertTrue(ResourceNameValidator.create(true, ResourceFolderType.LAYOUT)
+                .isValid("_foo") != null);
+        assertTrue(ResourceNameValidator.create(true, ResourceFolderType.DRAWABLE)
+                .isValid("_foo") != null);
     }
 }