Search in sources :

Example 1 with ParserContainer

use of pl.themolka.arcade.parser.ParserContainer in project Arcade2 by ShootGame.

the class ArcadePlugin method loadParsers.

private void loadParsers() {
    this.domEngines = new EngineManager();
    this.domEngines.registerDefault();
    this.domPreprocessor = new Preprocessor();
    this.domPreprocessor.install(new Import(this.domEngines, this.domPreprocessor));
    this.parsers = new ParserManager(this);
    this.parsers.setContextFactory(new ParserContext.Factory());
    try (InputStream input = this.getClass().getClassLoader().getResourceAsStream(ParsersFile.DEFAULT_FILENAME)) {
        ParsersFile file = new ParsersFile(this, input);
        int done = 0;
        int doneSilent = 0;
        ParserContainer container = new ParserContainer();
        for (Class<? extends Parser<?>> parser : file.getParsers()) {
            try {
                container.register(parser.newInstance());
                done++;
                Silent silent = parser.getAnnotation(Silent.class);
                Produces produces = parser.getAnnotation(Produces.class);
                if (produces == null) {
                    throw new ReflectiveOperationException(parser.getName() + " is not @Produces decorated!");
                } else if (silent == null) {
                    Class<?> producesType = produces.value();
                    if (producesType != null) {
                        this.parsers.registerType(producesType, parser);
                    }
                } else {
                    doneSilent++;
                }
            } catch (ReflectiveOperationException ex) {
                ex.printStackTrace();
            }
        }
        this.parsers.getContainer().register(container);
        String silentString = "";
        if (doneSilent > 0) {
            silentString = " (" + doneSilent + " of them " + (doneSilent == 1 ? "was" : "were") + " silent)";
        }
        this.getLogger().info("Registered " + done + " parsers" + silentString + ".");
    } catch (DOMException | IOException ex) {
        ex.printStackTrace();
    }
    // Install parser dependencies
    int done = 0;
    try {
        done = this.parsers.install();
    } catch (ParserNotSupportedException ex) {
        this.getLogger().log(Level.SEVERE, "Given parser is not supported", ex);
    }
    this.getLogger().info("Installed " + done + " parser dependencies.");
}
Also used : ParserContainer(pl.themolka.arcade.parser.ParserContainer) EngineManager(pl.themolka.arcade.dom.engine.EngineManager) Import(pl.themolka.arcade.dom.preprocess.Import) InputStream(java.io.InputStream) Silent(pl.themolka.arcade.parser.Silent) IOException(java.io.IOException) ParserManager(pl.themolka.arcade.parser.ParserManager) ParsersFile(pl.themolka.arcade.parser.ParsersFile) DOMException(pl.themolka.arcade.dom.DOMException) Produces(pl.themolka.arcade.parser.Produces) Preprocessor(pl.themolka.arcade.dom.preprocess.Preprocessor) ParserContext(pl.themolka.arcade.parser.ParserContext) ParserNotSupportedException(pl.themolka.arcade.parser.ParserNotSupportedException)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 DOMException (pl.themolka.arcade.dom.DOMException)1 EngineManager (pl.themolka.arcade.dom.engine.EngineManager)1 Import (pl.themolka.arcade.dom.preprocess.Import)1 Preprocessor (pl.themolka.arcade.dom.preprocess.Preprocessor)1 ParserContainer (pl.themolka.arcade.parser.ParserContainer)1 ParserContext (pl.themolka.arcade.parser.ParserContext)1 ParserManager (pl.themolka.arcade.parser.ParserManager)1 ParserNotSupportedException (pl.themolka.arcade.parser.ParserNotSupportedException)1 ParsersFile (pl.themolka.arcade.parser.ParsersFile)1 Produces (pl.themolka.arcade.parser.Produces)1 Silent (pl.themolka.arcade.parser.Silent)1