Search in sources :

Example 51 with XmlAttribute

use of com.intellij.psi.xml.XmlAttribute in project intellij-community by JetBrains.

the class XmlSchemaTest method testAny3.

public void testAny3() throws Exception {
    XmlNSDescriptor NSDescriptor = createDescriptor("<xsd:schema targetNamespace=\"http://foo\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" >" + "  <xsd:element name=\"root\">" + "    <xsd:complexType>" + "        <xsd:anyAttribute namespace=\"##other\" processContents=\"skip\"/>" + "    </xsd:complexType>" + "  </xsd:element>" + "</xsd:schema>");
    XmlFile xmlFile = (XmlFile) createFile("file.xml", "<root xmlns=\"http://foo\" y:a=\"1\">" + "</root>");
    final XmlTag rootTag = xmlFile.getDocument().getRootTag();
    XmlElementDescriptor rootDescriptor = NSDescriptor.getElementDescriptor(rootTag);
    assertNotNull(rootDescriptor);
    XmlAttribute attribute = rootTag.getAttribute("y:a", XmlUtil.EMPTY_URI);
    assertNotNull(attribute);
    XmlAttributeDescriptor aDescriptor = rootDescriptor.getAttributeDescriptor("y:a", rootTag);
    assertNotNull(aDescriptor);
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) XmlFile(com.intellij.psi.xml.XmlFile) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) XmlTag(com.intellij.psi.xml.XmlTag)

Example 52 with XmlAttribute

use of com.intellij.psi.xml.XmlAttribute in project intellij-community by JetBrains.

the class MvcFramework method getInstalledPluginNameByPath.

@Nullable
public String getInstalledPluginNameByPath(Project project, @NotNull VirtualFile pluginPath) {
    VirtualFile pluginXml = pluginPath.findChild("plugin.xml");
    if (pluginXml == null)
        return null;
    PsiFile pluginXmlPsi = PsiManager.getInstance(project).findFile(pluginXml);
    if (!(pluginXmlPsi instanceof XmlFile))
        return null;
    XmlTag rootTag = ((XmlFile) pluginXmlPsi).getRootTag();
    if (rootTag == null || !"plugin".equals(rootTag.getName()))
        return null;
    XmlAttribute attrName = rootTag.getAttribute("name");
    if (attrName == null)
        return null;
    String res = attrName.getValue();
    if (res == null)
        return null;
    res = res.trim();
    if (res.isEmpty())
        return null;
    return res;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 53 with XmlAttribute

use of com.intellij.psi.xml.XmlAttribute in project intellij-community by JetBrains.

the class MavenFilteredPropertiesCompletionAndResolutionTest method testReferencesInXml.

public void testReferencesInXml() throws Exception {
    createProjectSubDir("res");
    importProject("<groupId>test</groupId>" + "<artifactId>project</artifactId>" + "<version>1</version>" + "<build>" + "  <resources>" + "    <resource>" + "      <directory>res</directory>" + "      <filtering>true</filtering>" + "    </resource>" + "  </resources>" + "</build>");
    VirtualFile f = createProjectSubFile("res/foo.xml", "<root attr='${based<caret>ir}'>" + "</root>");
    myFixture.configureFromExistingVirtualFile(f);
    XmlAttribute attribute = PsiTreeUtil.getParentOfType(myFixture.getFile().findElementAt(myFixture.getCaretOffset()), XmlAttribute.class);
    PsiReference[] references = attribute.getReferences();
    for (PsiReference ref : references) {
        if (ref.resolve() instanceof PsiDirectory) {
            // Maven references was added.
            return;
        }
    }
    assertTrue("Maven filter reference was not added", false);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlAttribute(com.intellij.psi.xml.XmlAttribute) PsiDirectory(com.intellij.psi.PsiDirectory) MavenPropertyPsiReference(org.jetbrains.idea.maven.dom.references.MavenPropertyPsiReference) PsiReference(com.intellij.psi.PsiReference)

Example 54 with XmlAttribute

use of com.intellij.psi.xml.XmlAttribute in project intellij-community by JetBrains.

the class RngDocumentationProvider method generateDoc.

@Override
@Nullable
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
    final XmlElement c = PsiTreeUtil.getParentOfType(originalElement, XmlTag.class, XmlAttribute.class);
    if (c != null && c.getManager() == null) {
        LOG.warn("Invalid context element passed to generateDoc()", new Throwable("<stack trace>"));
        return null;
    }
    if (c instanceof XmlTag) {
        final XmlTag xmlElement = (XmlTag) c;
        final XmlElementDescriptor descriptor = xmlElement.getDescriptor();
        if (descriptor instanceof CompositeDescriptor) {
            final StringBuilder sb = new StringBuilder();
            final CompositeDescriptor d = (CompositeDescriptor) descriptor;
            final DElementPattern[] patterns = d.getElementPatterns();
            final THashSet<PsiElement> elements = ContainerUtil.newIdentityTroveSet();
            for (DElementPattern pattern : patterns) {
                final PsiElement psiElement = d.getDeclaration(pattern.getLocation());
                if (psiElement instanceof XmlTag && elements.add(psiElement)) {
                    if (sb.length() > 0) {
                        sb.append("<hr>");
                    }
                    sb.append(getDocumentationFromTag((XmlTag) psiElement, xmlElement.getLocalName(), "Element"));
                }
            }
            return makeDocumentation(sb);
        } else if (descriptor instanceof RngElementDescriptor) {
            final RngElementDescriptor d = (RngElementDescriptor) descriptor;
            final PsiElement declaration = d.getDeclaration();
            if (declaration instanceof XmlTag) {
                return makeDocumentation(getDocumentationFromTag((XmlTag) declaration, xmlElement.getLocalName(), "Element"));
            }
        }
    } else if (c instanceof XmlAttribute) {
        final XmlAttribute attribute = (XmlAttribute) c;
        final XmlAttributeDescriptor descriptor = attribute.getDescriptor();
        if (descriptor instanceof RngXmlAttributeDescriptor) {
            final RngXmlAttributeDescriptor d = (RngXmlAttributeDescriptor) descriptor;
            final StringBuilder sb = new StringBuilder();
            final Collection<PsiElement> declaration = ContainerUtil.newIdentityTroveSet(d.getDeclarations());
            for (PsiElement psiElement : declaration) {
                if (psiElement instanceof XmlTag) {
                    if (sb.length() > 0) {
                        sb.append("<hr>");
                    }
                    sb.append(getDocumentationFromTag((XmlTag) psiElement, d.getName(), "Attribute"));
                }
            }
            return makeDocumentation(sb);
        }
    } else if (element instanceof XmlTag) {
        return makeDocumentation(getDocumentationFromTag((XmlTag) element, ((XmlTag) element).getLocalName(), "Element"));
    }
    return null;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) RngXmlAttributeDescriptor(org.intellij.plugins.relaxNG.model.descriptors.RngXmlAttributeDescriptor) RngElementDescriptor(org.intellij.plugins.relaxNG.model.descriptors.RngElementDescriptor) RngXmlAttributeDescriptor(org.intellij.plugins.relaxNG.model.descriptors.RngXmlAttributeDescriptor) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) XmlElement(com.intellij.psi.xml.XmlElement) CompositeDescriptor(org.intellij.plugins.relaxNG.model.descriptors.CompositeDescriptor) Collection(java.util.Collection) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) DElementPattern(org.kohsuke.rngom.digested.DElementPattern) Nullable(org.jetbrains.annotations.Nullable)

Example 55 with XmlAttribute

use of com.intellij.psi.xml.XmlAttribute in project intellij-community by JetBrains.

the class AntDomExtender method defineAttributes.

private static void defineAttributes(XmlTag xmlTag, DomExtensionsRegistrar registrar, DomGenericInfo genericInfo, AbstractIntrospector parentIntrospector) {
    final Map<String, Pair<Type, Class>> registeredAttribs = getStaticallyRegisteredAttributes(genericInfo);
    // define attributes discovered by introspector and not yet defined statically
    final Iterator<String> introspectedAttributes = parentIntrospector.getAttributesIterator();
    while (introspectedAttributes.hasNext()) {
        final String attribName = introspectedAttributes.next();
        if (genericInfo.getAttributeChildDescription(attribName) == null) {
            // if not defined yet 
            final String _attribName = attribName.toLowerCase(Locale.US);
            final Pair<Type, Class> types = registeredAttribs.get(_attribName);
            Type type = types != null ? types.getFirst() : null;
            Class converterClass = types != null ? types.getSecond() : null;
            if (type == null) {
                // use String by default
                type = String.class;
                final Class attributeType = parentIntrospector.getAttributeType(attribName);
                if (attributeType != null) {
                    // handle well-known types
                    if (File.class.isAssignableFrom(attributeType)) {
                        type = PsiFileSystemItem.class;
                        converterClass = AntPathConverter.class;
                    } else if (Boolean.class.isAssignableFrom(attributeType)) {
                        type = Boolean.class;
                        converterClass = AntBooleanConverter.class;
                    } else if (isAssignableFrom(Reference.class.getName(), attributeType)) {
                        converterClass = AntDomRefIdConverter.class;
                    }
                }
            }
            LOG.assertTrue(type != null);
            registerAttribute(registrar, attribName, type, converterClass);
            if (types == null) {
                // augment the map if this was a newly added attribute
                registeredAttribs.put(_attribName, Pair.create(type, converterClass));
            }
        }
    }
    // additionaly register all attributes that exist in XML but differ from the registered ones only in case
    for (XmlAttribute xmlAttribute : xmlTag.getAttributes()) {
        final String existingAttribName = xmlAttribute.getName();
        if (genericInfo.getAttributeChildDescription(existingAttribName) == null) {
            final Pair<Type, Class> pair = registeredAttribs.get(existingAttribName.toLowerCase(Locale.US));
            if (pair != null) {
                // if such attribute should actually be here
                registerAttribute(registrar, existingAttribName, pair.getFirst(), pair.getSecond());
            }
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) XmlAttribute(com.intellij.psi.xml.XmlAttribute) Reference(org.apache.tools.ant.types.Reference) Pair(com.intellij.openapi.util.Pair)

Aggregations

XmlAttribute (com.intellij.psi.xml.XmlAttribute)220 XmlTag (com.intellij.psi.xml.XmlTag)116 PsiElement (com.intellij.psi.PsiElement)60 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)57 NotNull (org.jetbrains.annotations.NotNull)44 XmlFile (com.intellij.psi.xml.XmlFile)37 Nullable (org.jetbrains.annotations.Nullable)27 Project (com.intellij.openapi.project.Project)25 VirtualFile (com.intellij.openapi.vfs.VirtualFile)20 TextRange (com.intellij.openapi.util.TextRange)18 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)18 ArrayList (java.util.ArrayList)18 PsiFile (com.intellij.psi.PsiFile)17 PsiReference (com.intellij.psi.PsiReference)17 IncorrectOperationException (com.intellij.util.IncorrectOperationException)10 DomElement (com.intellij.util.xml.DomElement)10 Result (com.intellij.openapi.application.Result)9 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)9 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)8 XmlElement (com.intellij.psi.xml.XmlElement)6