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.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(ClassLoader classLoader, String location) {
033                    return _instance._get(classLoader, location, false);
034            }
035    
036            public static String get(
037                    ClassLoader classLoader, String location, boolean all) {
038    
039                    return _instance._get(classLoader, location, all);
040            }
041    
042            public static String get(String location) {
043                    return _instance._get(location, false);
044            }
045    
046            public static String get(String location, boolean all) {
047                    return _instance._get(location, all);
048            }
049    
050            private ContentUtil() {
051                    _contentPool = new HashMap<String, String>();
052            }
053    
054            private String _get(ClassLoader classLoader, String location, boolean all) {
055                    String content = _contentPool.get(location);
056    
057                    if (content == null) {
058                            try {
059                                    content = StringUtil.read(classLoader, location, all);
060    
061                                    _put(location, content);
062                            }
063                            catch (IOException ioe) {
064                                    _log.error(ioe, ioe);
065                            }
066                    }
067    
068                    return content;
069            }
070    
071            private String _get(String location, boolean all) {
072                    return _get(getClass().getClassLoader(), location, all);
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    }