当前开发环境在ECLIPSE下,国际化目标语言主要是英语和中文两种。项目分为不同的模块,每一个模块针对每一种语言有一个properties文件。
比如模块一,就有模块一_zh.properties,模块二_en.properties.
写这个工具的初衷时公司当前项目有10+个程序员同时开发与维护。属性文件一天可能会修改或添加好几次。
我的工作主要是根据英文版本的属性文件添加修改相关的中文版本。单纯的从CVS上CHECK OUT 出来后,比较中文版本与英文版本实在太麻烦了。(属性文件多,同时有的文件中有超过1.2W个属性)
所以想写一个小工具进行处理。
主要思路是这样子,分别在两个文件夹下放置不同语言的属性文件,如中文与英文。
然后一个专门的类进行处理,将中文与英文的的属性进行对比。(中文版本跟住英文版本)如果存在中文版本缺失,则生成EXCEL,将多出来的英文属性添加至EXCEL中,完成手动的翻译,最后再将该EXCEL重新生成中文版的properties文件。完成中文属性文件。
EXCEL 的字段格式:
主要字段分别为Properties中的KEY,然后英语值,中文值,标志位(表示该属性是否新增或修改)以及修改前的值
主要用于生成EXCEL的类
主要用于第一次基于英文版本生成EXCEL
package com.justcommodity.cxc.properties;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class XlsGenerator {
private static XlsGenerator gen = null;
/**
* @return
* @date:2012-10-9
* @author:Ji.huazhen
*/
public static XlsGenerator getInstance() {
synchronized (XlsGenerator.class) {
if (gen == null) {
gen = new XlsGenerator();
}
}
return gen;
}
/**
* @param args
*/
public static void main(String[] args) {
//TODO Auto-generated method stub
XlsGenerator generator = XlsGenerator.getInstance();
//Tapestry
generator.generatePropertiesXls(PropertiesConstants.TAPESTRY_PROPERTIES_PATH,
PropertiesConstants.TAPESTRY_XLS_PATH);
//Contract
generator.generatePropertiesXls(PropertiesConstants.CONTRACT_PROPERTIES_PATH,
PropertiesConstants.CONTRACT_XLS_PATH);
//Application
generator.generatePropertiesXls(PropertiesConstants.APPLICATION_PROPERTIES_PATH,
PropertiesConstants.APPLICATION_XLS_PATH);
//Vessel
generator.generatePropertiesXls(PropertiesConstants.VESSEL_PROPERTIES_PATH,
PropertiesConstants.VESSEL_XLS_PATH);
//System
generator.generatePropertiesXls(PropertiesConstants.SYSTEM_PROPERTIES_PATH,
PropertiesConstants.SYSTEM_XLS_PATH);
//Strategy
generator.generatePropertiesXls(PropertiesConstants.STRATEGY_PROPERTIES_PATH,
PropertiesConstants.STRATEGY_XLS_PATH);
//Stock
generator.generatePropertiesXls(PropertiesConstants.STOCK_PROPERTIES_PATH,
PropertiesConstants.STOCK_XLS_PATH);
//Rspo
generator.generatePropertiesXls(PropertiesConstants.RSPO_PROPERTIES_PATH,
PropertiesConstants.RSPO_XLS_PATH);
//Report
generator.generatePropertiesXls(PropertiesConstants.REPORT_PROPERTIES_PATH,
PropertiesConstants.REPORT_XLS_PATH);
//Profit Center
generator.generatePropertiesXls(PropertiesConstants.PROFITCENTER_PROPERTIES_PATH,
PropertiesConstants.PROFITCENTER_XLS_PATH);
//Production
generator.generatePropertiesXls(PropertiesConstants.PRODUCTION_PROPERTIES_PATH,
PropertiesConstants.PRODUCTION_XLS_PATH);
//Position
generator.generatePropertiesXls(PropertiesConstants.POSITION_PROPERTIES_PATH,
PropertiesConstants.POSITION_XLS_PATH);
//ManageInformation
generator.generatePropertiesXls(PropertiesConstants.MANAGEINFO_PROPERTIES_PATH,
PropertiesConstants.MANAGEINFO_XLS_PATH);
//Hedge
generator.generatePropertiesXls(PropertiesConstants.HEDGE_PROPERTIES_PATH,
PropertiesConstants.HEDGE_XLS_PATH);
//Future
generator.generatePropertiesXls(PropertiesConstants.FUTURE_PROPERTIES_PATH,
PropertiesConstants.FUTURE_XLS_PATH);
//Fulfillment
generator.generatePropertiesXls(PropertiesConstants.FULFILLMENT_PROPERTIES_PATH,
PropertiesConstants.FULFILLMENT_XLS_PATH);
//Forex
generator.generatePropertiesXls(PropertiesConstants.FOREX_PROPERTIES_PATH,
PropertiesConstants.FOREX_XLS_PATH);
//Financial
generator.generatePropertiesXls(PropertiesConstants.FINANCIAL_PROPERTIES_PATH,
PropertiesConstants.FINANCIAL_XLS_PATH);
//Derivative
generator.generatePropertiesXls(PropertiesConstants.DERIVATIVE_PROPERTIES_PATH,
PropertiesConstants.DERIVATIVE_XLS_PATH);
}
/**
* @date:2012-10-8
* @author:Ji.huazhen
*/
public void generatePropertiesXls(String propertiesPath, String xlsPath) {
System.out.println("Start to generate Excel for properties file: " + propertiesPath);
Properties prop = new SortedProperties();
//Excel Columns
String[] labelList = new String[] {PropertiesConstants.XLS_LABEL_PROPERTIES_KEY,
PropertiesConstants.XLS_LABEL_PROPERTIES_VALUE_ENG,
PropertiesConstants.XLS_LABEL_PROPERTIES_VALUE_CHN};
WritableWorkbook book = null;
WritableSheet sheet = null;
Label label = null;
int lineNumber = 1;
try {
//Load Original Properties
InputStream in = new BufferedInputStream(new FileInputStream(propertiesPath));
prop.load(in);
Enumeration propertiesName = prop.propertyNames();
List<String> propertiesList = sortProperties(propertiesName);
//Create Excel and Columns
book = Workbook.createWorkbook(new File(xlsPath));
sheet = book.createSheet("sheet", 0);
for (int i = 0; i < labelList.length; i++) {
label = new Label(i, 0, labelList[i]);
sheet.addCell(label);
}
//Put Properties
for (String key : propertiesList) {
label = new Label(0, lineNumber, key);
sheet.addCell(label);
label = new Label(1, lineNumber, prop.getProperty(key));
sheet.addCell(label);
lineNumber = lineNumber + 1;
}
//Generate excel
book.write();
book.close();
} catch (Exception e) {
System.out.println("Error when generating Excel file!");
e.printStackTrace();
}
System.out.println("Generated Excel file succeed! \nFile Path: " + xlsPath);
}
public void generatePropertiesXlsByCh(String propertiesPath, String propertiesPath_zh,
String xlsPath) {
System.out.println("Start to generate Excel for properties file: " + propertiesPath + ":"
+ System.currentTimeMillis());
Properties prop = new SortedProperties();
Properties prop_zh = new SortedProperties();
//Excel Columns
String[] labelList = new String[] {PropertiesConstants.XLS_LABEL_PROPERTIES_KEY,
PropertiesConstants.XLS_LABEL_PROPERTIES_VALUE_ENG,
PropertiesConstants.XLS_LABEL_PROPERTIES_VALUE_CHN};
WritableWorkbook book = null;
WritableSheet sheet = null;
Label label = null;
int lineNumber = 1;
try {
//Load Original Properties
FileInputStream fis = new FileInputStream(propertiesPath);
InputStream in = new BufferedInputStream(fis);
prop.load(in);
fis = new FileInputStream(propertiesPath_zh);
in = new BufferedInputStream(fis);
prop_zh.load(in);
fis.close();
in.close();
Enumeration propertiesName = prop.propertyNames();
List<String> propertiesList = sortProperties(propertiesName);
//Create Excel and Columns
book = Workbook.createWorkbook(new File(xlsPath));
sheet = book.createSheet("sheet", 0);
for (int i = 0; i < labelList.length; i++) {
label = new Label(i, 0, labelList[i]);
sheet.addCell(label);
sheet.setColumnView(i, 100);
}
//Put Properties
for (String key : propertiesList) {
label = new Label(0, lineNumber, key);
sheet.addCell(label);
//String temp = prop.getProperty(key);
label = new Label(1, lineNumber, prop.getProperty(key));
sheet.addCell(label);
label = new Label(2, lineNumber, prop_zh.getProperty(key));
sheet.addCell(label);
lineNumber = lineNumber + 1;
}
//Generate excel
book.write();
book.close();
} catch (Exception e) {
System.out.println("Error when generating Excel file!");
e.printStackTrace();
}
System.out.println("Generated Excel file succeed! \nFile Path: " + xlsPath + ":"
+ System.currentTimeMillis());
}
/**
* @param propertiesName
* @return
* @date:2012-10-9
* @author:Ji.huazhen
*/
private List sortProperties(Enumeration propertiesName) {
List propertiesList = new ArrayList();
while (propertiesName.hasMoreElements()) {
propertiesList.add(propertiesName.nextElement());
}
Collections.sort(propertiesList);
return propertiesList;
}
}
用于合并中文版与英文版EXCEL的类
package com.justcommodity.cxc.properties;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class XlsMergeConvert {
private static XlsMergeConvert convertor = null;
/**
* @return
* @date:2012-10-9
* @author:Ji.huazhen
*/
public static XlsMergeConvert getInstance() {
synchronized (XlsMergeConvert.class) {
if (convertor == null) {
convertor = new XlsMergeConvert();
}
}
return convertor;
}
/**
* @param args
* @date:2012-10-9
* @author:Ji.huazhen
*/
public static void main(String[] args) {
//Application
XlsMergeConvert.getInstance().mergeProperties(PropertiesConstants.APPLICATION_PROPERTIES_PATH,
PropertiesConstants.APPLICATION_PROPERTIES_ZH_PATH,
PropertiesConstants.APPLICATION_XLS_PATH, PropertiesConstants.APPLICATION_XLS_CONVERT_PATH);
//Tapestry
XlsMergeConvert.getInstance().mergeProperties(PropertiesConstants.TAPESTRY_PROPERTIES_PATH,
PropertiesConstants.TAPESTRY_PROPERTIES_ZH_PATH, PropertiesConstants.TAPESTRY_XLS_PATH,
PropertiesConstants.TAPESTRY_XLS_CONVERT_PATH);
//Contract
XlsMergeConvert.getInstance().mergeProperties(PropertiesConstants.CONTRACT_PROPERTIES_PATH,
PropertiesConstants.CONTRACT_PROPERTIES_ZH_PATH, PropertiesConstants.CONTRACT_XLS_PATH,
PropertiesConstants.CONTRACT_XLS_CONVERT_PATH);
//Vessel
XlsMergeConvert.getInstance().mergeProperties(PropertiesConstants.VESSEL_PROPERTIES_PATH,
PropertiesConstants.VESSEL_PROPERTIES_ZH_PATH, PropertiesConstants.VESSEL_XLS_PATH,
PropertiesConstants.VESSEL_XLS_CONVERT_PATH);
//System
XlsMergeConvert.getInstance().mergeProperties(PropertiesConstants.SYSTEM_PROPERTIES_PATH,
PropertiesConstants.SYSTEM_PROPERTIES_ZH_PATH, PropertiesConstants.SYSTEM_XLS_PATH,
PropertiesConstants.SYSTEM_XLS_CONVERT_PATH);
//Strategy
XlsMergeConvert.getInstance().mergeProperties(PropertiesConstants.STRATEGY_PROPERTIES_PATH,
PropertiesConstants.STRATEGY_PROPERTIES_ZH_PATH, PropertiesConstants.STRATEGY_XLS_PATH,
PropertiesConstants.STRATEGY_XLS_CONVERT_PATH);
//Stock
XlsMergeConvert.getInstance().mergeProperties(PropertiesConstants.STOCK_PROPERTIES_PATH,
PropertiesConstants.STOCK_PROPERTIES_ZH_PATH, PropertiesConstants.STOCK_XLS_PATH,
PropertiesConstants.STOCK_XLS_CONVERT_PATH);
//Rspo
XlsMergeConvert.getInstance().mergeProperties(PropertiesConstants.RSPO_PROPERTIES_PATH,
PropertiesConstants.RSPO_PROPERTIES_ZH_PATH, PropertiesConstants.RSPO_XLS_PATH,
PropertiesConstants.RSPO_XLS_CONVERT_PATH);
//Report
XlsMergeConvert.getInstance().mergeProperties(PropertiesConstants.REPORT_PROPERTIES_PATH,
PropertiesConstants.REPORT_PROPERTIES_ZH_PATH, PropertiesConstants.REPORT_XLS_PATH,
PropertiesConstants.REPORT_XLS_CONVERT_PATH);
//Profit Center
XlsMergeConvert.getInstance().mergeProperties(PropertiesConstants.PROFITCENTER_PROPERTIES_PATH,
PropertiesConstants.PROFITCENTER_PROPERTIES_ZH_PATH,
PropertiesConstants.PROFITCENTER_XLS_PATH,
PropertiesConstants.PROFITCENTER_XLS_CONVERT_PATH);
//Production
XlsMergeConvert.getInstance().mergeProperties(PropertiesConstants.PRODUCTION_PROPERTIES_PATH,
PropertiesConstants.PRODUCTION_PROPERTIES_ZH_PATH, PropertiesConstants.PRODUCTION_XLS_PATH,
PropertiesConstants.PRODUCTION_XLS_CONVERT_PATH);
//Position
XlsMergeConvert.getInstance().mergeProperties(PropertiesConstants.POSITION_PROPERTIES_PATH,
PropertiesConstants.POSITION_PROPERTIES_ZH_PATH, PropertiesConstants.POSITION_XLS_PATH,
PropertiesConstants.POSITION_XLS_CONVERT_PATH);
//ManageInformation
XlsMergeConvert.getInstance().mergeProperties(PropertiesConstants.MANAGEINFO_PROPERTIES_PATH,
PropertiesConstants.MANAGEINFO_PROPERTIES_ZH_PATH, PropertiesConstants.MANAGEINFO_XLS_PATH,
PropertiesConstants.MANAGEINFO_XLS_CONVERT_PATH);
//Hedge
XlsMergeConvert.getInstance().mergeProperties(PropertiesConstants.HEDGE_PROPERTIES_PATH,
PropertiesConstants.HEDGE_PROPERTIES_ZH_PATH, PropertiesConstants.HEDGE_XLS_PATH,
PropertiesConstants.HEDGE_XLS_CONVERT_PATH);
//Future
XlsMergeConvert.getInstance().mergeProperties(PropertiesConstants.FUTURE_PROPERTIES_PATH,
PropertiesConstants.FUTURE_PROPERTIES_ZH_PATH, PropertiesConstants.FUTURE_XLS_PATH,
PropertiesConstants.FUTURE_XLS_CONVERT_PATH);
//Fulfillment
XlsMergeConvert.getInstance().mergeProperties(PropertiesConstants.FULFILLMENT_PROPERTIES_PATH,
PropertiesConstants.FULFILLMENT_PROPERTIES_ZH_PATH,
PropertiesConstants.FULFILLMENT_XLS_PATH, PropertiesConstants.FULFILLMENT_XLS_CONVERT_PATH);
//Forex
XlsMergeConvert.getInstance().mergeProperties(PropertiesConstants.FOREX_PROPERTIES_PATH,
PropertiesConstants.FOREX_PROPERTIES_ZH_PATH, PropertiesConstants.FOREX_XLS_PATH,
PropertiesConstants.FOREX_XLS_CONVERT_PATH);
//Financial
XlsMergeConvert.getInstance().mergeProperties(PropertiesConstants.FINANCIAL_PROPERTIES_PATH,
PropertiesConstants.FINANCIAL_PROPERTIES_ZH_PATH, PropertiesConstants.FINANCIAL_XLS_PATH,
PropertiesConstants.FINANCIAL_XLS_CONVERT_PATH);
//Derivative
XlsMergeConvert.getInstance().mergeProperties(PropertiesConstants.DERIVATIVE_PROPERTIES_PATH,
PropertiesConstants.DERIVATIVE_PROPERTIES_ZH_PATH, PropertiesConstants.DERIVATIVE_XLS_PATH,
PropertiesConstants.DERIVATIVE_XLS_CONVERT_PATH);
}
/**
* @param xlsPath
* @param targetPropertiesPath
* @return
* @date:2012-10-9
* @author:Ji.huazhen
*/
private Properties catchLocalProperties(String xlsPath, Boolean isCatchCHN) {
Workbook book = null;
Sheet sheet = null;
Cell cell = null;
Properties properties = new Properties();
String key = null;
String value = null;
try {
book = Workbook.getWorkbook(new File(xlsPath));
sheet = book.getSheet(0);
int rownum = sheet.getRows();
for (int i = 1; i < rownum; i++) {
cell = sheet.getCell(0, i);
key = cell.getContents();
if (!isCatchCHN) {
cell = sheet.getCell(1, i);
} else {
cell = sheet.getCell(2, i);
}
value = cell.getContents();
properties.setProperty(key, value);
}
return properties;
} catch (Exception e) {
e.printStackTrace();
}
return properties;
}
public void mergeProperties(String cvsPropertiesPath, String localPropertiesPath, String xlsPath,
String xlsConvertPath) {
System.out.println("Start to merger properties and generate excel!");
Properties prop = new SortedProperties();
Properties propCHN = new SortedProperties();
//Excel Columns
String[] labelList = new String[] {PropertiesConstants.XLS_LABEL_PROPERTIES_KEY,
PropertiesConstants.XLS_LABEL_PROPERTIES_VALUE_ENG,
PropertiesConstants.XLS_LABEL_PROPERTIES_VALUE_CHN,
PropertiesConstants.XLS_LABEL_PROPERTIES_DIFFER_FLAG,
PropertiesConstants.XLS_LABEL_PROPERTIES_OLD_VALUE};
WritableWorkbook book = null;
WritableSheet sheet = null;
Label label = null;
int lineNumber = 1;
try {
//Load Original Properties
InputStream in = new BufferedInputStream(new FileInputStream(cvsPropertiesPath));
prop.load(in);
Enumeration propertiesName = prop.propertyNames();
List<String> propertiesList = sortProperties(propertiesName);
InputStream inChn = new BufferedInputStream(new FileInputStream(localPropertiesPath));
propCHN.load(inChn);
Enumeration propertiesNameCHN = propCHN.propertyNames();
List<String> localPropertiesWithCHN = sortProperties(propertiesNameCHN);
System.out.println(propertiesList.size());
Properties localPropertiesWithENG = catchLocalProperties(xlsPath, Boolean.FALSE);
//Create Excel and Columns
book = Workbook.createWorkbook(new File(xlsConvertPath));
sheet = book.createSheet("sheet", 0);
for (int i = 0; i < labelList.length; i++) {
label = new Label(i, 0, labelList[i]);
sheet.addCell(label);
}
for (String key : propertiesList) {
label = new Label(0, lineNumber, key);
sheet.addCell(label);
label = new Label(1, lineNumber, prop.getProperty(key));
sheet.addCell(label);
if (propCHN.getProperty(key) != null) {
label = new Label(2, lineNumber, propCHN.getProperty(key));
sheet.addCell(label);
}
if (localPropertiesWithENG.getProperty(key) == null
|| (!localPropertiesWithENG.getProperty(key).equals(prop.getProperty(key)))) {
label = new Label(3, lineNumber, "true");
sheet.addCell(label);
label = new Label(4, lineNumber, localPropertiesWithENG.getProperty(key));
sheet.addCell(label);
}
lineNumber = lineNumber + 1;
}
//Generate excel
book.write();
book.close();
} catch (Exception e) {
System.out.println("Error when generating Excel file!");
e.printStackTrace();
}
System.out.println("Generated Excel file succeed! \nFile Path: " + xlsConvertPath);
}
/**
* @param propertiesName
* @return
* @date:2012-10-9
* @author:Ji.huazhen
*/
private List sortProperties(Enumeration propertiesName) {
List propertiesList = new ArrayList();
while (propertiesName.hasMoreElements()) {
propertiesList.add(propertiesName.nextElement());
}
Collections.sort(propertiesList);
return propertiesList;
}
}
Properties 生成类:
package com.justcommodity.cxc.properties;
import java.io.File;
import java.io.PrintStream;
import java.util.Properties;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class PropertiesGenerator {
private static PropertiesGenerator gen = null;
/**
* @return
* @date:2012-10-9
* @author:Ji.huazhen
*/
public static PropertiesGenerator getInstance() {
synchronized (PropertiesGenerator.class) {
if (gen == null) {
gen = new PropertiesGenerator();
}
}
return gen;
}
/**
* @param args
* @date:2012-10-8
* @author:Ji.huazhen
*/
public static void main(String[] args) {
//TODO Auto-generated method stub
PropertiesGenerator generator = PropertiesGenerator.getInstance();
//Application
generator.genProperties(PropertiesConstants.APPLICATION_XLS_PATH,
PropertiesConstants.APPLICATION_PROPERTIES_ZH_PATH);
//Tapestry
generator.genProperties(PropertiesConstants.TAPESTRY_XLS_PATH,
PropertiesConstants.TAPESTRY_PROPERTIES_ZH_PATH);
//Contract
generator.genProperties(PropertiesConstants.CONTRACT_XLS_PATH,
PropertiesConstants.CONTRACT_PROPERTIES_ZH_PATH);
//Vessel
generator.genProperties(PropertiesConstants.VESSEL_XLS_PATH,
PropertiesConstants.VESSEL_PROPERTIES_ZH_PATH);
//System
generator.genProperties(PropertiesConstants.SYSTEM_XLS_PATH,
PropertiesConstants.SYSTEM_PROPERTIES_ZH_PATH);
//Strategy
generator.genProperties(PropertiesConstants.STRATEGY_XLS_PATH,
PropertiesConstants.STRATEGY_PROPERTIES_ZH_PATH);
//Stock
generator.genProperties(PropertiesConstants.STOCK_XLS_PATH,
PropertiesConstants.STOCK_PROPERTIES_ZH_PATH);
//Rspo
generator.genProperties(PropertiesConstants.RSPO_XLS_PATH,
PropertiesConstants.RSPO_PROPERTIES_ZH_PATH);
//Report
generator.genProperties(PropertiesConstants.REPORT_XLS_PATH,
PropertiesConstants.REPORT_PROPERTIES_ZH_PATH);
//Profit Center
generator.genProperties(PropertiesConstants.PROFITCENTER_XLS_PATH,
PropertiesConstants.PROFITCENTER_PROPERTIES_ZH_PATH);
//Production
generator.genProperties(PropertiesConstants.PRODUCTION_XLS_PATH,
PropertiesConstants.PRODUCTION_PROPERTIES_ZH_PATH);
//Position
generator.genProperties(PropertiesConstants.POSITION_XLS_PATH,
PropertiesConstants.POSITION_PROPERTIES_ZH_PATH);
//ManageInformation
generator.genProperties(PropertiesConstants.MANAGEINFO_XLS_PATH,
PropertiesConstants.MANAGEINFO_PROPERTIES_ZH_PATH);
//Hedge
generator.genProperties(PropertiesConstants.HEDGE_XLS_PATH,
PropertiesConstants.HEDGE_PROPERTIES_ZH_PATH);
//Future
generator.genProperties(PropertiesConstants.FUTURE_XLS_PATH,
PropertiesConstants.FUTURE_PROPERTIES_ZH_PATH);
//Fulfillment
generator.genProperties(PropertiesConstants.FULFILLMENT_XLS_PATH,
PropertiesConstants.FULFILLMENT_PROPERTIES_ZH_PATH);
//Forex
generator.genProperties(PropertiesConstants.FOREX_XLS_PATH,
PropertiesConstants.FOREX_PROPERTIES_ZH_PATH);
//Financial
generator.genProperties(PropertiesConstants.FINANCIAL_XLS_PATH,
PropertiesConstants.FINANCIAL_PROPERTIES_ZH_PATH);
//Derivative
generator.genProperties(PropertiesConstants.DERIVATIVE_XLS_PATH,
PropertiesConstants.DERIVATIVE_PROPERTIES_ZH_PATH);
}
/**
* @param s
* @return
* @date:2012-10-9
* @author:Ji.huazhen
*/
public static String toUNICODE(String s) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) > 256) {
sb.append('\\');
sb.append("u");
sb.append(Integer.toHexString(s.charAt(i)));
} else {
sb.append(s.charAt(i));
}
}
return sb.toString();
}
/**
* @date:2012-10-8
* @author:Ji.huazhen
*/
private Properties genProperties(String xlsPath, String targetPropertiesPath) {
System.out.println("Start to generate Properties for excel file: " + xlsPath);
Workbook book = null;
Sheet sheet = null;
Cell cell = null;
Properties properties = null;
String key = null;
String value = null;
try {
book = Workbook.getWorkbook(new File(xlsPath));
sheet = book.getSheet(0);
int rownum = sheet.getRows();
properties = new SortedProperties();
for (int i = 1; i < rownum; i++) {
cell = sheet.getCell(0, i);
key = cell.getContents();
cell = sheet.getCell(2, i);
value = cell.getContents();
value = toUNICODE(value);
if (value == null || value.equals("")) {
cell = sheet.getCell(1, i);
value = cell.getContents();
}
properties.setProperty(key, value);
}
book.close();
PrintStream fw = new PrintStream(new File(targetPropertiesPath), "UTF-8");
properties.list(fw);
fw.close();
} catch (Exception e) {
System.out.println("Error when generating Properties file!");
e.printStackTrace();
}
System.out.println("Generated Properties file succeed! \nFile Path: " + targetPropertiesPath);
return properties;
}
}
这些类里面处理起来有点搞的地方有:
1.比较Properties之间的不同,主要还是使用Properties之间的KEY - VALUE进行处理。
2.对Properties进行排序。这里重写了一个properties的子类进行排序。
3.生成中文Propertie需要进行UNICODE转换,但是如果是中文包含英文字母或数字,不能转为UNICODE。比如 编号123 ,要转为\u7f16\u53f7 123
4.对于中文properties与英文properties都是用常量定义了名称和具体项目路径,感觉有点累赘。
写在最后:
写这篇文章的初衷,不知不觉本人从事IT行业,写代码也已经4年了,从08年10月到现在都呆在一家公司。
开始1,2年感觉自己进步很快,公司很多项目很多功能都由我做。不过11年开始后,感觉自己进步的速度放慢了,今年由于忙于结婚,精力也分散了一大部分。
公司核心的一些功能开发我也参与的很少,今年有大半年都去做旧项目的维护与小规模开发,(非核心)。还有两三个月去跑市场+售前+参与写投标书。
由于一直在一家公司,且工作的第5个年头,项目也很熟悉,工作内容也感觉有些重复,不由每天觉得有些枯燥无聊,工作没有积极性。
说实话,对于技术没有工作1,2年的时候那种渴望,因此想在这里记录下每次工作的一些收获,否则感觉每天虽然写了一些代码,都是都没有总结,难免时间长
了觉得空虚,觉得自己没有进步。
最后的最后,希望自己能够坚持下去,哈哈!!