Search in sources :

Example 6 with DomManager

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

the class AndroidApplicationPackageRenameProcessor method processAllClassAttrValues.

private static void processAllClassAttrValues(@NotNull XmlFile file, @NotNull final Processor<Pair<GenericAttributeValue, PsiClass>> processor) {
    final DomManager domManager = DomManager.getDomManager(file.getProject());
    file.accept(new XmlRecursiveElementVisitor() {

        @Override
        public void visitXmlAttribute(XmlAttribute attribute) {
            final GenericAttributeValue domAttrValue = domManager.getDomElement(attribute);
            if (domAttrValue != null) {
                final Object value = domAttrValue.getValue();
                if (value instanceof PsiClass) {
                    processor.process(Pair.create(domAttrValue, (PsiClass) value));
                }
            }
        }
    });
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) DomManager(com.intellij.util.xml.DomManager) GenericAttributeValue(com.intellij.util.xml.GenericAttributeValue)

Example 7 with DomManager

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

the class AndroidDomElementDescriptorProvider method getDomElementAndBaseClassQName.

@Nullable
public static Pair<AndroidDomElement, String> getDomElementAndBaseClassQName(@NotNull XmlTag tag) {
    final PsiFile file = tag.getContainingFile();
    if (!(file instanceof XmlFile))
        return null;
    Project project = file.getProject();
    if (project.isDefault())
        return null;
    final DomManager domManager = DomManager.getDomManager(project);
    if (domManager.getFileElement((XmlFile) file, AndroidDomElement.class) == null)
        return null;
    final DomElement domElement = domManager.getDomElement(tag);
    if (!(domElement instanceof AndroidDomElement)) {
        return null;
    }
    String className = null;
    if (domElement instanceof LayoutViewElement) {
        className = AndroidUtils.VIEW_CLASS_NAME;
    } else if (domElement instanceof XmlResourceElement) {
        className = SdkConstants.CLASS_PREFERENCE;
    }
    return Pair.create((AndroidDomElement) domElement, className);
}
Also used : Project(com.intellij.openapi.project.Project) DomElement(com.intellij.util.xml.DomElement) LayoutViewElement(org.jetbrains.android.dom.layout.LayoutViewElement) XmlFile(com.intellij.psi.xml.XmlFile) DomManager(com.intellij.util.xml.DomManager) XmlResourceElement(org.jetbrains.android.dom.xml.XmlResourceElement) PsiFile(com.intellij.psi.PsiFile) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with DomManager

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

the class DataBindingCompletionUtil method fillAliases.

/**
   * Add completion suggestions for classes included via {@code <import>}s.
   * @param resultSet the set to add the suggestions to.
   */
private static void fillAliases(@NotNull CompletionResultSet resultSet, @NotNull String packagePrefix, @NotNull PsiElement originalPosition, @NotNull Module module, @NotNull PsiElement originalParent) {
    PsiFile containingFile = getRealContainingFile(originalParent.getContainingFile());
    if (containingFile instanceof XmlFile) {
        final Project project = module.getProject();
        DomManager domManager = DomManager.getDomManager(project);
        XmlTag tag = PsiTreeUtil.getParentOfType(originalPosition, XmlTag.class, false, PsiFile.class);
        if (domManager.getDomElement(tag) instanceof Import) {
            // No referencing aliases in import tags.
            return;
        }
        DomFileElement<Layout> file = domManager.getFileElement((XmlFile) containingFile, Layout.class);
        if (file != null) {
            JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
            for (Data data : file.getRootElement().getDatas()) {
                for (Import anImport : data.getImports()) {
                    String alias = AndroidLayoutUtil.getAlias(anImport);
                    if (packagePrefix.isEmpty()) {
                        XmlAttributeValue type = anImport.getType().getXmlAttributeValue();
                        if (type != null && alias != null) {
                            String typeValue = type.getValue().replace('$', '.');
                            PsiClass aClass = facade.findClass(typeValue, module.getModuleWithDependenciesAndLibrariesScope(false));
                            if (aClass != null) {
                                resultSet.addElement(getClassReferenceElement(alias, aClass));
                            }
                        }
                    } else {
                        int i = packagePrefix.indexOf('.');
                        String possibleAlias = i < 0 ? packagePrefix : packagePrefix.substring(0, i);
                        if (possibleAlias.equals(alias)) {
                            String type = anImport.getType().getStringValue();
                            if (type != null) {
                                String fqcn = packagePrefix.equals(alias) ? type : type + packagePrefix.substring(alias.length());
                                PsiClass aClass = facade.findClass(fqcn, module.getModuleWithDependenciesAndLibrariesScope(false));
                                if (aClass != null) {
                                    for (PsiClass innerClass : aClass.getInnerClasses()) {
                                        String name = innerClass.getQualifiedName();
                                        if (name != null) {
                                            resultSet.addElement(getClassReferenceElement(name.substring(type.length() + 1), innerClass));
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : Import(org.jetbrains.android.dom.layout.Import) XmlFile(com.intellij.psi.xml.XmlFile) DomManager(com.intellij.util.xml.DomManager) Data(org.jetbrains.android.dom.layout.Data) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Project(com.intellij.openapi.project.Project) Layout(org.jetbrains.android.dom.layout.Layout) XmlTag(com.intellij.psi.xml.XmlTag)

Example 9 with DomManager

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

the class AndroidExtractAsIncludeAction method isEnabledForTags.

@Override
protected boolean isEnabledForTags(@NotNull XmlTag[] tags) {
    if (tags.length == 0) {
        return false;
    }
    final DomManager domManager = DomManager.getDomManager(tags[0].getProject());
    boolean containsViewElement = false;
    for (XmlTag tag : tags) {
        final DomElement domElement = domManager.getDomElement(tag);
        if (!isSuitableDomElement(domElement)) {
            return false;
        }
        if (domElement instanceof LayoutViewElement) {
            containsViewElement = true;
        }
    }
    if (!containsViewElement) {
        return false;
    }
    final PsiElement parent = tags[0].getParent();
    if (!(parent instanceof XmlTag) || parent.getContainingFile() == null) {
        return false;
    }
    for (int i = 1; i < tags.length; i++) {
        if (tags[i].getParent() != parent) {
            return false;
        }
    }
    return true;
}
Also used : DomElement(com.intellij.util.xml.DomElement) LayoutViewElement(org.jetbrains.android.dom.layout.LayoutViewElement) DomManager(com.intellij.util.xml.DomManager) XmlTag(com.intellij.psi.xml.XmlTag)

Example 10 with DomManager

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

the class AntSupport method getAntLib.

@Nullable
public static AntDomAntlib getAntLib(PsiFile psiFile) {
    if (psiFile instanceof XmlFile) {
        final DomManager domManager = DomManager.getDomManager(psiFile.getProject());
        final DomFileElement<AntDomAntlib> fileElement = domManager.getFileElement((XmlFile) psiFile, AntDomAntlib.class);
        return fileElement != null ? fileElement.getRootElement() : null;
    }
    return null;
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) DomManager(com.intellij.util.xml.DomManager) AntDomAntlib(com.intellij.lang.ant.dom.AntDomAntlib) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

DomManager (com.intellij.util.xml.DomManager)15 XmlFile (com.intellij.psi.xml.XmlFile)9 Nullable (org.jetbrains.annotations.Nullable)7 DomElement (com.intellij.util.xml.DomElement)5 Project (com.intellij.openapi.project.Project)3 XmlTag (com.intellij.psi.xml.XmlTag)3 LayoutViewElement (org.jetbrains.android.dom.layout.LayoutViewElement)3 AntDomProject (com.intellij.lang.ant.dom.AntDomProject)2 AppEngineStandardWebApp (com.google.cloud.tools.intellij.appengine.descriptor.dom.AppEngineStandardWebApp)1 AntDomAntlib (com.intellij.lang.ant.dom.AntDomAntlib)1 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 PsiFile (com.intellij.psi.PsiFile)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)1 GenericAttributeValue (com.intellij.util.xml.GenericAttributeValue)1 StableElement (com.intellij.util.xml.StableElement)1 Data (org.jetbrains.android.dom.layout.Data)1 Import (org.jetbrains.android.dom.layout.Import)1 Layout (org.jetbrains.android.dom.layout.Layout)1