Java 类org.apache.velocity.texen.util.FileUtil 实例源码

项目:codegen    文件:Generator.java   
public void generate(File svcFile, String codeRootPath, String resRoootPath) {
    Log.info("codeRootPath=[" + codeRootPath + "]");
    Log.info("resxRoootPath=[" + resRoootPath + "]");
    Log.info("load service description file " + svcFile.getPath());
    Document doc = getSvcXml(svcFile);
    Element rootNode = doc.getRootElement();
    String dtoStyle = rootNode.attributeValue("dtoStyle", "pojo");
    String createHtml = rootNode.attributeValue("createHtml", "false");
    String basePkg = rootNode.attributeValue("javaPackageBase", "");
    String basePkgPath = FileUtil.combinePath(codeRootPath, basePkg.replace('.', File.separatorChar));
    TransformerFactory txFactory = TransformerFactory.newInstance();
    //
    String filename = svcFile.getName();
    filename = filename.substring(0, filename.indexOf('.'));
    genEnum(txFactory, doc, basePkgPath);


}
项目:jfinalQ-gencode    文件:QUtil.java   
/**
 * 生成代码 by velocity
 * @param map       变量
 * @param destPath  目的地址
 * @param destFile  目的文件名
 * @param tmpPath   模版地址
 * @param tmpFile   模版文件名
 * @return
 */
public static boolean generateCodeByVelocity(Map<String, Object> map, String destPath, String destFile, String tmpPath, String tmpFile){
    try {
        // 1.初始化
        Properties properties = new Properties();
        properties.put("file.resource.loader.path", tmpPath);  
        properties.put("input.encoding", "UTF-8");
        properties.put("output.encoding", "UTF-8");
        Velocity.init(properties);
        VelocityContext context = new VelocityContext(map);

        // 2.生成代码
        FileUtil.mkdir(destPath);
        BufferedWriter sw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(destPath, destFile)), "UTF-8"));
        Velocity.getTemplate(tmpFile).merge(context, sw);
        sw.flush();
        sw.close();

        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
项目:codegen    文件:Generator.java   
private void genEnum(TransformerFactory txFactory, Document doc, String basePkgPath) {
    try {
        InputStream xlsInputStream = getClass().getClassLoader().getResourceAsStream("xsl/enum.xsl");
        Transformer tx = txFactory.newTransformer(new StreamSource(xlsInputStream));
        List enumNodes = doc.selectNodes("root/enums/enum");
        for (Object n : enumNodes) {
            Element enumNode = (Element) n;
            for (Object n2 : enumNode.selectNodes("field")) {
                Element fieldNode = (Element) n2;
                fieldNode.addAttribute("name", fieldNode.attributeValue("name").toUpperCase());
                // System.out.println(fieldNode.attributeValue("name"));
            }
        }
        //
        DocumentSource src = new DocumentSource(doc);
        DocumentResult rzt = new DocumentResult();
        tx.transform(src, rzt);
        Document rztDoc = rzt.getDocument();
        String filePath = FileUtil.combinePath(basePkgPath, "constant");
        FileUtil.mkdir(filePath);
        for (Object jn : rztDoc.selectNodes("enums/enum")) {
            Element jenumNode = (Element) jn;
            String enumName = jenumNode.attributeValue("name");
            String jenumFilePath = FileUtil.combinePath(filePath, enumName + ".java");

            Writer writer = null;
            try {
                writer = new OutputStreamWriter(new FileOutputStream(jenumFilePath), "utf-8");
                String enumCodes = jenumNode.getText();
                enumCodes = enumCodes.replace(",;", ";");
                writer.write(enumCodes);
            } finally {
                if (writer != null) {
                    writer.close();
                }
            }

            Log.info("gen enum %s at %s", enumName, jenumFilePath);
        }

    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}