package news_type_mine_tool;

import java.util.List;

import reg.Reg;

import net.HttpPage;

public class HomePageMineUtil {
	
	public static List<String> getNewsList(String homePageUrl,String newsPageReg){
		return Reg.matches(newsPageReg, HttpPage.getPageSource(homePageUrl));
	}
	
	public static String reglizeNewsPageUrl(String newsPageUrl){
		String newsPageReg = "";
		newsPageUrl = newsPageUrl.trim().toLowerCase();
		
		if(newsPageUrl.startsWith("http://")){
			newsPageUrl = newsPageUrl.substring(newsPageUrl.indexOf('/') + 2);
			newsPageUrl = newsPageUrl.substring(newsPageUrl.indexOf('/') + 1);
		}
		
		if(newsPageUrl.contains(".")){
			newsPageReg = '\\' + newsPageUrl.substring(newsPageUrl.indexOf('.'));
			newsPageUrl = newsPageUrl.substring(0 , newsPageUrl.indexOf('.'));
		}
		
		String[] parts = newsPageUrl.split("/");
		for(int i = parts.length - 1 ; i >= 0; i--){
			newsPageReg = "/" + reglize(parts[i]) + newsPageReg;
		}
		
		return newsPageReg.substring(1);
	}
	
	private static int TYPE_CHAR = 1 , TYPE_NUM = 2, TYPE_OTHER = 3 , TYPE_NONE = 0;
	private static String reglize(String data){
		String regStr = "";
		
		int i = 0, count = data.length();
		int charCount = 0, numCount = 0, otherCount = 0, ch ,type = TYPE_NONE;
		while(i < count){
			ch = data.charAt(i++);
			if(ch >= '0' && ch <= '9'){
				if(type == TYPE_CHAR){
					regStr += "[a-zA-Z]{" + charCount + "}";
					charCount = 0;
				}else if(type == TYPE_OTHER){
					regStr += "[^0-9a-zA-Z]{" + otherCount + "}";
					otherCount = 0;
				}
				type = TYPE_NUM;
				numCount++;
			}else if(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'){
				if(type == TYPE_NUM){
					regStr += "[0-9]{" + numCount + "}";
					numCount = 0;
				}else if(type == TYPE_OTHER){
					regStr += "[^0-9a-zA-Z]{" + otherCount + "}";
					otherCount = 0;
				}
				type = TYPE_CHAR;
				charCount++;
			}else{
				if(type == TYPE_CHAR){
					regStr += "[a-zA-Z]{" + charCount + "}";
					charCount = 0;
				}else if(type == TYPE_NUM){
					regStr += "[0-9]{" + numCount + "}";
					numCount = 0;
				}
				type = TYPE_OTHER;
				otherCount++;
			}
		}
		
		if(charCount > 0){
			regStr += "[a-zA-Z]{" + charCount + "}";
		}
		if(numCount > 0){
			regStr += "[0-9]{" + numCount + "}";
		}
		if(otherCount > 0){
			regStr += "[^0-9a-zA-Z]{" + otherCount + "}";
		}
		
		return regStr;
	}
}

@@ -1209,24 +1209,8 @@
 = 0,
- otherCount = 0,
  ch 
@@ -1360,144 +1360,20 @@
 A-Z%5D
-%7B%22 + charCount + %22%7D%22;%0A%09%09%09%09%09charCount = 0;%0A%09%09%09%09%7Delse if(type == TYPE_OTHER)%7B%0A%09%09%09%09%09regStr += %22%5B%5E0-9a-zA-Z%5D%7B%22 + otherCount + %22%7D%22;%0A%09%09%09%09%09othe
++%22;%0A%09%09%09%09%09cha
 rCou
@@ -1577,114 +1577,8 @@
  0;%0A
-%09%09%09%09%7Delse if(type == TYPE_OTHER)%7B%0A%09%09%09%09%09regStr += %22%5B%5E0-9a-zA-Z%5D%7B%22 + otherCount + %22%7D%22;%0A%09%09%09%09%09otherCount = 0;%0A
 %09%09%09%09
@@ -1679,35 +1679,17 @@
 %5Ba-zA-Z%5D
-%7B%22 + charCount + %22%7D
++
 %22;%0A%09%09%09%09%09
@@ -1833,111 +1833,108 @@
 %09%09%09%09
-otherCount++;%0A%09%09%09%7D%0A%09%09%7D
+if(ch == '?' %7C%7C ch == '+')%7B%0A%09%09%09%09%09regStr += '%5C%5C';
 %0A%09%09
-%0A
 %09%09
-if(charCount %3E 0)%7B%0A%09%09%09regStr += %22%5Ba-zA-Z%5D%7B%22 + charCount + %22%7D%22;%0A%09%09%7D
+%7D%0A%09%09%09%09regStr += (char)ch;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09
 %0A%09%09if(
-num
+char
 Coun
@@ -1956,38 +1956,24 @@
 tr += %22%5B
-0-9%5D%7B%22 + numCount + %22%7D
+a-zA-Z%5D+
 %22;%0A%09%09%7D%0A%09
@@ -1976,21 +1976,19 @@
 %09%7D%0A%09%09if(
-other
+num
 Count %3E 
@@ -2010,29 +2010,20 @@
 = %22%5B
-%5E
 0-9
-a-zA-Z
 %5D%7B%22 + 
-other
+num
 Coun

Update HomePageMineUtil.java