Search in sources :

Example 26 with LeafPsiElement

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

Example 27 with LeafPsiElement

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);
}
Also used : LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Test(org.junit.Test)

Aggregations

LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)27 PsiElement (com.intellij.psi.PsiElement)13 NotNull (org.jetbrains.annotations.NotNull)5 Nullable (org.jetbrains.annotations.Nullable)4 TextRange (com.intellij.openapi.util.TextRange)2 IElementType (com.intellij.psi.tree.IElementType)2 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)2 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)2 Test (org.junit.Test)2 ASTNode (com.intellij.lang.ASTNode)1 Language (com.intellij.lang.Language)1 Annotation (com.intellij.lang.annotation.Annotation)1 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)1 NamedFoldingDescriptor (com.intellij.lang.folding.NamedFoldingDescriptor)1 JSAttribute (com.intellij.lang.javascript.psi.ecmal4.JSAttribute)1 JSAttributeList (com.intellij.lang.javascript.psi.ecmal4.JSAttributeList)1 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)1 Document (com.intellij.openapi.editor.Document)1 TextAttributesKey (com.intellij.openapi.editor.colors.TextAttributesKey)1 Project (com.intellij.openapi.project.Project)1