001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.util;
016    
017    import com.liferay.portal.kernel.util.StringUtil;
018    
019    import java.util.ArrayList;
020    import java.util.Iterator;
021    import java.util.LinkedHashMap;
022    import java.util.List;
023    import java.util.Map;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class ContextReplace implements Cloneable {
029    
030            public ContextReplace() {
031                    this(null);
032            }
033    
034            public ContextReplace(Map<String, String> context) {
035                    if (context != null) {
036                            _context.putAll(context);
037    
038                            _updateArrays();
039                    }
040            }
041    
042            public void addValue(String key, String value) {
043                    if ((key != null) && (value != null)) {
044                            _context.put(key, value);
045    
046                            _updateArrays();
047                    }
048            }
049    
050            public String replace(String text) {
051                    if (text == null) {
052                            return null;
053                    }
054    
055                    if (_keys.length == 0) {
056                            return text;
057                    }
058    
059                    return StringUtil.replace(text, _keys, _values);
060            }
061    
062            @Override
063            public Object clone() {
064                    return new ContextReplace(_context);
065            }
066    
067            private void _updateArrays() {
068                    List<String> keys = new ArrayList<String>();
069                    List<String> values = new ArrayList<String>();
070    
071                    Iterator<Map.Entry<String, String>> itr =
072                            _context.entrySet().iterator();
073    
074                    while (itr.hasNext()) {
075                            Map.Entry<String, String> entry = itr.next();
076    
077                            String entryKey = entry.getKey();
078                            String entryValue = entry.getValue();
079    
080                            keys.add("${" + entryKey + "}");
081                            values.add(entryValue);
082                    }
083    
084                    _keys = keys.toArray(new String[keys.size()]);
085                    _values = values.toArray(new String[values.size()]);
086            }
087    
088            private Map<String, String> _context = new LinkedHashMap<String, String>();
089            private String[] _keys = new String[0];
090            private String[] _values = new String[0];
091    
092    }