⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filewriter.java

📁 生成与Oracle相关的Ibatis相关配置文件及Java源码
💻 JAVA
字号:
/*
 * Copyright (c) 2008 胜利油田胜利软件有限责任公司. All rights reserved.
 */
package com.victorysoft.code.base;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.util.List;

import com.victorysoft.code.bean.TabInfo;
import com.victorysoft.code.maker.BaseMaker;
import com.victorysoft.code.maker.BeanMaker;
import com.victorysoft.code.maker.DaoMaker;
import com.victorysoft.code.maker.IbatisMaker;
import com.victorysoft.code.maker.ImplMaker;
import com.victorysoft.code.maker.SpringMaker;
import com.victorysoft.code.maker.SqlMapMaker;

/**
 * 
 * @author 于景洋
 * @newtime Oct 21, 2008,10:07:55 AM
 * @version 1.0
 * @see
 * @since JDK 1.5.0
 */
public class FileWriter {
	private String basedir = null;
	private String project = null;
	private BaseMaker basemaker = null;
	private BeanMaker beanmaker = null;
	private DaoMaker daomaker = null;
	private ImplMaker implmaker = null;
	private SqlMapMaker sqlmapmaker = null;
	private IbatisMaker ibatismaker = null;
	private SpringMaker springmaker = null;
	private String basepack = null;

	public FileWriter(String basedir, String project) {
		this.basedir = basedir;
		this.project = project;
		this.basemaker = new BaseMaker();
		this.beanmaker = new BeanMaker();
		this.daomaker = new DaoMaker();
		this.implmaker = new ImplMaker();
		this.sqlmapmaker = new SqlMapMaker();
		this.ibatismaker = new IbatisMaker();
		this.springmaker = new SpringMaker();
		this.basepack = this.basemaker.getTpack(project);
	}

	public void outputIbatis(List<TabInfo> tabList) {
		String path = this.basepack + ".ibatis.IbatisConfig#xml";
		path = path.replaceAll("[.]", "/").replaceAll("#", ".");
		path = this.addSrcBaseDir(path);
		String code = this.ibatismaker.getIbatis(project, tabList);
		this.ouputCode(path, code, true);
	}

	public void outputSpring(List<TabInfo> tabList) {
		String path = this.addSrcBaseDir("applicationContext.xml");
		String code = this.springmaker.getSpring(project, tabList);
		this.ouputCode(path, code, true);
	}

	public void outputFactory(List<TabInfo> tabList) {
		String name = upFirstChar(project.toLowerCase());
		String path = this.basepack + ".util." + name + "Factory#java";
		path = path.replaceAll("[.]", "/").replaceAll("#", ".");
		path = this.addSrcBaseDir(path);
		String code = this.basemaker.getFactory(project, tabList);
		this.ouputCode(path, code, false);
	}

	public void outputLog4j() {
		String path = this.addSrcBaseDir("log4j.properties");
		String code = this.basemaker.getLog4j(project);
		this.ouputCode(path, code, false);
	}

	public void outputBean(TabInfo tab) {
		String path = this.getBeanPath(tab);
		String code = this.beanmaker.getBeanm(project, tab);
		this.ouputCode(path, code, false);
	}

	public void outputDao(TabInfo tab) {
		String path = this.getDaoPath(tab);
		String code = this.daomaker.getDaom(project, tab);
		this.ouputCode(path, code, false);
	}

	public void outputImpl(TabInfo tab) {
		String path = this.getImplPath(tab);
		String code = this.implmaker.getImplm(project, tab);
		this.ouputCode(path, code, false);
	}

	public void outputSqlMap(TabInfo tab) {
		String path = this.getSqlMapPath(tab);
		String code = this.sqlmapmaker.getSqlMap(project, tab);
		this.ouputCode(path, code, true);
	}

	private String addSrcBaseDir(String path) {
		if (basedir.endsWith("\\") || basedir.endsWith("/")) {
			return basedir + project + "/src/" + path;
		} else {
			return basedir + "/" + project + "/src/" + path;
		}
	}

	private String getPath(TabInfo tab, String kind, String ext) {
		String name = upFirstChar(tab.getName().toLowerCase());
		String path = this.basepack + "." + kind + "." + name + ext;
		return path.replaceAll("[.]", "/").replaceAll("#", ".");
	}

	private String getBeanPath(TabInfo tab) {
		return addSrcBaseDir(getPath(tab, "bean", "#java"));
	}

	private String getDaoPath(TabInfo tab) {
		return addSrcBaseDir(getPath(tab, "dao", "Dao#java"));
	}

	private String getImplPath(TabInfo tab) {
		return addSrcBaseDir(getPath(tab, "impl", "Impl#java"));
	}

	private String getSqlMapPath(TabInfo tab) {
		return addSrcBaseDir(getPath(tab, "xml", "#xml"));
	}

	private void ouputCode(String path, String code, boolean isUTF8) {
		File file = null;
		StringReader sr = null;
		FileOutputStream os = null;
		try {
			file = new File(path);
			if (!file.exists()) {
				File dir = file.getParentFile();
				if (!dir.exists()) {
					dir.mkdirs();
				}
				if (!file.createNewFile()) {
					throw new FileNotFoundException("创建文件" + path + "失败");
				}
			}
			sr = new StringReader(code);
			BufferedReader br = new BufferedReader(sr);
			os = new FileOutputStream(path);
			OutputStreamWriter ow = null;
			if (isUTF8) {
				ow = new OutputStreamWriter(os, "UTF-8");
			} else {
				ow = new OutputStreamWriter(os);
			}
			BufferedWriter bow = new BufferedWriter(ow);
			String line = "";
			while ((line = br.readLine()) != null) {
				bow.write(line);
				bow.newLine();
				bow.flush();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (sr != null) {
				sr.close();
			}
		}
	}

	private String upFirstChar(String str) {
		if (str == null || str.length() == 0) {
			return str;
		}
		return String.valueOf(str.charAt(0)).toUpperCase() + str.substring(1);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -