Search in sources :

Example 41 with XmlFile

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

the class DomManagerImpl method createMockElement.

@Override
public final <T extends DomElement> T createMockElement(final Class<T> aClass, final Module module, final boolean physical) {
    final XmlFile file = (XmlFile) PsiFileFactory.getInstance(myProject).createFileFromText("a.xml", StdFileTypes.XML, "", (long) 0, physical);
    file.putUserData(MOCK_ELEMENT_MODULE, module);
    file.putUserData(MOCK, new Object());
    return getFileElement(file, aClass, "I_sincerely_hope_that_nobody_will_have_such_a_root_tag_name").getRootElement();
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile)

Example 42 with XmlFile

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

the class DomRootInvocationHandler method setEmptyXmlTag.

@Override
protected XmlTag setEmptyXmlTag() {
    final XmlTag[] result = new XmlTag[] { null };
    getManager().runChange(() -> {
        try {
            final String namespace = getXmlElementNamespace();
            @NonNls final String nsDecl = StringUtil.isEmpty(namespace) ? "" : " xmlns=\"" + namespace + "\"";
            final XmlFile xmlFile = getFile();
            final XmlTag tag = XmlElementFactory.getInstance(xmlFile.getProject()).createTagFromText("<" + getXmlElementName() + nsDecl + "/>");
            result[0] = ((XmlDocument) xmlFile.getDocument().replace(((XmlFile) tag.getContainingFile()).getDocument())).getRootTag();
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    });
    return result[0];
}
Also used : NonNls(org.jetbrains.annotations.NonNls) XmlFile(com.intellij.psi.xml.XmlFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlTag(com.intellij.psi.xml.XmlTag)

Example 43 with XmlFile

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

the class IndexedElementInvocationHandler method setEmptyXmlTag.

@Override
protected XmlTag setEmptyXmlTag() {
    final DomInvocationHandler parent = getParentHandler();
    assert parent != null : "write operations should be performed on the DOM having a parent, your DOM may be not very fresh";
    final FixedChildDescriptionImpl description = getChildDescription();
    final XmlFile xmlFile = getFile();
    parent.createFixedChildrenTags(getXmlName(), description, myIndex);
    final List<XmlTag> tags = DomImplUtil.findSubTags(parent.getXmlTag(), getXmlName(), xmlFile);
    if (tags.size() > myIndex) {
        return tags.get(myIndex);
    }
    final XmlTag[] newTag = new XmlTag[1];
    getManager().runChange(() -> {
        try {
            final XmlTag parentTag = parent.getXmlTag();
            newTag[0] = (XmlTag) parentTag.add(parent.createChildTag(getXmlName()));
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    });
    return newTag[0];
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlTag(com.intellij.psi.xml.XmlTag)

Example 44 with XmlFile

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

the class XmlNamespaceIndex method guessDtd.

@Nullable
public static XmlFile guessDtd(String dtdUri, @NotNull PsiFile baseFile) {
    if (!dtdUri.endsWith(".dtd") || DumbService.isDumb(baseFile.getProject()) || XmlUtil.isStubBuilding())
        return null;
    String dtdFileName = new File(dtdUri).getName();
    List<IndexedRelevantResource<String, XsdNamespaceBuilder>> list = getResourcesByNamespace(dtdFileName, baseFile.getProject(), ModuleUtilCore.findModuleForPsiElement(baseFile));
    if (list.isEmpty()) {
        return null;
    }
    IndexedRelevantResource<String, XsdNamespaceBuilder> resource;
    if (list.size() > 1) {
        final String[] split = dtdUri.split("/");
        resource = Collections.max(list, new Comparator<IndexedRelevantResource<String, XsdNamespaceBuilder>>() {

            @Override
            public int compare(IndexedRelevantResource<String, XsdNamespaceBuilder> o1, IndexedRelevantResource<String, XsdNamespaceBuilder> o2) {
                return weight(o1) - weight(o2);
            }

            int weight(IndexedRelevantResource<String, XsdNamespaceBuilder> o1) {
                VirtualFile file = o1.getFile();
                for (int i = split.length - 1; i >= 0 && file != null; i--) {
                    String s = split[i];
                    if (!s.equals(file.getName())) {
                        return split.length - i;
                    }
                    file = file.getParent();
                }
                return 0;
            }
        });
    } else {
        resource = list.get(0);
    }
    return findSchemaFile(resource.getFile(), baseFile);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) Nullable(org.jetbrains.annotations.Nullable)

Example 45 with XmlFile

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

the class HtmlPsiUtil method getRealXmlDocument.

@Nullable
public static XmlDocument getRealXmlDocument(@Nullable XmlDocument doc) {
    if (doc == null)
        return null;
    final PsiFile containingFile = doc.getContainingFile();
    final PsiFile templateFile = TemplateLanguageUtil.getTemplateFile(containingFile);
    if (templateFile instanceof XmlFile) {
        return ((XmlFile) templateFile).getDocument();
    }
    return doc;
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) PsiFile(com.intellij.psi.PsiFile) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

XmlFile (com.intellij.psi.xml.XmlFile)409 XmlTag (com.intellij.psi.xml.XmlTag)155 PsiFile (com.intellij.psi.PsiFile)121 VirtualFile (com.intellij.openapi.vfs.VirtualFile)102 Nullable (org.jetbrains.annotations.Nullable)74 Project (com.intellij.openapi.project.Project)69 NotNull (org.jetbrains.annotations.NotNull)66 PsiElement (com.intellij.psi.PsiElement)64 XmlAttribute (com.intellij.psi.xml.XmlAttribute)39 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)34 Module (com.intellij.openapi.module.Module)34 XmlDocument (com.intellij.psi.xml.XmlDocument)32 Result (com.intellij.openapi.application.Result)28 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)23 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)22 XmlNSDescriptor (com.intellij.xml.XmlNSDescriptor)21 ArrayList (java.util.ArrayList)20 Document (com.intellij.openapi.editor.Document)19 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)18 Editor (com.intellij.openapi.editor.Editor)15