/** * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved * * The contents of this file are subject to the terms * of the Common Development and Distribution License * (the License). You may not use this file except in * compliance with the License. * * You can obtain a copy of the License at * https://opensso.dev.java.net/public/CDDLv1.0.html or * opensso/legal/CDDLv1.0.txt * See the License for the specific language governing * permission and limitations under the License. * * When distributing Covered Code, include this CDDL * Header Notice in each file and include the License file * at opensso/legal/CDDLv1.0.txt. * If applicable, add the following below the CDDL Header, * with the fields enclosed by brackets [] replaced by * your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * * $Id: LocalizedMessage.java,v 1.2 2008/06/25 05:51:36 qcheng Exp $ * */ package com.sun.identity.agents.arch; import java.text.MessageFormat; import java.util.Locale; /** * Represents a localized message in the system. A LocalizedMessage * is strongly tied to the Module from where it originates from * and is uses a Module specific resource id to * locate the actual resource. It also facilitates the localization into a * different locale other than the Module's configured locale. */ public class LocalizedMessage { /** * Returns the string representation of this LocalizedMessage * in the given locale. * * @param locale the Locale to be used when rendering this message. * @return the localized String representing this message. */ public String toString(Locale locale) { return getFormattedMessage( ModuleList.getRegisteredModule(getModuleCode()).getResource( getId(), locale)); } /** * Returns the string representation of this LocalizedMessage * in the Locale of the originating Module. * * @return the localized String representing this message. */ public String toString() { String result = null; if(getLocale() != null) { result = toString(getLocale()); } else { result = getFormattedMessage( ModuleList.getRegisteredModule(getModuleCode()).getResource( getId())); } return result; } /** * Allows the caller to set a locale different from the default locale * associated with the Module from where the message originated. * * @param locale the Locale to be used when rendering the message. */ public void setLocale(Locale locale) { _locale = locale; } /** * Constructs a LocalizedMessage associated with the * Module corresponding to the given moduleCode, * and the id of the localized resource. * * @param moduleCode the code of the Module from where the * message originates. * @param id the id of the resource. */ LocalizedMessage(byte moduleCode, int id) { this(moduleCode, id, null); } /** * Constructs a LocalizedMessage associated with the * Module corresponding to the given moduleCode, * and the id of the localized resource. The optional * params argument allows any special values to be inserted * into the final message using the MessageFormat semantics. * * @param moduleCode the code of the Module from where the * message originates. * @param id the id of the resource. * @param params optional parameters that will be inserted into the final * message using the MessageFormat semantics. */ LocalizedMessage(byte moduleCode, int id, Object[] params) { setModuleCode(moduleCode); setId(id); setParams(params); } private String getFormattedMessage(String resourceString) { String message = null; if((getParams() == null) || (getParams().length == 0)) { message = resourceString; } else { message = MessageFormat.format(resourceString, getParams()); } return message; } private void setModuleCode(byte moduleCode) { _moduleCode = moduleCode; } private byte getModuleCode() { return _moduleCode; } private int getId() { return _id; } private void setId(int id) { _id = id; } private Object[] getParams() { return _params; } private void setParams(Object[] params) { _params = params; } private Locale getLocale() { return _locale; } private byte _moduleCode; private int _id; private Object[] _params; private Locale _locale; }