Example #1
0
  /**
   * Sets the Locale for table and column remarks.
   *
   * <p>
   */
  void setupBundles() {

    Locale oldLocale;

    synchronized (BundleHandler.class) {
      oldLocale = BundleHandler.getLocale();

      BundleHandler.setLocale(Locale.getDefault());

      hnd_column_remarks = BundleHandler.getBundleHandle("column-remarks", null);
      hnd_table_remarks = BundleHandler.getBundleHandle("table-remarks", null);

      BundleHandler.setLocale(oldLocale);
    }
  }
  void setMethod(Method m) {

    String remarkKey;

    method = m;
    clazz = method.getDeclaringClass();
    fqn = null;
    sig = null;
    csig = null;
    colsResolved = false;
    remarkKey = clazz.getName().replace('.', '_');
    hnd_remarks = BundleHandler.getBundleHandle(remarkKey, null);
  }
/**
 * Contains static factory methods to produce instances of HsqlException
 *
 * @author Loic Lefevre
 * @author Fred Toussi (fredt@users dot sourceforge.net)
 * @version 1.9.0
 * @since 1.9.0
 */
public class Error {

  //
  public static boolean TRACE = false;
  public static boolean TRACESYSTEMOUT = false;

  //
  private static final String errPropsName = "sql-state-messages";
  private static final int bundleHandle = BundleHandler.getBundleHandle(errPropsName, null);
  private static final String MESSAGE_TAG = "$$";
  private static final int SQL_STATE_DIGITS = 5;
  private static final int SQL_CODE_DIGITS = 4;
  private static final int ERROR_CODE_BASE = 11;

  public static RuntimeException runtimeError(int code, String add) {

    HsqlException e = error(code, add);

    return new RuntimeException(e.getMessage());
  }

  public static HsqlException error(int code, String add) {

    String s = getMessage(code);

    if (add != null) {
      s += ": " + add.toString();
    }

    return new HsqlException(
        s.substring(SQL_STATE_DIGITS + 1), s.substring(0, SQL_STATE_DIGITS), -code);
  }

  public static HsqlException error(int code) {
    return error(code, 0, null);
  }

  public static HsqlException error(int code, Throwable t) {

    String message = getMessage(code, 0, null);

    return new HsqlException(t, message.substring(0, SQL_STATE_DIGITS), -code);
  }

  /**
   * Compose error message by inserting the strings in the add parameters in placeholders within the
   * error message. The message string contains $$ markers for each context variable. Context
   * variables are supplied in the add parameters.
   *
   * @param code main error code
   * @param subCode sub error code (if 0 => no subMessage!)
   * @param add optional parameters
   * @return an <code>HsqlException</code>
   */
  public static HsqlException error(int code, int subCode, final Object[] add) {

    String message = getMessage(code, subCode, add);
    int sqlCode = subCode < ERROR_CODE_BASE ? code : subCode;

    return new HsqlException(
        message.substring(SQL_STATE_DIGITS + 1), message.substring(0, SQL_STATE_DIGITS), -sqlCode);
  }

  public static HsqlException error(int code, int code2) {
    return error(code, getMessage(code2));
  }

  /**
   * Compose error in placeholders within the error message. The message string contains $$ markers
   * for each context variable. Context variables are supplied in the add parameters.
   *
   * @see HsqlException#HsqlException(String, String, int)
   * @return an <code>HsqlException</code>
   */
  public static HsqlException error(String message, String sqlState, int i) {
    return new HsqlException(message, sqlState, i);
  }

  /**
   * Compose error message by inserting the strings in the add variables in placeholders within the
   * error message. The message string contains $$ markers for each context variable. Context
   * variables are supplied in the add parameter. (by Loic Lefevre)
   *
   * @param message message string
   * @param add optional parameters
   * @return an <code>HsqlException</code>
   */
  private static String insertStrings(String message, Object[] add) {

    StringBuffer sb = new StringBuffer(message.length() + 32);
    int lastIndex = 0;
    int escIndex = message.length();

    // removed test: i < add.length
    // because if mainErrorMessage is equal to "blabla $$"
    // then the statement escIndex = mainErrorMessage.length();
    // is never reached!  ???
    for (int i = 0; i < add.length; i++) {
      escIndex = message.indexOf(MESSAGE_TAG, lastIndex);

      if (escIndex == -1) {
        break;
      }

      sb.append(message.substring(lastIndex, escIndex));
      sb.append(add[i] == null ? "null exception message" : add[i].toString());

      lastIndex = escIndex + MESSAGE_TAG.length();
    }

    escIndex = message.length();

    sb.append(message.substring(lastIndex, escIndex));

    return sb.toString();
  }

  /**
   * Returns the error message given the error code.<br>
   * This method is be used when throwing exception other than <code>HsqlException</code>.
   *
   * @param errorCode the error code associated to the error message
   * @return the error message associated with the error code
   */
  public static String getMessage(final int errorCode) {
    return getMessage(errorCode, 0, null);
  }

  /**
   * Returns the error SQL STATE sting given the error code.<br>
   * This method is be used when throwing exception based on other exceptions.
   *
   * @param errorCode the error code associated to the error message
   * @return the error message associated with the error code
   */
  public static String getStateString(final int errorCode) {
    return getMessage(errorCode, 0, null).substring(0, SQL_STATE_DIGITS);
  }

  /**
   * Returns the error message given the error code.<br>
   * This method is used when throwing exception other than <code>HsqlException</code>.
   *
   * @param code the code for the error message
   * @param subCode the code for the addon message
   * @param add value(s) to use to replace the placeholer(s)
   * @return the error message associated with the error code
   */
  public static String getMessage(final int code, int subCode, final Object[] add) {

    String message = getResourceString(code);

    if (subCode != 0) {
      message += getResourceString(subCode);
    }

    if (add != null) {
      message = insertStrings(message, add);
    }

    return message;
  }

  private static String getResourceString(int code) {

    String key = StringUtil.toZeroPaddedString(code, SQL_CODE_DIGITS, SQL_CODE_DIGITS);

    return BundleHandler.getString(bundleHandle, key);
  }

  public static HsqlException error(final Result result) {
    return new HsqlException(result);
  }

  /**
   * Used to print messages to System.out
   *
   * @param message message to print
   */
  public static void printSystemOut(String message) {

    if (TRACESYSTEMOUT) {
      System.out.println(message);
    }
  }
}