Search in sources :

Example 41 with XmlDocument

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

the class ElementProcessor method processExternalFile.

public void processExternalFile(PsiFile psiFile, XmlTag place) {
    final XmlDocument document = ((XmlFile) psiFile).getDocument();
    assert document != null;
    final XmlTag rootTag = document.getRootTag();
    assert rootTag != null;
    myInclude++;
    try {
        rootTag.processElements(new PsiElementProcessor() {

            public boolean execute(@NotNull PsiElement element) {
                if (element instanceof XmlTag) {
                    return process((XmlTag) element);
                }
                return shouldContinue();
            }
        }, place);
    } finally {
        myInclude--;
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) XmlDocument(com.intellij.psi.xml.XmlDocument) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 42 with XmlDocument

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

the class JavaFxUnusedImportsInspection method checkFile.

@Nullable
@Override
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, final boolean isOnTheFly) {
    if (!JavaFxFileTypeFactory.isFxml(file))
        return null;
    final XmlDocument document = ((XmlFile) file).getDocument();
    if (document == null)
        return null;
    final Set<String> usedNames = new HashSet<>();
    file.accept(new JavaFxImportsOptimizer.JavaFxUsedClassesVisitor() {

        @Override
        protected void appendClassName(String fqn) {
            usedNames.add(fqn);
            final String packageName = StringUtil.getPackageName(fqn);
            if (!StringUtil.isEmpty(packageName)) {
                usedNames.add(packageName);
            }
        }

        @Override
        protected void appendDemandedPackageName(@NotNull String packageName) {
            usedNames.add(packageName);
        }
    });
    final InspectionManager inspectionManager = InspectionManager.getInstance(file.getProject());
    final List<ProblemDescriptor> problems = new ArrayList<>();
    final Collection<XmlProcessingInstruction> instructions = PsiTreeUtil.findChildrenOfType(document.getProlog(), XmlProcessingInstruction.class);
    final Map<String, XmlProcessingInstruction> targetProcessingInstructions = new LinkedHashMap<>();
    for (XmlProcessingInstruction instruction : instructions) {
        final String target = JavaFxPsiUtil.getInstructionTarget("import", instruction);
        if (target != null) {
            targetProcessingInstructions.put(target, instruction);
        }
    }
    for (String target : targetProcessingInstructions.keySet()) {
        final XmlProcessingInstruction instruction = targetProcessingInstructions.get(target);
        if (target.endsWith(".*")) {
            if (!usedNames.contains(StringUtil.trimEnd(target, ".*"))) {
                problems.add(inspectionManager.createProblemDescriptor(instruction, "Unused import", new JavaFxOptimizeImportsFix(), ProblemHighlightType.LIKE_UNUSED_SYMBOL, isOnTheFly));
            }
        } else if (!usedNames.contains(target) || targetProcessingInstructions.containsKey(StringUtil.getPackageName(target) + ".*")) {
            problems.add(inspectionManager.createProblemDescriptor(instruction, "Unused import", new JavaFxOptimizeImportsFix(), ProblemHighlightType.LIKE_UNUSED_SYMBOL, isOnTheFly));
        }
    }
    return problems.isEmpty() ? null : problems.toArray(new ProblemDescriptor[problems.size()]);
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) JavaFxImportsOptimizer(org.jetbrains.plugins.javaFX.fxml.codeInsight.JavaFxImportsOptimizer) XmlDocument(com.intellij.psi.xml.XmlDocument) XmlProcessingInstruction(com.intellij.psi.xml.XmlProcessingInstruction) Nullable(org.jetbrains.annotations.Nullable)

Example 43 with XmlDocument

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

the class MavenPropertyPsiReference method processSchema.

@Nullable
private <T> T processSchema(String schema, SchemaProcessor<T> processor) {
    VirtualFile file = MavenSchemaProvider.getSchemaFile(schema);
    PsiFile psiFile = PsiManager.getInstance(myProject).findFile(file);
    if (!(psiFile instanceof XmlFile))
        return null;
    XmlFile xmlFile = (XmlFile) psiFile;
    XmlDocument document = xmlFile.getDocument();
    XmlNSDescriptor desc = (XmlNSDescriptor) document.getMetaData();
    XmlElementDescriptor[] descriptors = desc.getRootElementsDescriptors(document);
    return doProcessSchema(descriptors, null, processor, new THashSet<>());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) XmlDocument(com.intellij.psi.xml.XmlDocument) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) Nullable(org.jetbrains.annotations.Nullable)

Example 44 with XmlDocument

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

the class RngNsDescriptor method validate.

@Override
public void validate(@NotNull PsiElement context, @NotNull final ValidationHost host) {
    final XmlDocument doc = PsiTreeUtil.getContextOfType(context, XmlDocument.class, false);
    if (doc == null) {
        return;
    }
    final XmlTag rootTag = doc.getRootTag();
    if (rootTag == null) {
        return;
    }
    // RNG XML itself is validated by parsing it with Jing, so we don't want to schema-validate it
    if (!ApplicationLoader.RNG_NAMESPACE.equals(rootTag.getNamespace())) {
        XmlInstanceValidator.doValidation(doc, host, getDescriptorFile());
    }
}
Also used : XmlDocument(com.intellij.psi.xml.XmlDocument) XmlTag(com.intellij.psi.xml.XmlTag)

Example 45 with XmlDocument

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

the class ConvertSchemaAction method getInputType.

private static SchemaType getInputType(Project project, VirtualFile... files) {
    if (files.length == 0)
        return null;
    final VirtualFile file = files[0];
    final FileType type = file.getFileType();
    if (type == StdFileTypes.XML) {
        final PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
        if (psiFile instanceof XmlFile) {
            final XmlDocument document = ((XmlFile) psiFile).getDocument();
            if (document != null && document.getRootTag() != null) {
                final XmlTag rootTag = document.getRootTag();
                assert rootTag != null;
                final String uri = rootTag.getNamespace();
                if (ApplicationLoader.RNG_NAMESPACE.equals(uri) && files.length == 1) {
                    return SchemaType.RNG;
                }
            }
        }
        if (files.length > 1) {
            for (VirtualFile virtualFile : files) {
                if (virtualFile.getFileType() != StdFileTypes.XML || getInputType(project, virtualFile) != null) {
                    return null;
                }
            }
        }
        return SchemaType.XML;
    } else if (type == StdFileTypes.DTD && files.length == 1) {
        return SchemaType.DTD;
    } else if (type == RncFileType.getInstance() && files.length == 1) {
        return SchemaType.RNC;
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) RncFileType(org.intellij.plugins.relaxNG.compact.RncFileType) FileType(com.intellij.openapi.fileTypes.FileType) PsiFile(com.intellij.psi.PsiFile) XmlDocument(com.intellij.psi.xml.XmlDocument) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

XmlDocument (com.intellij.psi.xml.XmlDocument)57 XmlTag (com.intellij.psi.xml.XmlTag)39 XmlFile (com.intellij.psi.xml.XmlFile)32 PsiElement (com.intellij.psi.PsiElement)13 Nullable (org.jetbrains.annotations.Nullable)11 XmlNSDescriptor (com.intellij.xml.XmlNSDescriptor)9 PsiFile (com.intellij.psi.PsiFile)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 ArrayList (java.util.ArrayList)6 XmlAttribute (com.intellij.psi.xml.XmlAttribute)5 PsiMetaData (com.intellij.psi.meta.PsiMetaData)4 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 NotNull (org.jetbrains.annotations.NotNull)4 Project (com.intellij.openapi.project.Project)3 XmlText (com.intellij.psi.xml.XmlText)3 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)3 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)2 Editor (com.intellij.openapi.editor.Editor)2 PsiErrorElement (com.intellij.psi.PsiErrorElement)2 FilterElementProcessor (com.intellij.psi.scope.processor.FilterElementProcessor)2