/* 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.Standard;


/** DEPRECATED — DO NOT USE.
 *    Helper class for implementing the {@link FieldListener} interface.
 *    <p>Use of this class will help ensure future compatibility with any CSV Manager API changes.
 *  Subclasses should implement the <code>*Impl</code> methods (for example, {@link #addFieldImpl}, 
 *  rather than the declared interface methods such as {@link #addField}.</p>
 *    <p>The <b><a href="FieldListenerSupport.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>
 *  @deprecated
 */
public abstract class FieldListenerSupport implements FieldListener {


  /** Helper implementation of  {@link FieldListener#startLoad}. 
   *  Subclasses should implement {@link #startLoadImpl}.
   */
  public void startLoad() {
    try {
      startLoadImpl();
    }
    catch( CsvManagerException ce ) { throw ce; }
    catch( Exception e ) {
      throw new CsvManagerException( CsvManagerException.CODE_fieldlistener_exception, 
                                     new String[] { "field-listener-class", this.getClass().getName(), 
                                                    "methodname", "startLoadImpl" }, e );
    }
  }


  /** Helper implementation of  {@link FieldListener#endLoad}. 
   *  Subclasses should implement {@link #endLoadImpl}.
   */
  public void endLoad() {
    try {
      endLoadImpl();
    }
    catch( CsvManagerException ce ) { throw ce; }
    catch( Exception e ) {
      throw new CsvManagerException( CsvManagerException.CODE_fieldlistener_exception, 
                                     new String[] { "field-listener-class", this.getClass().getName(), 
                                                    "methodname", "endLoadImpl" }, e );
    }
  }


  /** Helper implementation of  {@link FieldListener#addField}. 
   *  Subclasses should implement {@link #addFieldImpl}.
   *  @param pValue field data as text 
   */
  public void addField( String pValue ) {
    String value = pValue;
    if( null == value ) { value = Standard.EMPTY; }

    try {
      addFieldImpl( value );
    }
    catch( CsvManagerException ce ) { throw ce; }
    catch( Exception e ) {
      throw new CsvManagerException( CsvManagerException.CODE_fieldlistener_exception, 
                                     new String[] { "field-listener-class", this.getClass().getName(), 
                                                    "methodname", "addFieldImpl" }, e );
    }

  }


  /** Helper implementation of  {@link FieldListener#endLine}. 
   *  Subclasses should implement {@link #endLineImpl}.
   *  @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
   */
  public BadLine endLine( String pOriginalLine, long pLineNumber ) {
    String orig = pOriginalLine;
    if( null == orig ) { orig = Standard.EMPTY; }    

    long linenum = pLineNumber;
    if( linenum < 1 ) { linenum = 1; }

    BadLine badline = null;
    try {
      badline = endLineImpl( orig, linenum );
    }
    catch( CsvManagerException ce ) { throw ce; }
    catch( Exception e ) {
      throw new CsvManagerException( CsvManagerException.CODE_fieldlistener_exception, 
                                     new String[] { "field-listener-class", this.getClass().getName(), 
                                                    "methodname", "endLineImpl" }, e );
    }
    

    // NOTE: old method signature called for compatibility with 1.1.1 release
    BadLine oldbadline = endLineImpl();
    if( oldbadline != null && null != badline ) {
      badline = oldbadline;
    }

    // badline can be null - indicates line was ok
    return badline;
  }


  /** Helper implementation of  {@link FieldListener#badLine}. 
   *  Subclasses should implement {@link #badLineImpl}.
   *  @param pBadLine {@link BadLine} object describing problem
   */
  public void badLine( BadLine pBadLine ) {
    BadLine badline = pBadLine;
    if( null != badline ) {
      try {
        badLineImpl( badline );
      }
      catch( CsvManagerException ce ) { throw ce; }
      catch( Exception e ) {
        throw new CsvManagerException( CsvManagerException.CODE_fieldlistener_exception, 
                                       new String[] { "field-listener-class", this.getClass().getName(), 
                                                      "methodname", "badLineImpl" }, e );
      }
    }
  }


  /** Implement this method to receive notification that loading of CSV data is about to start. */
  public void startLoadImpl() throws Exception {
    // default does nothing
  }


  /** Implement this method to receive notification that loading of CSV data has ended. */
  public void endLoadImpl() throws Exception {
    // default does nothing
  }


  /** Implement this method to receive each data field as it is loaded. 
   *  @param pValue String value of data field
   *  @see FieldListener#addField
   */
  public abstract void addFieldImpl( String pValue ) throws Exception;


  /** Implement this method to be notified of the end of each data line. 
   *  @see FieldListener#endLine
   */
  public BadLine endLineImpl( String pOriginalLine, long pLineNumber ) throws Exception { return null; }


  /** Implement this method to be notified when badly formatted data is encountered. 
   *  @see FieldListener#badLine
   */
  public abstract void badLineImpl( BadLine pBadLine ) throws Exception;


  /** DO NOT USE THIS METHOD.
   *  Included for compatibility up to release 1.1.1.
   *  This method will be removed in version 1.2.
   **/
  public BadLine endLineImpl() { return null; }

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