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.freemarker;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.theme.ThemeLoader;
20  import com.liferay.portal.theme.ThemeLoaderFactory;
21  
22  import java.io.File;
23  import java.io.IOException;
24  
25  import java.net.URL;
26  
27  /**
28   * <a href="ThemeLoaderTemplateLoader.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Mika Koivisto
31   */
32  public class ThemeLoaderTemplateLoader extends URLTemplateLoader {
33  
34      public URL getURL(String name) throws IOException {
35          int pos = name.indexOf(THEME_LOADER_SEPARATOR);
36  
37          if (pos != -1) {
38              String ctxName = name.substring(0, pos);
39  
40              ThemeLoader themeLoader =
41                  ThemeLoaderFactory.getThemeLoader(ctxName);
42  
43              if (themeLoader != null) {
44                  String templateName =
45                      name.substring(pos + THEME_LOADER_SEPARATOR.length());
46  
47                  String themesPath = themeLoader.getThemesPath();
48  
49                  if (templateName.startsWith(themesPath)) {
50                      name =
51                          templateName.substring(
52                              themesPath.length(), templateName.length());
53                  }
54  
55                  if (_log.isDebugEnabled()) {
56                      _log.debug(
57                          name + " is associated with the theme loader " +
58                              ctxName + " " + themeLoader);
59                  }
60  
61                  File fileStorage = themeLoader.getFileStorage();
62  
63                  return new File(fileStorage.getPath() + name).toURI().toURL();
64  
65              }
66              else {
67                  _log.error(
68                      name + " is not valid because " + ctxName +
69                          " does not map to a theme loader");
70              }
71          }
72  
73          return null;
74      }
75  
76      private static Log _log = LogFactoryUtil.getLog(
77          ThemeLoaderTemplateLoader.class);
78  
79  }