org.shaman.rpg.editor.project.impl.ModuleBuildFileGenerator.java Source code

Java tutorial

Introduction

Here is the source code for org.shaman.rpg.editor.project.impl.ModuleBuildFileGenerator.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.shaman.rpg.editor.project.impl;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.jdom2.Comment;
import org.jdom2.Content;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.openide.filesystems.FileObject;
import org.openide.util.Exceptions;
import org.shaman.rpg.editor.project.BuildFileExtender;

/**
 * Generates the build-impl-engine.xml build file.
 * @author Sebastian Wei
 */
public class ModuleBuildFileGenerator implements BuildFileExtender.Extender {
    private final Map<String, String> properties;
    private final Map<String, Element> targets;
    private final List<Content> customContents;

    public ModuleBuildFileGenerator() {
        properties = new LinkedHashMap<>();
        targets = new LinkedHashMap<>();
        customContents = new ArrayList<>();
    }

    public void writeBuildFile(FileObject file) {
        Document doc = generateDoc();
        try (OutputStream out = file.getOutputStream();) {
            XMLOutputter o = new XMLOutputter(Format.getPrettyFormat());
            o.output(doc, out);
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

    private Document generateDoc() {
        Element root = new Element("project");
        root.setAttribute("name", "build-impl-engine");
        root.setAttribute("basedir", "..");
        root.addContent(new Comment("these ant tasks are called from the modules"));

        //add properties
        addProperties(root);
        root.addContent("\n");

        //add custom contents
        root.addContent(customContents);
        root.addContent("\n");

        //add main target
        addMainTarget(root);
        root.addContent("\n");

        //add property init target
        addPropertyInitTarget(root);
        root.addContent("\n");

        //add ant-contrib initializing target
        addTaskefTarget(root);
        root.addContent("\n");

        //add groovy-compile target
        addGroovyCTarget(root);
        root.addContent("\n");

        //add targets
        addTargets(root);

        //create document
        return new Document(root);
    }

    private void addProperties(Element root) {
        for (Map.Entry<String, String> e : properties.entrySet()) {
            Element prop = new Element("property");
            prop.setAttribute("name", e.getKey());
            prop.setAttribute("value", e.getValue());
            root.addContent(prop);
        }
    }

    private void addMainTarget(Element root) {
        Element target = new Element("target");
        target.setAttribute("name", "compile");
        String[] dependsArray = new String[4 + targets.size()];
        dependsArray[0] = "projectized-common.compile"; //original java-compile
        dependsArray[1] = PREFIX + "init"; //property initialization
        dependsArray[2] = PREFIX + "taskdefs"; //ant-contrib initialization
        dependsArray[3] = PREFIX + "groovyc"; //groovy compiling
        int i = 4;
        for (String t : targets.keySet()) {
            dependsArray[i++] = t;
        }
        StringBuilder depends = new StringBuilder();
        for (i = 0; i < dependsArray.length; ++i) {
            depends.append(dependsArray[i]);
            if (i < dependsArray.length - 1) {
                depends.append(", ");
            }
        }
        target.setAttribute("depends", depends.toString());
        Element echo = new Element("echo");
        echo.setAttribute("message", "all engine files processed");
        target.addContent(echo);
        root.addContent(target);
    }

    private void addPropertyInitTarget(Element root) {
        Element target = new Element("target");
        target.setAttribute("name", PREFIX + "init");
        //clear path
        Element path = new Element("path");
        path.setAttribute("id", "engine.in.path");
        Element pathelement = new Element("pathelement");
        pathelement.setAttribute("location", "${src.dir}/../");
        path.addContent(pathelement);
        target.addContent(path);
        //create properties
        Element rawProp = new Element("pathconvert");
        rawProp.setAttribute("property", "engine.in.raw");
        rawProp.setAttribute("dirsep", "/");
        rawProp.setAttribute("refid", "engine.in.path");
        target.addContent(rawProp);
        target.addContent(createProperty(INPUT_ROOT, "${engine.in.raw}/"));
        String out = "${build.classes.dir}/";
        target.addContent(createProperty(OUTPUT_ROOT, out + ModuleStructure.ROOT));
        target.addContent(createProperty(OUTPUT_ASSETS, out + ModuleStructure.ASSETS));
        target.addContent(createProperty(OUTPUT_LEVELS, out + ModuleStructure.LEVELS));
        target.addContent(createProperty(OUTPUT_ENTRY_FILE, out + ModuleStructure.ENTRY_FILE));
        //delete elements file
        Element delete = new Element("delete");
        delete.setAttribute("file", "${" + OUTPUT_ENTRY_FILE + "}");
        target.addContent(delete);

        root.addContent(target);
    }

    private Element createProperty(String name, String value) {
        Element p = new Element("property");
        p.setAttribute("name", name);
        p.setAttribute("value", value);
        return p;
    }

    private void addTaskefTarget(Element root) {
        Element target = new Element("target");
        target.setAttribute("name", PREFIX + "taskdefs");
        target.setAttribute("unless", "${" + PREFIX + "taskdefs.done}");

        Element taskdef = new Element("taskdef"); //ant-contrib
        taskdef.setAttribute("resource", "net/sf/antcontrib/antlib.xml");
        Element classpath = new Element("classpath");
        Element pathelement = new Element("pathelement");
        pathelement.setAttribute("location", "${suite.dir}/nbproject/lib/ant-contrib.jar");
        classpath.addContent(pathelement);
        taskdef.addContent(classpath);
        target.addContent(taskdef);

        taskdef = new Element("taskdef"); //groovy
        taskdef.setAttribute("name", "groovyc");
        taskdef.setAttribute("classpath", "${suite.dir}/nbproject/lib/groovy-all.jar");
        taskdef.setAttribute("classname", "org.codehaus.groovy.ant.Groovyc");
        target.addContent(taskdef);

        target.addContent(createProperty(PREFIX + "taskdefs.done", "true"));

        root.addContent(target);
    }

    private void addGroovyCTarget(Element root) {
        Element target = new Element("target");
        target.setAttribute("name", PREFIX + "groovyc");
        target.setAttribute("depends", PREFIX + "taskdefs");
        Element groovyc = new Element("groovyc");
        groovyc.setAttribute("srcdir", "${src.dir}");
        groovyc.setAttribute("destdir", "${build.classes.dir}");
        Element cp = new Element("classpath");
        cp.setAttribute("refid", "cp");
        groovyc.addContent(cp);
        target.addContent(groovyc);
        root.addContent(target);
    }

    private void addTargets(Element root) {
        for (Map.Entry<String, Element> t : targets.entrySet()) {
            Element target = t.getValue();
            assert (target.getName().equals("target"));
            target.setAttribute("name", t.getKey());
            root.addContent(target);
            root.addContent("\n");
        }
    }

    @Override
    public void addProperty(String name, String value) {
        properties.put(PREFIX + name, value);
    }

    @Override
    public void addTarget(String name, Element target) {
        targets.put(PREFIX + name, target);
    }

    @Override
    public void addCustomContent(Content content) {
        customContents.add(content);
    }

}