package com.yihu.hos.camel;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import javax.tools.JavaCompiler;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.FileWriter;
import java.util.List;

/**
 * Created by lingfeng on 2016/8/29.
 */
public class RouteClassBuilder {
    private String packageTemplate = "package com.yihu.hos.%s;%n";
    private String importTemplate = "import org.apache.camel.builder.RouteBuilder;%n" +
            "import org.apache.camel.model.dataformat.XmlJsonDataFormat;%n";
    private String classDefineTemplate = "public class %s extends RouteBuilder {%n";
    private String packagePathTemplate = System.getProperty("user.dir")//获取到项目的根路径
            + "/src/main/java/com/yihu/hos/%s";
    private String classPathTemplate = System.getProperty("user.dir")//获取到项目的根路径
            + "/src/main/java/com/yihu/hos/%s/%s.java";
    private String classUrlTemplate = "com.yihu.hos.%s;";
    private String configureTemplate = "    @Override%n" +
            "    public void configure() throws Exception {%n" +
            "%s%n"+
            "    }%n" +
            "}%n";

    private String className;
    private String packageName;
    private String configureXml;
    public void generator () throws Exception {
        Document document = DocumentHelper.parseText(configureXml);
        Element camelContext = document.getRootElement();
        ///分割數據
        Element route = camelContext.element("route");
        List<Element> endpoints = route.elements();
        StringBuilder configureInfo = new StringBuilder();
        for (Element element : endpoints) {
            configureInfo.append(element.getName() + "(\"" + element.attribute("uri").getValue() + "\").");
        }

        String source = String.format(packageTemplate, packageName) +
                String.format(importTemplate) +
                String.format(classDefineTemplate, className) +
                String.format(configureTemplate, configureInfo.substring(0, configureInfo.length() - 1) + ";");
        File packagePath = new File(String.format(packagePathTemplate, packageName));
        if (!packagePath.exists()) {
            packagePath.mkdir();
        }
        File f = new File(String.format(classPathTemplate, packageName, className));
        FileWriter fw = new FileWriter(f);
        fw.write(source);
        fw.flush();
        fw.close();//这里只是产生一个JAVA文件,简单的IO操作
        // compile下面开始编译这个Store.java
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        StandardJavaFileManager fileMgr = compiler.getStandardFileManager(null,
                null, null);
        Iterable units = fileMgr.getJavaFileObjects(String.format(classPathTemplate, packageName, className));
        JavaCompiler.CompilationTask t = compiler.getTask(null, fileMgr, null, null, null,
                units);
        t.call();
        fileMgr.close();
        // load into memory and create an instance
//        URL[] urls = new URL[] { new URL("file:/"
//                + System.getProperty("user.dir") + "/src/main/java") };
//        URLClassLoader ul = new URLClassLoader(urls);
//        //客户端调用
//        Class<IHelloWord> helloWordClass = (Class<IHelloWord>) ul.loadClass(String.format(classUrlTemplate, className));
//        if (helloWordClass != null) {
//            IHelloWord helloWord = helloWordClass.newInstance();
//            helloWord.getHelloWord();
//        }
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public String getConfigureXml() {
        return configureXml;
    }

    public void setConfigureXml(String configureXml) {
        this.configureXml = configureXml;
    }
}
