Search in sources :

Example 46 with XmlDocument

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

the class URLReference method resolve.

@Override
@Nullable
public PsiElement resolve() {
    myIncorrectResourceMapped = false;
    final String canonicalText = getCanonicalText();
    if (canonicalText.isEmpty()) {
        final XmlAttribute attr = PsiTreeUtil.getParentOfType(getElement(), XmlAttribute.class);
        if (attr != null && attr.isNamespaceDeclaration() && attr.getNamespacePrefix().isEmpty() || ExternalResourceManagerEx.getInstanceEx().isIgnoredResource(canonicalText)) {
            // of there being no default namespace
            return myElement;
        }
        return null;
    }
    if (ExternalResourceManagerEx.getInstanceEx().isIgnoredResource(canonicalText))
        return myElement;
    final XmlTag tag = PsiTreeUtil.getParentOfType(myElement, XmlTag.class);
    if (tag != null && canonicalText.equals(tag.getAttributeValue(TARGET_NAMESPACE_ATTR_NAME)))
        return tag;
    final PsiFile containingFile = myElement.getContainingFile();
    if (tag != null && tag.getAttributeValue("schemaLocation", XmlUtil.XML_SCHEMA_INSTANCE_URI) == null) {
        final PsiFile file = ExternalResourceManager.getInstance().getResourceLocation(canonicalText, containingFile, tag.getAttributeValue("version"));
        if (file != null)
            return file;
    }
    if (containingFile instanceof XmlFile) {
        final XmlDocument document = ((XmlFile) containingFile).getDocument();
        assert document != null;
        final XmlTag rootTag = document.getRootTag();
        if (rootTag == null) {
            return ExternalResourceManager.getInstance().getResourceLocation(canonicalText, containingFile, null);
        }
        final XmlNSDescriptor nsDescriptor = rootTag.getNSDescriptor(canonicalText, true);
        if (nsDescriptor != null)
            return nsDescriptor.getDescriptorFile();
        final String url = ExternalResourceManager.getInstance().getResourceLocation(canonicalText, myElement.getProject());
        if (!url.equals(canonicalText)) {
            myIncorrectResourceMapped = true;
            return null;
        }
        if (tag == rootTag && (tag.getNamespace().equals(XmlUtil.XML_SCHEMA_URI) || tag.getNamespace().equals(XmlUtil.WSDL_SCHEMA_URI))) {
            for (XmlTag t : tag.getSubTags()) {
                final String name = t.getLocalName();
                if ("import".equals(name)) {
                    if (canonicalText.equals(t.getAttributeValue("namespace")))
                        return t;
                } else if (!"include".equals(name) && !"redefine".equals(name) && !"annotation".equals(name))
                    break;
            }
        }
        final PsiElement[] result = new PsiElement[1];
        processWsdlSchemas(rootTag, t -> {
            if (canonicalText.equals(t.getAttributeValue(TARGET_NAMESPACE_ATTR_NAME))) {
                result[0] = t;
                return false;
            }
            for (XmlTag anImport : t.findSubTags("import", t.getNamespace())) {
                if (canonicalText.equals(anImport.getAttributeValue("namespace"))) {
                    final XmlAttribute location = anImport.getAttribute("schemaLocation");
                    if (location != null) {
                        result[0] = FileReferenceUtil.findFile(location.getValueElement());
                    }
                }
            }
            return true;
        });
        return result[0];
    }
    return null;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) XmlDocument(com.intellij.psi.xml.XmlDocument) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 47 with XmlDocument

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

the class DefaultXmlExtension method filterNamespaces.

public static Set<String> filterNamespaces(final Set<String> namespaces, final String tagName, final XmlFile context) {
    if (tagName == null) {
        return namespaces;
    }
    final HashSet<String> set = new HashSet<>();
    for (String namespace : namespaces) {
        final XmlFile xmlFile = XmlUtil.findNamespace(context, namespace);
        if (xmlFile != null) {
            final XmlDocument document = xmlFile.getDocument();
            assert document != null;
            final XmlNSDescriptor nsDescriptor = (XmlNSDescriptor) document.getMetaData();
            assert nsDescriptor != null;
            final XmlElementDescriptor[] elementDescriptors = nsDescriptor.getRootElementsDescriptors(document);
            for (XmlElementDescriptor elementDescriptor : elementDescriptors) {
                LOG.assertTrue(elementDescriptor != null, "Null returned from " + nsDescriptor);
                if (hasTag(elementDescriptor, tagName, new HashSet<>())) {
                    set.add(namespace);
                    break;
                }
            }
        }
    }
    return set;
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) XmlDocument(com.intellij.psi.xml.XmlDocument)

Example 48 with XmlDocument

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

the class TypeOrElementOrAttributeReference method getDescriptor.

XmlNSDescriptorImpl getDescriptor(final XmlTag tag, String text, boolean[] redefined) {
    if (myType != ReferenceType.ElementReference && myType != ReferenceType.AttributeReference) {
        final PsiElement parentElement = myElement.getContext();
        final PsiElement grandParentElement = parentElement != null ? parentElement.getParent() : null;
        boolean doRedefineCheck = false;
        if (parentElement instanceof XmlAttribute && grandParentElement instanceof XmlTag) {
            final String attrName = ((XmlAttribute) parentElement).getName();
            final String tagLocalName = ((XmlTag) grandParentElement).getLocalName();
            doRedefineCheck = (SchemaReferencesProvider.REF_ATTR_NAME.equals(attrName) && (SchemaReferencesProvider.GROUP_TAG_NAME.equals(tagLocalName) || SchemaReferencesProvider.ATTRIBUTE_GROUP_TAG_NAME.equals(tagLocalName))) || (SchemaReferencesProvider.BASE_ATTR_NAME.equals(attrName) || SchemaReferencesProvider.MEMBER_TYPES_ATTR_NAME.equals(attrName));
        }
        if (doRedefineCheck) {
            XmlNSDescriptorImpl redefinedDescriptor = SchemaReferencesProvider.findRedefinedDescriptor(tag, text);
            if (redefinedDescriptor != null) {
                redefined[0] = true;
                return redefinedDescriptor;
            }
        }
    }
    final String namespace = getNamespace(tag, text);
    XmlNSDescriptor nsDescriptor = tag.getNSDescriptor(namespace, true);
    final PsiFile file = tag.getContainingFile();
    if (!(file instanceof XmlFile))
        return null;
    final XmlDocument document = ((XmlFile) file).getDocument();
    if (nsDescriptor == null) {
        // import
        nsDescriptor = (XmlNSDescriptor) document.getMetaData();
    }
    if (nsDescriptor == null) {
        final XmlNSDescriptor[] descrs = new XmlNSDescriptor[1];
        URLReference.processWsdlSchemas(document.getRootTag(), xmlTag -> {
            if (namespace.equals(xmlTag.getAttributeValue(TARGET_NAMESPACE))) {
                descrs[0] = (XmlNSDescriptor) xmlTag.getMetaData();
                return false;
            }
            return true;
        });
        if (descrs[0] instanceof XmlNSDescriptorImpl)
            return (XmlNSDescriptorImpl) descrs[0];
    }
    return nsDescriptor instanceof XmlNSDescriptorImpl ? (XmlNSDescriptorImpl) nsDescriptor : null;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) XmlFile(com.intellij.psi.xml.XmlFile) PsiFile(com.intellij.psi.PsiFile) XmlDocument(com.intellij.psi.xml.XmlDocument) XmlNSDescriptorImpl(com.intellij.xml.impl.schema.XmlNSDescriptorImpl) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 49 with XmlDocument

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

the class XmlElementDescriptorByType method getNSDescriptor.

@Override
public XmlNSDescriptor getNSDescriptor() {
    XmlNSDescriptor nsDescriptor = NSDescriptor;
    if (nsDescriptor == null) {
        final XmlFile file = XmlUtil.getContainingFile(getType(null).getDeclaration());
        if (file == null)
            return null;
        final XmlDocument document = file.getDocument();
        if (document == null)
            return null;
        NSDescriptor = nsDescriptor = (XmlNSDescriptor) document.getMetaData();
    }
    return nsDescriptor;
}
Also used : XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) XmlFile(com.intellij.psi.xml.XmlFile) XmlDocument(com.intellij.psi.xml.XmlDocument)

Example 50 with XmlDocument

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

the class XmlNSDescriptorImpl method init.

@Override
public void init(PsiElement element) {
    myFile = (XmlFile) element.getContainingFile();
    if (element instanceof XmlTag) {
        myTag = (XmlTag) element;
    } else {
        final XmlDocument document = myFile.getDocument();
        if (document != null) {
            myTag = document.getRootTag();
        }
    }
    if (myTag != null) {
        myTargetNamespace = myTag.getAttributeValue("targetNamespace");
    }
    final THashSet<PsiFile> dependenciesSet = new THashSet<>();
    final Set<PsiFile> redefineProcessingSet = myRedefinedDescriptorsInProcessing.get();
    if (redefineProcessingSet != null) {
        dependenciesSet.addAll(redefineProcessingSet);
    }
    collectDependencies(myTag, myFile, dependenciesSet);
    dependencies = ArrayUtil.toObjectArray(dependenciesSet);
}
Also used : XmlDocument(com.intellij.psi.xml.XmlDocument) PsiFile(com.intellij.psi.PsiFile) THashSet(gnu.trove.THashSet) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

XmlDocument (com.intellij.psi.xml.XmlDocument)57 XmlTag (com.intellij.psi.xml.XmlTag)39 XmlFile (com.intellij.psi.xml.XmlFile)32 PsiElement (com.intellij.psi.PsiElement)13 Nullable (org.jetbrains.annotations.Nullable)11 XmlNSDescriptor (com.intellij.xml.XmlNSDescriptor)9 PsiFile (com.intellij.psi.PsiFile)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 ArrayList (java.util.ArrayList)6 XmlAttribute (com.intellij.psi.xml.XmlAttribute)5 PsiMetaData (com.intellij.psi.meta.PsiMetaData)4 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 NotNull (org.jetbrains.annotations.NotNull)4 Project (com.intellij.openapi.project.Project)3 XmlText (com.intellij.psi.xml.XmlText)3 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)3 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)2 Editor (com.intellij.openapi.editor.Editor)2 PsiErrorElement (com.intellij.psi.PsiErrorElement)2 FilterElementProcessor (com.intellij.psi.scope.processor.FilterElementProcessor)2