package com.Quincy.common;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;

import java.text.SimpleDateFormat;
import java.util.Date;
import com.Quincy.common.paged.pagedConstants;


public class Global implements pagedConstants
{

	public void CreateFile(String filePath,String content)
	{
		File myFilePath=new File(filePath);
		try
		{
			if(!myFilePath.exists()) myFilePath.createNewFile();
			FileWriter resultFile=new FileWriter(myFilePath);
			PrintWriter myFile=new PrintWriter(resultFile);
			myFile.println(new String(content.getBytes("GBK"),"ISO-8859-1"));
			resultFile.close();
		}catch(Exception ex)
		{
		}	
	}	
	
	public static String[] splitLines(String line, String pSeparator) //根据分隔符分隔字符串
	{
		if(line == null)throw new NullPointerException("内容不能为空．");
		if(pSeparator == null)throw new NullPointerException("分隔符不能为空．");
		if(pSeparator.length() == 0)	throw new IllegalArgumentException("分隔符长度为空．");
		ArrayList tmpArr = new ArrayList();
		int start = 0;
		int SeparatorLen = pSeparator.length();
		int end = line.indexOf(pSeparator);
		while(end != -1)
		{
			tmpArr.add(line.substring(start, end));
			start = end + SeparatorLen;
			end = line.indexOf(pSeparator, start);
		}
		tmpArr.add(line.substring(start, line.length()));
		String[] result = new String[tmpArr.size()];
		tmpArr.toArray(result);
		return result;
	}
	

	
	public String getFileContent(String fileName)
	{
		StringBuffer ret=new StringBuffer();
        try
        { 
        	File f=new File(fileName);
        	FileReader read=new FileReader(f);
        	BufferedReader buffer=new BufferedReader(read);	
			String tmpStr="";
        	while((tmpStr=buffer.readLine())!=null)
        	{
		        ret.append(tmpStr);
		    }    
		    return ret.toString();
        }
        catch(IOException e) 
        { 
        	return null;
        }			
	}
	
  	public String getFileName(String pFileName) 
  	{
    	int  i=pFileName.lastIndexOf("\\");
    	if(i<0||i>=pFileName.length()-1) 
    	{
      		i=pFileName.lastIndexOf("/");
      		if(i<0||i>=pFileName.length()-1) return  pFileName;
    	}
    	return  pFileName.substring(i+1);
  	}
  		
	public boolean fileRename(String srcName,String destName) throws IOException
	{
		boolean succeed=false;
       	File srcFile=new File(srcName);
       	if (srcFile.renameTo(new File(destName))) succeed=true;
        return succeed;
	}	
	
	public String uniteYYYYMMDD(String date)	
	{
		return " to_char("+date+",'YYYY')||'年'||to_char("+date+",'MM')||'月'||to_char("+date+",'DD')||'日' ";
	}	
	
    public String intiQuerySQL(String sql, int startIndex, int size)
    {
        StringBuffer querySQL = new StringBuffer();
        if (size <= MAX_PAGE_SIZE) 
        {
            querySQL.append("select * from (select my_table.*,rownum as my_rownum from(")
                    .append(sql)
                    .append(") my_table where rownum<").append(startIndex + size)
                    .append(") where my_rownum>=").append(startIndex);
        } else 
        {
            querySQL.append("select * from (select my_table.*,rownum as my_rownum from(")
                    .append(sql)
                    .append(") my_table ")
                    .append(") where my_rownum>=").append(startIndex);
        }
        return querySQL.toString();
    }
    
	public static String UnicodeToChinese(String str)
	{
		try
		{
			if(str==null || str.equals(""))  return "";
			return new String(str.getBytes("ISO8859_1"),"GBK");
		}
		catch(Exception e)
		{
			return str;
		} 
	}
	public static String ChineseToUnicode(String s)
	{
		try
		{
			if(s==null || s.equals(""))  return "";
			return new String(s.getBytes("GBK"),"ISO8859_1");
		}
		catch(Exception e) 
		{ 
			return s; 
		} 
	} 	
	
	public static String WriteToSQL(String s)
  	{
    	s = Replace(s,"[","[[]");
    	s = Replace(s,"'","''");
    	s = Replace(s,"_","[_]");
    	s = Replace(s,"%","[%]");
    	return s;
  	}
  	public static String WriteHtml(String s)
  	{
    	s = Replace(s,"&","&amp;");
    	s = Replace(s,"<","&lt;");
    	s = Replace(s,">","&gt;");
    	s = Replace(s,"\t","    ");
    	s = Replace(s,"\r\n","\n");
    	s = Replace(s,"\n","<br>");
    	s = Replace(s,"  "," &nbsp;");
   		return s;
  	}
  	
  	public static String ReadHtml(String s)
  	{
    	s = Replace(s,"&amp;","&");
    	s = Replace(s,"&lt;","<");
    	s = Replace(s,"&gt;",">");
    	s = Replace(s,"    ","\t");
    	s = Replace(s,"\n","\r\n");
    	s = Replace(s,"<br>","\n");
    	s = Replace(s," &nbsp;","  ");
   		return s;
  	}  	
  	
  	public static String Replace(String source,String oldString,String newString)
  	{
    	if(source == null) return null;
    	StringBuffer output = new StringBuffer();
    	int lengOfsource = source.length();
    	int lengOfold = oldString.length();
    	int posStart = 0;
    	int pos;
    	while((pos = source.indexOf(oldString,posStart)) >= 0)
    	{
      		output.append(source.substring(posStart,pos));
      		output.append(newString);
      		posStart = pos + lengOfold;
    	}
    	if(posStart < lengOfsource)
    	{
      		output.append(source.substring(posStart));
    	}
    	return output.toString();
  	}    
  
   	public String filterNull(String pstr)//处理空格
   	{
   		if (pstr==null||pstr.trim().equals("")) pstr="&nbsp;";
   		return pstr;
   	}
      
  
}	