Search in sources :

Example 1 with Document

use of pl.themolka.arcade.dom.Document in project Arcade2 by ShootGame.

the class MapLoaderModule method readMapDirectory.

private OfflineMap readMapDirectory(File worldDirectory) throws DOMException, IOException {
    File file = new File(worldDirectory, MapManifest.FILENAME);
    if (!file.exists()) {
        throw new FileNotFoundException("Missing " + file.getName());
    }
    DOMEngine engine = this.getPlugin().getDomEngines().forFile(file);
    Parser<OfflineMap> parser;
    try {
        parser = this.getPlugin().getParsers().forType(OfflineMap.class);
    } catch (ParserNotSupportedException ex) {
        throw new RuntimeException("No " + OfflineMap.class.getSimpleName() + " parser installed");
    }
    Document document = engine.read(file);
    this.getPlugin().getDomPreprocessor().preprocess(document);
    OfflineMap map = parser.parse(document).orFail();
    map.setDirectory(worldDirectory);
    map.setSettings(file);
    return map;
}
Also used : DOMEngine(pl.themolka.arcade.dom.engine.DOMEngine) FileNotFoundException(java.io.FileNotFoundException) Document(pl.themolka.arcade.dom.Document) File(java.io.File) ParserNotSupportedException(pl.themolka.arcade.parser.ParserNotSupportedException)

Example 2 with Document

use of pl.themolka.arcade.dom.Document in project Arcade2 by ShootGame.

the class SimpleGameManager method createGame.

@Override
public Game createGame(OfflineMap map) throws DOMException, IOException {
    File file = map.getSettings();
    DOMEngine engine = this.plugin.getDomEngines().forFile(file);
    Parser<MapManifest> parser;
    try {
        parser = this.plugin.getParsers().forType(MapManifest.class);
    } catch (ParserNotSupportedException ex) {
        throw new RuntimeException("No " + MapManifest.class.getSimpleName() + " parser installed");
    }
    Document document = engine.read(file);
    this.plugin.getDomPreprocessor().preprocess(document);
    ArcadeMap realMap = new ArcadeMap(map, parser.parse(document).orFail());
    WorldNameGenerator worldNameGenerator = new WorldNameGenerator(map);
    realMap.setWorldName(worldNameGenerator.nextWorldName());
    return this.createGame(realMap);
}
Also used : DOMEngine(pl.themolka.arcade.dom.engine.DOMEngine) WorldNameGenerator(pl.themolka.arcade.map.WorldNameGenerator) ArcadeMap(pl.themolka.arcade.map.ArcadeMap) MapManifest(pl.themolka.arcade.map.MapManifest) Document(pl.themolka.arcade.dom.Document) File(java.io.File) ParserNotSupportedException(pl.themolka.arcade.parser.ParserNotSupportedException)

Example 3 with Document

use of pl.themolka.arcade.dom.Document in project Arcade2 by ShootGame.

the class ImportStage method invokeNode.

@Override
public void invokeNode(Node node) throws PreprocessException {
    if (!node.hasParent()) {
        throw new PreprocessException(node, "Node must have its parent");
    }
    Node parent = node.getParent();
    String targetPath = node.getValue();
    if (targetPath == null) {
        throw new PreprocessException(node, "Target document path not specified");
    }
    Document target;
    try {
        Path directory = this.document.getPath().getParent();
        target = readDocument(this.engines, node, directory.resolve(targetPath).toUri());
    } catch (InvalidPathException ex) {
        throw new PreprocessException(node, "Invalid document path: " + ex.getReason(), ex);
    }
    if (!target.hasPath()) {
        throw new PreprocessException(target, "Cannot resolve target document path");
    } else if (!target.hasRoot()) {
        throw new PreprocessException(target, "Target document is empty");
    }
    Path path = target.getPath();
    if (stack.search(path) != -1) {
        throw new PreprocessException(target, "Import loop detected");
    }
    stack.push(path);
    // Preprocess the target document.
    this.preprocessor.preprocess(target);
    // Remove the old <import> node.
    Node.detach(node);
    // We don't support root node properties.
    parent.add(target.getRoot().children());
}
Also used : Path(java.nio.file.Path) Node(pl.themolka.arcade.dom.Node) Document(pl.themolka.arcade.dom.Document) InvalidPathException(java.nio.file.InvalidPathException)

Example 4 with Document

use of pl.themolka.arcade.dom.Document in project Arcade2 by ShootGame.

the class Include method invokeNode.

@Override
public void invokeNode(Node node) throws PreprocessException {
    if (!node.hasParent()) {
        throw new PreprocessException(node, "Node must have its parent");
    }
    Node parent = node.getParent();
    String targetPath = node.getValue();
    if (targetPath == null) {
        throw new PreprocessException(node, "Target document path not specified");
    }
    Document target;
    try {
        target = ImportStage.readDocument(this.engines, node, this.repository.resolve(targetPath).toUri());
    } catch (InvalidPathException ex) {
        throw new PreprocessException(node, "Invalid document path: " + ex.getReason(), ex);
    }
    if (!target.hasPath()) {
        throw new PreprocessException(target, "Cannot resolve target document path");
    } else if (!target.hasRoot()) {
        throw new PreprocessException(target, "Target document is empty");
    }
    // Preprocess the target document.
    this.preprocessor.preprocess(target);
    // Remove the old <include> node.
    Node.detach(node);
    // We don't support root node properties.
    parent.add(target.getRoot().children());
}
Also used : Node(pl.themolka.arcade.dom.Node) Document(pl.themolka.arcade.dom.Document) InvalidPathException(java.nio.file.InvalidPathException)

Aggregations

Document (pl.themolka.arcade.dom.Document)4 File (java.io.File)2 InvalidPathException (java.nio.file.InvalidPathException)2 Node (pl.themolka.arcade.dom.Node)2 DOMEngine (pl.themolka.arcade.dom.engine.DOMEngine)2 ParserNotSupportedException (pl.themolka.arcade.parser.ParserNotSupportedException)2 FileNotFoundException (java.io.FileNotFoundException)1 Path (java.nio.file.Path)1 ArcadeMap (pl.themolka.arcade.map.ArcadeMap)1 MapManifest (pl.themolka.arcade.map.MapManifest)1 WorldNameGenerator (pl.themolka.arcade.map.WorldNameGenerator)1