1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.StringUtil;
20  
21  import java.io.IOException;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  /**
27   * <a href="ContentUtil.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   * @author Raymond Augé
31   */
32  public class ContentUtil {
33  
34      public static String get(String location) {
35          return _instance._get(location, false);
36      }
37  
38      public static String get(String location, boolean all) {
39          return _instance._get(location, all);
40      }
41  
42      public static String get(ClassLoader classLoader, String location) {
43          return _instance._get(classLoader, location, false);
44      }
45  
46      public static String get(
47          ClassLoader classLoader, String location, boolean all) {
48  
49          return _instance._get(classLoader, location, all);
50      }
51  
52      private ContentUtil() {
53          _contentPool = new HashMap<String, String>();
54      }
55  
56      private String _get(String location, boolean all) {
57          return _get(getClass().getClassLoader(), location, all);
58      }
59  
60      private String _get(ClassLoader classLoader, String location, boolean all) {
61          String content = _contentPool.get(location);
62  
63          if (content == null) {
64              try {
65                  content = StringUtil.read(classLoader, location, all);
66  
67                  _put(location, content);
68              }
69              catch (IOException ioe) {
70                  _log.error(ioe, ioe);
71              }
72          }
73  
74          return content;
75      }
76  
77      private void _put(String location, String content) {
78          _contentPool.put(location, content);
79      }
80  
81      private static Log _log = LogFactoryUtil.getLog(ContentUtil.class);
82  
83      private static ContentUtil _instance = new ContentUtil();
84  
85      private Map<String, String> _contentPool;
86  
87  }