Search in sources :

Example 46 with StringReader

use of java.io.StringReader in project androidannotations by androidannotations.

the class SetXmlSerializer method deserialize.

public static Set<String> deserialize(String data) {
    Set<String> stringSet = new TreeSet<>();
    XmlPullParser parser = Xml.newPullParser();
    try {
        parser.setInput(new StringReader(data));
        parser.next();
        parser.require(XmlPullParser.START_TAG, NAMESPACE, SET_TAG);
        while (parser.next() != XmlPullParser.END_TAG) {
            parser.require(XmlPullParser.START_TAG, NAMESPACE, STRING_TAG);
            parser.next();
            parser.require(XmlPullParser.TEXT, null, null);
            stringSet.add(parser.getText());
            parser.next();
            parser.require(XmlPullParser.END_TAG, null, STRING_TAG);
        }
    } catch (XmlPullParserException e) {
        Log.w("getStringSet", e);
        return null;
    } catch (IOException e) {
        Log.w("getStringSet", e);
        return null;
    }
    return stringSet;
}
Also used : TreeSet(java.util.TreeSet) XmlPullParser(org.xmlpull.v1.XmlPullParser) StringReader(java.io.StringReader) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 47 with StringReader

use of java.io.StringReader in project antlr4 by antlr.

the class TestUnbufferedTokenStream method testMarkThenRelease.

@Test
public void testMarkThenRelease() throws Exception {
    LexerGrammar g = new LexerGrammar("lexer grammar t;\n" + "ID : 'a'..'z'+;\n" + "INT : '0'..'9'+;\n" + "SEMI : ';';\n" + "ASSIGN : '=';\n" + "PLUS : '+';\n" + "MULT : '*';\n" + "WS : ' '+;\n");
    // Tokens: 012345678901234567
    // Input:  x = 302;
    CharStream input = new ANTLRInputStream(new StringReader("x = 302 + 1;"));
    LexerInterpreter lexEngine = g.createLexerInterpreter(input);
    TestingUnbufferedTokenStream<Token> tokens = new TestingUnbufferedTokenStream<Token>(lexEngine);
    int m = tokens.mark();
    assertEquals("[[@0,0:0='x',<1>,1:0]]", tokens.getBuffer().toString());
    assertEquals("x", tokens.LT(1).getText());
    // consume x
    tokens.consume();
    assertEquals("[[@0,0:0='x',<1>,1:0], [@1,1:1=' ',<7>,1:1]]", tokens.getBuffer().toString());
    // ' '
    tokens.consume();
    // =
    tokens.consume();
    // ' '
    tokens.consume();
    assertEquals("302", tokens.LT(1).getText());
    // "x = 302" is in buffer. will kill buffer
    tokens.release(m);
    // 302
    tokens.consume();
    // ' '
    tokens.consume();
    // mark at the +
    m = tokens.mark();
    assertEquals("+", tokens.LT(1).getText());
    // '+'
    tokens.consume();
    // ' '
    tokens.consume();
    // 1
    tokens.consume();
    // ;
    tokens.consume();
    assertEquals("<EOF>", tokens.LT(1).getText());
    // we marked at the +, so that should be the start of the buffer
    assertEquals("[[@6,8:8='+',<5>,1:8], [@7,9:9=' ',<7>,1:9]," + " [@8,10:10='1',<2>,1:10], [@9,11:11=';',<3>,1:11]," + " [@10,12:11='<EOF>',<-1>,1:12]]", tokens.getBuffer().toString());
    tokens.release(m);
}
Also used : LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) StringReader(java.io.StringReader) Token(org.antlr.v4.runtime.Token) LexerGrammar(org.antlr.v4.tool.LexerGrammar) CharStream(org.antlr.v4.runtime.CharStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) Test(org.junit.Test)

Example 48 with StringReader

use of java.io.StringReader in project antlr4 by antlr.

the class TestUnbufferedTokenStream method testNoBuffering.

@Test
public void testNoBuffering() throws Exception {
    LexerGrammar g = new LexerGrammar("lexer grammar t;\n" + "ID : 'a'..'z'+;\n" + "INT : '0'..'9'+;\n" + "SEMI : ';';\n" + "ASSIGN : '=';\n" + "PLUS : '+';\n" + "MULT : '*';\n" + "WS : ' '+;\n");
    // Tokens: 012345678901234567
    // Input:  x = 302;
    CharStream input = new ANTLRInputStream(new StringReader("x = 302;"));
    LexerInterpreter lexEngine = g.createLexerInterpreter(input);
    TestingUnbufferedTokenStream<Token> tokens = new TestingUnbufferedTokenStream<Token>(lexEngine);
    assertEquals("[[@0,0:0='x',<1>,1:0]]", tokens.getBuffer().toString());
    assertEquals("x", tokens.LT(1).getText());
    // move to WS
    tokens.consume();
    assertEquals(" ", tokens.LT(1).getText());
    assertEquals("[[@1,1:1=' ',<7>,1:1]]", tokens.getRemainingBuffer().toString());
    tokens.consume();
    assertEquals("=", tokens.LT(1).getText());
    assertEquals("[[@2,2:2='=',<4>,1:2]]", tokens.getRemainingBuffer().toString());
    tokens.consume();
    assertEquals(" ", tokens.LT(1).getText());
    assertEquals("[[@3,3:3=' ',<7>,1:3]]", tokens.getRemainingBuffer().toString());
    tokens.consume();
    assertEquals("302", tokens.LT(1).getText());
    assertEquals("[[@4,4:6='302',<2>,1:4]]", tokens.getRemainingBuffer().toString());
    tokens.consume();
    assertEquals(";", tokens.LT(1).getText());
    assertEquals("[[@5,7:7=';',<3>,1:7]]", tokens.getRemainingBuffer().toString());
}
Also used : LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) StringReader(java.io.StringReader) Token(org.antlr.v4.runtime.Token) LexerGrammar(org.antlr.v4.tool.LexerGrammar) CharStream(org.antlr.v4.runtime.CharStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) Test(org.junit.Test)

Example 49 with StringReader

use of java.io.StringReader in project otter by alibaba.

the class JsonUtilsTest method test_bytes.

@Test
public void test_bytes() {
    Map data = new HashMap<String, byte[]>();
    byte[] one = new byte[] { 1, 2, 3 };
    byte[] two = new byte[] { 4, 5, 6 };
    data.put("one", one);
    data.put("two", two);
    StringWriter jsonStr = new StringWriter();
    //超大文本写入
    JSONWriter writer = new JSONWriter(jsonStr);
    writer.startArray();
    writer.writeValue(one);
    writer.writeValue(two);
    writer.endArray();
    try {
        writer.close();
    } catch (IOException e) {
    }
    JSONReader reader = new JSONReader(new StringReader(jsonStr.getBuffer().toString()));
    byte[] oneDates = null;
    byte[] twoDates = null;
    reader.startArray();
    while (reader.hasNext()) {
        if (oneDates == null) {
            oneDates = reader.readObject(byte[].class);
        } else if (twoDates == null) {
            twoDates = reader.readObject(byte[].class);
        } else {
            want.fail("not possible");
        }
    }
    reader.endArray();
    reader.close();
    check(oneDates, one);
    check(twoDates, two);
}
Also used : JSONWriter(com.alibaba.fastjson.JSONWriter) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) StringReader(java.io.StringReader) IOException(java.io.IOException) JSONReader(com.alibaba.fastjson.JSONReader) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 50 with StringReader

use of java.io.StringReader in project gradle by gradle.

the class EnvJsBrowserEvaluator method evaluate.

public void evaluate(String url, Writer writer) {
    EnvJvEvaluateProtocol evaluator = rhinoWorkerHandleFactory.create(rhinoClasspath, EnvJvEvaluateProtocol.class, EnvJsEvaluateWorker.class, logLevel, workingDir);
    final String result = evaluator.process(new EnvJsEvaluateSpec(envJsFactory.create(), url));
    try {
        IOUtils.copy(new StringReader(result), writer);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : StringReader(java.io.StringReader) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException)

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