Search in sources :

Example 31 with DomElement

use of com.intellij.util.xml.DomElement in project android by JetBrains.

the class AndroidInlineStyleReferenceAction method doRefactorForTags.

@Override
protected void doRefactorForTags(@NotNull Project project, @NotNull final XmlTag[] tags) {
    assert tags.length == 1;
    final XmlTag tag = tags[0];
    final PsiFile file = tag.getContainingFile();
    if (file == null) {
        return;
    }
    final StyleUsageData usageData = AndroidInlineUtil.getStyleUsageData(tag);
    if (usageData == null) {
        return;
    }
    final AndroidResourceReferenceBase reference = usageData.getReference();
    final String title = AndroidBundle.message("android.inline.style.title");
    final PsiElement[] styleElements = reference.computeTargetElements();
    if (styleElements.length == 0) {
        AndroidUtils.reportError(project, "Cannot find style by reference '" + reference.getValue() + "'", title);
        return;
    }
    if (styleElements.length > 1) {
        AndroidUtils.reportError(project, RefactoringBundle.getCannotRefactorMessage("Unambiguous style reference."), title);
        return;
    }
    final PsiElement styleElement = styleElements[0];
    final XmlTag styleTag = PsiTreeUtil.getParentOfType(styleElement, XmlTag.class);
    final DomElement domElement = styleTag != null ? DomManager.getDomManager(project).getDomElement(styleTag) : null;
    if (!(domElement instanceof Style)) {
        AndroidUtils.reportError(project, "Cannot find style by reference '" + reference.getValue() + "'", title);
        return;
    }
    final Style style = (Style) domElement;
    String styleName = style.getName().getStringValue();
    if (styleName == null) {
        AndroidUtils.reportError(project, RefactoringBundle.getCannotRefactorMessage("Style name is not specified."), title);
        return;
    }
    AndroidInlineUtil.doInlineStyleDeclaration(project, new AndroidInlineUtil.MyStyleData(styleName, style, styleElement), usageData, new ProjectBasedErrorReporter(project), myTestConfig);
}
Also used : DomElement(com.intellij.util.xml.DomElement) ProjectBasedErrorReporter(org.jetbrains.android.util.ProjectBasedErrorReporter) AndroidResourceReferenceBase(org.jetbrains.android.dom.converters.AndroidResourceReferenceBase) Style(org.jetbrains.android.dom.resources.Style) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 32 with DomElement

use of com.intellij.util.xml.DomElement in project android by JetBrains.

the class AndroidInlineUtil method getStyleUsageData.

@Nullable
static StyleUsageData getStyleUsageData(@NotNull XmlTag tag) {
    final DomElement domElement = DomManager.getDomManager(tag.getProject()).getDomElement(tag);
    if (domElement instanceof LayoutViewElement) {
        final GenericAttributeValue<ResourceValue> styleAttribute = ((LayoutViewElement) domElement).getStyle();
        final AndroidResourceReferenceBase reference = AndroidDomUtil.getAndroidResourceReference(styleAttribute, true);
        if (reference != null) {
            return new ViewStyleUsageData(tag, styleAttribute, reference);
        }
    } else if (domElement instanceof Style) {
        final AndroidResourceReferenceBase reference = AndroidDomUtil.getAndroidResourceReference(((Style) domElement).getParentStyle(), true);
        if (reference != null) {
            return new ParentStyleUsageData((Style) domElement, reference);
        }
    }
    return null;
}
Also used : DomElement(com.intellij.util.xml.DomElement) LayoutViewElement(org.jetbrains.android.dom.layout.LayoutViewElement) AndroidResourceReferenceBase(org.jetbrains.android.dom.converters.AndroidResourceReferenceBase) ResourceValue(org.jetbrains.android.dom.resources.ResourceValue) Style(org.jetbrains.android.dom.resources.Style) Nullable(org.jetbrains.annotations.Nullable)

Example 33 with DomElement

use of com.intellij.util.xml.DomElement in project android by JetBrains.

the class AndroidFindStyleApplicationsProcessor method isPossibleApplicationOfStyle.

private boolean isPossibleApplicationOfStyle(XmlTag candidate) {
    final DomElement domCandidate = DomManager.getDomManager(myProject).getDomElement(candidate);
    if (!(domCandidate instanceof LayoutViewElement)) {
        return false;
    }
    final LayoutViewElement candidateView = (LayoutViewElement) domCandidate;
    final Map<Pair<String, String>, String> attrsInCandidateMap = new HashMap<Pair<String, String>, String>();
    final List<XmlAttribute> attrsInCandidate = AndroidExtractStyleAction.getExtractableAttributes(candidate);
    if (attrsInCandidate.size() < myAttrMap.size()) {
        return false;
    }
    for (XmlAttribute attribute : attrsInCandidate) {
        final String attrValue = attribute.getValue();
        if (attrValue != null) {
            attrsInCandidateMap.put(Pair.create(attribute.getNamespace(), attribute.getLocalName()), attrValue);
        }
    }
    for (Map.Entry<AndroidAttributeInfo, String> entry : myAttrMap.entrySet()) {
        final String ns = entry.getKey().getNamespace();
        final String name = entry.getKey().getName();
        final String value = entry.getValue();
        final String valueInCandidate = attrsInCandidateMap.get(Pair.create(ns, name));
        if (valueInCandidate == null || !valueInCandidate.equals(value)) {
            return false;
        }
    }
    if (candidateView.getStyle().getStringValue() != null) {
        if (myParentStyleNameAttrValue == null) {
            return false;
        }
        final PsiElement styleNameAttrValueForTag = getStyleNameAttrValueForTag(candidateView);
        if (styleNameAttrValueForTag == null || !myParentStyleNameAttrValue.equals(styleNameAttrValueForTag)) {
            return false;
        }
    } else if (myParentStyleNameAttrValue != null) {
        return false;
    }
    return true;
}
Also used : DomElement(com.intellij.util.xml.DomElement) LayoutViewElement(org.jetbrains.android.dom.layout.LayoutViewElement) XmlAttribute(com.intellij.psi.xml.XmlAttribute) HashMap(com.intellij.util.containers.HashMap) HashMap(com.intellij.util.containers.HashMap) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair)

Example 34 with DomElement

use of com.intellij.util.xml.DomElement in project intellij-community by JetBrains.

the class ExtensionPointLocator method isRegisteredExtension.

public static boolean isRegisteredExtension(@NotNull PsiClass psiClass) {
    String name = psiClass.getQualifiedName();
    if (name == null)
        return false;
    Project project = psiClass.getProject();
    GlobalSearchScope scope = getCandidatesScope(project);
    return !PsiSearchHelper.SERVICE.getInstance(project).processUsagesInNonJavaFiles(name, new PsiNonJavaFileReferenceProcessor() {

        @Override
        public boolean process(PsiFile file, int startOffset, int endOffset) {
            PsiElement at = file.findElementAt(startOffset);
            String tokenText = at instanceof XmlToken ? at.getText() : null;
            if (!StringUtil.equals(name, tokenText))
                return true;
            XmlTag tag = PsiTreeUtil.getParentOfType(at, XmlTag.class);
            if (tag == null)
                return true;
            DomElement dom = DomUtil.getDomElement(tag);
            return !(dom instanceof Extension && ((Extension) dom).getExtensionPoint() != null);
        }
    }, scope);
}
Also used : Extension(org.jetbrains.idea.devkit.dom.Extension) Project(com.intellij.openapi.project.Project) DomElement(com.intellij.util.xml.DomElement) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiNonJavaFileReferenceProcessor(com.intellij.psi.search.PsiNonJavaFileReferenceProcessor) XmlToken(com.intellij.psi.xml.XmlToken) XmlTag(com.intellij.psi.xml.XmlTag)

Example 35 with DomElement

use of com.intellij.util.xml.DomElement in project intellij-community by JetBrains.

the class AppEngineFacet method getRootElement.

//todo[nik] copied from JamCommonUtil
@Nullable
private static <T> T getRootElement(final PsiFile file, final Class<T> domClass, final Module module) {
    if (!(file instanceof XmlFile))
        return null;
    final DomManager domManager = DomManager.getDomManager(file.getProject());
    final DomFileElement<DomElement> element = domManager.getFileElement((XmlFile) file, DomElement.class);
    if (element == null)
        return null;
    final DomElement root = element.getRootElement();
    if (!ReflectionUtil.isAssignable(domClass, root.getClass()))
        return null;
    return (T) root;
}
Also used : DomElement(com.intellij.util.xml.DomElement) XmlFile(com.intellij.psi.xml.XmlFile) DomManager(com.intellij.util.xml.DomManager) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

DomElement (com.intellij.util.xml.DomElement)97 XmlTag (com.intellij.psi.xml.XmlTag)34 PsiElement (com.intellij.psi.PsiElement)19 Nullable (org.jetbrains.annotations.Nullable)19 NotNull (org.jetbrains.annotations.NotNull)15 Project (com.intellij.openapi.project.Project)11 PsiFile (com.intellij.psi.PsiFile)11 XmlFile (com.intellij.psi.xml.XmlFile)11 XmlAttribute (com.intellij.psi.xml.XmlAttribute)10 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)9 ArrayList (java.util.ArrayList)8 LayoutViewElement (org.jetbrains.android.dom.layout.LayoutViewElement)7 ResourceElement (org.jetbrains.android.dom.resources.ResourceElement)6 DomManager (com.intellij.util.xml.DomManager)5 Style (org.jetbrains.android.dom.resources.Style)5 AntDomTarget (com.intellij.lang.ant.dom.AntDomTarget)4 XmlElement (com.intellij.psi.xml.XmlElement)4 DomElementProblemDescriptor (com.intellij.util.xml.highlighting.DomElementProblemDescriptor)4 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 TextRange (com.intellij.openapi.util.TextRange)3