/* Copyright (c) 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; /** Helper class for implementing the {@link LineListener} interface. * <p>Please do not extend this class directly. {@link CustomLineListener} extends this class, and * <code>CustomLineListener</code> is the class you should should extend when implementing your own * {@link LineListener LineListeners}. This class only provides internal error checking.</p> * <p>Compatibility note: This class will replace {@link LineListenerSupport} in <i>CSV Manager 2.x</i>. * The current <code>LineListenerSupport</code> class in <i>CSV Manager 1.x</i> is a compatibility * class for existing customers with custom <code>LineListeners</code>, and should not be used for new * code. If this means nothing to you, just remember that the golden rule is * "use <i>CustomLineListener</i>, and things will <i>just work</i>."</p> * <p>The <b><a href="LineListenerSupportImpl.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 LineListenerSupport * @see CustomLineListener */ public abstract class LineListenerSupportImpl implements LineListener { // LineListener interface /** Helper implementation of {@link LineListener#setCsvSpec LineListener#setCsvSpec}. */ public void setCsvSpec( CsvSpec pCsvSpec ) { try { setCsvSpecImpl( pCsvSpec ); } catch( CsvManagerException ce ) { throw ce; } catch( Exception e ) { throw new CsvManagerException( CsvManagerException.CODE_linelistener_exception, new String[] { "line-listener-class", this.getClass().getName(), "methodname", "setCsvSpecImpl" }, e ); } } /** Helper implementation of {@link LineListener#setLineSpec LineListener#setLineSpec}. */ public void setLineSpec( LineSpec pLineSpec ) { try { setLineSpecImpl( pLineSpec ); } catch( CsvManagerException ce ) { throw ce; } catch( Exception e ) { throw new CsvManagerException( CsvManagerException.CODE_linelistener_exception, new String[] { "line-listener-class", this.getClass().getName(), "methodname", "setLineSpecImpl" }, e ); } } /** Helper implementation of {@link LineListener#startProcess LineListener.startProcess} */ public void startProcess() { try { startProcessImpl(); } catch( CsvManagerException ce ) { throw ce; } catch( Exception e ) { throw new CsvManagerException( CsvManagerException.CODE_linelistener_exception, new String[] { "line-listener-class", this.getClass().getName(), "methodname", "startLoadImpl" }, e ); } } /** Helper implementation of {@link LineListener#endProcess LineListener.endProcess} */ public void endProcess() { try { endProcessImpl(); } catch( CsvManagerException ce ) { throw ce; } catch( Exception e ) { throw new CsvManagerException( CsvManagerException.CODE_linelistener_exception, new String[] { "line-listener-class", this.getClass().getName(), "methodname", "endLoadImpl" }, e ); } } /** Helper implementation of {@link LineListener#handleLine LineListener.handleLine} */ public BadLine handleLine( String[] pLine, int pNumFields, long pLineNumber, String pOriginalLine ) { // this code is for fault tolerance - it can only make best guesses due to limited information // so it does not attempt to be consistent with the parsing context int numf = pNumFields; if( numf < 0 ) { if ( null != pLine ) { numf = pLine.length; } else { numf = 0; } } String[] line = pLine; if( null == line ) { line = new String[ numf ]; } for( int cI = 0; cI < line.length; cI++ ) { if( null == line[cI] ) { line[cI] = Standard.EMPTY; } } String orig = pOriginalLine; if( null == orig ) { orig = Standard.EMPTY; } long linenum = pLineNumber; if( linenum < 1 ) { linenum = 1; } BadLine badline = null; try { badline = handleLineImpl( line, numf, linenum, orig ); } catch( CsvManagerException ce ) { throw ce; } catch( Exception e ) { throw new CsvManagerException( CsvManagerException.CODE_linelistener_exception, new String[] { "line-listener-class", this.getClass().getName(), "methodname", "addLineImpl" }, e ); } return badline; } /** Helper implementation of {@link LineListener#handleBadLine LineListener.handleBadLine} */ public void handleBadLine( BadLine pBadLine ) { BadLine badline = pBadLine; if( null != badline ) { try { handleBadLineImpl( badline ); } catch( CsvManagerException ce ) { throw ce; } catch( Exception e ) { throw new CsvManagerException( CsvManagerException.CODE_linelistener_exception, new String[] { "line-listener-class", this.getClass().getName(), "methodname", "badLineImpl" }, e ); } } } /** The default implementation of this method is provided by {@link CustomLineListener} */ protected abstract void setCsvSpecImpl( CsvSpec pCsvSpec ) throws Exception; /** The default implementation of this method is provided by {@link CustomLineListener} */ protected abstract void setLineSpecImpl( LineSpec pLineSpec ) throws Exception; /** The default implementation of this method is provided by {@link CustomLineListener} */ protected abstract void startProcessImpl() throws Exception; /** The default implementation of this method is provided by {@link CustomLineListener} */ protected abstract void endProcessImpl() throws Exception; /** The default implementation of this method is provided by {@link CustomLineListener} */ protected abstract void handleBadLineImpl( BadLine pBadLine ) throws Exception; /** The default implementation of this method is provided by {@link CustomLineListener} */ protected abstract BadLine handleLineImpl( String[] pLine, int pNumFields, long pLineNumber, String pOriginalLine ) throws Exception; }