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.util.string.interpolator;
018
019import java.util.Map;
020
021import org.apache.wicket.util.string.Strings;
022
023
024/**
025 * Interpolates variables into a <code>String</code> from a <code>Map</code>.
026 * 
027 * @author Jonathan Locke
028 * @since 1.2.6
029 */
030@SuppressWarnings("serial")
031public class MapVariableInterpolator extends VariableInterpolator
032{
033        /** Map of variables */
034        private Map<?, ?> variables;
035
036        /**
037         * Constructor.
038         * 
039         * @param string
040         *            a <code>String</code> to interpolate into
041         * @param variables
042         *            the variables to substitute
043         */
044        public MapVariableInterpolator(final String string, final Map<?, ?> variables)
045        {
046                super(string);
047                this.variables = variables;
048        }
049
050        /**
051         * Constructor.
052         * 
053         * @param string
054         *            a <code>String</code> to interpolate into
055         * @param variables
056         *            the variables to substitute
057         * @param exceptionOnNullVarValue
058         *            if <code>true</code> an {@link IllegalStateException} will be thrown if
059         *            {@link #getValue(String)} returns <code>null</code>, otherwise the
060         *            <code>${varname}</code> string will be left in the <code>String</code> so that
061         *            multiple interpolators can be chained
062         */
063        public MapVariableInterpolator(final String string, final Map<?, ?> variables,
064                final boolean exceptionOnNullVarValue)
065        {
066                super(string, exceptionOnNullVarValue);
067                this.variables = variables;
068        }
069
070        /**
071         * Sets the <code>Map</code> of variables.
072         * 
073         * @param variables
074         *            the <code>Map</code> of variables
075         */
076        public final void setVariables(final Map<?, ?> variables)
077        {
078                this.variables = variables;
079        }
080
081        /**
082         * Retrieves a value for a variable name during interpolation.
083         * 
084         * @param variableName
085         *            the variable name
086         * @return the value
087         */
088        @Override
089        protected String getValue(final String variableName)
090        {
091                return Strings.toString(variables.get(variableName));
092        }
093
094        /**
095         * Interpolates a <code>String</code> with the arguments defined in the given <code>Map</code>.
096         * 
097         * @param string
098         *            a <code>String</code> to interpolate into
099         * @param variables
100         *            the variables to substitute
101         * @return the interpolated <code>String</code>
102         */
103        public static String interpolate(final String string, final Map<?, ?> variables)
104        {
105                return new MapVariableInterpolator(string, variables).toString();
106        }
107
108}