android: validation of non-reference attribute values

This commit is contained in:
Eugene Kudelevsky
2011-12-19 16:07:24 +04:00
parent 3b672e3c4d
commit 10f3d1b980
7 changed files with 43 additions and 5 deletions
@@ -185,11 +185,16 @@ public class AndroidDomUtil {
Set<AttributeFormat> formats = attr.getFormats();
CompositeConverter composite = new CompositeConverter();
String[] values = attr.getValues();
boolean containsUnsupportedFormats = false;
for (AttributeFormat format : formats) {
ResolvingConverter<String> converter = getStringConverter(format, values);
if (converter != null) {
composite.addConverter(converter);
}
else {
containsUnsupportedFormats = true;
}
}
ResourceReferenceConverter resConverter = getResourceReferenceConverter(attr);
if (formats.contains(AttributeFormat.Flag)) {
@@ -200,7 +205,7 @@ public class AndroidDomUtil {
}
ResolvingConverter<String> stringConverter = simplify(composite);
if (resConverter != null) {
resConverter.setAdditionalConverter(simplify(composite));
resConverter.setAdditionalConverter(simplify(composite), containsUnsupportedFormats);
return resConverter;
}
return stringConverter;
@@ -58,6 +58,7 @@ public class ResourceReferenceConverter extends ResolvingConverter<ResourceValue
private final List<String> myResourceTypes;
private ResolvingConverter<String> myAdditionalConverter;
private boolean myAdditionalConverterSoft = false;
private boolean myWithPrefix = true;
private boolean myWithExplicitResourceType = true;
@@ -75,8 +76,9 @@ public class ResourceReferenceConverter extends ResolvingConverter<ResourceValue
myWithExplicitResourceType = withExplicitResourceType;
}
public void setAdditionalConverter(ResolvingConverter<String> additionalConverter) {
public void setAdditionalConverter(ResolvingConverter<String> additionalConverter, boolean soft) {
myAdditionalConverter = additionalConverter;
myAdditionalConverterSoft = soft;
}
@NotNull
@@ -207,11 +209,14 @@ public class ResourceReferenceConverter extends ResolvingConverter<ResourceValue
public ResourceValue fromString(@Nullable @NonNls String s, ConvertContext context) {
if (s == null) return null;
ResourceValue parsed = ResourceValue.parse(s, true, myWithPrefix);
if (parsed == null && myAdditionalConverter != null) {
if ((parsed == null || !parsed.isReference()) && myAdditionalConverter != null) {
String value = myAdditionalConverter.fromString(s, context);
if (value != null) {
return ResourceValue.literal(value);
}
else if (!myAdditionalConverterSoft) {
return null;
}
}
if (parsed != null && parsed.getResourceType() == null && myResourceTypes.size() == 1) {
parsed.setResourceType(myResourceTypes.get(0));
@@ -2,7 +2,7 @@
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:color="#ffff0000" />
<item android:state_pressed="" android:color="@color/myColor" />
<item android:state_pressed="false" android:color="@color/myColor" />
<item android:state_focused="true" android:color="#ffff0000" />
<<error>item</error> android:state_middle="true"/>
<item android:color="<error>@color/myColo</error>" />
@@ -2,7 +2,7 @@
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="@drawable/myDrawable" />
<item android:state_pressed="" android:drawable="@drawable/myDrawable" />
<item android:state_pressed="false" android:drawable="@drawable/myDrawable" />
<item android:state_focused="true" android:drawable="@drawable/myDrawable" />
<item android:drawable="<error>@drawable/myDrawab</error>" />
</selector>
@@ -0,0 +1,20 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarFadeDuration="<error>aba</error>"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarFadeDuration="10"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarFadeDuration="@integer/myInt"
/>
</LinearLayout>
@@ -0,0 +1,3 @@
<resources>
<integer name="myInt">10</integer>
</resources>
@@ -60,6 +60,11 @@ public class AndroidLayoutDomTest extends AndroidDomTest {
doTestHighlighting("hl.xml");
}
public void testHighlighting2() throws Throwable {
copyFileToProject("integers.xml", "res/values/integers.xml");
doTestHighlighting("hl2.xml");
}
public void testCheckLayoutAttrs() throws Throwable {
doTestHighlighting("layoutAttrs.xml");
}