001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package javax.faces.validator;
017:
018: import javax.el.ValueExpression;
019: import javax.faces.application.FacesMessage;
020: import javax.faces.component.UIComponent;
021: import javax.faces.context.FacesContext;
022: import java.text.MessageFormat;
023: import java.util.Locale;
024: import java.util.MissingResourceException;
025: import java.util.ResourceBundle;
026:
027: /**
028: * @author Manfred Geiler (latest modification by $Author: pmcmahan $)
029: * @version $Revision: 529681 $ $Date: 2007-04-17 19:33:09 +0200 (Di, 17 Apr 2007) $
030: */
031: class _MessageUtils {
032: private static final String DETAIL_SUFFIX = "_detail";
033:
034: static FacesMessage getErrorMessage(FacesContext facesContext,
035: String messageId, Object args[]) {
036: return getMessage(facesContext, facesContext.getViewRoot()
037: .getLocale(), FacesMessage.SEVERITY_ERROR, messageId,
038: args);
039: }
040:
041: static FacesMessage getMessage(FacesContext facesContext,
042: Locale locale, FacesMessage.Severity severity,
043: String messageId, Object args[]) {
044: ResourceBundle appBundle;
045: ResourceBundle defBundle;
046: String summary;
047: String detail;
048:
049: if (locale == null) {
050: locale = Locale.getDefault();
051: }
052:
053: appBundle = getApplicationBundle(facesContext, locale);
054: summary = getBundleString(appBundle, messageId);
055: if (summary != null) {
056: detail = getBundleString(appBundle, messageId
057: + DETAIL_SUFFIX);
058: } else {
059: defBundle = getDefaultBundle(facesContext, locale);
060: summary = getBundleString(defBundle, messageId);
061: if (summary != null) {
062: detail = getBundleString(defBundle, messageId
063: + DETAIL_SUFFIX);
064: } else {
065: //Try to find detail alone
066: detail = getBundleString(appBundle, messageId
067: + DETAIL_SUFFIX);
068: if (detail != null) {
069: summary = null;
070: } else {
071: detail = getBundleString(defBundle, messageId
072: + DETAIL_SUFFIX);
073: if (detail != null) {
074: summary = null;
075: } else {
076: //Neither detail nor summary found
077: facesContext.getExternalContext().log(
078: "No message with id " + messageId
079: + " found in any bundle");
080: return new FacesMessage(severity, messageId,
081: null);
082: }
083: }
084: }
085: }
086:
087: if (args != null && args.length > 0) {
088: MessageFormat format;
089:
090: if (summary != null) {
091: format = new MessageFormat(summary, locale);
092: summary = format.format(args);
093: }
094:
095: if (detail != null) {
096: format = new MessageFormat(detail, locale);
097: detail = format.format(args);
098: }
099: }
100:
101: return new FacesMessage(severity, summary, detail);
102: }
103:
104: private static String getBundleString(ResourceBundle bundle,
105: String key) {
106: try {
107: return bundle == null ? null : bundle.getString(key);
108: } catch (MissingResourceException e) {
109: return null;
110: }
111: }
112:
113: private static ResourceBundle getApplicationBundle(
114: FacesContext facesContext, Locale locale) {
115: String bundleName = facesContext.getApplication()
116: .getMessageBundle();
117:
118: return bundleName != null ? getBundle(facesContext, locale,
119: bundleName) : null;
120: }
121:
122: private static ResourceBundle getDefaultBundle(
123: FacesContext facesContext, Locale locale) {
124: return getBundle(facesContext, locale,
125: FacesMessage.FACES_MESSAGES);
126: }
127:
128: private static ResourceBundle getBundle(FacesContext facesContext,
129: Locale locale, String bundleName) {
130: try {
131: //First we try the JSF implementation class loader
132: return ResourceBundle.getBundle(bundleName, locale,
133: facesContext.getClass().getClassLoader());
134: } catch (MissingResourceException ignore1) {
135: try {
136: //Next we try the JSF API class loader
137: return ResourceBundle.getBundle(bundleName, locale,
138: _MessageUtils.class.getClassLoader());
139: } catch (MissingResourceException ignore2) {
140: try {
141: //Last resort is the context class loader
142: return ResourceBundle.getBundle(bundleName, locale,
143: Thread.currentThread()
144: .getContextClassLoader());
145: } catch (MissingResourceException damned) {
146: facesContext.getExternalContext().log(
147: "resource bundle " + bundleName
148: + " could not be found");
149: return null;
150: }
151: }
152: }
153: }
154:
155: static String getLabel(FacesContext facesContext,
156: UIComponent component) {
157: Object label = component.getAttributes().get("label");
158: if (label != null)
159: return label.toString();
160:
161: ValueExpression expression = component
162: .getValueExpression("label");
163: if (expression != null)
164: return (String) expression.getValue(facesContext
165: .getELContext());
166:
167: //If no label is not specified, use clientId
168: return component.getClientId(facesContext);
169: }
170:
171: }
|