package org.flexunit.ant.launcher.commands.player;

import java.io.File;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.Java;
import org.apache.tools.ant.types.FilterSet;
import org.apache.tools.ant.types.FilterSetCollection;
import org.apache.tools.ant.types.Commandline.Argument;
import org.apache.tools.ant.types.resources.FileResource;
import org.apache.tools.ant.types.resources.URLResource;
import org.apache.tools.ant.util.ResourceUtils;
import org.flexunit.ant.LoggingUtil;

public class AdlCommand extends DefaultPlayerCommand
{
   private final String ADT_JAR_PATH = "lib" + File.separatorChar + "adt.jar";
   private final String DESCRIPTOR_TEMPLATE = "flexUnitDescriptor.template";
   private final String DESCRIPTOR_FILE = "flexUnitDescriptor.xml";
   
   @Override
   public File getFileToExecute()
   {
      return new File(getSwf().getParentFile().getAbsolutePath() + File.separatorChar + DESCRIPTOR_FILE);
   }

   /**
    * Used to create the application descriptor used to invoke adl
    */
   private void createApplicationDescriptor()
   {
      try
      {
         //Template location in JAR
         URLResource template = new URLResource(getClass().getResource("/" + DESCRIPTOR_TEMPLATE));
         
         //Descriptor location, same location as SWF due to relative path required in descriptor
         File descriptor = new File(getSwf().getParentFile().getAbsolutePath() + File.separatorChar + DESCRIPTOR_FILE);
         
         //Create tokens to filter
         FilterSet filters = new FilterSet();
         filters.addFilter("ADL_SWF", getSwf().getName());
         filters.addFilter("ADT_VERSION", Double.toString(getVersion()));
         
         //Copy descriptor template to SWF folder performing token replacement
         ResourceUtils.copyResource(
            template,
            new FileResource(descriptor),
            new FilterSetCollection(filters),
            null,
            true,
            false,
            null,
            null,
            getProject()
         );
         
         LoggingUtil.log("Created application descriptor at [" + descriptor.getAbsolutePath() + "]");
      }
      catch (Exception e)
      {
         throw new BuildException("Could not create application descriptor");
      }
   }
   
   private double getVersion()
   {
      String outputProperty = "AIR_VERSION";
      
      //Execute mxmlc to find SDK version number
      Java task = new Java();
      task.setFork(true);
      task.setFailonerror(true);
      task.setJar(new File(getProject().getProperty("FLEX_HOME") + File.separatorChar + ADT_JAR_PATH));
      task.setProject(getProject());
      task.setDir(getProject().getBaseDir());
      task.setOutputproperty(outputProperty);
      
      Argument versionArgument = task.createArg();
      versionArgument.setValue("-version");
      
      task.execute();
      
      //Parse version number and return as int
      String output = getProject().getProperty(outputProperty);
      int prefixIndex = output.indexOf("adt version \"");
      double version = Double.parseDouble(output.substring(prefixIndex + 13, prefixIndex + 16));
      
      LoggingUtil.log("Found AIR version: " + version);
      
      return version;
   }
   
   @Override
   public void prepare()
   {
      getCommandLine().setExecutable(generateExecutable());
      getCommandLine().addArguments(new String[]{getFileToExecute().getAbsolutePath()});   
      
      //Create Adl descriptor file
      createApplicationDescriptor();
   }
   
   private String generateExecutable()
   {
      return getProject().getProperty("FLEX_HOME") + "/bin/" + getDefaults().getAdlCommand();
   }
}
@@ -2915,113 +2915,461 @@
     
-//Parse version number and return as int
+%0A      double version = parseAdtVersionNumber( getProject().getProperty(outputProperty) );%0A      %0A      LoggingUtil.log(%22Found AIR version: %22 + version);%0A      %0A      return version;%0A   %7D
 %0A   
+%0A
    
-String output = getProject().getProperty(outputProperty);%0A
+private double parseAdtVersionNumber( String versionString )%0A   %7B%0A%09  double version;%0A%09      %0A      //AIR 2.6 and greater only returns the version number.%0A      if( versionString.startsWith(%22adt%22) )%0A      %7B%0A          //Parse version number and return as int%0A%09
     
@@ -3388,22 +3388,29 @@
 Index = 
-output
+versionString
 .indexOf
@@ -3429,28 +3429,22 @@
 n %5C%22%22);%0A
- 
+%09
      
-double
  version
@@ -3465,22 +3465,29 @@
 eDouble(
-output
+versionString
 .substri
@@ -3531,68 +3531,100 @@
 ));%0A
+%09    %0A
       
+%7Delse
 %0A      
-LoggingUtil.log(%22Found AIR version: %22 + version
+%7B%0A    %09  version = Double.parseDouble(versionString.substring(0, 3) 
 );%0A 
@@ -3620,39 +3620,45 @@
 (0, 3) );%0A      
-%0A
+%7D%0A%09
       
+%0A%09  
 return version;%0A

Update to AdlCommand for changes in adt API for Fx 4.5