package com.lerx.v6.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lerx.entities.UploadedFile;
import com.lerx.portal.obj.UploadArgs;
import com.lerx.sys.util.FileUtil;
import com.lerx.sys.util.StringUtil;

public class ReciveUtil {
	
	
	public static void logw(String path,String file,String title) {
		FileUtil.addStringToFile(path, file, title, "utf-8");
	}
	
	public static  String contentImgDown(UploadArgs ua,String htmlStr,HttpServletRequest request,HttpServletResponse response) {
		String img = "";
	    Pattern p_image;
	    Matcher m_image;
	    //     String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
	    String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
	    
	    p_image = Pattern.compile
	            (regEx_img, Pattern.CASE_INSENSITIVE);
	    m_image = p_image.matcher(htmlStr);
	    String srcurl;
	    
	    while (m_image.find()) {
            // 得到<img />数据
            img = m_image.group();
            // 匹配<img>中的src数据
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)",Pattern.CASE_INSENSITIVE).matcher(img);
            while (m.find()) {
            	srcurl= m.group(1);
            	UploadedFile uf=UploadUtil.downUrlFile(ua, srcurl,request, response);
            	if (uf!=null) {
            		htmlStr=StringUtil.strReplace(htmlStr, srcurl, uf.getUrl());
                	System.out.println("正文中发现："+srcurl+" 已下载替换成："+uf.getUrl());
            	}
            	
            }
        }
	    
	    return htmlStr;
	}

}
