/*
* @(#)MisUtils.java 1.00.000 2005-6-12
*
* Copyright 2005 Xian KaiWang Technology, Inc. All rights reserved.
* Xian KaiWang PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package sample.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.BreakIterator;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
/**
*
*
* @author Zhou Weijun
*
* 2005-6-12 16:48:24
*/
public class MisUtils {
private static final char[] QUOTE_ENCODE = """.toCharArray();
private static final char[] AMP_ENCODE = "&".toCharArray();
private static final char[] LT_ENCODE = "<".toCharArray();
private static final char[] GT_ENCODE = ">".toCharArray();
private static MessageDigest digest = null;
/**
* @param string
*/
public static boolean validateString(String string) {
if ((string != null) && (!string.equals(""))) {
return true;
}
return false;
}
/**
*
* @param floatString
* @param minMaxFlag
* @return
*/
public static BigDecimal convertFloat(String floatString, int minMaxFlag) {
BigDecimal floatValue = null;
// Parse a string to float value
if (validateString(floatString)) {
try {
floatValue = new BigDecimal(Float.parseFloat(floatString));
} catch (NumberFormatException e) {
floatValue = null;
}
}
// If convert failed, then set a default value depend on flag
if (floatValue == null) {
if (minMaxFlag == MisConstants.MIN_FLAG) {
floatValue = new BigDecimal(-10000000.00f);
} else {
floatValue = new BigDecimal(10000000.00f);
}
}
return floatValue;
}
/**
*
* @param dateString
* @param startEndFlag
* @return
*/
public static Date convertDate(String dateString, int startEndFlag) {
Date date = null;
// Parse a string to date value
if (validateString(dateString)) {
DateFormat dateFormat = new SimpleDateFormat(MisConstants.DATE_PATTERN);
try {
date = dateFormat.parse(dateString);
} catch (ParseException e) {
date = null;
}
}
// If convert failed, then set a default date depend on flag
if (date == null) {
if (startEndFlag == MisConstants.MIN_FLAG) {
Calendar calendar = Calendar.getInstance();
calendar.set(1900, 0, 1, 0, 0, 0);
date = calendar.getTime();
} else {
date = new Date();
}
}
return date;
}
/**
*
* @param dateString
* @param startEndFlag
* @return
*/
public static Calendar convertCalendar(String dateString, int startEndFlag) {
Calendar calendar = null;
// Parse a string to date value
if (validateString(dateString)) {
DateFormat dateFormat = new SimpleDateFormat(MisConstants.DATE_PATTERN);
try {
Date date = dateFormat.parse(dateString);
calendar = Calendar.getInstance();
calendar.setTime(date);
} catch (ParseException e) {
calendar = null;
}
}
// If convert failed, then set a default date depend on flag
if (calendar == null) {
if (startEndFlag == MisConstants.MIN_FLAG) {
calendar = Calendar.getInstance();
calendar.set(1900, 0, 1, 0, 0, 0);
} else {
calendar = Calendar.getInstance();
calendar.setTime(new Date());
}
}
return calendar;
}
/*
* @param path
* @param request
* @return
*/
public static String retrieveAbsolutePath(String path, ServletContext servletContext, HttpServletRequest request) {
String realContextPath = servletContext.getRealPath(MisConstants.PATH_SEPARATOR);
String absolutePath = realContextPath + path;
// Format it
if (absolutePath != null) {
absolutePath = absolutePath.replace('/', '\\');
}
return absolutePath;
}
/**
*
* @param path
* @return
*/
public static String formatPath(String path) {
String formattedPath = path;
String fileSeparator = System.getProperty(MisConstants.FILE_SEPARATOR_NAME);
if (MisUtils.validateString(formattedPath)) {
if (!formattedPath.endsWith(fileSeparator)) {
formattedPath += fileSeparator;
}
}
return formattedPath;
}
/**
*
* @param fileName
* @param request
* @return
* @throws IOException
*/
public static String getUploadOutputFile(String fileName, ServletContext servletContext, HttpServletRequest request) throws IOException {
String filePath = MisConstants.BLANK_STRING;
String fileSeparator = System.getProperty(MisConstants.FILE_SEPARATOR_NAME);
// Check upload existed? If not, Create it
String realPath = servletContext.getRealPath(MisConstants.PATH_SEPARATOR);
String uploadPath = formatPath(realPath);
if (!uploadPath.endsWith(fileSeparator)) {
uploadPath += fileSeparator;
}
uploadPath += MisConstants.UPLOAD_FOLDER;
File uploadFolderFile = new File(uploadPath);
if (!uploadFolderFile.exists()) {
uploadFolderFile.mkdir();
}
// Generate file name
int ipAddress = 0;
String remoteHost = request.getRemoteHost();
InetAddress inetAddress = InetAddress.getByName(remoteHost);
if (inetAddress != null) {
byte[] byteAddress = inetAddress.getAddress();
for (int i=0; i<4; i++) {
ipAddress = ( ipAddress << 8 ) - Byte.MIN_VALUE + (int) byteAddress[i];
}
}
if (ipAddress < 0) {
ipAddress = Math.abs(ipAddress);
}
long currentTime = new Date().getTime();
// Get file path
uploadPath = formatPath(uploadPath);
filePath = uploadPath + ipAddress + currentTime;
File folder = new File(filePath);
if (!folder.exists()) {
folder.mkdir();
}
// Make upload file full path
filePath = formatPath(filePath);
filePath += fileName;
return filePath;
}
/**
* @param fileName
*/
public static void deleteUploadFile(String fileName) throws Exception {
if (MisUtils.validateString(fileName)) {
try {
File file = new File(fileName);
if (file.exists()) {
file.delete();
}
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
}
/*
* @param string
* @param i
* @param j
* @param request
*/
public static void setDateField(String listName, int start, int end, int step, HttpServletRequest request) {
List listData = new ArrayList();
for (int i = start; i <= end; i += step) {
listData.add(new Integer(i));
}
request.setAttribute(listName, listData);
}
/**
* @param id
* @return
*/
public static boolean isValidId(String id) {
boolean result = false;
if (MisUtils.validateString(id)) {
if (id.length() == MisConstants.UUID_LENGTH) {
result = true;
}
}
return result;
}
/**
*
* @param string
* @return
*/
public static String getLikeString(String string) {
String likeString = "";
if (MisUtils.validateString(string)) {
likeString = "%" + string + "%";
}
return likeString;
}
/**
*
* @param values
* @return
*/
public static String Array2String(String[] values) {
String result = "";
if (values == null) {
return result;
}
int len = values.length;
for (int i = 0; i < len; i++) {
result += values[i] + ",";