001    package echopoint.template;
002    /* 
003     * This file is part of the Echo Point Project.  This project is a collection
004     * of Components that have extended the Echo Web Application Framework.
005     *
006     * Version: MPL 1.1/GPL 2.0/LGPL 2.1
007     *
008     * The contents of this file are subject to the Mozilla Public License Version
009     * 1.1 (the "License"); you may not use this file except in compliance with
010     * the License. You may obtain a copy of the License at
011     * http://www.mozilla.org/MPL/
012     *
013     * Software distributed under the License is distributed on an "AS IS" basis,
014     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
015     * for the specific language governing rights and limitations under the
016     * License.
017     *
018     * Alternatively, the contents of this file may be used under the terms of
019     * either the GNU General Public License Version 2 or later (the "GPL"), or
020     * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
021     * in which case the provisions of the GPL or the LGPL are applicable instead
022     * of those above. If you wish to allow use of your version of this file only
023     * under the terms of either the GPL or the LGPL, and not to allow others to
024     * use your version of this file under the terms of the MPL, indicate your
025     * decision by deleting the provisions above and replace them with the notice
026     * and other provisions required by the GPL or the LGPL. If you do not delete
027     * the provisions above, a recipient may use your version of this file under
028     * the terms of any one of the MPL, the GPL or the LGPL.
029     */
030    
031    import java.io.Serializable;
032    import java.util.HashMap;
033    /**
034     * The class <code>SimpleTemplateTextSubstitution</code> is a simple implementation 
035     * of <code>TemplateTextSubstitution</code> that uses a HashMap to substitute named
036     * string values.
037     * <p>
038     * Note it uses the toString() method rather than casting associated value objects 
039     * to Strings and hence you can technically use objects other than Strings.
040     */
041    public class SimpleTemplateTextSubstitution implements TemplateTextSubstitution,Serializable {
042            HashMap stringMap;
043            /**
044             * SimpleTemplateTextSubstitution constructor that creates its own empty HashMap.
045             */
046            public SimpleTemplateTextSubstitution() {
047                    super();
048                    stringMap = new HashMap();
049            }
050            /**
051             * SimpleTemplateTextSubstitution constructor that takes a primed HashMap of string mappings.
052             */
053            public SimpleTemplateTextSubstitution(HashMap newStringMap) {
054                    super();
055                    stringMap = newStringMap;
056            }
057            /**
058             * Returns the underlying HashMap containing named string.
059             * 
060             * @return java.util.HashMap
061             */
062            public java.util.HashMap getStringMap() {
063                    return stringMap;
064            }
065            /**
066             * This method is called to return a String that has been associated with the 
067             * given substitution name.
068             */
069            public String getSubstitutionText(String substitutionName) {
070                    Object o = stringMap.get(substitutionName);
071                    if (o != null)
072                            return o.toString();
073                    else
074                            return null;
075            }
076            /**
077             * This method associated the given substitution name with a string value.
078             */
079            public void put(String substitutionName, Object substitutionValue) {
080                    stringMap.put(substitutionName, substitutionValue);
081            }
082            /**
083             * Sets the underlying HashMap of named String values.  99% of the time there is 
084             * no need to call this method.
085             * 
086             * @param newStringMap java.util.HashMap
087             */
088            public void setStringMap(java.util.HashMap newStringMap) {
089                    stringMap = newStringMap;
090            }
091    }