Search in sources :

Example 51 with StringReader

use of java.io.StringReader in project j2objc by google.

the class SourceBuilder method reindent.

/**
   * Fix line indention, based on brace count.
   */
public String reindent(String code) {
    try {
        // Remove indention from each line.
        StringBuffer sb = new StringBuffer();
        LineReader lr = new LineReader(new StringReader(code));
        String line = lr.readLine();
        while (line != null) {
            sb.append(line.trim());
            line = lr.readLine();
            if (line != null) {
                sb.append('\n');
            }
        }
        String strippedCode = sb.toString();
        // Now indent it again.
        int indent = indention * DEFAULT_INDENTION;
        // reset buffer
        sb.setLength(0);
        lr = new LineReader(new StringReader(strippedCode));
        line = lr.readLine();
        while (line != null) {
            if (line.startsWith("}")) {
                indent -= DEFAULT_INDENTION;
            }
            if (!line.startsWith("#line")) {
                sb.append(pad(indent));
            }
            sb.append(line);
            if (line.endsWith("{")) {
                indent += DEFAULT_INDENTION;
            }
            line = lr.readLine();
            if (line != null) {
                sb.append('\n');
            }
        }
        return sb.toString();
    } catch (IOException e) {
        // Should never happen with string readers.
        throw new AssertionError(e);
    }
}
Also used : LineReader(com.google.common.io.LineReader) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 52 with StringReader

use of java.io.StringReader in project groovy-core by groovy.

the class SourceBufferTest method getSourceBuffer.

private SourceBuffer getSourceBuffer(String text) throws Exception {
    SourceBuffer buffer = new SourceBuffer();
    Reader reader = new UnicodeEscapingReader(new StringReader(text), buffer);
    while (reader.read() != -1) {
    // empty loop
    // - read all characters till the end of the reader
    // UnicodeEscapingReader has side effects of
    // filling the buffer
    }
    return buffer;
}
Also used : StringReader(java.io.StringReader) StringReader(java.io.StringReader) Reader(java.io.Reader)

Example 53 with StringReader

use of java.io.StringReader in project groovy-core by groovy.

the class CompositeVisitorTest method assertCompositeTransparency.

private void assertCompositeTransparency(String input) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GroovyRecognizer parser;
    SourceBuffer sourceBuffer = new SourceBuffer();
    UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(new StringReader(input), sourceBuffer);
    GroovyLexer lexer = new GroovyLexer(unicodeReader);
    unicodeReader.setLexer(lexer);
    parser = GroovyRecognizer.make(lexer);
    parser.setSourceBuffer(sourceBuffer);
    String[] tokenNames = parser.getTokenNames();
    parser.compilationUnit();
    AST ast = parser.getAST();
    // determine direct result
    Visitor directVisitor = new SourcePrinter(new PrintStream(baos), tokenNames, false);
    AntlrASTProcessor traverser = new SourceCodeTraversal(directVisitor);
    traverser.process(ast);
    String directResult = new String(baos.toByteArray());
    // determine composite result
    baos.reset();
    List wrappedVisitors = new ArrayList();
    wrappedVisitors.add(directVisitor);
    Visitor compositeVisitor = new CompositeVisitor(wrappedVisitors);
    traverser = new SourceCodeTraversal(compositeVisitor);
    traverser.process(ast);
    String compositeResult = new String(baos.toByteArray());
    assertEquals(directResult, compositeResult);
}
Also used : PrintStream(java.io.PrintStream) AST(antlr.collections.AST) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SourceBuffer(org.codehaus.groovy.antlr.SourceBuffer) AntlrASTProcessor(org.codehaus.groovy.antlr.AntlrASTProcessor) UnicodeEscapingReader(org.codehaus.groovy.antlr.UnicodeEscapingReader) GroovyLexer(org.codehaus.groovy.antlr.parser.GroovyLexer) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List) GroovyRecognizer(org.codehaus.groovy.antlr.parser.GroovyRecognizer)

Example 54 with StringReader

use of java.io.StringReader in project groovy-core by groovy.

the class TraversalTestHelper method traverse.

// todo - the visitor doesn't always take PrintStreams as constructor params!  Could be a more reusable implementation than this...
public String traverse(String input, Class visitorClass, Boolean extraParam) throws Exception {
    if (!Visitor.class.isAssignableFrom(visitorClass)) {
        throw new RuntimeException("Invalid class for traversal: " + visitorClass.getName());
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GroovyRecognizer parser;
    SourceBuffer sourceBuffer = new SourceBuffer();
    UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(new StringReader(input), sourceBuffer);
    GroovyLexer lexer = new GroovyLexer(unicodeReader);
    unicodeReader.setLexer(lexer);
    parser = GroovyRecognizer.make(lexer);
    parser.setSourceBuffer(sourceBuffer);
    String[] tokenNames = parser.getTokenNames();
    parser.compilationUnit();
    AST ast = parser.getAST();
    Class[] paramTypes;
    Object[] params;
    if (extraParam == null) {
        paramTypes = new Class[] { PrintStream.class, String[].class };
        params = new Object[] { new PrintStream(baos), tokenNames };
    } else {
        paramTypes = new Class[] { PrintStream.class, String[].class, Boolean.TYPE };
        params = new Object[] { new PrintStream(baos), tokenNames, extraParam };
    }
    Constructor constructor = visitorClass.getConstructor(paramTypes);
    Visitor visitor = (Visitor) constructor.newInstance(params);
    AntlrASTProcessor traverser = new SourceCodeTraversal(visitor);
    traverser.process(ast);
    return new String(baos.toByteArray());
}
Also used : PrintStream(java.io.PrintStream) AST(antlr.collections.AST) Constructor(java.lang.reflect.Constructor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SourceBuffer(org.codehaus.groovy.antlr.SourceBuffer) AntlrASTProcessor(org.codehaus.groovy.antlr.AntlrASTProcessor) UnicodeEscapingReader(org.codehaus.groovy.antlr.UnicodeEscapingReader) GroovyLexer(org.codehaus.groovy.antlr.parser.GroovyLexer) StringReader(java.io.StringReader) GroovyRecognizer(org.codehaus.groovy.antlr.parser.GroovyRecognizer)

Example 55 with StringReader

use of java.io.StringReader in project groovy-core by groovy.

the class AnnotationSourceParsingTest method testMultiLineAttributes.

public void testMultiLineAttributes() {
    StringReader reader = new StringReader("class OtherSection\n" + "{\n" + "    @CollectionOfElements\n" + "    @JoinTable\n" + "    (\n" + "        table=@Table(name=\"gaga\"),\n" + "        joinColumns = @JoinColumn(name=\"BoyId\")\n" + "    )\n" + "    @Column(name=\"favoritepoupon\", \n" + "nullable=false)\n" + "    Set<String> questions = new HashSet<String> ()\n\n" + "}");
    parse("testMultiLineAttributes", reader);
}
Also used : StringReader(java.io.StringReader)

Aggregations

StringReader (java.io.StringReader)4150 Test (org.junit.Test)1003 IOException (java.io.IOException)589 Reader (java.io.Reader)445 InputSource (org.xml.sax.InputSource)408 BufferedReader (java.io.BufferedReader)342 TokenStream (org.apache.lucene.analysis.TokenStream)302 ArrayList (java.util.ArrayList)273 StringWriter (java.io.StringWriter)251 Tokenizer (org.apache.lucene.analysis.Tokenizer)241 Document (org.w3c.dom.Document)232 JSONReader (com.alibaba.fastjson.JSONReader)195 DocumentBuilder (javax.xml.parsers.DocumentBuilder)180 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)157 Map (java.util.Map)144 HashMap (java.util.HashMap)136 Element (org.w3c.dom.Element)134 StreamSource (javax.xml.transform.stream.StreamSource)132 ParserResult (org.jabref.logic.importer.ParserResult)130 MockTokenizer (org.apache.lucene.analysis.MockTokenizer)120