Search in sources :

Example 6 with XmlElementFactory

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

the class DefaultXmlNamespaceHelper method insertNamespaceDeclaration.

@Override
public void insertNamespaceDeclaration(@NotNull final XmlFile file, @Nullable final Editor editor, @NotNull final Set<String> possibleNamespaces, @Nullable String nsPrefix, @Nullable final Runner<String, IncorrectOperationException> runAfter) throws IncorrectOperationException {
    final String namespace = possibleNamespaces.iterator().next();
    final Project project = file.getProject();
    final XmlTag rootTag = file.getRootTag();
    assert rootTag != null;
    XmlAttribute anchor = getAnchor(rootTag);
    final List<XmlSchemaProvider> providers = XmlSchemaProvider.getAvailableProviders(file);
    String prefix = getPrefix(file, nsPrefix, namespace, providers);
    final XmlElementFactory elementFactory = XmlElementFactory.getInstance(project);
    String location = getLocation(file, namespace, providers);
    String xsiPrefix = null;
    if (location != null) {
        xsiPrefix = rootTag.getPrefixByNamespace(XmlUtil.XML_SCHEMA_INSTANCE_URI);
        if (xsiPrefix == null) {
            xsiPrefix = "xsi";
            rootTag.add(elementFactory.createXmlAttribute("xmlns:xsi", XmlUtil.XML_SCHEMA_INSTANCE_URI));
        }
    }
    @NonNls final String qname = "xmlns" + (prefix.length() > 0 ? ":" + prefix : "");
    final XmlAttribute attribute = elementFactory.createXmlAttribute(qname, namespace);
    if (anchor == null) {
        rootTag.add(attribute);
    } else {
        rootTag.addAfter(attribute, anchor);
    }
    if (location != null) {
        XmlAttribute locationAttribute = rootTag.getAttribute(XmlUtil.SCHEMA_LOCATION_ATT, XmlUtil.XML_SCHEMA_INSTANCE_URI);
        final String pair = namespace + " " + location;
        if (locationAttribute == null) {
            locationAttribute = elementFactory.createXmlAttribute(xsiPrefix + ":" + XmlUtil.SCHEMA_LOCATION_ATT, pair);
            rootTag.add(locationAttribute);
        } else {
            final String value = locationAttribute.getValue();
            if (!StringUtil.notNullize(value).contains(namespace)) {
                if (value == null || StringUtil.isEmptyOrSpaces(value)) {
                    locationAttribute.setValue(pair);
                } else {
                    locationAttribute.setValue(value.trim() + " " + pair);
                }
            }
        }
    }
    XmlUtil.reformatTagStart(rootTag);
    if (editor != null && namespace.length() == 0) {
        final XmlAttribute xmlAttribute = rootTag.getAttribute(qname);
        if (xmlAttribute != null) {
            final XmlAttributeValue value = xmlAttribute.getValueElement();
            assert value != null;
            final int startOffset = value.getTextOffset();
            editor.getCaretModel().moveToOffset(startOffset);
            editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
        }
    }
    if (runAfter != null) {
        runAfter.run(prefix);
    }
}
Also used : Project(com.intellij.openapi.project.Project) NonNls(org.jetbrains.annotations.NonNls) XmlElementFactory(com.intellij.psi.XmlElementFactory)

Example 7 with XmlElementFactory

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

the class XmlTagWriteTest method test1.

public void test1() throws IncorrectOperationException {
    XmlElementFactory elementFactory = XmlElementFactory.getInstance(getProject());
    final XmlTag xmlTag = XmlElementFactory.getInstance(getProject()).createTagFromText("<tag1/>");
    WriteCommandAction.runWriteCommandAction(null, () -> {
        xmlTag.add(xmlTag.createChildTag("tag2", XmlUtil.EMPTY_URI, null, false));
    });
    assertEquals("<tag1><tag2/></tag1>", xmlTag.getText());
    XmlTag createdFromText = elementFactory.createTagFromText(xmlTag.getText());
    assertEquals("tag1", createdFromText.getName());
    assertEquals(1, createdFromText.getSubTags().length);
    assertEquals("tag2", createdFromText.getSubTags()[0].getName());
}
Also used : XmlElementFactory(com.intellij.psi.XmlElementFactory) XmlTag(com.intellij.psi.xml.XmlTag)

Example 8 with XmlElementFactory

use of com.intellij.psi.XmlElementFactory in project android by JetBrains.

the class LayoutUsageData method inlineMultiTags.

private static void inlineMultiTags(XmlTag includeTag, XmlTag includeTagParent, XmlTag mergeTag, Project project) throws AndroidRefactoringErrorException {
    final Map<String, String> namespacesFromParent = includeTagParent.getLocalNamespaceDeclarations();
    final Map<String, String> namespacesToAddToParent = new HashMap<String, String>();
    final Map<String, String> namespacesToAddToEachTag = new HashMap<String, String>();
    for (Map.Entry<String, String> entry : mergeTag.getLocalNamespaceDeclarations().entrySet()) {
        final String prefix = entry.getKey();
        final String namespace = entry.getValue();
        final String declaredNamespace = namespacesFromParent.get(prefix);
        if (declaredNamespace != null && !declaredNamespace.equals(namespace)) {
            namespacesToAddToEachTag.put(prefix, namespace);
        } else {
            namespacesToAddToParent.put(prefix, namespace);
        }
    }
    final XmlTag mergeTagCopy = (XmlTag) mergeTag.copy();
    final XmlElementFactory xmlElementFactory = XmlElementFactory.getInstance(project);
    for (XmlTag subtag : mergeTagCopy.getSubTags()) {
        final XmlAttribute[] attributes = subtag.getAttributes();
        final XmlAttribute firstAttribute = attributes.length > 0 ? attributes[0] : null;
        for (Map.Entry<String, String> entry : namespacesToAddToEachTag.entrySet()) {
            final String prefix = entry.getKey();
            final String namespace = entry.getValue();
            if (!subtag.getLocalNamespaceDeclarations().containsKey(prefix)) {
                final XmlAttribute xmlnsAttr = xmlElementFactory.createXmlAttribute("xmlns:" + prefix, namespace);
                if (firstAttribute != null) {
                    subtag.addBefore(xmlnsAttr, firstAttribute);
                } else {
                    subtag.add(xmlnsAttr);
                }
            }
        }
    }
    replaceByTagContent(project, includeTag, mergeTagCopy);
    addNamespaceAttributes(includeTagParent, namespacesToAddToParent, project);
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) HashMap(com.intellij.util.containers.HashMap) XmlElementFactory(com.intellij.psi.XmlElementFactory) HashMap(com.intellij.util.containers.HashMap) Map(java.util.Map) XmlTag(com.intellij.psi.xml.XmlTag)

Example 9 with XmlElementFactory

use of com.intellij.psi.XmlElementFactory in project android by JetBrains.

the class LayoutUsageData method addNamespaceAttributes.

private static void addNamespaceAttributes(XmlTag tag, Map<String, String> namespaces, Project project) {
    final XmlAttribute[] parentAttributes = tag.getAttributes();
    final XmlAttribute firstParentAttribute = parentAttributes.length > 0 ? parentAttributes[0] : null;
    final XmlElementFactory factory = XmlElementFactory.getInstance(project);
    for (Map.Entry<String, String> entry : namespaces.entrySet()) {
        final String prefix = entry.getKey();
        final String namespace = entry.getValue();
        if (!namespace.equals(tag.getNamespaceByPrefix(prefix))) {
            final XmlAttribute xmlnsAttr = factory.createXmlAttribute("xmlns:" + prefix, namespace);
            if (firstParentAttribute != null) {
                tag.addBefore(xmlnsAttr, firstParentAttribute);
            } else {
                tag.add(xmlnsAttr);
            }
        }
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlElementFactory(com.intellij.psi.XmlElementFactory) HashMap(com.intellij.util.containers.HashMap) Map(java.util.Map)

Example 10 with XmlElementFactory

use of com.intellij.psi.XmlElementFactory in project android by JetBrains.

the class AppBarConfigurationDialog method applyChanges.

private void applyChanges(@NotNull XmlFile file) {
    Map<String, String> namespaces = getNameSpaces(file.getRootTag(), false);
    String xml = getXml(getDesignContent(file), false, namespaces);
    XmlElementFactory elementFactory = XmlElementFactory.getInstance(file.getProject());
    XmlTag tag = elementFactory.createTagFromText(xml);
    if (file.getRootTag() == null) {
        file.add(tag);
    } else {
        file.getRootTag().replace(tag);
    }
}
Also used : XmlElementFactory(com.intellij.psi.XmlElementFactory) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

XmlElementFactory (com.intellij.psi.XmlElementFactory)12 XmlTag (com.intellij.psi.xml.XmlTag)9 XmlAttribute (com.intellij.psi.xml.XmlAttribute)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 HashMap (com.intellij.util.containers.HashMap)2 Map (java.util.Map)2 NotNull (org.jetbrains.annotations.NotNull)2 ViewInfo (com.android.ide.common.rendering.api.ViewInfo)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 Project (com.intellij.openapi.project.Project)1 Computable (com.intellij.openapi.util.Computable)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1 XmlFile (com.intellij.psi.xml.XmlFile)1 XmlText (com.intellij.psi.xml.XmlText)1 Task (com.intellij.tasks.Task)1 BufferedImage (java.awt.image.BufferedImage)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1