package com.xtremelabs.robolectric.res;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

public class ResourceExtractor {
    private Map<String, Integer> resourceStringToId = new HashMap<String, Integer>();
    private Map<Integer, String> resourceIdToString = new HashMap<Integer, String>();

    public void addRClass(Class rClass) throws Exception {
        for (Class innerClass : rClass.getClasses()) {
            for (Field field : innerClass.getDeclaredFields()) {
                if (field.getType().equals(Integer.TYPE) && Modifier.isStatic(field.getModifiers())) {
                    String name = innerClass.getSimpleName() + "/" + field.getName();
                    int value = field.getInt(null);
                    resourceStringToId.put(name, value);
                    resourceIdToString.put(value, name);
                }
            }
        }
    }

    public int resourceIdOrZero(String resourceName) {
        Integer resId = getResourceId(resourceName);
        return (resId == null) ? 0 : resId;
    }

    public Integer getResourceId(String resourceName) {
        if (resourceName == null) {
            return null;
        }
        if (resourceName.startsWith("@+id")) {
            resourceName = resourceName.substring(2);
        } else if (resourceName.startsWith("@")) {
            resourceName = resourceName.substring(1);
        }
        return resourceStringToId.get(resourceName);
    }

    public String getResourceName(int resourceId) {
        String ret = resourceIdToString.get(resourceId);
        if (ret == null)
            throw new RuntimeException("Can't find resource "+resourceId);
        return ret;
    }
}

@@ -1326,16 +1326,170 @@
 ing(2);%0A
+        %7D else if (resourceName.startsWith(%22@android:id%22)) %7B // namespace needed for platform files%0A            resourceName = resourceName.substring(9);%0A
         

Add namespace for internal ids to list of excludes