{
static OMSLog log = Tools.getLog(Tools.class);
/**
* 文件内容集合 key :实际文件名称 value :Map<实际文件中的sheet名称, Map<表字段名称,
* String[文件中列索引,文件中实际列名]>>
*/
private static Map
null;
/**
* 名称集合 <文件名称,<实际文件中的sheet名称,表名(常量)>
*/
private static Map
/**
* 表名称对应的列集合 <表名,<列名,String[文件中列索引,文件中实际列名]>
*/
private static Map
/**
* 数据表集合 key :表名(常量) value :map<列索引,列名>
*/
private static Map
/**
* 文件下的sheet名称集合(按读入顺序),导出
*/
private static Map
/**
* 支持导出文件名称集合
*/
private static List
/**
* 插入
*/
public final static int insert = 1;
/**
* 修改
*/
public final static int update = 2;
/**
* 删除
*/
public final static int delete = 3;
/**
* 文件语言版本
*/
private static Map
/**
* 取得日志
*
* @param clas
* @return
*/
public static OMSLog getLog(Class> clas)
{
String log4jName = getStringNoNull(System.getProperty("log4jname"));
if (!"".equals(log4jName))
{
PropertyConfigurator.configure(Constant.PROJECT_DIR + "/config/"
+ log4jName + ".properties");
}
return OMSLogFactory.getLog(clas);
}
/**
* 得到对应资源的资源实例
*
* @param resourceFilePath 资源文件路径
* @return ResourceBundle 资源实例
*/
public static ResourceBundle getResourceBundle(String resourceFilePath)
{
ResourceBundle resourceBundle =
ResourceBundle.getBundle(resourceFilePath);
return resourceBundle;
}
/**
* 得到对应资源的资源实例
*
* @param resourceFilePath 资源文件路径
* @return ResourceBundle 资源实例
*/
public static ResourceBundle getResourceBundle(String resourceFilePath,
Locale locale)
{
ResourceBundle resourceBundle =
ResourceBundle.getBundle(resourceFilePath, locale);
return resourceBundle;
}
/**
* 得到属性文件实例
*/
public static Properties getPropertiesByFile(String filePath)
{
InputStream inputStream = null;
Properties properties = new Properties();
try
{
File file = new File(filePath);
inputStream = new FileInputStream(file);
properties.load(inputStream);
}
catch (Exception e)
{
log.error(e.getMessage(), e);
}
finally
{
try
{
inputStream.close();
}
catch (IOException e)
{
log.error(e.getMessage(), e);
}
}
return properties;
}
/**
* 初始化导入文件功能的配置文件
*/
private static void initConfig()
{
XMLReader reader = new XMLReader();
fileMap = reader.readConfigXMLFile();
dataBaseMap = reader.readDatabaseXMLFile();
fileNameMap = reader.getSheetNameMap();
exportFileNameList = reader.getExportFileNameList();
tableMap = reader.getTableMap();
fileSheetNameMap = reader.getFileSheetNameMap();
fileLanguageVersion = reader.getFileLanguageVersion();
}
/**
* 支持导出文件名称集合
*/
public static List
{
if (exportFileNameList == null)
{
initConfig();
}
return exportFileNameList;
}
/**
* 根据表名称从database.xml文件中得到表字段集合
*
* @param tableName 表名称
* @return Map
*/
public static Map
{
if (dataBaseMap == null)
{
initConfig();
}
return dataBaseMap.get(tableName.toLowerCase(Locale.getDefault()));
}
/**
* 得到对应的EXCEL文件内容集合
*
* @param fileName 文件名称
* @return Map
*/
public static Map
String fileName)
{
if (fileMap == null)
{
initConfig();
}
return fileMap.get(fileName.toLowerCase(Locale.getDefault()));
}
/**
* 得到对应的EXCEL文件内一个sheet页的内容集合
*
* @param fileName 文件名称
* @param sheetName sheet页名称
* @return Map
*/
public static Map
String sheetName)
{
Map
getSheetsByFile(fileName.toLowerCase(Locale.getDefault()));
return map.get(sheetName.toLowerCase(Locale.getDefault()));
}
/**
* 得到表名对应配置的列属性集合
*
* @param fileName 表名称
* @return Map
*/
public static Map
{
if (tableMap == null)
{
initConfig();
}
return tableMap.get(tableName.toLowerCase(Locale.getDefault()));
}
/**
* 得到EXCEL中sheet页对应的表名
*
* @param sheetName sheet页名
* @return String 表名
*/
public static String getTableName(String fileName, String sheetName)
{
if (fileNameMap == null)
{
initConfig();
}
return fileNameMap.get(fileName.toLowerCase(Locale.getDefault())).get(
sheetName.toLowerCase(Locale.getDefault()));
}
/**
* 得到EXCEL文件中sheet页的名称集合
*
* @return List
*/
public static List
{
if (fileSheetNameMap == null)
{
initConfig();
}
return fileSheetNameMap.get(fileName.toLowerCase(Locale.getDefault()));
}
/**
* 配置文件中的文件集合
*
* @return Map<实际文件中的sheet名称, Map<表字段名称, String[文件中列索引,文件中实际列名]>>
*/
public static Map
{
if (fileMap == null)
{
initConfig();
}
return fileMap;
}
/**
* 取当前数据库中数据版本
*
* @return String 当前数据版本
*/
public static int getDataBaseVersion(int languageId)
{
int version = 0;
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try
{
connection = JdbcUtil.getConnection();
statement = connection.createStatement();
rs =
statement
.executeQuery("select Version,languageId from version where languageId="
+ languageId);
while (rs.next())
{
String s = rs.getString("version");
version = Integer.parseInt(s);
}
}
catch (DataBaseException e)
{
log.error(e.getMessage(), e);
}
catch (SQLException e)
{
log.error(e.getMessage(), e);
}
finally
{
JdbcUtil.release(rs, statement);
}
return version;
}
/**
* 取出当前表中最大ID值
*
* @return 表中ID值
*/
public static long getTableMaxID(String tableName, String idName)
{
long id = 0;
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try
{
connection = JdbcUtil.getConnection();
statement = connection.createStatement();
rs =
statement.executeQuery("select max(" + idName + ") from "
+ tableName);
while (rs.next())
{
id = rs.getLong(1);
}
}
catch (DataBaseException e)
{
log.error(e.getMessage(), e);
}
catch (SQLException e)
{
log.error(e.getMessage(), e);
}
finally
{
JdbcUtil.release(rs, statement);
}
id++;
return id;
}
/**
* 取出当前表中最大ID值(默认ID列名称为“id”)
*
* @return 表中ID值
*/
public static long getTableMaxID(String tableName)
{
return getTableMaxID(tableName, "id");
}
/**
* 请空表数据
*
* @param histableName 表名
* @throws DataBaseException
*/
public static void clearTableData(String tableName)
throws DataBaseException
{
String sql = "delete from " + tableName;
PreparedStatement preparedStatement = null;
try
{
preparedStatement = JdbcUtil.getConnection().prepareStatement(sql);
preparedStatement.execute();
}
catch (SQLException e)
{
throw new DataBaseException(e.getMessage(), e);
}
finally
{
JdbcUtil.release(null, preparedStatement);
}
}
/**
* 从文件路径中取出文件名称,不包含文件类型后缀
*
* @param filePath
* @return
*/
public static String getFileNameFromFilePath(String filePath)
{
String fileName =
filePath.substring(filePath.lastIndexOf("\\") + 1,
filePath.lastIndexOf("."));
// 处理有些文件配置路径为test/file.xml格式
if (fileName.indexOf("/") != -1)
{
fileName =
fileName.substring(fileName.lastIndexOf("/") + 1,
fileName.length());
}
return fileName;
}
/**
* 获取操作类型
*
* @param action
* @return
*/
public static String getOperationByAction(int action)
{
String op = "";
switch (action)
{
case insert:
op = Constant.OPERATION_CREATE;
break;
case update:
op = Constant.OPERATION_UPDATE;
break;
case delete:
op = Constant.OPERATION_DELETE;
break;
default:
break;
}
return op;
}
/**
* 获取操作类型对应值
*
* @param action
* @return
*/
public static int getActionByOperation(String operation)
{
int action = 0;
if (Constant.OPERATION_CREATE.equalsIgnoreCase(operation))
{
action = insert;
}
else if (Constant.OPERATION_UPDATE.equalsIgnoreCase(operation))
{
action = update;
}
else if (Constant.OPERATION_DELETE.equalsIgnoreCase(operation))
{
action = delete;
}
return action;
}
/**
* 得到文件中配置版本号子,如果配置不正确返-1。
*
* @return int 文件中配置版本号
*/
public static int getFileVersion(int languageId, ExcelReader excelReader)
{
int result = 0;
try
{
Locale locale = Locale.getDefault();
if (languageId == 1)
{
locale = Locale.CHINA;
}
else if (languageId == 2)
{
locale = Locale.US;
}
String fileVersion =
getResourceBundle(
"com.huawei.idesign.tablesetdatas.dataprocess.properties.tool",
locale).getString("file_version");
for (int i = 0; i < excelReader.getSheetNum(); i++)
{
String execlSheetName = excelReader.getSheetNameByIndex(i);
if (fileVersion.equalsIgnoreCase(execlSheetName))
{
List
- > list = excelReader.getAllData(i);
String version = list.get(0).get(0);
result = (int) Double.parseDouble(version);
}
}
}
catch (Exception e)
{
log.error(e.getMessage(), e);
}
return result;
}
/**
* 文件语言版本
*
* @return map<文件名,语言版本>
*/
public static Map
{
if (fileLanguageVersion == null)
{
initConfig();
}
return fileLanguageVersion;
}
/**
* 将excel中的列索引转换为下标
*
* @param op
* @return
*/
public static int getIndex(String op)
{
String alphabet = "abcdefghijklmnopqrstuvwxyz";
try
{
// 如果是数字,直接返回,是字母再转换
int index = Integer.parseInt(op) - 1;
return index;
}
catch (NumberFormatException e)
{
String s = op.toLowerCase(Locale.getDefault());
if (s.length() == 1)
{
return alphabet.indexOf(s);
}
else if (s.length() == 2)
{
String fir = s.substring(0, 1);
String sed = s.substring(1, 2);
int f =
(alphabet.indexOf(fir) + 1) * 26 + alphabet.indexOf(sed);
return f;
}
else
{
return -1;
}
}
}
public static String getStringNoNull(Object str)
{
if (str == null)
{
return "";
}
else
{
return str.toString().trim();
}
}
public static String getPropertyByString(String key, String defaultValue)
{
return getStringNoNull(Tools.getPropertiesByFile(
Constant.CipherKey_FILE_PATH).getProperty(key, defaultValue));
}
/**
* add fuqiang
* 需要修改的properties键值对集合
* @param listproperties
* @see [类、类#方法、类#成员]
*/
public static void modifyProperties(Properties properties)
{
InputStream inputStream = null;
OutputStream fos = null;
Properties tempProper = new Properties();
File file = new File(Constant.CipherKey_FILE_PATH);
try
{
inputStream = new FileInputStream(file);
tempProper.load(inputStream);
fos = new FileOutputStream(file);
tempProper.setProperty("userName", properties.getProperty("userName"));
tempProper.setProperty("passwd", properties.getProperty("passwd"));
tempProper.store(fos, null);
}
catch (Exception e)
{
log.error("Tools calss Setting PropertyValue is fail!"+e.getMessage());
}finally
{
try
{
if(null != fos)
{
fos.close();
}
if(null != inputStream)
{
inputStream.close();
}
}
catch (IOException e)
{
log.error("Tools calss close OutputStream fial!"+e.getMessage());
}
}
}
public static int getPropertyByInt(String key, int defaultValue)
{
int result = defaultValue;
String strValue = getStringNoNull(Tools.getPropertiesByFile(
Constant.CipherKey_FILE_PATH).getProperty(key));
try {
result = Integer.parseInt(strValue);
} catch (NumberFormatException e) {
log.error(e.getMessage());
}
return result;
}
public static boolean authorise()
{
/**
* add fuqiang
* 增加用户鉴权
*/
boolean isLogin = false;
try
{
//得到解密后的passwd值
String tempPasswd = CipherUtil.decryptPwd(getPropertyByString("passwd",""));
//得到配置文件中的user值
String tempUser = getPropertyByString("userName","");
//得到配置的URL
String tempUrl = getPropertyByString("url","");
isLogin = HttpValidate.validConnector(tempUser, tempPasswd, tempUrl);
// if("admin".equals(tempUser) && "admin".equals(tempPasswd))
// {
// isLogin = true;
// }
}
catch (Exception e1)
{
log.error("CFGSelectDialog calss Getting PropertyValue is fail!"+e1.getMessage());
}
return isLogin;
}
==================================================================================================================
public class XMLReader
{
OMSLog log = Tools.getLog(this.getClass());
private Map
new HashMap
private Map
new HashMap
private Map
new HashMap
/**
* 支持导出文件名称集合
*/
List
/**
* 文件语言版本
*/
Map
/**
* 文件语言版本
*/
public Map
{
return fileLanguageVersion;
}
/**
* 支持导出文件名称集合
*/
public List
{
return exportFileNameList;
}
/**
* 文件下的sheet名称集合(按读入顺序)
*
* @return
*/
public Map
{
return fileSheetNameMap;
}
/**
* 表名对应的Sheet页名称
*
* @return Map<文件名称,
*/
public Map
{
return fileNameMap;
}
public Map
{
return this.tableMap;
}
/**
* 解析Sheet页节点下的所有列配置
*
* @param columnNodes 列节点List
* @return Map<数据库表字段,对应Sheet页中的列名>
*/
private Map
{
Map
for (int k = 0; k < columnNodes.getLength(); k++)
{
Node columnNode = columnNodes.item(k);
if (columnNode.getNodeType() == 1)
{
String tablecolumn =
columnNode.getAttributes().getNamedItem("tablecolumn")
.getNodeValue().trim().toLowerCase(Locale.getDefault());
String columnName =
columnNode.getAttributes().getNamedItem("columnname")
.getNodeValue().trim().toLowerCase(Locale.getDefault());
String index =
columnNode.getAttributes().getNamedItem("index")
.getNodeValue().trim();
columnMap.put(tablecolumn, new String[] { index, columnName });
}
}
return columnMap;
}
/**
* 解析表节点下的所有列配置
*
* @param columnNodes 列节点List
* @return Map<列索引,列名>
*/
private Map
{
Map
for (int k = 0; k < columnNodes.getLength(); k++)
{
Node columnNode = columnNodes.item(k);
if (columnNode.getNodeType() == 1)
{
String columnName =
columnNode.getAttributes().getNamedItem("name")
.getNodeValue().trim().toLowerCase(Locale.getDefault());
String index =
columnNode.getAttributes().getNamedItem("index")
.getNodeValue().trim().toLowerCase(Locale.getDefault());
columnMap.put(Integer.parseInt(index), columnName);
}
}
return columnMap;
}
/**
* 解析EXCEL文件配置文件
*
* @return Map
*/
public Map
{
String filePath =Constant.PROJECT_DIR+ Constant.CONFIG_FILE_PATH;
DocumentBuilder builder;
Document doc;
Map
new HashMap
try
{
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
log.info("Load file [" + filePath + "]");
doc = builder.parse(new File(filePath));
Element root = doc.getDocumentElement();
NodeList nodeList = root.getElementsByTagName("excelfile");
for (int i = 0; i < nodeList.getLength(); i++)
{
Node excelfileNode = nodeList.item(i);
if (excelfileNode.getNodeType() == 1)
{
String excelfileName =
excelfileNode.getAttributes().getNamedItem("filename")
.getNodeValue().trim()
.toLowerCase(Locale.getDefault());
String isExport= excelfileNode.getAttributes().getNamedItem("supportExport")
.getNodeValue().trim();
String language= excelfileNode.getAttributes().getNamedItem("language")
.getNodeValue().trim();
//保存支持导出文件名称
if (Boolean.valueOf(isExport))
{
exportFileNameList.add(excelfileName);
}
fileLanguageVersion.put(excelfileName, language);
NodeList sheetNodes = excelfileNode.getChildNodes();
fileMap.put(excelfileName,
readSheetNode(excelfileName, sheetNodes));
}
}
}
catch (Exception e)
{
log.error(e.getMessage(), e);
}
return fileMap;
}
/**
* 读取数据库配置表信息
*
* @return Map<表名,Map<列索引,列名>>
*/
public Map
{
String filePath = Constant.PROJECT_DIR+Constant.DATABASE_FILE_PATH;
DocumentBuilder builder;
Document doc;
Map
new HashMap
try
{
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
log.info("Load file [" + filePath + "]");
doc = builder.parse(new File(filePath));
Element root = doc.getDocumentElement();
NodeList nodeList = root.getElementsByTagName("table");
for (int i = 0; i < nodeList.getLength(); i++)
{
Node excelfileNode = nodeList.item(i);
if (excelfileNode.getNodeType() == 1)
{
String tableName =
excelfileNode.getAttributes().getNamedItem("name")
.getNodeValue().trim()
.toLowerCase(Locale.getDefault());
NodeList columnNodes = excelfileNode.getChildNodes();
fileMap.put(tableName, readColumnNode(columnNodes));
}
}
}
catch (Exception e)
{
log.error(e.getMessage(), e);
}
return fileMap;
}
/**
* 解析文件节点下的所有Sheet页配置
*
* @param columnNodes Sheet页节点List
* @return Map
*/
private Map
String excelfileName, NodeList sheetNodes)
{
Map
new HashMap
Map
List
for (int k = 0; k < sheetNodes.getLength(); k++)
{
Node sheetNode = sheetNodes.item(k);
if (sheetNode.getNodeType() == 1)
{
String tableName =
sheetNode.getAttributes().getNamedItem("tableName")
.getNodeValue().trim().toLowerCase(Locale.getDefault());
String sheetName =
sheetNode.getAttributes().getNamedItem("sheetname")
.getNodeValue().trim().toLowerCase(Locale.getDefault());
sheetNameMap.put(sheetName, tableName);
NodeList columnNodes = sheetNode.getChildNodes();
Map
sheetMap.put(sheetName, columnMap);
if (tableMap.containsKey(tableName))
{
tableName = tableName + "_" + sheetName;
}
tableMap.put(tableName, columnMap);
list.add(sheetName);
}
}
fileNameMap.put(excelfileName, sheetNameMap);
fileSheetNameMap.put(excelfileName, list);
return sheetMap;
}
=========================================================================================================================
加密与解密
public class CipherUtil
{
private static OMSLog log = Tools.getLog(CipherUtil.class);
private static String CIPHER_TYPE = "AES";
private static String CIPHER_MODE = "AES/ECB/PKCS5Padding";
/**
* 用AES算法解密
*/
public static String decryptPwd(String input)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IOException, IllegalBlockSizeException,
BadPaddingException
{
return decryptPwd(input,false);
}
/**
* 服务端用AES算法解密
*/
public static String decryptPwd(String input,boolean isServer)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IOException, IllegalBlockSizeException,
BadPaddingException
{
byte[] keydata = isServer?getKey(Constant.CipherKey_SERVER_FILE_PATH):getKey();
SecretKeySpec key = new SecretKeySpec(keydata, 0, 16, CIPHER_TYPE);
Cipher cipher = Cipher.getInstance(CIPHER_MODE);
cipher.init(Cipher.DECRYPT_MODE, key);
BASE64Decoder decoder = new BASE64Decoder();
byte[] data = decoder.decodeBuffer(input);
byte[] decryptData = cipher.doFinal(data, 0, data.length);
return new String(decryptData);
}
/**
* 用AES算法加密
*
* @throws IOException
*/
public static String encryptPwd(String input)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
IOException
{
SecretKeySpec key = new SecretKeySpec(getKey(), 0, 16, CIPHER_TYPE);
Cipher cipher = Cipher.getInstance(CIPHER_MODE);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] bs = cipher.doFinal(input.getBytes());
return new sun.misc.BASE64Encoder().encode(bs);
}
/**
* 生成AES加密密钥
*
* @throws IOException
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
private static byte[] getKey()
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
IOException
{
return getKey(Constant.CipherKey_FILE_PATH);
}
/**
* 生成AES加密密钥
*
* @throws IOException
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
private static byte[] getKey(String path)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
IOException
{
String initKey = "ci~+Ides5ign*1=)";
String input =
Tools.getStringNoNull(Tools.getPropertiesByFile(
path).getProperty("CipherKey"));
SecretKeySpec key = new SecretKeySpec(initKey.getBytes(), CIPHER_TYPE);
Cipher cipher = Cipher.getInstance(CIPHER_MODE);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] bs = cipher.doFinal(input.getBytes());
return bs;
}
=========================
http://www.iteye.com/topic/1122076