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