📄 stringresourcemodel.java
字号:
* <p> * The relative component parameter should generally be supplied, as without it resources can * not be obtained from resource bundles that are held relative to a particular component or * page. However, for application that use only global resources then this parameter may be * null. * <p> * The model parameter is also optional and only needs to be supplied if value substitutions are * to take place on either the resource key or the actual resource strings. * <p> * The parameters parameter is also optional and is used for substitutions. * * @param resourceKey * The resource key for this string resource * @param component * The component that the resource is relative to * @param model * The model to use for property substitutions * @param parameters * The parameters to substitute using a Java MessageFormat object */ public StringResourceModel(final String resourceKey, final Component component, final IModel model, final Object[] parameters) { this(resourceKey, component, model, parameters, null); } /** * Creates a new string resource model using the supplied parameters. * <p> * The relative component parameter should generally be supplied, as without it resources can * not be obtained from resource bundles that are held relative to a particular component or * page. However, for application that use only global resources then this parameter may be * null. * <p> * The model parameter is also optional and only needs to be supplied if value substitutions are * to take place on either the resource key or the actual resource strings. * <p> * The parameters parameter is also optional and is used for substitutions. * * @param resourceKey * The resource key for this string resource * @param component * The component that the resource is relative to * @param model * The model to use for property substitutions * @param parameters * The parameters to substitute using a Java MessageFormat object * @param defaultValue * The default value if the resource key is not found. */ public StringResourceModel(final String resourceKey, final Component component, final IModel model, final Object[] parameters, final String defaultValue) { if (resourceKey == null) { throw new IllegalArgumentException("Resource key must not be null"); } this.resourceKey = resourceKey; this.component = component; this.model = model; this.parameters = parameters; this.defaultValue = defaultValue; } /** * Gets the localizer that is being used by this string resource model. * * @return The localizer */ public Localizer getLocalizer() { return localizer; } /** * Gets the string currently represented by this string resource model. The string that is * returned may vary for each call to this method depending on the values contained in the model * and an the parameters that were passed when this string resource model was created. * * @return The string */ public final String getString() { // Make sure we have a localizer before commencing if (getLocalizer() == null) { if (component != null) { setLocalizer(component.getLocalizer()); } else { throw new IllegalStateException("No localizer has been set"); } } // Get the string resource, doing any property substitutions as part // of the get operation String value = localizer.getString(getResourceKey(), component, model, defaultValue); if (value == null) { value = defaultValue; } if (value != null) { // Substitute any parameters if necessary Object[] parameters = getParameters(); if (parameters != null) { // Build the real parameters Object[] realParams = new Object[parameters.length]; for (int i = 0; i < parameters.length; i++) { if (parameters[i] instanceof IModel) { realParams[i] = ((IModel)parameters[i]).getObject(); } else if (model != null && parameters[i] instanceof String) { realParams[i] = PropertyVariableInterpolator.interpolate( (String)parameters[i], model.getObject()); } else { realParams[i] = parameters[i]; } } // escape single quotes for MessageFormat value = Strings.replaceAll(value, "'", "''").toString(); // Apply the parameters final MessageFormat format = new MessageFormat(value, component != null ? component.getLocale() : locale); value = format.format(realParams); } } // Return the string resource return value; } /** * Sets the localizer that is being used by this string resource model. This method is provided * to allow the default application localizer to be overridden if required. * * @param localizer * The localizer to use */ public void setLocalizer(final Localizer localizer) { this.localizer = localizer; } /** * This method just returns debug information, so it won't return the localized string. Please * use getString() for that. * * @return The string for this model object */ public String toString() { StringBuffer sb = new StringBuffer("StringResourceModel["); sb.append("key:"); sb.append(resourceKey); sb.append(",default:"); sb.append(defaultValue); sb.append(",params:"); if (parameters != null) { sb.append(Arrays.asList(parameters)); } sb.append("]"); return sb.toString(); } /** * Gets the Java MessageFormat substitution parameters. * * @return The substitution parameters */ protected Object[] getParameters() { return parameters; } /** * Gets the resource key for this string resource. If the resource key contains property * expressions and the model is not null then the returned value is the actual resource key with * all substitutions undertaken. * * @return The (possibly substituted) resource key */ protected final String getResourceKey() { if (model != null) { return PropertyVariableInterpolator.interpolate(resourceKey, model.getObject()); } else { return resourceKey; } } /** * Gets the string that this string resource model currently represents. The string is returned * as an object to allow it to be used generically within components. * */ protected Object load() { // Initialize information that we need to work successfully final Session session = Session.get(); if (session != null) { localizer = Application.get().getResourceSettings().getLocalizer(); locale = session.getLocale(); } else { throw new WicketRuntimeException( "Cannot attach a string resource model without a Session context because that is required to get a Localizer"); } return getString(); } /** * Detaches from the given session */ protected final void onDetach() { // Detach any model if (model != null) { model.detach(); } // Null out references localizer = null; locale = null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -