/* Copyright (c) 2005 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.xmlman.in;


import com.ricebridge.xmlman.XmlManagerException;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;


/** Implementation of {@link java.sql.ResultSetMetaData} to present headers from XML data (if defined).
 *    <p>This <code>ResultSetMetaData</code> implementation provides much the same interface as a normal <code>ResultSetMetaData</code>.
 *  It assumes that the first data record from XML file specifies the column headers, which are
 *  are returned by {@link #getColumnName}. 
 *  Apart from methods relating to column names, all other methods return default values which assume 
 *  that all data is in the form of <code>String</code>s.</p>
 *    <p>The <b><a href="XmlResultSetMetaData.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>
 */
public class XmlResultSetMetaData implements ResultSetMetaData {

  /** Number of columns of data. */
  protected int iColumnCount = 0;

  /** Column names */
  protected String[] iColumnName = null;

  /** Map from column name to index. */
  protected HashMap  iColumnMap = new HashMap();


  /** Initialised with a <code>List</code> of <code>String[]</code> containing column field names. 
   *  @param pFieldNames header names
   */
  public XmlResultSetMetaData( String[] pFieldNames ) {
    iColumnCount = pFieldNames.length;
    iColumnName  = new String[ iColumnCount + 1 ];
    for( int cI = 1; cI <= iColumnCount; cI++ ) {
      iColumnName[cI] = pFieldNames[cI-1];
      iColumnMap.put( iColumnName[cI], new Integer(cI) );
    }
  }


  /** Find the index (starting at one, not zero) of a named column.
   *  @param pColumnName name of column
   */
  public int findColumn( String pColumnName ) {
    if( iColumnMap.containsKey( pColumnName ) ) {
      return ((Integer) iColumnMap.get(pColumnName)).intValue();
    }
    else {
      throw new XmlManagerException( XmlManagerException.CODE_unknown_column, pColumnName );
    }
  }


  /** Get the number of columns. */
  public int getColumnCount() throws SQLException {
    return iColumnCount;
  }


  /** Get the name of a column.
   *  @param pColumn index of column (from one, not zero)
   */
  public String getColumnLabel( int pColumn ) throws SQLException {
    return getColumnName( pColumn );
  }


  /** Get the name of a column.
   *  @param pColumn index of column (from one, not zero)
   */
  public String getColumnName( int pColumn ) throws SQLException {
    return iColumnName[pColumn];
  }


  // defaults

  public boolean isAutoIncrement(int pColumn ) throws SQLException { return true;  }
  public boolean isCaseSensitive(int pColumn) throws SQLException {	return true; }
  public boolean isSearchable(int pColumn) throws SQLException { return false; }
  public boolean isCurrency(int pColumn) throws SQLException { return false; }
  public int     isNullable(int pColumn) throws SQLException { return columnNoNulls; }
  public boolean isSigned(int pColumn) throws SQLException  { return true; }
  public int     getColumnDisplaySize(int pColumn) throws SQLException { return 255; }
  public String  getSchemaName(int pColumn) throws SQLException { return ""; }
  public int     getPrecision(int pColumn) throws SQLException { return 0; }
  public int     getScale(int pColumn) throws SQLException { return 0; }	
  public String  getTableName(int pColumn) throws SQLException { return ""; }
  public String  getCatalogName(int pColumn) throws SQLException { return ""; }
  public int     getColumnType(int pColumn) throws SQLException { return Types.VARCHAR; }
  public String  getColumnTypeName(int pColumn) throws SQLException { return "VARCHAR"; }
  public boolean isReadOnly(int pColumn) throws SQLException { return true; }
  public boolean isWritable(int pColumn) throws SQLException { return false; }
  public boolean isDefinitelyWritable(int pColumn) throws SQLException { return false; }
  public String  getColumnClassName(int pColumn) throws SQLException { return "java.lang.String"; }
}
Syntax Highlighting created using the com.Ostermiller.Syntax package.
Thursday, February 23 2006 at 16:47