Search in sources :

Example 1 with Produces

use of pl.themolka.arcade.parser.Produces 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)

Example 2 with Produces

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

the class ItemMetaParser method install.

@Override
public void install(ParserContext context) throws ParserNotSupportedException {
    this.parsers = new HashMap<>();
    for (Class<? extends Nested> clazz : types) {
        Produces produces = clazz.getDeclaredAnnotation(Produces.class);
        if (produces == null) {
            continue;
        }
        Class<?> metaType = produces.value();
        if (metaType == null || !ItemMeta.class.isAssignableFrom(metaType)) {
            continue;
        }
        try {
            this.parsers.put((Class<? extends ItemMeta>) metaType, clazz.newInstance());
        } catch (ReflectiveOperationException ignored) {
        }
    }
    for (Nested<?> parser : this.parsers.values()) {
        parser.install(context);
    }
}
Also used : Produces(pl.themolka.arcade.parser.Produces)

Aggregations

Produces (pl.themolka.arcade.parser.Produces)2 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 Silent (pl.themolka.arcade.parser.Silent)1