com.android.ide.common.res2.ResourceItem Java Examples

The following examples show how to use com.android.ide.common.res2.ResourceItem. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ViewTypeDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Nullable
protected Collection<String> getViewTags(
        @NonNull Context context,
        @NonNull ResourceItem item) {
    // Check view tag in this file. Can I do it cheaply? Try with
    // an XML pull parser. Or DOM if we have multiple resources looked
    // up?
    ResourceFile source = item.getSource();
    if (source != null) {
        File file = source.getFile();
        Multimap<String,String> map = getIdToTagsIn(context, file);
        if (map != null) {
            return map.get(item.getName());
        }
    }

    return null;
}
 
Example #2
Source File: AppIndexingApiDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
private static String replaceUrlWithValue(@NonNull XmlContext context,
        @NonNull String str) {
    Project project = context.getProject();
    LintClient client = context.getClient();
    if (!client.supportsProjectResources()) {
        return str;
    }
    ResourceUrl style = ResourceUrl.parse(str);
    if (style == null || style.type != ResourceType.STRING || style.framework) {
        return str;
    }
    AbstractResourceRepository resources = client.getProjectResources(project, true);
    if (resources == null) {
        return str;
    }
    List<ResourceItem> items = resources.getResourceItem(ResourceType.STRING, style.name);
    if (items == null || items.isEmpty()) {
        return str;
    }
    ResourceValue resourceValue = items.get(0).getResourceValue(false);
    if (resourceValue == null) {
        return str;
    }
    return resourceValue.getValue() == null ? str : resourceValue.getValue();
}
 
Example #3
Source File: WrongIdDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private boolean idDefined(@NonNull Context context, @NonNull String id,
        @Nullable File notIn) {
    AbstractResourceRepository resources =
            context.getClient().getProjectResources(context.getProject(), true);
    if (resources != null) {
        List<ResourceItem> items = resources.getResourceItem(ResourceType.ID,
                stripIdPrefix(id));
        if (items == null || items.isEmpty()) {
            return false;
        }
        for (ResourceItem item : items) {
            ResourceFile source = item.getSource();
            if (source != null) {
                File file = source.getFile();
                if (file.getParentFile().getName().startsWith(FD_RES_VALUES)) {
                    if (mDeclaredIds == null) {
                        mDeclaredIds = Sets.newHashSet();
                    }
                    mDeclaredIds.add(id);
                    continue;
                }

                // Ignore definitions in the given file. This is used to ignore
                // matches in the same file as the reference, since the reference
                // is often expressed as a definition
                if (!isSameResourceFile(file, notIn)) {
                    return true;
                }
            }
        }
    }

    return false;
}
 
Example #4
Source File: LayoutInflationDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private static boolean hasLayoutParams(@NonNull JavaContext context, String name) {
    LintClient client = context.getClient();
    if (!client.supportsProjectResources()) {
        return true; // not certain
    }

    Project project = context.getProject();
    AbstractResourceRepository resources = client.getProjectResources(project, true);
    if (resources == null) {
        return true; // not certain
    }

    List<ResourceItem> items = resources.getResourceItem(ResourceType.LAYOUT, name);
    if (items == null || items.isEmpty()) {
        return false;
    }

    for (ResourceItem item : items) {
        ResourceFile source = item.getSource();
        if (source == null) {
            return true; // not certain
        }
        File file = source.getFile();
        if (file.exists()) {
            try {
                String s = context.getClient().readFile(file);
                if (hasLayoutParams(new StringReader(s))) {
                    return true;
                }
            } catch (Exception e) {
                context.log(e, "Could not read/parse inflated layout");
                return true; // not certain
            }
        }
    }

    return false;
}
 
Example #5
Source File: ValueResourceParser.java    From paraphrase with Apache License 2.0 5 votes vote down vote up
/**
 * Parses the file and returns a list of {@link ResourceItem} objects.
 * @return a list of resources.
 *
 * @throws MergingException if a merging exception happens
 */
private @NonNull List<ResourceItem> parseFile() throws MergingException {
  Document document = parseDocument(file);

  // get the root node
  Node rootNode = document.getDocumentElement();
  if (rootNode == null) {
    return Collections.emptyList();
  }
  NodeList nodes = rootNode.getChildNodes();

  final int count = nodes.getLength();
  // list containing the result
  List<ResourceItem> resources = Lists.newArrayListWithExpectedSize(count);

  for (int i = 0, n = nodes.getLength(); i < n; i++) {
    Node node = nodes.item(i);

    if (node.getNodeType() != Node.ELEMENT_NODE) {
      continue;
    }

    ResourceItem resource = getResource(node);
    if (resource != null) {
      resources.add(resource);
    }
  }

  return resources;
}
 
Example #6
Source File: ValueResourceParser.java    From paraphrase with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a new ResourceItem object for a given node.
 * @param node the node representing the resource.
 * @return a ResourceItem object or null.
 */
static ResourceItem getResource(Node node) {
  ResourceType type = getType(node);
  String name = getName(node);

  if (type != null && name != null) {
    return new ResourceItem(name, type, node);
  }

  return null;
}
 
Example #7
Source File: Location.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public ResourceItemHandle(@NonNull ResourceItem item) {
    mItem = item;
}
 
Example #8
Source File: LintDriver.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@NonNull
@Override
public Location.Handle createResourceItemHandle(@NonNull ResourceItem item) {
    return mDelegate.createResourceItemHandle(item);
}
 
Example #9
Source File: ValueResourceParser.java    From paraphrase with Apache License 2.0 4 votes vote down vote up
static List<ResourceItem> parse(File file) throws MergingException {
  return new ValueResourceParser(file).parseFile();
}
 
Example #10
Source File: LintClient.java    From javaide with GNU General Public License v3.0 2 votes vote down vote up
/**
 * For a lint client which supports resource items (via {@link #supportsProjectResources()})
 * return a handle for a resource item
 *
 * @param item the resource item to look up a location handle for
 * @return a corresponding handle
 */
@NonNull
public Location.Handle createResourceItemHandle(@NonNull ResourceItem item) {
    return new Location.ResourceItemHandle(item);
}