001    /**
002     * Copyright (c) 2000-2012 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.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    }