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.template;
018
019import java.util.Map;
020
021import org.apache.wicket.util.resource.AbstractStringResourceStream;
022import org.apache.wicket.util.string.interpolator.MapVariableInterpolator;
023
024
025/**
026 * Represents a text template that can do variable interpolation.
027 * 
028 * @see org.apache.wicket.util.string.interpolator.VariableInterpolator
029 * 
030 * @author Eelco Hillenius
031 * @author Jonathan Locke
032 * @since 1.2.6
033 */
034public abstract class TextTemplate extends AbstractStringResourceStream
035{
036        private static final long serialVersionUID = 1L;
037
038        /**
039         * Constructor.
040         */
041        public TextTemplate()
042        {
043        }
044
045        /**
046         * Constructor.
047         * 
048         * @param contentType
049         *            the mime type of this resource, such as "<code>image/jpeg</code>" or "
050         *            <code>text/html</code>"
051         */
052        public TextTemplate(String contentType)
053        {
054                super(contentType);
055        }
056
057        /**
058         * Interpolates the <code>Map</code> of variables with the content and returns the resulting
059         * <code>String</code> without replacing the content. Variables are denoted in this string by
060         * the syntax <code>${variableName}</code>. The contents will be altered by replacing each
061         * variable of the form <code>${variableName}</code> with the value returned by
062         * <code>variables.getValue("variableName")</code>.
063         * 
064         * @param variables
065         *            the variables to interpolate
066         * @return the result of the interpolation
067         */
068        public String asString(Map<String, ?> variables)
069        {
070                if (variables != null)
071                {
072                        return new MapVariableInterpolator(getString(), variables).toString();
073                }
074                return getString();
075        }
076
077        /**
078         * @see org.apache.wicket.util.resource.AbstractStringResourceStream#asString()
079         */
080        @Override
081        public String asString()
082        {
083                return getString();
084        }
085
086        /**
087         * Retrieves the <code>String</code> resource.
088         * 
089         * @return the <code>String</code> resource
090         */
091        @Override
092        public abstract String getString();
093
094        /**
095         * Interpolates values into this <code>TextTemplate</code>.
096         * 
097         * @param variables
098         *            variables to interpolate into this <code>TextTemplate</code>
099         * @return <code>this</code>, for chaining purposes
100         */
101        public abstract TextTemplate interpolate(Map<String, ?> variables);
102}