package org.zhios.website.admin.model;


public class HtmlOpt {
	
	//If the attribute is only in innerStr,use this function to get attribute value. 
	//attr: attribute name.
	//return: attribute value.
	public static String readFirstAttrValue(String HTML, String attr){
		String tempattr=attr+"=";
		String tempbattr=attr.toUpperCase()+"=";
		
		String result="",tempstr;
		int tempint=HTML.indexOf(tempattr);
		if(tempint<0) tempint=HTML.indexOf(tempbattr);
		if(tempint>=0)
		{
			tempstr=HTML.substring(tempint+tempattr.length());
			if(tempstr.substring(0,1).equals("'")) result=tempstr.substring(1,tempstr.indexOf("'"));
			else if(tempstr.substring(0,1).equals("\"")) result=tempstr.substring(1,tempstr.indexOf("\""));
			else{
				int tempmaxint=tempstr.indexOf(">");
				tempint=tempstr.indexOf(" ");
				if(tempint>0&&tempint<tempmaxint) result=tempstr.substring(0,tempint);
				else result=tempstr.substring(0,tempmaxint);
			}
		}
		return result;
	}

	//If the tag is only in innerStr,use this function to get it's attribute value. 
	//tag: tag name.
	//attr: attribute name.
	//return: attribute value.
	public static String readFirstTagAttrValue(String HTML, String tag, String attr){
		String temptag="<"+tag;
		String tempbtag="<"+tag.toUpperCase();
		String tempattr=attr+"=";
		String tempbattr=attr.toUpperCase()+"=";
		
		String result="",tempstr;
		int tempstart=HTML.indexOf(temptag);
		if(tempstart<0) tempstart=HTML.indexOf(tempbtag);
		if(tempstart>=0){
			int tempend=HTML.indexOf(">",tempstart);
			if(tempend>=0){
				tempstr=HTML.substring(tempstart,tempend);

				tempstart=tempstr.indexOf(tempattr);
				if(tempstart<0) tempstart=tempstr.indexOf(tempbattr);
				if(tempstart>0){
					tempstr=tempstr.substring(tempstart+tempattr.length()).trim();
					if(tempstr.substring(0,1).equals("'")){
						tempstr=tempstr.substring(1);
						result=tempstr.substring(0,tempstr.indexOf("'"));
					}
					else if(tempstr.substring(0,1).equals("\"")){
						tempstr=tempstr.substring(1);
						result=tempstr.substring(0,tempstr.indexOf("\""));
					}
					else{
						tempend=tempstr.indexOf(" ");
						if(tempend<0) tempend=tempstr.indexOf(">");
						if(tempend>0) result=tempstr.substring(0,tempend);
						else result="";
					}
				}
			}
		}
		
		return result;
	}
	

	//Use this function to get it's attribute value in style of the first tag. 
	//tag: tag name.
	//attr: attribute name.
	//return: attribute value.
	public static String readFirstTagStyleAttrValue(String HTML, String tag, String attr){
		String temptag="<"+tag;
		String tempbtag="<"+tag.toUpperCase();
		String tempstyle="style=";
		String tempbstyle="STYLE=";
		String tempattr=attr+":";
		String tempbattr=attr.toUpperCase()+":";
		
		String result="",tempstr;
		int tempstart=HTML.indexOf(temptag);
		if(tempstart<0) tempstart=HTML.indexOf(tempbtag);
		if(tempstart>=0){
			int tempend=HTML.indexOf(">",tempstart);
			if(tempend>=0){
				tempstr=HTML.substring(tempstart,tempend);
				
				tempstart=tempstr.indexOf(tempstyle);
				if(tempstart<0) tempstart=tempstr.indexOf(tempbstyle);
				if(tempstart>0){
					tempstr=tempstr.substring(tempstart);
	
					tempstart=tempstr.indexOf(tempattr);
					if(tempstart<0) tempstart=tempstr.indexOf(tempbattr);
					if(tempstart>0){
						tempstart+=tempattr.length();
						
						tempend=tempstr.indexOf(";",tempstart);
						if(tempend<0) tempend=tempstr.indexOf("'",tempstart);
						if(tempend<0) tempend=tempstr.indexOf("\"",tempstart);
						if(tempend<0) tempend=tempstr.indexOf(">",tempstart);
						if(tempend>0)
							result=tempstr.substring(tempstart,tempend).trim();
					}
				}
			}
		}
		
		return result;
	}
	
	//If the tag is only in innerStr,use this function to get it's inner HTML code. 
	//tag: tag name.
	//return: inner HTML code.
	public static String readFirstTaginnerHTML(String HTML, String tag){
		String temptag="<"+tag;
		String tempbtag="<"+tag.toUpperCase();
		String tempendtag="</"+tag+">";
		String tempbendtag="</"+tag.toUpperCase()+">";
		
		String result="",tempstr;
		int tempint=HTML.indexOf(temptag);
		if(tempint<0) tempint=HTML.indexOf(tempbtag);
		if(tempint>=0){
			tempstr=HTML.substring(tempint);
			
			tempint=tempstr.indexOf(tempendtag);
			if(tempint<0) tempint=tempstr.indexOf(tempbendtag);
			
			result=tempstr.substring(tempstr.indexOf(">")+1,tempint);
		}
		
		return result;
	}
	
	
	//Use this function to get it's inner HTML code. 
	//tagid: tag ID.
	//return: inner HTML code.
	public static String readTaginnerHTML(String HTML, String tagid){
		String temptagid1="id='"+tagid+"'";
		String tempbtagid1="ID='"+tagid+"'";
		String temptagid2="id=\""+tagid+"\"";
		String tempbtagid2="ID=\""+tagid+"\"";
		String temptagid3="id="+tagid;
		String tempbtagid3="ID="+tagid+"";

		String result="",tempstr;
		int tempint=HTML.indexOf(temptagid1);
		if(tempint<0){
			tempint=HTML.indexOf(tempbtagid1);
			if(tempint<0){
				tempint=HTML.indexOf(temptagid2);
				if(tempint<0){
					tempint=HTML.indexOf(tempbtagid2);
					if(tempint<0){
						tempint=HTML.indexOf(temptagid3);
						if(tempint<0) tempint=HTML.indexOf(tempbtagid3);
					}
				}
			}
		}
		if(tempint>=0){
			int start,end;
			start=HTML.indexOf(">",tempint)+1;
			tempint=HTML.lastIndexOf("<", tempint);
			
			String temptagname=HTML.substring(tempint+1,HTML.indexOf(" ", tempint));
			String temptag="<"+temptagname;
			int ltemptag=temptag.length();
			String tempendtag="</"+temptagname+">";
			int ltempendtag=tempendtag.length();
			
			tempstr=HTML.substring(start);
			int temp1,temp2;
			temp1=0;
			int tagcount=1;
			while(tagcount>0){
				temp2=tempstr.indexOf(tempendtag,temp1);
				tagcount--;
				String tempstr2=tempstr.substring(temp1,temp2);
				while((temp1=tempstr2.indexOf(temptag))>=0){
					tempstr2=tempstr2.substring(temp1+ltemptag);
					tagcount++;
				}
				temp1=temp2+ltempendtag;
			}
			end=start+temp1-ltempendtag;
			
			result=HTML.substring(start,end);
		}	
		
		return result;
	}

	//Use this function to replace "\r\n" into "<br />". 
	//instr: string with "\r\n"
	//return: string with "<br />".
	public static String replaceLineFeedtoBR(String instr){
		String outstr="",tempstr;
		int tempint=instr.indexOf(">");
		if(tempint>=0){
			while(tempint>=0){
				outstr+=instr.substring(0,tempint+1);
				instr=instr.substring(tempint+1);
				tempint=instr.indexOf("<");
				if(tempint>=0){
					tempstr=instr.substring(0,tempint);
					if(!tempstr.trim().isEmpty()){
						outstr+=tempstr.replaceAll("\r\n", "<br>");
					}
					else{
						outstr+=tempstr;
					}
					instr=instr.substring(tempint);
				}
				else{
					if(!instr.trim().isEmpty()){
						outstr+=instr.replaceAll("\r\n", "<br>");
					}
					else{
						outstr+=instr;
					}
					break;
				}
				tempint=instr.indexOf(">");
			}
		}
		else{
			outstr=instr.replaceAll("\r\n", "<br>");
		}

		return outstr;
	}

	//Use this function to replace "<br />" into "\r\n". 
	//instr: string with "<br />".
	//return: string with "\r\n".
	public static String replaceLineFeedtoRN(String instr){
		String outstr=instr.replaceAll("<br>", "\r\n");
		outstr=outstr.replaceAll("<BR>", "\r\n");
		
		return outstr;
	}
}
