001    /**
002     * Copyright (c) 2000-present 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     * @see com.liferay.petra.content.ContentUtil
030     */
031    public class ContentUtil {
032    
033            public static String get(ClassLoader classLoader, String location) {
034                    return _instance._get(classLoader, location, false);
035            }
036    
037            public static String get(
038                    ClassLoader classLoader, String location, boolean all) {
039    
040                    return _instance._get(classLoader, location, all);
041            }
042    
043            public static String get(String location) {
044                    return _instance._get(location, false);
045            }
046    
047            public static String get(String location, boolean all) {
048                    return _instance._get(location, all);
049            }
050    
051            private ContentUtil() {
052                    _contentPool = new HashMap<>();
053            }
054    
055            private String _get(ClassLoader classLoader, String location, boolean all) {
056                    String content = _contentPool.get(location);
057    
058                    if (content == null) {
059                            try {
060                                    content = StringUtil.read(classLoader, location, all);
061    
062                                    _put(location, content);
063                            }
064                            catch (IOException ioe) {
065                                    _log.error(ioe, ioe);
066                            }
067                    }
068    
069                    return content;
070            }
071    
072            private String _get(String location, boolean all) {
073                    Class<?> clazz = getClass();
074    
075                    return _get(clazz.getClassLoader(), location, all);
076            }
077    
078            private void _put(String location, String content) {
079                    _contentPool.put(location, content);
080            }
081    
082            private static final Log _log = LogFactoryUtil.getLog(ContentUtil.class);
083    
084            private static final ContentUtil _instance = new ContentUtil();
085    
086            private final Map<String, String> _contentPool;
087    
088    }