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