Search in sources :

Example 41 with DomElement

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

the class TilesOgnlInjector method getLanguagesToInject.

@Override
public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) {
    final PsiFile containingFile = context.getContainingFile();
    if (!JamCommonUtil.isPlainXmlFile(containingFile)) {
        return;
    }
    assert context instanceof XmlAttributeValue;
    if (!((XmlAttributeValue) context).getValue().startsWith(OGNL_PREFIX)) {
        return;
    }
    PsiElement parent = context.getParent();
    if (parent instanceof XmlAttribute) {
        String name = ((XmlAttribute) parent).getLocalName();
        if ("expression".equals(name) || "templateExpression".equals(name)) {
            DomElement domElement = DomManager.getDomManager(context.getProject()).getDomElement((XmlTag) parent.getParent());
            if (domElement instanceof Put || domElement instanceof Add || domElement instanceof Definition) {
                final TextRange attributeTextRange = ElementManipulators.getValueTextRange(context);
                final TextRange ognlTextRange = TextRange.from(attributeTextRange.getStartOffset() + OGNL_PREFIX.length(), attributeTextRange.getLength() - OGNL_PREFIX.length());
                registrar.startInjecting(OgnlLanguage.INSTANCE).addPlace(OgnlLanguage.EXPRESSION_PREFIX, OgnlLanguage.EXPRESSION_SUFFIX, (PsiLanguageInjectionHost) context, ognlTextRange).doneInjecting();
            }
        }
    }
}
Also used : Add(com.intellij.struts.dom.tiles.Add) XmlAttribute(com.intellij.psi.xml.XmlAttribute) DomElement(com.intellij.util.xml.DomElement) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) Definition(com.intellij.struts.dom.tiles.Definition) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) PsiElement(com.intellij.psi.PsiElement) Put(com.intellij.struts.dom.tiles.Put)

Example 42 with DomElement

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

the class ActivityLocatorUtils method getQualifiedName.

@Nullable
public static String getQualifiedName(@NotNull ActivityAlias alias) {
    ApplicationManager.getApplication().assertReadAccessAllowed();
    String name = alias.getName().getStringValue();
    if (name == null) {
        return null;
    }
    int dotIndex = name.indexOf('.');
    if (dotIndex > 0) {
        // fully qualified
        return name;
    }
    // attempt to retrieve the package name from the manifest in which this alias was defined
    String pkg = null;
    DomElement parent = alias.getParent();
    if (parent instanceof Application) {
        parent = parent.getParent();
        if (parent instanceof Manifest) {
            Manifest manifest = (Manifest) parent;
            pkg = manifest.getPackage().getStringValue();
        }
    }
    // if we have a package name, prepend that to the activity alias
    return pkg == null ? name : pkg + (dotIndex == -1 ? "." : "") + name;
}
Also used : DomElement(com.intellij.util.xml.DomElement) Nullable(org.jetbrains.annotations.Nullable)

Example 43 with DomElement

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

the class RtlSupportProcessor method getLayoutRefactoringForTag.

private List<UsageInfo> getLayoutRefactoringForTag(@NotNull XmlTag tag, boolean createV17, int minSdk) {
    final DomElement domElement = DomManager.getDomManager(myProject).getDomElement(tag);
    if (!(domElement instanceof LayoutViewElement)) {
        return Collections.emptyList();
    }
    final List<UsageInfo> result = new ArrayList<UsageInfo>();
    final XmlAttribute[] attributes = tag.getAttributes();
    for (XmlAttribute attributeToMirror : attributes) {
        final String localName = attributeToMirror.getLocalName();
        final String namespacePrefix = attributeToMirror.getNamespacePrefix();
        final String mirroredLocalName = ourMapMirroredAttributeName.get(localName);
        // Check if this is a RTL attribute to mirror or if it is a Gravity attribute
        if (mirroredLocalName != null) {
            // Mirror only attributes that has not been mirrored before
            final XmlAttribute attributeMirrored = tag.getAttribute(namespacePrefix + ":" + mirroredLocalName);
            if (attributeMirrored == null) {
                final int startOffset = 0;
                final int endOffset = attributeToMirror.getTextLength();
                RtlRefactoringUsageInfo usageInfoForAttribute = new RtlRefactoringUsageInfo(attributeToMirror, startOffset, endOffset);
                usageInfoForAttribute.setType(LAYOUT_FILE_ATTRIBUTE);
                usageInfoForAttribute.setCreateV17(createV17);
                usageInfoForAttribute.setAndroidManifestMinSdkVersion(minSdk);
                result.add(usageInfoForAttribute);
            }
        } else if (localName.equals(ATTR_GRAVITY) || localName.equals(ATTR_LAYOUT_GRAVITY)) {
            final String value = attributeToMirror.getValue();
            if (value != null && (value.contains(GRAVITY_VALUE_LEFT) || value.contains(GRAVITY_VALUE_RIGHT))) {
                final int startOffset = 0;
                final int endOffset = attributeToMirror.getTextLength();
                RtlRefactoringUsageInfo usageInfoForAttribute = new RtlRefactoringUsageInfo(attributeToMirror, startOffset, endOffset);
                usageInfoForAttribute.setType(LAYOUT_FILE_ATTRIBUTE);
                usageInfoForAttribute.setCreateV17(createV17);
                result.add(usageInfoForAttribute);
            }
        }
    }
    return result;
}
Also used : DomElement(com.intellij.util.xml.DomElement) LayoutViewElement(org.jetbrains.android.dom.layout.LayoutViewElement) XmlAttribute(com.intellij.psi.xml.XmlAttribute) ArrayList(java.util.ArrayList) UsageInfo(com.intellij.usageView.UsageInfo)

Example 44 with DomElement

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

the class AndroidResourceRenameResourceProcessor method getResourceName.

/** Looks up the resource name for the given refactored element. Uses the same
   * instanceof chain checkups as is done in {@link #canProcessElement} */
@Nullable
private static String getResourceName(PsiElement originalElement) {
    PsiElement element = LazyValueResourceElementWrapper.computeLazyElement(originalElement);
    if (element == null) {
        return null;
    }
    if (element instanceof PsiFile) {
        PsiFile file = (PsiFile) element;
        LocalResourceManager manager = LocalResourceManager.getInstance(element);
        if (manager != null) {
            String type = manager.getFileResourceType(file);
            if (type != null) {
                String name = file.getName();
                return AndroidCommonUtils.getResourceName(type, name);
            }
        }
        return LintUtils.getBaseName(file.getName());
    } else if (element instanceof PsiField) {
        PsiField field = (PsiField) element;
        return field.getName();
    } else if (element instanceof XmlAttributeValue) {
        if (AndroidResourceUtil.isIdDeclaration((XmlAttributeValue) element)) {
            return AndroidResourceUtil.getResourceNameByReferenceText(((XmlAttributeValue) element).getValue());
        }
        XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
        if (tag != null) {
            DomElement domElement = DomManager.getDomManager(tag.getProject()).getDomElement(tag);
            if (domElement instanceof ResourceElement) {
                return ((ResourceElement) domElement).getName().getValue();
            }
        }
    }
    return null;
}
Also used : ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) DomElement(com.intellij.util.xml.DomElement) LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 45 with DomElement

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

the class AndroidRenameHandler method isPackageAttributeInManifest.

static boolean isPackageAttributeInManifest(@NotNull Project project, @Nullable PsiElement element) {
    if (element == null) {
        return false;
    }
    final PsiFile psiFile = element.getContainingFile();
    if (!(psiFile instanceof XmlFile)) {
        return false;
    }
    final AndroidFacet facet = AndroidFacet.getInstance(psiFile);
    if (facet == null) {
        return false;
    }
    final VirtualFile vFile = psiFile.getVirtualFile();
    if (vFile == null || !vFile.equals(AndroidRootUtil.getPrimaryManifestFile(facet))) {
        return false;
    }
    if (!(element instanceof XmlAttributeValue)) {
        return false;
    }
    final PsiElement parent = element.getParent();
    if (!(parent instanceof XmlAttribute)) {
        return false;
    }
    final GenericAttributeValue attrValue = DomManager.getDomManager(project).getDomElement((XmlAttribute) parent);
    if (attrValue == null) {
        return false;
    }
    final DomElement parentDomElement = attrValue.getParent();
    return parentDomElement instanceof Manifest && attrValue.equals(((Manifest) parentDomElement).getPackage());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DomElement(com.intellij.util.xml.DomElement) PsiFile(com.intellij.psi.PsiFile) Manifest(org.jetbrains.android.dom.manifest.Manifest) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) PsiElement(com.intellij.psi.PsiElement) GenericAttributeValue(com.intellij.util.xml.GenericAttributeValue)

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