use of com.intellij.psi.impl.source.tree.LeafPsiElement 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;
}
use of com.intellij.psi.impl.source.tree.LeafPsiElement in project Intellij-Plugin by getgauge.
the class SpecInspectionProviderTest method testGetElementReturnsSpecHeading.
@Test
public void testGetElementReturnsSpecHeading() throws Exception {
PsiElement e = mock(PsiElement.class);
LeafPsiElement leafPsiElement = mock(LeafPsiElement.class);
when(leafPsiElement.getElementType()).thenReturn(SpecTokenTypes.SPEC_HEADING);
when(e.getParent()).thenReturn(leafPsiElement);
PsiElement element = new SpecInspectionProvider().getElement(e);
assertEquals(leafPsiElement, element);
}
Aggregations