<%
   class Util
   {
      private Set generatedTestSuites;
      private StringBuffer stringBuffer;

   	  public Util(StringBuffer stringBuffer)
   	  {
   	     this.stringBuffer = stringBuffer;
   	  }
   	  
   	  public void dispose()
   	  {
   	     if(generatedTestSuites != null)
   	        generatedTestSuites.clear();
   	  }

	  public void createXMLAttribute(String name, String value)
	  {
	     if((name == null) || (value == null))
	        return;
	     stringBuffer.append(XMLUtil.createXMLAttribute(name, value, false));
      }
      
      public void generate(ITestSuite testSuite)
      {
         if(generatedTestSuites == null)
            generatedTestSuites = new HashSet();
         else if(generatedTestSuites.contains(testSuite))
            return;
         generatedTestSuites.add(testSuite);
      
         stringBuffer.append(NL).append("<testSuite");
         createXMLAttribute("id", testSuite.getId());
         createXMLAttribute("name", testSuite.getName());
         createXMLAttribute("location", helper.getFilePath(testSuite));
         createXMLAttribute("description", testSuite.getDescription());
         
         boolean complex = false;
         
         List list = testSuite.getITestCases();
         if(!list.isEmpty())
         {
            complex = true;
            stringBuffer.append(">");

            stringBuffer.append(NL).append("<testCases>");
            for(Iterator i=list.iterator(); i.hasNext();)
            {
               ITestCase testCase = (ITestCase)i.next();
               stringBuffer.append(NL).append("<testCase");
               createXMLAttribute("id", testCase.getId());
               createXMLAttribute("name", testCase.getName());
               createXMLAttribute("description", testCase.getDescription());
               stringBuffer.append("/>");
            }
            stringBuffer.append(NL).append("</testCases>");
         }

         list = testSuite.getIReferencedSuites();
         if(!list.isEmpty())
         {
            complex = true;
            stringBuffer.append(">");

            stringBuffer.append(NL).append("<referencedTestSuites>");
            complex = true;
            for(Iterator i=list.iterator(); i.hasNext();)
               generate((ITestSuite)i.next());
            stringBuffer.append(NL).append("</referencedTestSuites>");
         }

         if(testSuite.getImplementor() != null)
         {
            list = testSuite.getImplementor().getBlock().getActions();
            if(!list.isEmpty())
            {
               complex = true;
               stringBuffer.append(">");
               
               stringBuffer.append(NL).append("<behavior>");
               complex = true;
               for(Iterator i=list.iterator(); i.hasNext();)
               {
                  IAction action = (IAction)i.next();
                  if(action instanceof ITestInvocation)
                     generate((ITestInvocation)action);
                  else if(action instanceof ILoop)
                     generate((ILoop)action);
               }
               stringBuffer.append(NL).append("</behavior>");
            }
         }
         
         if(complex)
         	stringBuffer.append(NL).append("</testSuite>");
         else
         	stringBuffer.append("/>");
      }
       
      public void generate(ITestInvocation testInvocation)
      {
         stringBuffer.append(NL).append("<testInvocation");
         createXMLAttribute("id", testInvocation.getId());
         createXMLAttribute("name", testInvocation.getName());
         createXMLAttribute("synchronous", new Boolean(testInvocation.isSynchronous()).toString());
         if(testInvocation.getInvokedTest() != null)
            createXMLAttribute("testId", testInvocation.getInvokedTest().getId());
         createXMLAttribute("description", testInvocation.getDescription());
         stringBuffer.append("/>");
      }

      public void generate(ILoop loop)
      {
         stringBuffer.append(NL).append("<loop");
         createXMLAttribute("id", loop.getId());
         createXMLAttribute("name", loop.getName());
         createXMLAttribute("synchronous", new Boolean(loop.isSynchronous()).toString());
         createXMLAttribute("iterations", loop.getCondition().getConstraint());
         createXMLAttribute("description", loop.getDescription());
          
         List actions = loop.getBlock().getActions();
         if(!actions.isEmpty())
         {
            stringBuffer.append(">");
            for(Iterator i=actions.iterator(); i.hasNext();)
            {
               IAction action = (IAction)i.next();
               if(action instanceof ITestInvocation)
                  generate((ITestInvocation)action);
               else if(action instanceof ILoop)
                  generate((ILoop)action);
            }
            stringBuffer.append(NL).append("</loop>");
         }
         else
         { 
            stringBuffer.append("/>");
         }
      }
   }
   Util util = new Util(stringBuffer);
%>
<?xml version="1.0" encoding="UTF-8"?>
<testScript>
<%
   util.generate(testSuite);
%>
<verdicts>
<%
   for(Iterator i=TPFVerdict.VALUES.iterator(); i.hasNext();)
   {
      TPFVerdict verdict = (TPFVerdict)i.next();
      String labelAtt = XMLUtil.createXMLAttribute("label", verdict.getLabel(), false).toString();
      String valueAtt = XMLUtil.createXMLAttribute("value", Integer.toString(verdict.getValue()), false).toString();
      String isDefaultAtt = XMLUtil.createXMLAttribute("isDefault", new Boolean(verdict.getValue() == TPFVerdict.PASS).toString(), false).toString();
%>
<verdict<%=labelAtt%><%=valueAtt%><%=isDefaultAtt%>/>
<%
	}
	util.dispose();
%>
</verdicts>
</testScript>