001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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            @Override
051            public Object clone() {
052                    return new ContextReplace(_context);
053            }
054    
055            public String replace(String text) {
056                    if (text == null) {
057                            return null;
058                    }
059    
060                    if (_keys.length == 0) {
061                            return text;
062                    }
063    
064                    return StringUtil.replace(text, _keys, _values);
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    }