1
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
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 }