com.sxjun.core.generate.Generate.java Source code

Java tutorial

Introduction

Here is the source code for com.sxjun.core.generate.Generate.java

Source

/**
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.sxjun.core.generate;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;

import com.google.common.collect.Maps;
import com.sxjun.core.common.utils.DateUtils;
import com.sxjun.core.common.utils.FileUtils;
import com.sxjun.core.common.utils.FreeMarkers;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * ??
 * @author ThinkGem
 * @version 2013-03-15
 */
public class Generate {
    private static Logger logger = LoggerFactory.getLogger(Generate.class);

    public static void main(String[] args) throws Exception {

        // ==========  ?? ====================
        // ??????
        // ?{packageName}/{moduleName}/{dao,entity,service,web}/{subModuleName}/{className}
        // packageName ????applicationContext.xmlsrping-mvc.xml?base-package?packagesToScan?4?
        String packageName = "com.sxjun";
        String moduleName = "retrieval"; // ???sys
        String subModuleName = ""; // ????? 
        String className = "IndexManager"; // ??user
        String classAuthor = "sxjun"; // ThinkGem
        String functionName = "?"; // ??

        // ???
        Boolean isEnable = true;
        // ==========  ?? ====================
        if (!isEnable) {
            logger.error("????isEnable = true");
            return;
        }

        if (StringUtils.isBlank(moduleName) || StringUtils.isBlank(moduleName) || StringUtils.isBlank(className)
                || StringUtils.isBlank(functionName)) {
            logger.error("??????????????");
            return;
        }

        // ?
        String separator = File.separator;
        String classPath = new DefaultResourceLoader().getResource("").getFile().getPath();
        String templatePath = classPath.replace(
                separator + "WebRoot" + separator + "WEB-INF" + separator + "classes",
                separator + "src" + separator + "com" + separator + "sxjun" + separator + "retrieval");
        String javaPath = classPath.replace(separator + "WebRoot" + separator + "WEB-INF" + separator + "classes",
                separator + "src" + separator + (StringUtils.lowerCase(packageName)).replace(".", separator));
        String viewPath = classPath.replace(separator + "classes", separator + "retrieval");

        // ???
        Configuration cfg = new Configuration();
        cfg.setDirectoryForTemplateLoading(new File(templatePath.substring(0, templatePath.lastIndexOf(separator))
                + separator + "generate" + separator + "template"));

        // ???
        Map<String, String> model = Maps.newHashMap();
        model.put("packageName", StringUtils.lowerCase(packageName));
        model.put("moduleName", StringUtils.lowerCase(moduleName));
        model.put("subModuleName",
                StringUtils.isNotBlank(subModuleName) ? "." + StringUtils.lowerCase(subModuleName) : "");
        model.put("className", StringUtils.uncapitalize(className));
        model.put("ClassName", StringUtils.capitalize(className));
        model.put("classAuthor", StringUtils.isNotBlank(classAuthor) ? classAuthor : "Generate Tools");
        model.put("classVersion", DateUtils.getDate());
        model.put("functionName", functionName);
        model.put("urlPrefix",
                (StringUtils.isNotBlank(subModuleName) ? "/" + StringUtils.lowerCase(subModuleName) + "/" : "")
                        + model.get("className"));
        model.put("viewPrefix",
                StringUtils.substringAfterLast(model.get("packageName"), ".") + "/" + model.get("urlPrefix"));

        // ? Entity
        Template template = cfg.getTemplate("pojo.ftl");
        String content = FreeMarkers.renderTemplate(template, model);
        String filePath = javaPath + separator + model.get("moduleName") + separator + "pojo" + separator
                + StringUtils.lowerCase(subModuleName) + separator + model.get("ClassName") + ".java";
        writeFile(content, filePath);
        logger.info(filePath);

        // ? Dao
        /*template = cfg.getTemplate("dao.ftl");
        content = FreeMarkers.renderTemplate(template, model);
        filePath = javaPath+separator+model.get("moduleName")+separator+"dao"+separator
        +StringUtils.lowerCase(subModuleName)+separator+model.get("ClassName")+"Dao.java";
        writeFile(content, filePath);
        logger.info(filePath);
            
        // ? Service
        template = cfg.getTemplate("service.ftl");
        content = FreeMarkers.renderTemplate(template, model);
        filePath = javaPath+separator+model.get("moduleName")+separator+"service"+separator
        +StringUtils.lowerCase(subModuleName)+separator+model.get("ClassName")+"Service.java";
        writeFile(content, filePath);
        logger.info(filePath);*/

        // ? Controller
        template = cfg.getTemplate("controller.ftl");
        content = FreeMarkers.renderTemplate(template, model);
        filePath = javaPath + separator + model.get("moduleName") + separator + "controller" + separator
                + StringUtils.lowerCase(subModuleName) + separator + model.get("ClassName") + "Controller.java";
        writeFile(content, filePath);
        logger.info(filePath);

        // ? ViewForm
        template = cfg.getTemplate("viewForm.ftl");
        content = FreeMarkers.renderTemplate(template, model);
        filePath = viewPath + separator + model.get("className") + separator + model.get("className") + "Form.jsp";
        writeFile(content, filePath);
        logger.info(filePath);

        // ? ViewList
        template = cfg.getTemplate("viewList.ftl");
        content = FreeMarkers.renderTemplate(template, model);
        filePath = viewPath + separator + model.get("className") + separator + model.get("className") + "List.jsp";
        writeFile(content, filePath);
        logger.info(filePath);

        logger.info("????");
    }

    /**
     * 
     * @param content
     * @param filePath
     */
    public static void writeFile(String content, String filePath) {
        try {
            if (FileUtils.createFile(filePath)) {
                FileWriter fileWriter = new FileWriter(filePath, true);
                BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
                bufferedWriter.write(content);
                bufferedWriter.close();
                fileWriter.close();
            } else {
                logger.info("??");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}