001    /**
002     * Copyright (c) 2000-2010 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.portal.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.StringUtil;
020    
021    import java.io.IOException;
022    
023    import java.util.HashMap;
024    import java.util.Map;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     * @author Raymond Augé
029     */
030    public class ContentUtil {
031    
032            public static String get(String location) {
033                    return _instance._get(location, false);
034            }
035    
036            public static String get(String location, boolean all) {
037                    return _instance._get(location, all);
038            }
039    
040            public static String get(ClassLoader classLoader, String location) {
041                    return _instance._get(classLoader, location, false);
042            }
043    
044            public static String get(
045                    ClassLoader classLoader, String location, boolean all) {
046    
047                    return _instance._get(classLoader, location, all);
048            }
049    
050            private ContentUtil() {
051                    _contentPool = new HashMap<String, String>();
052            }
053    
054            private String _get(String location, boolean all) {
055                    return _get(getClass().getClassLoader(), location, all);
056            }
057    
058            private String _get(ClassLoader classLoader, String location, boolean all) {
059                    String content = _contentPool.get(location);
060    
061                    if (content == null) {
062                            try {
063                                    content = StringUtil.read(classLoader, location, all);
064    
065                                    _put(location, content);
066                            }
067                            catch (IOException ioe) {
068                                    _log.error(ioe, ioe);
069                            }
070                    }
071    
072                    return content;
073            }
074    
075            private void _put(String location, String content) {
076                    _contentPool.put(location, content);
077            }
078    
079            private static Log _log = LogFactoryUtil.getLog(ContentUtil.class);
080    
081            private static ContentUtil _instance = new ContentUtil();
082    
083            private Map<String, String> _contentPool;
084    
085    }