
import tauzaman.*;
import java.net.URL;

import java.io.IOException;

/**
* This is an example program, which uses TauZaman System to
* declare two local TauZamanServices. And it uses this local
* service to load an american Calendric System and an islamic 
* Calendric System. And then program creates one Instant by using
* american Calendric System and prints out this Instant as string 
* again. Then it gets this string and forms an Instant by using 
* islamic Calendric System.
*/

/* Compare the code with RemoteTauZaman.java */

public class LocalTauZaman{

  /* note that main method throws only IOexception */
  public static void main(String args[]) throws IOException{

      /***************BEGINNING OF INITIALIZATION AND CONFIGURATION PHASE***************/

      /* get TauZamanServiceManager via a static method */
      TauZamanServiceManager tzsm = TauZamanServiceManagerImpl.getTauZamanSystem();
      
      URL urlOfDefaultProperties, urlOfAmericanCalendricSystem, urlOfIslamicCalendricSystem;

      /* form URLs that we are interested */
      try{
          urlOfDefaultProperties = new URL("http://www.eecs.wsu.edu/~burgun/files/defaultProperties.xml");
          urlOfAmericanCalendricSystem = new URL("http://www.eecs.wsu.edu/~burgun/files/americanv1.xml");
          urlOfIslamicCalendricSystem = new URL("http://www.eecs.wsu.edu/~burgun/files/islamicv1.xml");
      }
      catch(MalformedURLException mue){
          // take corresponding action
          System.exit(1);
      }

      /* declare two local TauZamanServices */
      TauZamanService localServiceAmerican, localServiceIslamic = null, ;


      try{
          /* initialize services */          
          localServiceIslamic = tzsm.getService(urlOfIslamicCalendricSystem, urlOfDefaultProperties));

          /* this service will be active service */
          localServiceAmerican = tzsm.getService(urlOfAmericanCalendricSystem, urlOfDefaultProperties));
      }
      catch(TauZamanException tze){
          //analyze the exception, tze contains causes.
          System.exit(1);
      }

      /***************END OF INITIALIZATION AND CONFIGURATION PHASE***************/


      /* declare temporal data types, we'll use */
      Instant i = null, k = null;

      String temporalConstant;

      try{ 

         /* form an Instant */
         i = new Instant("September, 17, 2003");

         /* output it */
         temporalConstant = i.output();
      }
      catch(TauZamanException tze){
          //analyze the exception, tze contains message and possible causes.
          System.exit(1);
      }
      
       
      try{
          tzsm.setActiveService(localServiceIslamic);
      }
      catch(TauZamanException tze){
          //analyze the exception, tze contains message and possible causes.
          System.exit(1);
      }
  
      try{

         /* form an Instant */
         k = new Instant(temporalConstant);
                  
         System.out.println(k.output()); // or System.out.println(Output.formatInstant(k));

      }
      catch(TauZamanException tze){
          //analyze the exception, tze contains message and possible causes.
          System.exit(1);
      }
      
  }

}

