001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.core.util.string.interpolator; 018 019import java.util.Locale; 020 021import org.apache.wicket.IConverterLocator; 022import org.apache.wicket.util.convert.IConverter; 023 024/** 025 * A {@link PropertyVariableInterpolator} converting values with {@link IConverter}s. 026 * 027 * @author svenmeier 028 */ 029public class ConvertingPropertyVariableInterpolator extends PropertyVariableInterpolator 030{ 031 private static final long serialVersionUID = 1L; 032 033 private IConverterLocator locator; 034 035 private Locale locale; 036 037 /** 038 * Constructor. 039 * 040 * @param string 041 * a <code>String</code> to interpolate into 042 * @param object 043 * the object to apply property expressions to 044 * @param locator 045 * the locator of converters 046 * @param locale 047 * the locale for conversion 048 */ 049 public ConvertingPropertyVariableInterpolator(final String string, final Object object, 050 IConverterLocator locator, Locale locale) 051 { 052 super(string, object); 053 054 this.locator = locator; 055 this.locale = locale; 056 } 057 058 /** 059 * Use an {@link IConverter} to convert the given value to a String. 060 * 061 * @param value 062 * the value, never {@code null} 063 * 064 * @return converted value 065 */ 066 @Override 067 @SuppressWarnings({ "rawtypes", "unchecked" }) 068 protected String toString(Object value) 069 { 070 IConverter converter = locator.getConverter(value.getClass()); 071 072 return converter.convertToString(value, locale); 073 } 074}