/** {@inheritDoc} */ @Override public XGModelComposition parse(byte[] mdl) { try (ByteArrayInputStream bais = new ByteArrayInputStream(mdl)) { CharStream cStream = CharStreams.fromStream(bais); XGBoostModelLexer lexer = new XGBoostModelLexer(cStream); CommonTokenStream tokens = new CommonTokenStream(lexer); XGBoostModelParser parser = new XGBoostModelParser(tokens); XGModelVisitor visitor = new XGModelVisitor(); return visitor.visit(parser.xgModel()); } catch (IOException e) { throw new RuntimeException(e); } } }
/** * Creates a {@link CharStream} given an opened {@link InputStream} * containing UTF-8 bytes. * * Reads the entire contents of the {@code InputStream} into * the result before returning, then closes the {@code InputStream}. */ public static CharStream fromStream(InputStream is) throws IOException { return fromStream(is, StandardCharsets.UTF_8); }
/** * Creates a {@link CharStream} given an opened {@link InputStream} and the * charset of the bytes contained in the stream. * * Reads the entire contents of the {@code InputStream} into * the result before returning, then closes the {@code InputStream}. */ public static CharStream fromStream(InputStream is, Charset charset) throws IOException { return fromStream(is, charset, -1); }
/** * Creates a {@link CharStream} given an opened {@link InputStream} * containing UTF-8 bytes. * * Reads the entire contents of the {@code InputStream} into * the result before returning, then closes the {@code InputStream}. */ public static CharStream fromStream(InputStream is) throws IOException { return fromStream(is, StandardCharsets.UTF_8); }
/** * Creates a {@link CharStream} given an opened {@link InputStream} and the * charset of the bytes contained in the stream. * * Reads the entire contents of the {@code InputStream} into * the result before returning, then closes the {@code InputStream}. */ public static CharStream fromStream(InputStream is, Charset charset) throws IOException { return fromStream(is, charset, -1); }
/** * Creates a {@link CharStream} given an opened {@link InputStream} and the * charset of the bytes contained in the stream. * * Reads the entire contents of the {@code InputStream} into * the result before returning, then closes the {@code InputStream}. */ public static CharStream fromStream(InputStream is, Charset charset) throws IOException { return fromStream(is, charset, IntStream.UNKNOWN_SOURCE_NAME, -1); }
/** * Creates a {@link CharStream} given an opened {@link InputStream} * containing UTF-8 bytes. * * Reads the entire contents of the {@code InputStream} into * the result before returning, then closes the {@code InputStream}. */ public static CharStream fromStream(InputStream is) throws IOException { return fromStream(is, Charset.forName("UTF-8")); }
/** * get the CharStream for the given text * * @param text * @return the CharStream * @throws IOException */ public static CharStream streamForText(String text) throws IOException { InputStream stream = new ByteArrayInputStream( text.getBytes(StandardCharsets.UTF_8)); in = CharStreams.fromStream(stream); return in; }
/** * Creates a {@link CharStream} given a path to a file on disk and the * charset of the bytes contained in the file. * * Reads the entire contents of the file into the result before returning. */ public static CharStream fromFile(File file, Charset charset) throws IOException { long size = file.length(); return fromStream(new FileInputStream(file), charset, file.toString(), size); }
@Override public List<Triple> fromSiDIFStream(InputStream is) throws Exception { CharStream in = CharStreams.fromStream(is); List<Triple> tripleList=fromCharStream(in); return tripleList; }
@Nullable @Override public CharStream read(String name) { try { InputStream resource = readResource(name); if (resource != null) { return CharStreams.fromStream(resource); } } catch (Exception e) { LOGGER.error("Could not read {}", name, e); } return null; }
@Nullable @Override public CharStream read(String name) { try { InputStream resource = readResource(name); if (resource != null) { return CharStreams.fromStream(resource); } } catch (Exception e) { LOGGER.error("Could not read {}", name, e); } return null; }
public static Properties parse(InputStream is) throws IOException, ConfigException { Configuration conf = new Configuration(); return conf.runparsing(CharStreams.fromStream(is)); }
public static XMLParser.DocumentContext parseRaw( InputStream inputStream ) { try { XMLLexer lexer = new XMLLexer( CharStreams.fromStream( inputStream ) ); CommonTokenStream tokens = new CommonTokenStream( lexer ); XMLParser parser = new XMLParser( tokens ); return parser.document(); } catch( Exception e ) { throw new RuntimeException( e ); } }
@Override public CompileStatus compile(Location location) throws CompileException { try { return compile(CharStreams.fromStream(location.getInputStream())); } catch (IOException e) { throw new CompileException(e.getMessage(), e); } catch (Exception e) { throw new CompileException(e.getMessage(), e); } }
public static ParseTree parse(InputStream input) throws IOException { return doParse(CharStreams.fromStream(input)); }
public static final ProfilerSession getProfilerSession(InputStream input) throws IOException { ProfilerGrammarLexer lexer = new ProfilerGrammarLexer(CharStreams.fromStream(input)); CommonTokenStream tokens = new CommonTokenStream(lexer); ProfilerGrammarParser parser = new ProfilerGrammarParser(tokens); ParseTree tree = parser.profiler(); ProfilerSessionVisitor visitor = new ProfilerSessionVisitor(); visitor.visit(tree); return visitor.getSession(); }
@VisibleForTesting static AtlasDSLParser.QueryContext parse(String queryStr) throws AtlasBaseException { AtlasDSLParser.QueryContext ret; try { InputStream stream = new ByteArrayInputStream(queryStr.getBytes()); AtlasDSLLexer lexer = new AtlasDSLLexer(CharStreams.fromStream(stream)); Validator validator = new Validator(); TokenStream inputTokenStream = new CommonTokenStream(lexer); AtlasDSLParser parser = new AtlasDSLParser(inputTokenStream); parser.removeErrorListeners(); parser.addErrorListener(validator); // Validate the syntax of the query here ret = parser.query(); if (!validator.isValid()) { LOG.error("Invalid DSL: {} Reason: {}", queryStr, validator.getErrorMsg()); throw new AtlasBaseException(AtlasErrorCode.INVALID_DSL_QUERY, queryStr, validator.getErrorMsg()); } } catch (IOException e) { throw new AtlasBaseException(e); } return ret; } }
@Override public void load(DataResource aData) throws ResourceInitializationException { CharStream input; try { UIMAFramework.getLogger().log(Level.FINE, "Loading resource " + KEY_TOKEN_REGEX_RULES + " at: " + aData.getUri()); input = CharStreams.fromStream(aData.getInputStream()); UimaTokenRegexLexer lexer = new UimaTokenRegexLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); UimaTokenRegexParser parser = new UimaTokenRegexParser(tokens); listener = new AutomataParserListener( parser ); listener.setAllowMatchingEmptySequences(false); ParseTreeWalker.DEFAULT.walk(listener, parser.ruleList()); } catch (IOException e) { throw new ResourceInitializationException(e); } }
private static StatementContext parseYangSource(final SourceIdentifier source, final InputStream stream) throws IOException, YangSyntaxErrorException { final YangStatementLexer lexer = new YangStatementLexer(CharStreams.fromStream(stream)); final YangStatementParser parser = new YangStatementParser(new CommonTokenStream(lexer)); // disconnect from console error output lexer.removeErrorListeners(); parser.removeErrorListeners(); final YangErrorListener errorListener = new YangErrorListener(source); lexer.addErrorListener(errorListener); parser.addErrorListener(errorListener); final StatementContext result = parser.statement(); errorListener.validate(); // Walk the resulting tree and replace each children with an immutable list, lowering memory requirements // and making sure the resulting tree will not get accidentally modified. An alternative would be to use // org.antlr.v4.runtime.Parser.TrimToSizeListener, but that does not make the tree immutable. ParseTreeWalker.DEFAULT.walk(MAKE_IMMUTABLE_LISTENER, result); return result; }