package examples;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;

/**
 * TestTag class is used to tests tagext API's 
 */
public class TestTag  extends ExampleTagBase 
    implements BodyTag {


    boolean toBrowser = false;
    String TestString="";
    private String atts1,atts2;
    int count=0;
   
 
    
    // setting the value from jsp att1,att1 from JSP test case 
    public void setAtt1(String value) {
        atts1 = value;
    }
  
    public String getAtt1() {
    	return atts1;
     }
    

    public void setAtt2(String value) {
        atts2 = value;
    }
  
    public String getAtt2() {
    	return atts2;
     }

  
    public void setToBrowser(String value) {
        if (value == null)
            toBrowser = false;
        else if (value.equalsIgnoreCase("true"))
            toBrowser = true;
        else
            toBrowser = false;
    }

// Test for setPageContext in TagSupport class.
		public void setPageContext(PageContext pc) {
			pc.setAttribute("name","pageContext");
		}
	
// Test for doStartTag method in TagSupport class.

    public int doStartTag() {
       if ("doStartTag".equalsIgnoreCase (this.getAtt1())) { 
 
// Test for methods in variableInfo class.
   
// Creating a hashttable to be passed TagData constructor, 	
   java.util.Hashtable ht = new java.util.Hashtable();
	ht.put("a1","98.5");
	ht.put("a2","107.1");
	ht.put("a3","105.1");

	TagData td = new TagData(ht);
    	
    TestTagExtraInfo ftet = new TestTagExtraInfo();
    VariableInfo[] vi = ftet.getVariableInfo(td);
    BodyTagSupport bts = new BodyTagSupport();
     	
	if(this.getAtt2().equals("getClassName")) {
		TestString = TestString + vi[0].getClassName();
	} else if (this.getAtt2().equals("getDeclare")) {
	TestString = TestString + vi[0].getDeclare();
	} else if (this.getAtt2().equals("getScope")) {
	TestString = TestString + vi[0].getScope();
	} else if (this.getAtt2().equals("getVarName")) {
	TestString = TestString + vi[0].getVarName();
	}
 
} // if condition
return EVAL_BODY_TAG;
}
    
// Test for doInitBody method in BodyTagsupport class     
    public void doInitBody() throws JspException {
       if ("doInitBody".equalsIgnoreCase (this.getAtt1())) { 
       	TestString +=this.getAtt1(); } 

// Test for getParent method in TagSupport

     	if ("getParent".equalsIgnoreCase (this.getAtt2())) { 
       		TagSupport ts = new TagSupport();
       		setParent(this);
       		Tag tt = getParent();
			if(tt.toString().regionMatches(true,9,"examples.TestTag",9,7)) { 
       			TestString = TestString + "Pass";
       		}
       		else {	
       			TestString = TestString + "Fails";
       		}
		}
	}

//Test for doAfterBody method in BodyTagSupport class
    
    public int doAfterBody() throws JspException {
        try {

       if ("doAfterBody".equalsIgnoreCase (this.getAtt1())) { 
            TestString += this.getAtt1();
        }
            String s = bodyOut.getString();
            System.err.println(s);
            if (false)
            	bodyOut.println(TestString);
            return SKIP_BODY;
        } catch (IOException ex) {
            throw new JspException(ex.toString());
        }
    }
    
// Test for doEndTag method in BodyTagSupport class
    
    public int doEndTag() throws JspException{
    	
       if ("doEndTag".equalsIgnoreCase (this.getAtt1())) { 
       	TestString +=this.getAtt1(); 
		} 
       return EVAL_PAGE;	
    }	


// Test for release method in BodyTagSupport class

    public void release() {
       if ("release".equalsIgnoreCase (this.getAtt1())) { 
       	TestString +=this.getAtt1(); } 

    try {
    
        if (toBrowser)
            bodyOut.println(TestString);
     		bodyOut.writeOut(bodyOut.getEnclosingWriter());
     } catch (IOException ioe) {}

    }	



}

    
        
    
