package com.qxueyou.scc.base.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
/**
* ǰÈý¸öÊôÐÔ¸ù¾Ýʵ¼Ê´æ´¢Î»ÖÃÉèÖÃ
* @author ody.yuan
*
*/
public class MyJsCssVersionUtils {
/** web¹¤³ÌµÄ·¢²¼Â·¾¶ */
private static String webContent = "qxueyou";
/** web¹¤³ÌÖÐwebÎļþ¼ÐµÄ¾ø¶Ô·¾¶ D:\project\workspace_web\versioncontroller0\web\js\qxueyou.js */
/** web¹¤³ÌÖÐwebÎļþ¼ÐµÄ¾ø¶Ô·¾¶ D:\project\workspaceStkkk\qxueyou_web\web\js\qxueyou.js */
private static String webFileAbsolutePath = "C:" + File.separator + "workspace" + File.separator + "qxueyou_web";
/** ɨÃèjs¡¢css¡¢htmlÎļþµÄÎļþ¼Ð¾ø¶Ô·¾¶£¬¹Ì¶¨ÉèÖÃΪwebĿ¼ */
private static String fileScanDirec = "C:" + File.separator + "workspace" + File.separator + "qxueyou_web";
/** Æ¥ÅähtmlÖÐÒýÈëjsÎļþ */
private static Pattern scriptImportPattrrn = Pattern.compile("
if (scriptImportPattrrn.matcher(line).find()) {
//ÒÔÒýºÅ·Ö¸ô£¬ÒÔ/¿ªÊ¼
String md5Version = null;
String strLine[] = line.split("\"");
for(String str:strLine){
if(null != md5Version){
break;
}
if(str.startsWith("/")){
//Èç¹ûÒÑÓа汾ºÅ£¬°´?·Ö¸ô£¬È¡µÚÒ»¸ö
if(str.lastIndexOf("?version") != -1){
str = str.substring(0, str.lastIndexOf("?version"));
}
md5Version = map.get(str);
}
}
int pos = line.lastIndexOf(".js");
if(null != md5Version){
String modLine = line.substring(0, pos) + ".js?version=" + md5Version+ "\">";
return modLine;
}
//Æ¥ÅähtmlÒýÈëcssÎļþ eg£º
} else if (cssImportPattrrn.matcher(line).find()) {
//ÒÔÒýºÅ·Ö¸ô£¬ÒÔ/¿ªÊ¼
String md5Version = null;
String strLine[] = line.split("\"");
for(String str:strLine){
if(null != md5Version){
break;
}
if(str.startsWith("/")){
//Èç¹ûÒÑÓа汾ºÅ£¬°´?·Ö¸ô£¬È¡µÚÒ»¸ö
if(str.lastIndexOf("?version") != -1){
str = str.substring(0, str.lastIndexOf("?version"));
}
md5Version = map.get(str);
}
}
int pos = line.lastIndexOf(".css");
if(null != md5Version){
String modLine = line.substring(0, pos) + ".css?version=" + md5Version+ "\" rel=\"stylesheet\">";
return modLine;
}
}
return line;
}
/**
*
* @param args
*/
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
String version = df.format(date);
Map objMap = new HashMap();
//Ìí¼ÓjsÎļþ
getFileMD5Map(fileScanDirec,".js",objMap,version);
//Ìí¼ÓCSSÎļþ
getFileMD5Map(fileScanDirec,".css",objMap,version);
//Ñ»·htmlÎļþ£¬ÐÞ¸ÄÆäÖеÄcssºÍjsÒýÈë·¾¶
getHtml(fileScanDirec,".ftl",objMap);
System.out.println("successful");
}
}