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
.nio
.charset
.StandardCharsets
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Enumeration
;
13 import java
.util
.List
;
16 import java
.util
.TreeSet
;
17 import java
.util
.stream
.Collectors
;
19 import org
.apache
.commons
.io
.IOUtils
;
20 import org
.osgi
.framework
.Bundle
;
21 import org
.osgi
.framework
.BundleContext
;
24 * Simplifies the theming of an app (only RAP is supported at this stage).<br>
26 * Additional fonts listed in <code>/fonts.txt</code>.<br>
27 * Additional (standard CSS) header in <code>/header.css</code>.<br>
28 * RAP specific CSS files in <code>/rap/*.css</code>.<br>
29 * All images added as additional resources based on extensions
30 * <code>/ ** /*.{png,gif,jpeg,...}</code>.<br>
32 public class BundleCmsTheme
extends AbstractCmsTheme
{
33 public final static String DEFAULT_CMS_THEME_BUNDLE
= "org.argeo.theme.argeo2";
35 public final static String CMS_THEME_PROPERTY
= "argeo.cms.theme";
36 public final static String CMS_THEME_BUNDLE_PROPERTY
= "argeo.cms.theme.bundle";
38 private final static String HEADER_CSS
= "header.css";
39 private final static String FONTS_TXT
= "fonts.txt";
40 private final static String BODY_HTML
= "body.html";
42 // private final static Log log = LogFactory.getLog(BundleCmsTheme.class);
44 private String themeId
;
45 private Set
<String
> webCssPaths
= new TreeSet
<>();
46 private Set
<String
> rapCssPaths
= new TreeSet
<>();
47 private Set
<String
> swtCssPaths
= new TreeSet
<>();
48 private Set
<String
> imagesPaths
= new TreeSet
<>();
49 private Set
<String
> fontsPaths
= new TreeSet
<>();
51 private String headerCss
;
52 private List
<String
> fonts
= new ArrayList
<>();
54 private String bodyHtml
="<body></body>";
56 private String basePath
;
57 private String styleCssPath
;
58 // private String webCssPath;
59 // private String rapCssPath;
60 // private String swtCssPath;
61 private Bundle themeBundle
;
63 public BundleCmsTheme() {
67 public void init(BundleContext bundleContext
, Map
<String
, String
> properties
) {
68 initResources(bundleContext
, null);
71 public void destroy(BundleContext bundleContext
, Map
<String
, String
> properties
) {
76 public BundleCmsTheme(BundleContext bundleContext
) {
77 this(bundleContext
, null);
81 public BundleCmsTheme(BundleContext bundleContext
, String symbolicName
) {
82 initResources(bundleContext
, symbolicName
);
85 private void initResources(BundleContext bundleContext
, String symbolicName
) {
86 if (symbolicName
== null) {
87 themeBundle
= bundleContext
.getBundle();
88 // basePath = "/theme/";
89 // cssPath = basePath;
91 themeBundle
= findThemeBundle(bundleContext
, symbolicName
);
94 styleCssPath
= "/style/";
95 // webCssPath = "/css/";
96 // rapCssPath = "/rap/";
97 // swtCssPath = "/swt/";
98 // this.themeId = RWT.DEFAULT_THEME_ID;
99 this.themeId
= themeBundle
.getSymbolicName();
100 webCssPaths
= addCss(themeBundle
, "/css/");
101 rapCssPaths
= addCss(themeBundle
, "/rap/");
102 swtCssPaths
= addCss(themeBundle
, "/swt/");
114 URL fontsUrl
= themeBundle
.getEntry(basePath
+ FONTS_TXT
);
115 if (fontsUrl
!= null) {
116 loadFontsUrl(fontsUrl
);
119 // common CSS header (plain CSS)
120 URL headerCssUrl
= themeBundle
.getEntry(basePath
+ HEADER_CSS
);
121 if (headerCssUrl
!= null) {
122 // added to plain Web CSS
123 webCssPaths
.add(basePath
+ HEADER_CSS
);
124 // and it will also be used by RAP:
125 try (BufferedReader buffer
= new BufferedReader(new InputStreamReader(headerCssUrl
.openStream(), UTF_8
))) {
126 headerCss
= buffer
.lines().collect(Collectors
.joining("\n"));
127 } catch (IOException e
) {
128 throw new IllegalArgumentException("Cannot read " + headerCssUrl
, e
);
133 URL bodyUrl
= themeBundle
.getEntry(basePath
+ BODY_HTML
);
134 if (bodyUrl
!= null) {
135 loadBodyHtml(bodyUrl
);
139 public String
getHtmlHeaders() {
140 StringBuilder sb
= new StringBuilder();
141 if (headerCss
!= null) {
142 sb
.append("<style type='text/css'>\n");
143 sb
.append(headerCss
);
144 sb
.append("\n</style>\n");
146 for (String link
: fonts
) {
147 sb
.append("<link rel='stylesheet' href='");
151 if (sb
.length() == 0)
154 return sb
.toString();
160 public String
getBodyHtml() {
164 Set
<String
> addCss(Bundle themeBundle
, String path
) {
165 Set
<String
> paths
= new TreeSet
<>();
168 Enumeration
<URL
> commonResources
= themeBundle
.findEntries(styleCssPath
, "*.css", true);
169 if (commonResources
!= null) {
170 while (commonResources
.hasMoreElements()) {
171 String resource
= commonResources
.nextElement().getPath();
172 // remove first '/' so that RWT registers it
173 resource
= resource
.substring(1);
174 if (!resource
.endsWith("/")) {
181 Enumeration
<URL
> themeResources
= themeBundle
.findEntries(path
, "*.css", true);
182 if (themeResources
!= null) {
183 while (themeResources
.hasMoreElements()) {
184 String resource
= themeResources
.nextElement().getPath();
185 // remove first '/' so that RWT registers it
186 resource
= resource
.substring(1);
187 if (!resource
.endsWith("/")) {
195 void loadFontsUrl(URL url
) {
196 try (BufferedReader in
= new BufferedReader(new InputStreamReader(url
.openStream(), UTF_8
))) {
198 while ((line
= in
.readLine()) != null) {
200 if (!line
.equals("") && !line
.startsWith("#")) {
204 } catch (IOException e
) {
205 throw new IllegalArgumentException("Cannot load URL " + url
, e
);
209 void loadBodyHtml(URL url
) {
210 try (BufferedReader in
= new BufferedReader(new InputStreamReader(url
.openStream(), UTF_8
))) {
211 bodyHtml
= IOUtils
.toString(url
,StandardCharsets
.UTF_8
);
212 } catch (IOException e
) {
213 throw new IllegalArgumentException("Cannot load URL " + url
, e
);
217 void addImages(String pattern
) {
218 Enumeration
<URL
> themeResources
= themeBundle
.findEntries(basePath
, pattern
, true);
219 if (themeResources
== null)
221 while (themeResources
.hasMoreElements()) {
222 String resource
= themeResources
.nextElement().getPath();
223 // remove first '/' so that RWT registers it
224 resource
= resource
.substring(1);
225 if (!resource
.endsWith("/")) {
226 // if (resources.containsKey(resource))
227 // log.warn("Overriding " + resource + " from " + themeBundle.getSymbolicName());
228 // resources.put(resource, themeBRL);
229 imagesPaths
.add(resource
);
236 void addFonts(String pattern
) {
237 Enumeration
<URL
> themeResources
= themeBundle
.findEntries(basePath
, pattern
, true);
238 if (themeResources
== null)
240 while (themeResources
.hasMoreElements()) {
241 String resource
= themeResources
.nextElement().getPath();
242 // remove first '/' so that RWT registers it
243 resource
= resource
.substring(1);
244 if (!resource
.endsWith("/")) {
245 // if (resources.containsKey(resource))
246 // log.warn("Overriding " + resource + " from " + themeBundle.getSymbolicName());
247 // resources.put(resource, themeBRL);
248 fontsPaths
.add(resource
);
256 public InputStream
getResourceAsStream(String resourceName
) throws IOException
{
257 URL res
= themeBundle
.getEntry(resourceName
);
259 res
= themeBundle
.getResource(resourceName
);
262 // throw new IllegalArgumentException(
263 // "Resource " + resourceName + " not found in bundle " + themeBundle.getSymbolicName());
265 return res
.openStream();
268 public String
getThemeId() {
272 // public void setThemeId(String themeId) {
273 // this.themeId = themeId;
276 // public String getBasePath() {
280 // public void setBasePath(String basePath) {
281 // this.basePath = basePath;
284 // public String getRapCssPath() {
285 // return rapCssPath;
288 // public void setRapCssPath(String cssPath) {
289 // this.rapCssPath = cssPath;
293 public Set
<String
> getWebCssPaths() {
298 public Set
<String
> getRapCssPaths() {
303 public Set
<String
> getSwtCssPaths() {
308 public Set
<String
> getImagesPaths() {
313 public Set
<String
> getFontsPaths() {
318 public InputStream
loadPath(String path
) throws IOException
{
319 URL url
= themeBundle
.getResource(path
);
321 throw new IllegalArgumentException(
322 "Path " + path
+ " not found in bundle " + themeBundle
.getSymbolicName());
323 return url
.openStream();
326 private static Bundle
findThemeBundle(BundleContext bundleContext
, String themeId
) {
330 // TODO deal with multiple versions
331 Bundle themeBundle
= null;
332 if (themeId
!= null) {
333 for (Bundle bundle
: bundleContext
.getBundles())
334 if (themeId
.equals(bundle
.getSymbolicName())) {
335 themeBundle
= bundle
;
343 public int hashCode() {
344 return themeId
.hashCode();
348 public String
toString() {
349 return "Bundle CMS Theme " + themeId
;