Search in sources :

Example 1 with HTMLLanguage

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;
}
Also used : Language(com.intellij.lang.Language) XMLLanguage(com.intellij.lang.xml.XMLLanguage) XHTMLLanguage(com.intellij.lang.xhtml.XHTMLLanguage) HTMLLanguage(com.intellij.lang.html.HTMLLanguage) XHTMLLanguage(com.intellij.lang.xhtml.XHTMLLanguage) HTMLLanguage(com.intellij.lang.html.HTMLLanguage) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Aggregations

Language (com.intellij.lang.Language)1 HTMLLanguage (com.intellij.lang.html.HTMLLanguage)1 XHTMLLanguage (com.intellij.lang.xhtml.XHTMLLanguage)1 XMLLanguage (com.intellij.lang.xml.XMLLanguage)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1