All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.enterprise.fmt.FmtParseException Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

// @(#) $Id: FmtParseException.java 2093 2007-11-28 14:23:36Z s3460 $
package at.spardat.enterprise.fmt;

import java.util.*;
import java.text.MessageFormat;

/**
 * This class denotes a parse exception. Associated with this class is a ressource bundle
 * of the name FmtErrors. It must hold MessageFormat strings. This class is for internal
 * usage in the package fmt only.
 */
public class FmtParseException extends AParseException {

    // key of ressource bundle entry
    private String            bundleKey_;

    // message format arguments
    private Object []         args_;

    /**
     * resource bundle with messages, can be altered by this.setResBundle(String resBundle)
     */
    private String resBundle = "at.spardat.enterprise.fmt.FmtErrors";

    /**
     * Constructor for FmtParseException.
     */
    public FmtParseException (String bundleKey) {
        bundleKey_ = bundleKey;
        args_ = new Object[0];
    }

    /**
     * Constructor for FmtParseException.
     */
    public FmtParseException (String bundleKey, Object arg) {
        bundleKey_ = bundleKey;
        args_ = new Object[]{arg};
    }

    /**
     * Constructor for FmtParseException.
     */
    public FmtParseException (String bundleKey, Object arg1, Object arg2) {
        bundleKey_ = bundleKey;
        args_ = new Object[]{arg1,arg2};
    }

    /**
     * @see java.lang.Throwable#getMessage()
     */
    public String getMessage() {
        return getUIMessage (Locale.ENGLISH);
    }

    // just to be very defensively
    private static final String         DEFAULT_MESSAGE = "The provided input cannot be accepted";

    /**
     * Returns an UI displayable message explaing the reason for this exception.
     * The text is taken from ressource bundle FmtErrors.
     *
     * @param the Locale used to format the text
     * @return Non null string with an explanation which may be shown directly to
     *          the user.
     */
    public String getUIMessage (Locale l) {
        // get ressource bundle
        ResourceBundle      b = ResourceBundle.getBundle(this.resBundle, l);
        // extract message format string
        String              messageFormat = DEFAULT_MESSAGE;
        if (b != null) {
            try {
                messageFormat = b.getString (bundleKey_);
            } catch (MissingResourceException ex) {
            }
        }
        // format the MessageFormat string
        return MessageFormat.format (messageFormat, args_);
    }

    /**
     * Sets the resource bundle used for error messages.
     * If this method is not called than an enterprise internal resource bundle is used.
     * @param resBundle
     * @since version_number
     * @author s3460
     */
    protected void setResBundle(String resBundle) {
        this.resBundle = resBundle;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy