Search in sources :

Example 1 with XmlRecursiveElementVisitor

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

the class XmlHighlightVisitorBasedInspection method checkFile.

@Override
public void checkFile(@NotNull final PsiFile file, @NotNull final InspectionManager manager, @NotNull ProblemsHolder problemsHolder, @NotNull final GlobalInspectionContext globalContext, @NotNull final ProblemDescriptionsProcessor problemDescriptionsProcessor) {
    HighlightInfoHolder myHolder = new HighlightInfoHolder(file) {

        @Override
        public boolean add(@Nullable HighlightInfo info) {
            if (info != null) {
                GlobalInspectionUtil.createProblem(file, info, new TextRange(info.startOffset, info.endOffset), null, manager, problemDescriptionsProcessor, globalContext);
            }
            return true;
        }
    };
    final XmlHighlightVisitor highlightVisitor = new XmlHighlightVisitor();
    highlightVisitor.analyze(file, true, myHolder, new Runnable() {

        @Override
        public void run() {
            file.accept(new XmlRecursiveElementVisitor() {

                @Override
                public void visitElement(PsiElement element) {
                    highlightVisitor.visit(element);
                    super.visitElement(element);
                }
            });
        }
    });
}
Also used : XmlRecursiveElementVisitor(com.intellij.psi.XmlRecursiveElementVisitor) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) TextRange(com.intellij.openapi.util.TextRange) Nullable(org.jetbrains.annotations.Nullable) PsiElement(com.intellij.psi.PsiElement)

Example 2 with XmlRecursiveElementVisitor

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

the class PsiDocumentNavigator method getElementById.

public Object getElementById(Object object, final String elementId) {
    final XmlTag rootTag = ((XmlFile) ((XmlElement) object).getContainingFile()).getRootTag();
    if (rootTag == null) {
        return null;
    }
    final Ref<XmlTag> ref = new Ref<>();
    rootTag.accept(new XmlRecursiveElementVisitor() {

        @Override
        public void visitElement(PsiElement element) {
            if (ref.get() == null) {
                super.visitElement(element);
            }
        }

        @Override
        public void visitXmlAttribute(XmlAttribute attribute) {
            final XmlAttributeDescriptor descriptor = attribute.getDescriptor();
            final String value = attribute.getValue();
            if ((value != null && (descriptor != null && descriptor.hasIdType()))) {
                if (elementId.equals(value)) {
                    ref.set(attribute.getParent());
                }
            }
        }
    });
    return ref.get();
}
Also used : Ref(com.intellij.openapi.util.Ref) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) XmlRecursiveElementVisitor(com.intellij.psi.XmlRecursiveElementVisitor) PsiElement(com.intellij.psi.PsiElement)

Example 3 with XmlRecursiveElementVisitor

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

the class MicrodataUtil method findScopesWithItemRef.

private static Map<String, XmlTag> findScopesWithItemRef(@Nullable final PsiFile file) {
    if (!(file instanceof XmlFile))
        return Collections.emptyMap();
    return CachedValuesManager.getCachedValue(file, new CachedValueProvider<Map<String, XmlTag>>() {

        @Nullable
        @Override
        public Result<Map<String, XmlTag>> compute() {
            final Map<String, XmlTag> result = new THashMap<>();
            file.accept(new XmlRecursiveElementVisitor() {

                @Override
                public void visitXmlTag(final XmlTag tag) {
                    super.visitXmlTag(tag);
                    XmlAttribute refAttr = tag.getAttribute(ITEM_REF);
                    if (refAttr != null && tag.getAttribute(ITEM_SCOPE) != null) {
                        getReferencesForAttributeValue(refAttr.getValueElement(), (t, v) -> {
                            result.put(t, tag);
                            return null;
                        });
                    }
                }
            });
            return Result.create(result, file);
        }
    });
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XmlRecursiveElementVisitor(com.intellij.psi.XmlRecursiveElementVisitor) THashMap(gnu.trove.THashMap) Nullable(org.jetbrains.annotations.Nullable) XmlTag(com.intellij.psi.xml.XmlTag)

Example 4 with XmlRecursiveElementVisitor

use of com.intellij.psi.XmlRecursiveElementVisitor in project intellij-plugins by JetBrains.

the class DartPackageAwareFileIncludeProvider method getIncludeInfos.

private static FileIncludeInfo[] getIncludeInfos(@NotNull final XmlFile xmlFile) {
    final List<FileIncludeInfo> result = new ArrayList<>();
    xmlFile.acceptChildren(new XmlRecursiveElementVisitor() {

        @Override
        public void visitXmlTag(XmlTag tag) {
            final String path = "link".equalsIgnoreCase(tag.getName()) ? getPathRelativeToPackageRoot(tag.getAttributeValue("href")) : "script".equalsIgnoreCase(tag.getName()) ? getPathRelativeToPackageRoot(tag.getAttributeValue("src")) : null;
            if (!StringUtil.isEmptyOrSpaces(path)) {
                result.add(new FileIncludeInfo(path));
            }
            super.visitXmlTag(tag);
        }

        @Override
        public void visitElement(PsiElement element) {
            if (element.getLanguage() instanceof XMLLanguage) {
                super.visitElement(element);
            }
        }
    });
    return ContainerUtil.toArray(result, FileIncludeInfo.EMPTY);
}
Also used : FileIncludeInfo(com.intellij.psi.impl.include.FileIncludeInfo) XmlRecursiveElementVisitor(com.intellij.psi.XmlRecursiveElementVisitor) ArrayList(java.util.ArrayList) XMLLanguage(com.intellij.lang.xml.XMLLanguage) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 5 with XmlRecursiveElementVisitor

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

the class Xsd2InstanceUtils method processAndSaveAllSchemas.

public static String processAndSaveAllSchemas(@NotNull XmlFile file, @NotNull final Map<String, String> scannedToFileName, @NotNull final SchemaReferenceProcessor schemaReferenceProcessor) {
    final String fileName = file.getName();
    String previous = scannedToFileName.get(fileName);
    if (previous != null)
        return previous;
    scannedToFileName.put(fileName, fileName);
    final StringBuilder result = new StringBuilder();
    file.acceptChildren(new XmlRecursiveElementVisitor() {

        @Override
        public void visitElement(PsiElement psiElement) {
            super.visitElement(psiElement);
            if (psiElement instanceof LeafPsiElement) {
                final String text = psiElement.getText();
                result.append(text);
            }
        }

        @Override
        public void visitXmlAttribute(XmlAttribute xmlAttribute) {
            boolean replaced = false;
            if (xmlAttribute.isNamespaceDeclaration()) {
                replaced = true;
                final String value = xmlAttribute.getValue();
                result.append(xmlAttribute.getText()).append(" ");
                if (!scannedToFileName.containsKey(value)) {
                    final XmlNSDescriptor nsDescriptor = xmlAttribute.getParent().getNSDescriptor(value, true);
                    if (nsDescriptor != null) {
                        processAndSaveAllSchemas(nsDescriptor.getDescriptorFile(), scannedToFileName, schemaReferenceProcessor);
                    }
                }
            } else if ("schemaLocation".equals(xmlAttribute.getName())) {
                final PsiReference[] references = xmlAttribute.getValueElement().getReferences();
                if (references.length > 0) {
                    PsiElement psiElement = references[0].resolve();
                    if (psiElement instanceof XmlFile) {
                        final String s = processAndSaveAllSchemas(((XmlFile) psiElement), scannedToFileName, schemaReferenceProcessor);
                        if (s != null) {
                            result.append(xmlAttribute.getName()).append("='").append(s).append('\'');
                            replaced = true;
                        }
                    }
                }
            }
            if (!replaced)
                result.append(xmlAttribute.getText());
        }
    });
    final VirtualFile virtualFile = file.getVirtualFile();
    final String content = result.toString();
    byte[] bytes;
    if (virtualFile != null) {
        bytes = content.getBytes(virtualFile.getCharset());
    } else {
        try {
            final String charsetName = XmlUtil.extractXmlEncodingFromProlog(content.getBytes());
            bytes = charsetName != null ? content.getBytes(charsetName) : content.getBytes();
        } catch (UnsupportedEncodingException e) {
            bytes = content.getBytes();
        }
    }
    schemaReferenceProcessor.processSchema(fileName, bytes);
    return fileName;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XmlRecursiveElementVisitor(com.intellij.psi.XmlRecursiveElementVisitor) PsiReference(com.intellij.psi.PsiReference) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiElement(com.intellij.psi.PsiElement)

Aggregations

XmlRecursiveElementVisitor (com.intellij.psi.XmlRecursiveElementVisitor)8 PsiElement (com.intellij.psi.PsiElement)5 XmlTag (com.intellij.psi.xml.XmlTag)5 XmlAttribute (com.intellij.psi.xml.XmlAttribute)4 PsiReference (com.intellij.psi.PsiReference)3 ArrayList (java.util.ArrayList)3 Result (com.intellij.openapi.application.Result)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 SchemaPrefixReference (com.intellij.psi.impl.source.xml.SchemaPrefixReference)2 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)2 XmlFile (com.intellij.psi.xml.XmlFile)2 Nullable (org.jetbrains.annotations.Nullable)2 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 XMLLanguage (com.intellij.lang.xml.XMLLanguage)1 InputValidator (com.intellij.openapi.ui.InputValidator)1 Ref (com.intellij.openapi.util.Ref)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 FileIncludeInfo (com.intellij.psi.impl.include.FileIncludeInfo)1 TypeOrElementOrAttributeReference (com.intellij.psi.impl.source.resolve.reference.impl.providers.TypeOrElementOrAttributeReference)1