1 package org
.argeo
.cms
.script
;
3 import static java
.nio
.charset
.StandardCharsets
.UTF_8
;
5 import java
.io
.BufferedReader
;
6 import java
.io
.IOException
;
7 import java
.io
.InputStreamReader
;
9 import java
.util
.ArrayList
;
10 import java
.util
.Enumeration
;
11 import java
.util
.HashMap
;
12 import java
.util
.List
;
14 import java
.util
.stream
.Collectors
;
16 import org
.apache
.commons
.logging
.Log
;
17 import org
.apache
.commons
.logging
.LogFactory
;
18 import org
.argeo
.cms
.CmsException
;
19 import org
.argeo
.cms
.util
.BundleResourceLoader
;
20 import org
.argeo
.cms
.util
.ThemeUtils
;
21 import org
.eclipse
.rap
.rwt
.RWT
;
22 import org
.eclipse
.rap
.rwt
.application
.Application
;
23 import org
.eclipse
.rap
.rwt
.service
.ResourceLoader
;
24 import org
.osgi
.framework
.Bundle
;
25 import org
.osgi
.framework
.BundleContext
;
28 private final static Log log
= LogFactory
.getLog(Theme
.class);
30 private String themeId
;
31 private Map
<String
, ResourceLoader
> css
= new HashMap
<>();
32 private Map
<String
, ResourceLoader
> resources
= new HashMap
<>();
34 private String headerCss
;
35 private List
<String
> fonts
= new ArrayList
<>();
37 private String basePath
;
38 private String cssPath
;
40 public Theme(BundleContext bundleContext
) {
41 this(bundleContext
, null);
44 public Theme(BundleContext bundleContext
, String symbolicName
) {
46 if (symbolicName
== null) {
47 themeBundle
= bundleContext
.getBundle();
48 // basePath = "/theme/";
49 // cssPath = basePath;
51 themeBundle
= ThemeUtils
.findThemeBundle(bundleContext
, symbolicName
);
55 this.themeId
= RWT
.DEFAULT_THEME_ID
;
56 addStyleSheets(themeBundle
, new BundleResourceLoader(themeBundle
));
57 BundleResourceLoader themeBRL
= new BundleResourceLoader(themeBundle
);
58 addResources(themeBRL
, "*.png");
59 addResources(themeBRL
, "*.gif");
60 addResources(themeBRL
, "*.jpg");
61 addResources(themeBRL
, "*.jpeg");
62 addResources(themeBRL
, "*.svg");
63 addResources(themeBRL
, "*.ico");
66 URL fontsUrl
= themeBundle
.getEntry(basePath
+ "fonts.txt");
67 if (fontsUrl
!= null) {
68 loadFontsUrl(fontsUrl
);
71 // common CSS header (plain CSS)
72 URL headerCssUrl
= themeBundle
.getEntry(basePath
+ "header.css");
73 if (headerCssUrl
!= null) {
74 try (BufferedReader buffer
= new BufferedReader(new InputStreamReader(headerCssUrl
.openStream(), UTF_8
))) {
75 headerCss
= buffer
.lines().collect(Collectors
.joining("\n"));
76 } catch (IOException e
) {
77 throw new CmsException("Cannot read " + headerCssUrl
, e
);
83 public void apply(Application application
) {
84 resources
: for (String name
: resources
.keySet()) {
85 if (name
.startsWith("target/"))
86 continue resources
; // skip maven output
87 application
.addResource(name
, resources
.get(name
));
88 if (log
.isDebugEnabled())
89 log
.debug("Added resource " + name
);
91 for (String name
: css
.keySet()) {
92 application
.addStyleSheet(themeId
, name
, css
.get(name
));
93 if (log
.isDebugEnabled())
94 log
.debug("Added RAP CSS " + name
);
98 public String
getAdditionalHeaders() {
99 StringBuilder sb
= new StringBuilder();
100 if (headerCss
!= null) {
101 sb
.append("<style type='text/css'>\n");
102 sb
.append(headerCss
);
103 sb
.append("\n</style>\n");
105 for (String link
: fonts
) {
106 sb
.append("<link rel='stylesheet' href='");
110 if (sb
.length() == 0)
113 return sb
.toString();
116 void addStyleSheets(Bundle themeBundle
, ResourceLoader ssRL
) {
117 Enumeration
<URL
> themeResources
= themeBundle
.findEntries(cssPath
, "*.css", true);
118 if (themeResources
== null)
120 while (themeResources
.hasMoreElements()) {
121 String resource
= themeResources
.nextElement().getPath();
122 // remove first '/' so that RWT registers it
123 resource
= resource
.substring(1);
124 if (!resource
.endsWith("/")) {
125 if (css
.containsKey(resource
))
126 log
.warn("Overriding " + resource
+ " from " + themeBundle
.getSymbolicName());
127 css
.put(resource
, ssRL
);
134 void loadFontsUrl(URL url
) {
135 try (BufferedReader in
= new BufferedReader(new InputStreamReader(url
.openStream(), UTF_8
))) {
137 while ((line
= in
.readLine()) != null) {
139 if (!line
.equals("") && !line
.startsWith("#")) {
143 } catch (IOException e
) {
144 throw new CmsException("Cannot load URL " + url
, e
);
148 void addResources(BundleResourceLoader themeBRL
, String pattern
) {
149 Bundle themeBundle
= themeBRL
.getBundle();
150 Enumeration
<URL
> themeResources
= themeBundle
.findEntries(basePath
, pattern
, true);
151 if (themeResources
== null)
153 while (themeResources
.hasMoreElements()) {
154 String resource
= themeResources
.nextElement().getPath();
155 // remove first '/' so that RWT registers it
156 resource
= resource
.substring(1);
157 if (!resource
.endsWith("/")) {
158 if (resources
.containsKey(resource
))
159 log
.warn("Overriding " + resource
+ " from " + themeBundle
.getSymbolicName());
160 resources
.put(resource
, themeBRL
);
167 public String
getThemeId() {
171 public void setThemeId(String themeId
) {
172 this.themeId
= themeId
;
175 public String
getBasePath() {
179 public void setBasePath(String basePath
) {
180 this.basePath
= basePath
;
183 public String
getCssPath() {
187 public void setCssPath(String cssPath
) {
188 this.cssPath
= cssPath
;