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 org.apache.wicket.core.util.lang.PropertyResolver; 020import org.apache.wicket.util.string.Strings; 021import org.apache.wicket.util.string.interpolator.VariableInterpolator; 022 023/** 024 * Interpolates values into <code>String</code>s that are produced by interpreting property 025 * expressions against an object. 026 * <p> 027 * Takes a string such as "<code>My name is ${name}</code>" and a bean such as a <code>Person</code> 028 * , and reflects on the object using any property expressions found inside <code>${}</code> markers 029 * in the <code>String</code>. In this case, if the <code>Person</code> model has a 030 * <code>getName()</code> method. The results of calling that method would be substituted for 031 * <code>${name}</code>. If <code>getName()</code> returned <code>"Jonathan"</code>, then the result 032 * would return <code>"My name is Jonathan"</code>. 033 * <p> 034 * "$" is the escape char. Thus "$${text}" can be used to escape it (ignore interpretation). If 035 * '$3.24' is needed then '$$${amount}' should be used. The first $ sign escapes the second, and the 036 * third is used to interpolate the variable. 037 * 038 * @author Jonathan Locke 039 * @since 1.2.6 040 */ 041public class PropertyVariableInterpolator extends VariableInterpolator 042{ 043 private static final long serialVersionUID = 1L; 044 045 /** The object to introspect on */ 046 private final Object oject; 047 048 /** 049 * Constructor. 050 * 051 * @param string 052 * a <code>String</code> to interpolate into 053 * @param object 054 * the model to apply property expressions to 055 */ 056 public PropertyVariableInterpolator(final String string, final Object object) 057 { 058 super(string); 059 oject = object; 060 } 061 062 @Override 063 protected String getValue(final String variableName) 064 { 065 Object value = PropertyResolver.getValue(variableName, oject); 066 067 if (value != null) 068 { 069 return toString(value); 070 } 071 return null; 072 } 073 074 /** 075 * Convert the given value to a string for interpolation. 076 * <p> 077 * This default implementation delegates to {@link Strings#toString(Object)}. 078 * 079 * @param value 080 * the value, never {@code null} 081 * 082 * @return string representation 083 */ 084 protected String toString(Object value) 085 { 086 return Strings.toString(value); 087 } 088}