ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-ant-myrmidon/api/src/java/org/apache/myrmidon/listeners TaskToProjectListenerAdapter.java
Date Thu, 25 Apr 2002 08:15:26 GMT
donaldp     02/04/25 01:15:26

  Added:       api/src/java/org/apache/myrmidon/listeners
                        TaskToProjectListenerAdapter.java
  Log:
  Add an adaptor between TaskListener and ProjectListener.
  
  Revision  Changes    Path
  1.1                  jakarta-ant-myrmidon/api/src/java/org/apache/myrmidon/listeners/TaskToProjectListenerAdapter.java
  
  Index: TaskToProjectListenerAdapter.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.myrmidon.listeners;
  
  import java.util.StringTokenizer;
  import org.apache.myrmidon.api.event.TaskEvent;
  import org.apache.myrmidon.api.event.TaskListener;
  
  /**
   * This is a class that adapts {@link TaskListener} interface to
   * the {@link ProjectListener} interface.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/04/25 08:15:26 $
   */
  public class TaskToProjectListenerAdapter
      implements TaskListener
  {
      private final ProjectListener m_listener;
  
      /**
       * Create an adaptor for specified listener.
       *
       * @param listener the ProjectListener to redirect events to
       */
      public TaskToProjectListenerAdapter( final ProjectListener listener )
      {
          m_listener = listener;
      }
  
      /**
       * Notify the listener that the task is starting.
       * This is called prior to task starting.
       *
       * @param event the TaskEvent
       */
      public void taskStarting( final TaskEvent event )
      {
          final ProjectEvent projectEvent = toProjectEvent( event );
          if( null == projectEvent.getTarget() )
          {
              m_listener.projectStarted( projectEvent );
          }
          else if( null == projectEvent.getTask() )
          {
              m_listener.targetStarted( projectEvent );
          }
          else
          {
              m_listener.taskStarted( projectEvent );
          }
      }
  
      /**
       * Notify the listener that the task logged
       * a message.
       *
       * @param event the TaskEvent
       */
      public void taskMessage( TaskEvent event )
      {
          final ProjectEvent projectEvent = toProjectEvent( event );
          m_listener.log( projectEvent );
      }
  
      /**
       * Notify the listener that the task has finished.
       * This is called after task has finished.
       *
       * @param event the TaskEvent
       */
      public void taskFinished( TaskEvent event )
      {
          final ProjectEvent projectEvent = toProjectEvent( event );
          if( null == projectEvent.getTarget() )
          {
              m_listener.projectFinished( projectEvent );
          }
          else if( null == projectEvent.getTask() )
          {
              m_listener.targetFinished( projectEvent );
          }
          else
          {
              m_listener.taskFinished( projectEvent );
          }
      }
  
      /**
       * Convert the specified {@link TaskEvent} to a {@link ProjectEvent}.
       *
       * @param event the TaskEvent
       * @return the Project event representing TaskEvent
       */
      private ProjectEvent toProjectEvent( final TaskEvent event )
      {
          final String path = event.getPath();
          final String[] elements = split( path, "/" );
  
          String projectName = null;
          String targetName = null;
          String taskName = null;
          if( 1 >= elements.length )
          {
              projectName = elements[ 0 ];
          }
  
          if( 2 >= elements.length )
          {
              targetName = elements[ 1 ];
          }
  
          if( 3 >= elements.length )
          {
              taskName = elements[ elements.length - 1 ];
          }
  
          return new ProjectEvent( event,
                                   projectName,
                                   targetName,
                                   taskName,
                                   event.getMessage(),
                                   event.getThrowable() );
      }
  
      /**
       * Splits the string on every token into an array of strings.
       *
       * @param string the string
       * @param onToken the token
       * @return the resultant array
       */
      protected final String[] split( final String string, final String onToken )
      {
          final StringTokenizer tokenizer = new StringTokenizer( string, onToken );
          final String[] result = new String[ tokenizer.countTokens() ];
  
          for( int i = 0; i < result.length; i++ )
          {
              result[ i ] = tokenizer.nextToken();
          }
  
          return result;
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message