use of com.intellij.lang.html.HTMLLanguage in project intellij-community by JetBrains.
the class XmlUtil method createChildTag.
/**
* @param xmlTag
* @param localName
* @param namespace
* @param bodyText pass null to create collapsed tag, empty string means creating expanded one
* @param enforceNamespacesDeep
* @return
*/
public static XmlTag createChildTag(final XmlTag xmlTag, String localName, String namespace, @Nullable String bodyText, boolean enforceNamespacesDeep) {
String qname;
final String prefix = xmlTag.getPrefixByNamespace(namespace);
if (prefix != null && !prefix.isEmpty()) {
qname = prefix + ":" + localName;
} else {
qname = localName;
}
try {
String tagStart = qname + (!StringUtil.isEmpty(namespace) && xmlTag.getPrefixByNamespace(namespace) == null && !(StringUtil.isEmpty(xmlTag.getNamespacePrefix()) && namespace.equals(xmlTag.getNamespace())) ? " xmlns=\"" + namespace + "\"" : "");
Language language = xmlTag.getLanguage();
if (!(language instanceof HTMLLanguage))
language = XMLLanguage.INSTANCE;
XmlTag retTag;
if (bodyText != null) {
retTag = XmlElementFactory.getInstance(xmlTag.getProject()).createTagFromText("<" + tagStart + ">" + bodyText + "</" + qname + ">", language);
if (enforceNamespacesDeep) {
retTag.acceptChildren(new XmlRecursiveElementVisitor() {
@Override
public void visitXmlTag(XmlTag tag) {
final String namespacePrefix = tag.getNamespacePrefix();
if (namespacePrefix.isEmpty()) {
String qname;
if (prefix != null && !prefix.isEmpty()) {
qname = prefix + ":" + tag.getLocalName();
} else {
qname = tag.getLocalName();
}
try {
tag.setName(qname);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
super.visitXmlTag(tag);
}
});
}
} else {
retTag = XmlElementFactory.getInstance(xmlTag.getProject()).createTagFromText("<" + tagStart + "/>", language);
}
return retTag;
} catch (IncorrectOperationException e) {
LOG.error(e);
}
return null;
}
Aggregations