/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2002 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.except;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * An exception thrown by objects that implement <CODE>ObjectProvider</CODE> when they
 * don't contain a specified object.
 *
 * @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
 * @version X
 * @see com.silverwrist.dynamo.iface.ObjectProvider
 */
public class NoSuchObjectException extends DynamoRuntimeException
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final String BUNDLE = "com.silverwrist.dynamo.except.DynamoExceptionMessages";

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String m_context;           // context of request
  private Object[] m_args;            // arguments list to generate

  /*--------------------------------------------------------------------------------
   * Constructors
   *--------------------------------------------------------------------------------
   */

  /**
   * Constructs a new <CODE>NoSuchObjectException</CODE>.
   *
   * @param context Context for the generated exception.
   * @param namespace Namespace of the object that was requested.
   * @param name Name of the object that was requested.
   */
  public NoSuchObjectException(String context, String namespace, String name)
  {
    super(context + ": no such object " + name + " in namespace " + namespace);
    m_context = context;
    m_args = new Object[3];
    m_args[0] = context;
    m_args[1] = namespace;
    m_args[2] = name;

  } // end constructor

  public NoSuchObjectException(String context, String namespace, String name, Throwable inner)
  {
    super(context + ": no such object " + name + " in namespace " + namespace,inner);
    m_context = context;
    m_args = new Object[3];
    m_args[0] = context;
    m_args[1] = namespace;
    m_args[2] = name;

  } // end constructor

  public NoSuchObjectException(String context, NoSuchObjectException other)
  {
    super(morphMessage(other,context),other);
    m_context = context;
    m_args = new Object[other.m_args.length];
    System.arraycopy(other.m_args,0,m_args,0,other.m_args.length);
    m_args[0] = context;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  private static final String morphMessage(NoSuchObjectException other, String new_context)
  {
    return new_context + other.getMessage().substring(other.m_context.length());

  } // end morphMessage

  /*--------------------------------------------------------------------------------
   * Overrides from class Throwable
   *--------------------------------------------------------------------------------
   */

  /**
   * Creates a localized description of this exception. Subclasses may override this method in
   * order to produce a locale-specific message. For subclasses that do not override this method,
   * the default implementation returns the same result as <CODE>getMessage()</CODE>.
   *
   * @return The localized description of this exception.
   */
  public String getLocalizedMessage()
  {
    return this.getLocalizedMessage(Locale.getDefault());

  } // end getLocalizedMessage

  /*--------------------------------------------------------------------------------
   * Overrides from class DynamoRuntimeException
   *--------------------------------------------------------------------------------
   */

  /**
   * Creates a localized description of this exception. Subclasses may override this method in
   * order to produce a locale-specific message. For subclasses that do not override this method,
   * the default implementation returns the same result as <CODE>getLocalizedMessage()</CODE>.
   *
   * @param locale The locale to render the message in.
   * @return The localized description of this exception.
   */
  public String getLocalizedMessage(Locale locale)
  {
    ResourceBundle b = ResourceBundle.getBundle(BUNDLE,locale,getClass().getClassLoader());
    return MessageFormat.format(b.getString("noSuchObjectException"),m_args);

  } // end getLocalizedMessage

} // end class NoSuchObjectException
