/* Copyright (c) 2003-2006 Ricebridge. All Rights Reserved.
 *
 * This file is available under the terms and conditions of the
 * Ricebridge "Open Source API" policy; Ricebridge grants use of this
 * copyrighted work under the terms of a BSD-style license only. See
 * http://www.opensource.org/licenses/bsd-license.php for more
 * information.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 *  - Redistributions of source code must retain the above copyright
 *  notice, this list of conditions and the following disclaimer.
 *
 *  - Redistributions in binary form must reproduce the above
 *  copyright notice, this list of conditions and the following
 *  disclaimer in the documentation and/or other materials provided
 *  with the distribution.
 *
 *  - Neither the name of the Ricebridge nor the names of its
 *  contributors may be used to endorse or promote products derived
 *  from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.  
 */

package com.ricebridge.csvman;


import org.jostraca.util.*;


import java.util.*;


/** Multiple {@link LineListener}s can receive data lines as they are loaded using this class.</p>
 *    <p>If you need to send the data stream to multiple locations, use this class to register
 *  multiple <code>LineListener</code>s with the {@link #registerLineListener} method. Then provide this
 *  class as the <code>LineListener</code> to {@link CsvManager} using the 
 *  {@link CsvManager#load(Object,LineListener)} method.</p>
 *    <p>The <b><a href="MultiLineListener.java.html">Source Code</a></b> of this Java class 
 *    is available under a <a href="http://www.opensource.org/licenses/bsd-license.php">BSD-style license</a>.</p>
 *  
 *  @see LineListener
 *  @see CustomLineListener
 */
public class MultiLineListener extends CustomLineListener {

  // private instance 
  private ArrayList iLineListeners = new ArrayList();


  // CustomLineListener methods

  
  /** Pass {@link CsvSpec} to all registered {@link LineListener LineListeners}. 
   *  @param pCsvSpec {@link CsvSpec} for registered {@link LineListener LineListeners}. 
   */
  protected void setCsvSpecImpl( CsvSpec pCsvSpec ) {
    int numL = iLineListeners.size();
    for( int lnI = 0; lnI < numL; lnI++ ) {
      LineListener ln = (LineListener) iLineListeners.get( lnI );
      ln.setCsvSpec( pCsvSpec );
    }
  }


  /** Pass {@link LineSpec} to all registered {@link LineListener LineListeners}. 
   *  @param pLineSpec {@link LineSpec} for registered {@link LineListener LineListeners}. 
   */
  protected void setLineSpecImpl( LineSpec pLineSpec ) {
    int numL = iLineListeners.size();
    for( int lnI = 0; lnI < numL; lnI++ ) {
      LineListener ln = (LineListener) iLineListeners.get( lnI );
      ln.setLineSpec( pLineSpec );
    }
  }


  /** Calls {@link LineListener#startProcess} on each registered {@link LineListener} in turn. 
   *    <p>Note: If an error occurs, no further calls are made and thus some registered {@link LineListener}s may not
   *  receive this method call. Future versions of CsvManager will resolve this issue.</p>
   *  @see LineListener#startProcess
   */
  protected void startProcessImpl() {
    int numL = iLineListeners.size();
    for( int lnI = 0; lnI < numL; lnI++ ) {
      LineListener ln = (LineListener) iLineListeners.get( lnI );
      ln.startProcess();
    }
  }


  /** Calls {@link LineListener#endProcess} on each registered {@link LineListener} in turn.
   *    <p>Note: If an error occurs, no further calls are made and thus some registered {@link LineListener}s may not
   *  receive this method call. Future versions of CsvManager will resolve this issue.</p>
   *  @see LineListener#endProcess
   */
  protected void endProcessImpl() {
    int numL = iLineListeners.size();
    for( int lnI = 0; lnI < numL; lnI++ ) {
      LineListener ln = (LineListener) iLineListeners.get( lnI );
      ln.endProcess();
    }
  }


  /** <p>Add data line to each registered {@link LineListener}.</p>
   *    <p>If any {@link LineListener} returns a non-null {@link BadLine}, processing continues until all 
   *  registered {@link LineListener}s have received the data line, and then the last non-null {@link BadLine}
   *  is returned. NOTE: if there is more than one {@link BadLine} then all except the last are <i>lost</i>.
   *  Future versions of CsvManager will add functionality to recover these {@link BadLine} objects.</p> 
   *  @param pLine data line
   *  @param pNumFields number of data fields found
   *  @param pLineNumber line number index, counting from 1
   *  @param pOriginalLine original line of data
   *  @return <code>null</code> if line is OK, {@link BadLine} object if line was bad in some way
   *  @see LineListener#handleLine
   */
  protected BadLine handleLineImpl( String[] pLine, int pNumFields, long pLineNumber, String pOriginalLine ) {
    BadLine badline = null;
    int numL = iLineListeners.size();
    for( int lnI = 0; lnI < numL; lnI++ ) {
      LineListener ln = (LineListener) iLineListeners.get( lnI );
      badline = ln.handleLine( pLine, pNumFields, pLineNumber, pOriginalLine );
    }

    return badline;
  }


  /** Calls {@link LineListener#handleBadLine} on each registered {@link LineListener} in turn.
   *    <p>Note: If an error occurs, no further calls are made and thus some registered {@link LineListener}s may not
   *  receive this method call. Future versions of CsvManager will resolve this issue.</p>
   *  @param pBadLine  {@link BadLine} object describing problem
   *  @see LineListener#handleBadLine
   */
  protected void handleBadLineImpl( BadLine pBadLine ) {
    int numL = iLineListeners.size();
    for( int lnI = 0; lnI < numL; lnI++ ) {
      LineListener ln = (LineListener) iLineListeners.get( lnI );
      ln.handleBadLine( pBadLine );
    }
  }
  



  // public methods 

  /** Register a <code>LineListener</code>. This class calls the {@link #handleLine} method
   *  of each registered <code>LineListener</code> in turn.
   *  @param pLineListener <code>LineListener</code> to register
   */
  public void registerLineListener( LineListener pLineListener ) {
    LineListener ln = (LineListener) Internal.null_arg( pLineListener );
    if( ln instanceof LineListenerSupport ) {
      ln = new LineListenerFixer( (LineListenerSupport) ln );
    }
    iLineListeners.add( ln );
  }

}
Syntax Highlighting created using the com.Ostermiller.Syntax package.
Wednesday, November 08 2006 at 19:54