Search in sources :

Example 6 with ResourceUrl

use of com.android.ide.common.resources.ResourceUrl in project kotlin by JetBrains.

the class PrivateResourceDetector method checkChildRefs.

private static void checkChildRefs(@NonNull XmlContext context, Element item) {
    // Look for ?attr/ and @dimen/foo etc references in the item children
    NodeList childNodes = item.getChildNodes();
    for (int i = 0, n = childNodes.getLength(); i < n; i++) {
        Node child = childNodes.item(i);
        if (child.getNodeType() == Node.TEXT_NODE) {
            String text = child.getNodeValue();
            int index = text.indexOf(ATTR_REF_PREFIX);
            if (index != -1) {
                String name = text.substring(index + ATTR_REF_PREFIX.length()).trim();
                if (isPrivate(context, ResourceType.ATTR, name)) {
                    String message = createUsageErrorMessage(context, ResourceType.ATTR, name);
                    context.report(ISSUE, item, context.getLocation(child), message);
                }
            } else {
                for (int j = 0, m = text.length(); j < m; j++) {
                    char c = text.charAt(j);
                    if (c == '@') {
                        ResourceUrl url = ResourceUrl.parse(text.trim());
                        if (isPrivate(context, url)) {
                            String message = createUsageErrorMessage(context, url.type, url.name);
                            context.report(ISSUE, item, context.getLocation(child), message);
                        }
                        break;
                    } else if (!Character.isWhitespace(c)) {
                        break;
                    }
                }
            }
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ResourceUrl(com.android.ide.common.resources.ResourceUrl)

Example 7 with ResourceUrl

use of com.android.ide.common.resources.ResourceUrl in project android by JetBrains.

the class AndroidXmlDocumentationProvider method generateDoc.

@Override
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
    if (element instanceof ProvidedDocumentationPsiElement) {
        return ((ProvidedDocumentationPsiElement) element).getDocumentation();
    }
    if (element instanceof LazyValueResourceElementWrapper) {
        LazyValueResourceElementWrapper wrapper = (LazyValueResourceElementWrapper) element;
        ValueResourceInfo resourceInfo = wrapper.getResourceInfo();
        ResourceType type = resourceInfo.getType();
        String name = resourceInfo.getName();
        Module module = ModuleUtilCore.findModuleForPsiElement(element);
        if (module == null) {
            return null;
        }
        AndroidFacet facet = AndroidFacet.getInstance(element);
        if (facet == null) {
            return null;
        }
        ResourceUrl url;
        ResourceUrl originalUrl = originalElement != null ? ResourceUrl.parse(originalElement.getText()) : null;
        if (originalUrl != null && name.equals(originalUrl.name)) {
            url = originalUrl;
        } else {
            boolean isFramework = false;
            if (originalUrl != null) {
                isFramework = originalUrl.framework;
            } else {
                // Figure out if this resource is a framework file.
                // We really should store that info in the ValueResourceInfo instances themselves.
                // For now, attempt to figure it out
                SystemResourceManager systemResourceManager = facet.getSystemResourceManager();
                VirtualFile containingFile = resourceInfo.getContainingFile();
                if (systemResourceManager != null) {
                    VirtualFile parent = containingFile.getParent();
                    if (parent != null) {
                        VirtualFile resDir = parent.getParent();
                        if (resDir != null) {
                            isFramework = systemResourceManager.isResourceDir(resDir);
                        }
                    }
                }
            }
            url = ResourceUrl.create(type, name, isFramework, false);
        }
        return generateDoc(element, url);
    } else if (element instanceof MyResourceElement) {
        return getResourceDocumentation(element, ((MyResourceElement) element).myResource);
    } else if (element instanceof XmlAttributeValue) {
        return getResourceDocumentation(element, ((XmlAttributeValue) element).getValue());
    }
    if (originalElement instanceof XmlToken) {
        XmlToken token = (XmlToken) originalElement;
        if (token.getTokenType() == XML_ATTRIBUTE_VALUE_START_DELIMITER) {
            PsiElement next = token.getNextSibling();
            if (next instanceof XmlToken) {
                token = (XmlToken) next;
            }
        } else if (token.getTokenType() == XML_ATTRIBUTE_VALUE_END_DELIMITER) {
            PsiElement prev = token.getPrevSibling();
            if (prev instanceof XmlToken) {
                token = (XmlToken) prev;
            }
        }
        if (token.getTokenType() == XML_ATTRIBUTE_VALUE_TOKEN) {
            String documentation = getResourceDocumentation(originalElement, token.getText());
            if (documentation != null) {
                return documentation;
            }
        } else if (token.getTokenType() == XML_DATA_CHARACTERS) {
            String text = token.getText().trim();
            String documentation = getResourceDocumentation(originalElement, text);
            if (documentation != null) {
                return documentation;
            }
        }
    }
    if (element instanceof PomTargetPsiElement && originalElement != null) {
        final PomTarget target = ((PomTargetPsiElement) element).getTarget();
        if (target instanceof DomAttributeChildDescription) {
            synchronized (ANDROID_ATTRIBUTE_DOCUMENTATION_CACHE_KEY) {
                return generateDocForXmlAttribute((DomAttributeChildDescription) target, originalElement);
            }
        }
    }
    if (element instanceof MyDocElement) {
        return ((MyDocElement) element).myDocumentation;
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceType(com.android.resources.ResourceType) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) DomAttributeChildDescription(com.intellij.util.xml.reflect.DomAttributeChildDescription) XmlToken(com.intellij.psi.xml.XmlToken) PomTarget(com.intellij.pom.PomTarget) LazyValueResourceElementWrapper(org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) Module(com.intellij.openapi.module.Module) ResourceUrl(com.android.ide.common.resources.ResourceUrl) SystemResourceManager(org.jetbrains.android.resourceManagers.SystemResourceManager) FakePsiElement(com.intellij.psi.impl.FakePsiElement) PsiElement(com.intellij.psi.PsiElement) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) ValueResourceInfo(org.jetbrains.android.resourceManagers.ValueResourceInfo)

Example 8 with ResourceUrl

use of com.android.ide.common.resources.ResourceUrl in project android by JetBrains.

the class AndroidXmlDocumentationProvider method getResourceDocumentation.

@Nullable
private static String getResourceDocumentation(PsiElement element, String value) {
    ResourceUrl url = ResourceUrl.parse(value);
    if (url != null) {
        return generateDoc(element, url);
    } else {
        XmlAttribute attribute = PsiTreeUtil.getParentOfType(element, XmlAttribute.class, false);
        XmlTag tag = null;
        // True if getting the documentation of the XML value (not the value of the name attribute)
        boolean isXmlValue = false;
        // get the XmlAttribute using the containing tag
        if (element instanceof XmlToken && XML_DATA_CHARACTERS.equals(((XmlToken) element).getTokenType())) {
            tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
            attribute = tag == null ? null : tag.getAttribute(ATTR_NAME);
            isXmlValue = true;
        }
        if (attribute == null) {
            return null;
        }
        if (ATTR_NAME.equals(attribute.getName())) {
            tag = tag != null ? tag : attribute.getParent();
            XmlTag parentTag = tag.getParentTag();
            if (parentTag == null) {
                return null;
            }
            if (TAG_RESOURCES.equals(parentTag.getName())) {
                // Handle ID definitions, http://developer.android.com/guide/topics/resources/more-resources.html#Id
                String typeName = tag.getName();
                if (TAG_ITEM.equals(typeName)) {
                    typeName = tag.getAttributeValue(ATTR_TYPE);
                }
                ResourceType type = ResourceType.getEnum(typeName);
                if (type != null) {
                    return generateDoc(element, type, value, false);
                }
            } else if (TAG_STYLE.equals(parentTag.getName())) {
                // String used to get attribute definitions
                String targetValue = value;
                if (isXmlValue && attribute.getValue() != null) {
                    // In this case, the target is the name attribute of the <item> tag, which contains the key of the attr enum
                    targetValue = attribute.getValue();
                }
                if (targetValue.startsWith(ANDROID_NS_NAME_PREFIX)) {
                    targetValue = targetValue.substring(ANDROID_NS_NAME_PREFIX.length());
                }
                // Handle style item definitions, http://developer.android.com/guide/topics/resources/style-resource.html
                AttributeDefinition attributeDefinition = getAttributeDefinitionForElement(element, targetValue);
                if (attributeDefinition == null) {
                    return null;
                }
                // Return the doc of the value if searching for an enum value, otherwise return the doc of the enum itself
                return StringUtil.trim(isXmlValue ? attributeDefinition.getValueDoc(value) : attributeDefinition.getDocValue(null));
            }
        }
        // Display documentation for enum values defined in attrs.xml file, if it's present
        if (ANDROID_URI.equals(attribute.getNamespace())) {
            AttributeDefinition attributeDefinition = getAttributeDefinitionForElement(element, attribute.getLocalName());
            if (attributeDefinition == null) {
                return null;
            }
            return StringUtil.trim(attributeDefinition.getValueDoc(value));
        }
    }
    return null;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) ResourceType(com.android.resources.ResourceType) ResourceUrl(com.android.ide.common.resources.ResourceUrl) XmlTag(com.intellij.psi.xml.XmlTag) XmlToken(com.intellij.psi.xml.XmlToken) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with ResourceUrl

use of com.android.ide.common.resources.ResourceUrl in project android by JetBrains.

the class ThemeEditorUtils method getResourceDialog.

@NotNull
public static ChooseResourceDialog getResourceDialog(@NotNull EditedStyleItem item, @NotNull ThemeEditorContext context, EnumSet<ResourceType> allowedTypes) {
    Module module = context.getModuleForResources();
    ItemResourceValue itemSelectedValue = item.getSelectedValue();
    String value = itemSelectedValue.getValue();
    boolean isFrameworkValue = itemSelectedValue.isFramework();
    String nameSuggestion = value;
    ResourceUrl url = ResourceUrl.parse(value, isFrameworkValue);
    if (url != null) {
        nameSuggestion = url.name;
    }
    nameSuggestion = getDefaultResourceName(context, nameSuggestion);
    ChooseResourceDialog.ResourceNameVisibility resourceNameVisibility = ChooseResourceDialog.ResourceNameVisibility.FORCE;
    if (nameSuggestion.startsWith("#")) {
        nameSuggestion = null;
        resourceNameVisibility = ChooseResourceDialog.ResourceNameVisibility.SHOW;
    }
    ChooseResourceDialog dialog = ChooseResourceDialog.builder().setModule(module).setTypes(allowedTypes).setCurrentValue(value).setIsFrameworkValue(isFrameworkValue).setResourceNameVisibility(resourceNameVisibility).setResourceNameSuggestion(nameSuggestion).build();
    dialog.setUseGlobalUndo(true);
    return dialog;
}
Also used : ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ChooseResourceDialog(com.android.tools.idea.ui.resourcechooser.ChooseResourceDialog) Module(com.intellij.openapi.module.Module) ResourceUrl(com.android.ide.common.resources.ResourceUrl) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with ResourceUrl

use of com.android.ide.common.resources.ResourceUrl in project android by JetBrains.

the class GradleInstantRunContext method addAppResourceReferences.

private static void addAppResourceReferences(@NotNull Element element, @NotNull Set<ResourceUrl> refs) {
    NamedNodeMap attributes = element.getAttributes();
    if (attributes != null) {
        for (int i = 0, n = attributes.getLength(); i < n; i++) {
            Node attribute = attributes.item(i);
            String value = attribute.getNodeValue();
            if (value.startsWith(PREFIX_RESOURCE_REF)) {
                ResourceUrl url = ResourceUrl.parse(value);
                if (url != null && !url.framework) {
                    refs.add(url);
                }
            }
        }
    }
    NodeList children = element.getChildNodes();
    for (int i = 0, n = children.getLength(); i < n; i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            addAppResourceReferences((Element) child, refs);
        }
    }
}
Also used : ResourceUrl(com.android.ide.common.resources.ResourceUrl)

Aggregations

ResourceUrl (com.android.ide.common.resources.ResourceUrl)24 PsiElement (com.intellij.psi.PsiElement)7 Nullable (com.android.annotations.Nullable)6 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)5 AbstractResourceRepository (com.android.ide.common.res2.AbstractResourceRepository)5 ResourceItem (com.android.ide.common.res2.ResourceItem)5 ResourceType (com.android.resources.ResourceType)4 PsiMethod (com.intellij.psi.PsiMethod)4 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)3 LintClient (com.android.tools.klint.client.api.LintClient)3 UExpression (org.jetbrains.uast.UExpression)3 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)2 Module (com.intellij.openapi.module.Module)2 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)2 PsiClass (com.intellij.psi.PsiClass)2 PsiConditionalExpression (com.intellij.psi.PsiConditionalExpression)2 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)2 PsiExpression (com.intellij.psi.PsiExpression)2 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)2 PsiField (com.intellij.psi.PsiField)2