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 RouteJavaBuilder {
    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 javaDefineTemplate = "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 javaUrlTemplate = "com.yihu.hos.%s;";
    private String configureTemplate = "    @Override%n" +
            "    public void configure() throws Exception {%n" +
            "%s%n"+
            "    }%n" +
            "}%n";

    private String javaName;
    private String packageName;
    private String configureXml;
    public void generator (String configureInfo) throws Exception {

        String source = String.format(packageTemplate, packageName) +
                String.format(importTemplate) +
                String.format(javaDefineTemplate, javaName) +
                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, javaName));
        FileWriter fw = new FileWriter(f);
        fw.write(source);
        fw.flush();
        fw.close();//这里只是产生一个JAVA文件,简单的IO操作
    }
}
