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
.application
.Application
;
22 import org
.eclipse
.rap
.rwt
.service
.ResourceLoader
;
23 import org
.osgi
.framework
.Bundle
;
24 import org
.osgi
.framework
.BundleContext
;
27 private final static Log log
= LogFactory
.getLog(Theme
.class);
29 private final String themeId
;
30 private Map
<String
, ResourceLoader
> css
= new HashMap
<>();
31 private Map
<String
, ResourceLoader
> resources
= new HashMap
<>();
33 private String headerCss
;
34 private List
<String
> fonts
= new ArrayList
<>();
36 private String basePath
;
37 private String cssPath
;
39 public Theme(BundleContext bundleContext
) {
40 this(bundleContext
, null);
43 public Theme(BundleContext bundleContext
, String symbolicName
) {
45 if (symbolicName
== null) {
46 themeBundle
= bundleContext
.getBundle();
47 basePath
= "/theme/internal/";
50 themeBundle
= ThemeUtils
.findThemeBundle(bundleContext
, symbolicName
);
54 this.themeId
= themeBundle
.getSymbolicName();
55 addStyleSheets(themeBundle
, new BundleResourceLoader(themeBundle
));
56 BundleResourceLoader themeBRL
= new BundleResourceLoader(themeBundle
);
57 addResources(themeBRL
, "*.png");
58 addResources(themeBRL
, "*.gif");
59 addResources(themeBRL
, "*.jpg");
60 addResources(themeBRL
, "*.jpeg");
61 addResources(themeBRL
, "*.svg");
62 addResources(themeBRL
, "*.ico");
65 URL fontsUrl
= themeBundle
.getEntry(basePath
+ "fonts.txt");
66 if (fontsUrl
!= null) {
67 loadFontsUrl(fontsUrl
);
70 // common CSS header (plain CSS)
71 URL headerCssUrl
= themeBundle
.getEntry(basePath
+ "header.css");
72 if (headerCssUrl
!= null) {
73 try (BufferedReader buffer
= new BufferedReader(new InputStreamReader(headerCssUrl
.openStream(), UTF_8
))) {
74 headerCss
= buffer
.lines().collect(Collectors
.joining("\n"));
75 } catch (IOException e
) {
76 throw new CmsException("Cannot read " + headerCssUrl
, e
);
82 public void apply(Application application
) {
83 for (String name
: resources
.keySet()) {
84 application
.addResource(name
, resources
.get(name
));
86 for (String name
: css
.keySet()) {
87 application
.addStyleSheet(themeId
, name
, css
.get(name
));
91 public String
getAdditionalHeaders() {
92 StringBuilder sb
= new StringBuilder();
93 if (headerCss
!= null) {
94 sb
.append("<style type='text/css'>\n");
96 sb
.append("\n</style>\n");
98 for (String link
: fonts
) {
99 sb
.append("<link rel='stylesheet' href='");
103 if (sb
.length() == 0)
106 return sb
.toString();
109 void addStyleSheets(Bundle themeBundle
, ResourceLoader ssRL
) {
110 Enumeration
<URL
> themeResources
= themeBundle
.findEntries(cssPath
, "*.css", true);
111 if (themeResources
== null)
113 while (themeResources
.hasMoreElements()) {
114 String resource
= themeResources
.nextElement().getPath();
115 // remove first '/' so that RWT registers it
116 resource
= resource
.substring(1);
117 if (!resource
.endsWith("/")) {
118 if (css
.containsKey(resource
))
119 log
.warn("Overriding " + resource
+ " from " + themeBundle
.getSymbolicName());
120 css
.put(resource
, ssRL
);
127 void loadFontsUrl(URL url
) {
128 try (BufferedReader in
= new BufferedReader(new InputStreamReader(url
.openStream(), UTF_8
))) {
130 while ((line
= in
.readLine()) != null) {
132 if (!line
.equals("") && !line
.startsWith("#")) {
136 } catch (IOException e
) {
137 throw new CmsException("Cannot load URL " + url
, e
);
141 void addResources(BundleResourceLoader themeBRL
, String pattern
) {
142 Bundle themeBundle
= themeBRL
.getBundle();
143 Enumeration
<URL
> themeResources
= themeBundle
.findEntries(basePath
, pattern
, true);
144 if (themeResources
== null)
146 while (themeResources
.hasMoreElements()) {
147 String resource
= themeResources
.nextElement().getPath();
148 // remove first '/' so that RWT registers it
149 resource
= resource
.substring(1);
150 if (!resource
.endsWith("/")) {
151 if (resources
.containsKey(resource
))
152 log
.warn("Overriding " + resource
+ " from " + themeBundle
.getSymbolicName());
153 resources
.put(resource
, themeBRL
);
160 public String
getThemeId() {
164 public String
getBasePath() {
168 public void setBasePath(String basePath
) {
169 this.basePath
= basePath
;
172 public String
getCssPath() {
176 public void setCssPath(String cssPath
) {
177 this.cssPath
= cssPath
;