use of com.intellij.xml.XmlNSDescriptor in project intellij-community by JetBrains.
the class XmlDocumentImpl method getNsDescriptorFormDocType.
@Nullable
private XmlNSDescriptor getNsDescriptorFormDocType(final XmlDoctype doctype, final XmlFile containingFile, final boolean forHtml) {
XmlNSDescriptor descriptor = getNSDescriptorFromMetaData(doctype.getMarkupDecl(), true);
final String filePath = getFilePathForLogging(containingFile);
final String dtdUri = XmlUtil.getDtdUri(doctype);
LOG.debug("DTD url for doctype " + doctype.getText() + " in file " + filePath + " is " + dtdUri);
if (dtdUri != null && !dtdUri.isEmpty()) {
XmlFile xmlFile = XmlUtil.findNamespace(containingFile, dtdUri);
if (xmlFile == null) {
// try to auto-detect it
xmlFile = XmlNamespaceIndex.guessDtd(dtdUri, containingFile);
}
final String schemaFilePath = getFilePathForLogging(xmlFile);
LOG.debug("Schema file for " + filePath + " is " + schemaFilePath);
XmlNSDescriptor descriptorFromDtd = getNSDescriptorFromMetaData(xmlFile == null ? null : xmlFile.getDocument(), forHtml);
LOG.debug("Descriptor from meta data for schema file " + schemaFilePath + " is " + (descriptorFromDtd != null ? descriptorFromDtd.getClass().getCanonicalName() : "NULL"));
if (descriptor != null && descriptorFromDtd != null) {
descriptor = new XmlNSDescriptorSequence(new XmlNSDescriptor[] { descriptor, descriptorFromDtd });
} else if (descriptorFromDtd != null) {
descriptor = descriptorFromDtd;
}
}
return descriptor;
}
use of com.intellij.xml.XmlNSDescriptor in project intellij-community by JetBrains.
the class XmlDocumentImpl method getDefaultNSDescriptorInner.
private XmlNSDescriptor getDefaultNSDescriptorInner(final String namespace, final boolean strict) {
final XmlFile containingFile = XmlUtil.getContainingFile(this);
if (containingFile == null)
return null;
final XmlProlog prolog = getProlog();
final XmlDoctype doctype = prolog != null ? prolog.getDoctype() : null;
boolean dtdUriFromDocTypeIsNamespace = false;
if (XmlUtil.HTML_URI.equals(namespace)) {
XmlNSDescriptor nsDescriptor = doctype != null ? getNsDescriptorFormDocType(doctype, containingFile, true) : null;
if (doctype != null) {
LOG.debug("Descriptor from doctype " + doctype + " is " + (nsDescriptor != null ? nsDescriptor.getClass().getCanonicalName() : "NULL"));
}
if (nsDescriptor == null) {
String htmlns = ExternalResourceManagerEx.getInstanceEx().getDefaultHtmlDoctype(getProject());
if (htmlns.isEmpty()) {
htmlns = Html5SchemaProvider.getHtml5SchemaLocation();
}
nsDescriptor = getDefaultNSDescriptor(htmlns, false);
}
final XmlFile descriptorFile = nsDescriptor.getDescriptorFile();
if (descriptorFile != null) {
return getCachedHtmlNsDescriptor(descriptorFile);
}
return new HtmlNSDescriptorImpl(nsDescriptor);
} else if (XmlUtil.XHTML_URI.equals(namespace)) {
String xhtmlNamespace = XmlUtil.getDefaultXhtmlNamespace(getProject());
if (xhtmlNamespace == null || xhtmlNamespace.isEmpty()) {
xhtmlNamespace = Html5SchemaProvider.getXhtml5SchemaLocation();
}
return getDefaultNSDescriptor(xhtmlNamespace, false);
} else if (namespace != null && namespace != XmlUtil.EMPTY_URI) {
if (doctype == null || !namespace.equals(XmlUtil.getDtdUri(doctype))) {
boolean documentIsSchemaThatDefinesNs = namespace.equals(XmlUtil.getTargetSchemaNsFromTag(getRootTag()));
final XmlFile xmlFile = documentIsSchemaThatDefinesNs ? containingFile : XmlUtil.findNamespace(containingFile, namespace);
if (xmlFile != null) {
final XmlDocument document = xmlFile.getDocument();
if (document != null) {
return (XmlNSDescriptor) document.getMetaData();
}
}
} else {
dtdUriFromDocTypeIsNamespace = true;
}
}
if (strict && !dtdUriFromDocTypeIsNamespace)
return null;
if (doctype != null) {
XmlNSDescriptor descr = getNsDescriptorFormDocType(doctype, containingFile, false);
if (descr != null) {
return XmlExtension.getExtension(containingFile).getDescriptorFromDoctype(containingFile, descr);
}
}
if (strict)
return null;
if (namespace == XmlUtil.EMPTY_URI) {
final XmlFile xmlFile = XmlUtil.findNamespace(containingFile, namespace);
if (xmlFile != null) {
return (XmlNSDescriptor) xmlFile.getDocument().getMetaData();
}
}
try {
final PsiFile fileFromText = PsiFileFactory.getInstance(getProject()).createFileFromText(containingFile.getName() + ".dtd", DTDLanguage.INSTANCE, XmlUtil.generateDocumentDTD(this, false), false, false);
if (fileFromText instanceof XmlFile) {
fileFromText.putUserData(AUTO_GENERATED, Boolean.TRUE);
return (XmlNSDescriptor) ((XmlFile) fileFromText).getDocument().getMetaData();
}
} catch (ProcessCanceledException ex) {
throw ex;
} catch (RuntimeException ignored) {
}
return null;
}
use of com.intellij.xml.XmlNSDescriptor 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.xml.XmlNSDescriptor in project intellij-community by JetBrains.
the class XmlElementDescriptorImpl method doCollectXmlDescriptors.
// Read-only action
@Override
protected final XmlElementDescriptor[] doCollectXmlDescriptors(final XmlTag context) {
final LinkedHashSet<XmlElementDescriptor> result = new LinkedHashSet<>();
final XmlElementContentSpec contentSpecElement = myElementDecl.getContentSpecElement();
final XmlNSDescriptor nsDescriptor = getNSDescriptor();
final XmlNSDescriptor NSDescriptor = nsDescriptor != null ? nsDescriptor : getNsDescriptorFrom(context);
XmlUtil.processXmlElements(contentSpecElement, new PsiElementProcessor() {
@Override
public boolean execute(@NotNull PsiElement child) {
if (child instanceof XmlToken) {
final XmlToken token = (XmlToken) child;
if (token.getTokenType() == XmlTokenType.XML_NAME) {
final String text = child.getText();
XmlElementDescriptor element = getElementDescriptor(text, NSDescriptor);
if (element != null) {
result.add(element);
}
} else if (token.getTokenType() == XmlTokenType.XML_CONTENT_ANY) {
if (NSDescriptor instanceof XmlNSDescriptorImpl) {
ContainerUtil.addAll(result, ((XmlNSDescriptorImpl) NSDescriptor).getElements());
} else if (NSDescriptor instanceof XmlNSDescriptorSequence) {
for (XmlNSDescriptor xmlNSDescriptor : ((XmlNSDescriptorSequence) NSDescriptor).getSequence()) {
if (xmlNSDescriptor instanceof XmlNSDescriptorImpl) {
ContainerUtil.addAll(result, ((XmlNSDescriptorImpl) xmlNSDescriptor).getElements());
}
}
}
}
}
return true;
}
}, true, false, XmlUtil.getContainingFile(getDeclaration()));
return result.toArray(new XmlElementDescriptor[result.size()]);
}
use of com.intellij.xml.XmlNSDescriptor in project intellij-community by JetBrains.
the class Xsd2InstanceUtils method processAndSaveAllSchemas.
public static String processAndSaveAllSchemas(@NotNull XmlFile file, @NotNull final Map<String, String> scannedToFileName, @NotNull final SchemaReferenceProcessor schemaReferenceProcessor) {
final String fileName = file.getName();
String previous = scannedToFileName.get(fileName);
if (previous != null)
return previous;
scannedToFileName.put(fileName, fileName);
final StringBuilder result = new StringBuilder();
file.acceptChildren(new XmlRecursiveElementVisitor() {
@Override
public void visitElement(PsiElement psiElement) {
super.visitElement(psiElement);
if (psiElement instanceof LeafPsiElement) {
final String text = psiElement.getText();
result.append(text);
}
}
@Override
public void visitXmlAttribute(XmlAttribute xmlAttribute) {
boolean replaced = false;
if (xmlAttribute.isNamespaceDeclaration()) {
replaced = true;
final String value = xmlAttribute.getValue();
result.append(xmlAttribute.getText()).append(" ");
if (!scannedToFileName.containsKey(value)) {
final XmlNSDescriptor nsDescriptor = xmlAttribute.getParent().getNSDescriptor(value, true);
if (nsDescriptor != null) {
processAndSaveAllSchemas(nsDescriptor.getDescriptorFile(), scannedToFileName, schemaReferenceProcessor);
}
}
} else if ("schemaLocation".equals(xmlAttribute.getName())) {
final PsiReference[] references = xmlAttribute.getValueElement().getReferences();
if (references.length > 0) {
PsiElement psiElement = references[0].resolve();
if (psiElement instanceof XmlFile) {
final String s = processAndSaveAllSchemas(((XmlFile) psiElement), scannedToFileName, schemaReferenceProcessor);
if (s != null) {
result.append(xmlAttribute.getName()).append("='").append(s).append('\'');
replaced = true;
}
}
}
}
if (!replaced)
result.append(xmlAttribute.getText());
}
});
final VirtualFile virtualFile = file.getVirtualFile();
final String content = result.toString();
byte[] bytes;
if (virtualFile != null) {
bytes = content.getBytes(virtualFile.getCharset());
} else {
try {
final String charsetName = XmlUtil.extractXmlEncodingFromProlog(content.getBytes());
bytes = charsetName != null ? content.getBytes(charsetName) : content.getBytes();
} catch (UnsupportedEncodingException e) {
bytes = content.getBytes();
}
}
schemaReferenceProcessor.processSchema(fileName, bytes);
return fileName;
}
Aggregations