Search in sources :

Example 1 with TreeAppendable

use of org.eclipse.xtext.xbase.compiler.output.TreeAppendable in project xtext-xtend by eclipse.

the class UnicodeAwarePostProcessorTest method testUTF8_tree.

@Test
public void testUTF8_tree() {
    TreeAppendable treeAppendable = new TreeAppendable(null, null, this, this, EcoreFactory.eINSTANCE.createEObject(), "  ", "\n");
    treeAppendable.append("\u03b1\u03c1\u03b5\u03c4\u03b7");
    CharSequence result = postProcessor.postProcess(URI.createFileURI("com/acme/MyFile.java"), treeAppendable, Charsets.UTF_8);
    assertSame(treeAppendable, result);
    assertEquals("\u03b1\u03c1\u03b5\u03c4\u03b7", result.toString());
}
Also used : TreeAppendable(org.eclipse.xtext.xbase.compiler.output.TreeAppendable) Test(org.junit.Test)

Example 2 with TreeAppendable

use of org.eclipse.xtext.xbase.compiler.output.TreeAppendable in project xtext-xtend by eclipse.

the class UnicodeAwarePostProcessor method postProcess.

@Override
public CharSequence postProcess(URI fileURI, CharSequence content, /* @Nullable */
Charset targetCharset) {
    if (targetCharset != null && "java".equalsIgnoreCase(fileURI.fileExtension())) {
        final String lineSeparator = getWhitespaceInformationProvider().getLineSeparatorInformation(fileURI).getLineSeparator();
        final CharsetEncoder encoder = targetCharset.newEncoder();
        if (content instanceof TreeAppendable) {
            return ((TreeAppendable) content).acceptVisitor(new TreeAppendable.Visitor() {

                @Override
                protected /* @NonNull */
                String visit(/* @NonNull */
                String string) {
                    return replaceLineSeparatorsAndEscapeChars(string, lineSeparator, encoder);
                }
            });
        } else {
            String result = replaceLineSeparatorsAndEscapeChars(content, lineSeparator, encoder);
            return result;
        }
    } else {
        return postProcess(fileURI, content);
    }
}
Also used : TreeAppendable(org.eclipse.xtext.xbase.compiler.output.TreeAppendable) CharsetEncoder(java.nio.charset.CharsetEncoder)

Example 3 with TreeAppendable

use of org.eclipse.xtext.xbase.compiler.output.TreeAppendable in project xtext-xtend by eclipse.

the class UnicodeAwarePostProcessorTest method testISO_8859_1_tree.

@Test
public void testISO_8859_1_tree() {
    TreeAppendable treeAppendable = new TreeAppendable(null, null, this, this, EcoreFactory.eINSTANCE.createEObject(), "  ", "\n");
    treeAppendable.append("\u03b1\u03c1\u03b5\u03c4\u03b7");
    CharSequence result = postProcessor.postProcess(URI.createFileURI("com/acme/MyFile.java"), treeAppendable, Charsets.ISO_8859_1);
    assertSame(treeAppendable, result);
    assertEquals("\\u03b1\\u03c1\\u03b5\\u03c4\\u03b7", result.toString());
}
Also used : TreeAppendable(org.eclipse.xtext.xbase.compiler.output.TreeAppendable) Test(org.junit.Test)

Aggregations

TreeAppendable (org.eclipse.xtext.xbase.compiler.output.TreeAppendable)3 Test (org.junit.Test)2 CharsetEncoder (java.nio.charset.CharsetEncoder)1