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;
}
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;
}
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;
}
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;
}
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);
}
Aggregations