
import tauzaman.*;
import java.net.URL;

import java.io.IOException;

/**
* This is an example program, which uses TauZaman System to
* declare a single TauZamanService locally. And it uses this local
* service to load an american Calendric System and creates an Instant
* with this outputs it by using a format other than input format.
*/

public class StandAloneTauZaman{

  /* 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;

      /* 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");
      }
      catch(MalformedURLException mue){
          // take corresponding action
          System.exit(1);
      }

      /* declare a local TauZamanService */
      TauZamanService localService = null;

      /* initialize the local service */
      try{
          localService = 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; Interval j = null;

      try{ 
         /* form an Instant */
         i = new Instant("September, 17, 2003");
      }
      catch(TauZamanException tze){
          //analyze the exception, tze contains message and possible causes.
          System.exit(1);
      }
      
      try{ 
         /* form an Interval */
         j = new Interval("6 days");
      }
      catch(TauZamanException tze){
          //analyze the exception, tze contains message and possible causes.
          System.exit(1);
      }

      /* form a left operand semantic */
      DeterminateSemantics ops= new LeftOperandSemantics();
      
      try{
          /* add Interval j to Instant i */
          k = ops.add(i,j);
      }
      catch(TauZamanException tze){
          //analyze the exception, tze contains message and possible causes.
          System.exit(1);
      }

      String focusPropertyNames [] = {"InstantInputFormat"};

      try{
          localService.activateProperty(new URL("http://www.eecs.wsu.edu/~burgun/otherProperties.xml"), focusPropertyNames);
      }
      catch(TauZamanException tze){
          //analyze the exception, tze contains message and possible causes.
          System.exit(1);
      }
      catch(MalformedURLException mue){
          // take corresponding action
          System.exit(1);
      }
       
      try{
          System.out.println(k.output()); // or System.out.println(Output.formatInstant(i));
      }
      catch(TauZamanException tze){
          //analyze the exception, tze contains message and possible causes.
          System.exit(1);
      }
      
  }

}

