1 package org
.argeo
.cms
.ui
.util
;
3 import static java
.nio
.charset
.StandardCharsets
.UTF_8
;
5 import java
.io
.BufferedReader
;
6 import java
.io
.IOException
;
7 import java
.io
.InputStream
;
8 import java
.io
.InputStreamReader
;
10 import java
.util
.ArrayList
;
11 import java
.util
.Enumeration
;
12 import java
.util
.List
;
15 import java
.util
.TreeSet
;
16 import java
.util
.stream
.Collectors
;
18 import org
.osgi
.framework
.Bundle
;
19 import org
.osgi
.framework
.BundleContext
;
22 * Simplifies the theming of an app (only RAP is supported at this stage).<br>
24 * Additional fonts listed in <code>/fonts.txt</code>.<br>
25 * Additional (standard CSS) header in <code>/header.css</code>.<br>
26 * RAP specific CSS files in <code>/rap/*.css</code>.<br>
27 * All images added as additional resources based on extensions
28 * <code>/ ** /*.{png,gif,jpeg,...}</code>.<br>
30 public class BundleCmsTheme
extends AbstractCmsTheme
{
31 public final static String DEFAULT_CMS_THEME_BUNDLE
= "org.argeo.theme.argeo2";
33 public final static String CMS_THEME_PROPERTY
= "argeo.cms.theme";
34 public final static String CMS_THEME_BUNDLE_PROPERTY
= "argeo.cms.theme.bundle";
36 private final static String HEADER_CSS
= "header.css";
37 private final static String FONTS_TXT
= "fonts.txt";
39 // private final static Log log = LogFactory.getLog(BundleCmsTheme.class);
41 private String themeId
;
42 private Set
<String
> webCssPaths
= new TreeSet
<>();
43 private Set
<String
> rapCssPaths
= new TreeSet
<>();
44 private Set
<String
> swtCssPaths
= new TreeSet
<>();
45 private Set
<String
> imagesPaths
= new TreeSet
<>();
46 private Set
<String
> fontsPaths
= new TreeSet
<>();
48 private String headerCss
;
49 private List
<String
> fonts
= new ArrayList
<>();
51 private String basePath
;
52 private String styleCssPath
;
53 // private String webCssPath;
54 // private String rapCssPath;
55 // private String swtCssPath;
56 private Bundle themeBundle
;
58 public BundleCmsTheme() {
62 public void init(BundleContext bundleContext
, Map
<String
, String
> properties
) {
63 initResources(bundleContext
, null);
66 public void destroy(BundleContext bundleContext
, Map
<String
, String
> properties
) {
71 public BundleCmsTheme(BundleContext bundleContext
) {
72 this(bundleContext
, null);
76 public BundleCmsTheme(BundleContext bundleContext
, String symbolicName
) {
77 initResources(bundleContext
, symbolicName
);
80 private void initResources(BundleContext bundleContext
, String symbolicName
) {
81 if (symbolicName
== null) {
82 themeBundle
= bundleContext
.getBundle();
83 // basePath = "/theme/";
84 // cssPath = basePath;
86 themeBundle
= findThemeBundle(bundleContext
, symbolicName
);
89 styleCssPath
= "/style/";
90 // webCssPath = "/css/";
91 // rapCssPath = "/rap/";
92 // swtCssPath = "/swt/";
93 // this.themeId = RWT.DEFAULT_THEME_ID;
94 this.themeId
= themeBundle
.getSymbolicName();
95 webCssPaths
= addCss(themeBundle
, "/css/");
96 rapCssPaths
= addCss(themeBundle
, "/rap/");
97 swtCssPaths
= addCss(themeBundle
, "/swt/");
109 URL fontsUrl
= themeBundle
.getEntry(basePath
+ FONTS_TXT
);
110 if (fontsUrl
!= null) {
111 loadFontsUrl(fontsUrl
);
114 // common CSS header (plain CSS)
115 URL headerCssUrl
= themeBundle
.getEntry(basePath
+ HEADER_CSS
);
116 if (headerCssUrl
!= null) {
117 // added to plain Web CSS
118 webCssPaths
.add(basePath
+ HEADER_CSS
);
119 // and it will also be used by RAP:
120 try (BufferedReader buffer
= new BufferedReader(new InputStreamReader(headerCssUrl
.openStream(), UTF_8
))) {
121 headerCss
= buffer
.lines().collect(Collectors
.joining("\n"));
122 } catch (IOException e
) {
123 throw new IllegalArgumentException("Cannot read " + headerCssUrl
, e
);
128 public String
getHtmlHeaders() {
129 StringBuilder sb
= new StringBuilder();
130 if (headerCss
!= null) {
131 sb
.append("<style type='text/css'>\n");
132 sb
.append(headerCss
);
133 sb
.append("\n</style>\n");
135 for (String link
: fonts
) {
136 sb
.append("<link rel='stylesheet' href='");
140 if (sb
.length() == 0)
143 return sb
.toString();
146 Set
<String
> addCss(Bundle themeBundle
, String path
) {
147 Set
<String
> paths
= new TreeSet
<>();
150 Enumeration
<URL
> commonResources
= themeBundle
.findEntries(styleCssPath
, "*.css", true);
151 if (commonResources
!= null) {
152 while (commonResources
.hasMoreElements()) {
153 String resource
= commonResources
.nextElement().getPath();
154 // remove first '/' so that RWT registers it
155 resource
= resource
.substring(1);
156 if (!resource
.endsWith("/")) {
163 Enumeration
<URL
> themeResources
= themeBundle
.findEntries(path
, "*.css", true);
164 if (themeResources
!= null) {
165 while (themeResources
.hasMoreElements()) {
166 String resource
= themeResources
.nextElement().getPath();
167 // remove first '/' so that RWT registers it
168 resource
= resource
.substring(1);
169 if (!resource
.endsWith("/")) {
177 void loadFontsUrl(URL url
) {
178 try (BufferedReader in
= new BufferedReader(new InputStreamReader(url
.openStream(), UTF_8
))) {
180 while ((line
= in
.readLine()) != null) {
182 if (!line
.equals("") && !line
.startsWith("#")) {
186 } catch (IOException e
) {
187 throw new IllegalArgumentException("Cannot load URL " + url
, e
);
191 void addImages(String pattern
) {
192 Enumeration
<URL
> themeResources
= themeBundle
.findEntries(basePath
, pattern
, true);
193 if (themeResources
== null)
195 while (themeResources
.hasMoreElements()) {
196 String resource
= themeResources
.nextElement().getPath();
197 // remove first '/' so that RWT registers it
198 resource
= resource
.substring(1);
199 if (!resource
.endsWith("/")) {
200 // if (resources.containsKey(resource))
201 // log.warn("Overriding " + resource + " from " + themeBundle.getSymbolicName());
202 // resources.put(resource, themeBRL);
203 imagesPaths
.add(resource
);
210 void addFonts(String pattern
) {
211 Enumeration
<URL
> themeResources
= themeBundle
.findEntries(basePath
, pattern
, true);
212 if (themeResources
== null)
214 while (themeResources
.hasMoreElements()) {
215 String resource
= themeResources
.nextElement().getPath();
216 // remove first '/' so that RWT registers it
217 resource
= resource
.substring(1);
218 if (!resource
.endsWith("/")) {
219 // if (resources.containsKey(resource))
220 // log.warn("Overriding " + resource + " from " + themeBundle.getSymbolicName());
221 // resources.put(resource, themeBRL);
222 fontsPaths
.add(resource
);
230 public InputStream
getResourceAsStream(String resourceName
) throws IOException
{
231 URL res
= themeBundle
.getEntry(resourceName
);
233 res
= themeBundle
.getResource(resourceName
);
236 // throw new IllegalArgumentException(
237 // "Resource " + resourceName + " not found in bundle " + themeBundle.getSymbolicName());
239 return res
.openStream();
242 public String
getThemeId() {
246 // public void setThemeId(String themeId) {
247 // this.themeId = themeId;
250 // public String getBasePath() {
254 // public void setBasePath(String basePath) {
255 // this.basePath = basePath;
258 // public String getRapCssPath() {
259 // return rapCssPath;
262 // public void setRapCssPath(String cssPath) {
263 // this.rapCssPath = cssPath;
267 public Set
<String
> getWebCssPaths() {
272 public Set
<String
> getRapCssPaths() {
277 public Set
<String
> getSwtCssPaths() {
282 public Set
<String
> getImagesPaths() {
287 public Set
<String
> getFontsPaths() {
292 public InputStream
loadPath(String path
) throws IOException
{
293 URL url
= themeBundle
.getResource(path
);
295 throw new IllegalArgumentException(
296 "Path " + path
+ " not found in bundle " + themeBundle
.getSymbolicName());
297 return url
.openStream();
300 private static Bundle
findThemeBundle(BundleContext bundleContext
, String themeId
) {
304 // TODO deal with multiple versions
305 Bundle themeBundle
= null;
306 if (themeId
!= null) {
307 for (Bundle bundle
: bundleContext
.getBundles())
308 if (themeId
.equals(bundle
.getSymbolicName())) {
309 themeBundle
= bundle
;
317 public int hashCode() {
318 return themeId
.hashCode();
322 public String
toString() {
323 return "Bundle CMS Theme " + themeId
;