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 Log log = LogFactory.getLog(BundleCmsTheme.class);
38 private String themeId
;
39 private Set
<String
> webCssPaths
= new TreeSet
<>();
40 private Set
<String
> rapCssPaths
= new TreeSet
<>();
41 private Set
<String
> swtCssPaths
= new TreeSet
<>();
42 private Set
<String
> imagesPaths
= new TreeSet
<>();
44 private String headerCss
;
45 private List
<String
> fonts
= new ArrayList
<>();
47 private String basePath
;
48 private String styleCssPath
;
49 // private String webCssPath;
50 // private String rapCssPath;
51 // private String swtCssPath;
52 private Bundle themeBundle
;
54 public BundleCmsTheme() {
58 public void init(BundleContext bundleContext
, Map
<String
, String
> properties
) {
59 initResources(bundleContext
, null);
62 public void destroy(BundleContext bundleContext
, Map
<String
, String
> properties
) {
67 public BundleCmsTheme(BundleContext bundleContext
) {
68 this(bundleContext
, null);
72 public BundleCmsTheme(BundleContext bundleContext
, String symbolicName
) {
73 initResources(bundleContext
, symbolicName
);
76 private void initResources(BundleContext bundleContext
, String symbolicName
) {
77 if (symbolicName
== null) {
78 themeBundle
= bundleContext
.getBundle();
79 // basePath = "/theme/";
80 // cssPath = basePath;
82 themeBundle
= findThemeBundle(bundleContext
, symbolicName
);
85 styleCssPath
= "/style/";
86 // webCssPath = "/css/";
87 // rapCssPath = "/rap/";
88 // swtCssPath = "/swt/";
89 // this.themeId = RWT.DEFAULT_THEME_ID;
90 this.themeId
= themeBundle
.getSymbolicName();
91 webCssPaths
= addCss(themeBundle
, "/css/");
92 rapCssPaths
= addCss(themeBundle
, "/rap/");
93 swtCssPaths
= addCss(themeBundle
, "/swt/");
94 addResources("*.png");
95 addResources("*.gif");
96 addResources("*.jpg");
97 addResources("*.jpeg");
98 addResources("*.svg");
99 addResources("*.ico");
102 URL fontsUrl
= themeBundle
.getEntry(basePath
+ "fonts.txt");
103 if (fontsUrl
!= null) {
104 loadFontsUrl(fontsUrl
);
107 // common CSS header (plain CSS)
108 URL headerCssUrl
= themeBundle
.getEntry(basePath
+ "header.css");
109 if (headerCssUrl
!= null) {
110 try (BufferedReader buffer
= new BufferedReader(new InputStreamReader(headerCssUrl
.openStream(), UTF_8
))) {
111 headerCss
= buffer
.lines().collect(Collectors
.joining("\n"));
112 } catch (IOException e
) {
113 throw new IllegalArgumentException("Cannot read " + headerCssUrl
, e
);
118 public String
getHtmlHeaders() {
119 StringBuilder sb
= new StringBuilder();
120 if (headerCss
!= null) {
121 sb
.append("<style type='text/css'>\n");
122 sb
.append(headerCss
);
123 sb
.append("\n</style>\n");
125 for (String link
: fonts
) {
126 sb
.append("<link rel='stylesheet' href='");
130 if (sb
.length() == 0)
133 return sb
.toString();
136 Set
<String
> addCss(Bundle themeBundle
, String path
) {
137 Set
<String
> paths
= new TreeSet
<>();
138 Enumeration
<URL
> themeResources
= themeBundle
.findEntries(path
, "*.css", true);
139 if (themeResources
== null)
141 while (themeResources
.hasMoreElements()) {
142 String resource
= themeResources
.nextElement().getPath();
143 // remove first '/' so that RWT registers it
144 resource
= resource
.substring(1);
145 if (!resource
.endsWith("/")) {
151 Enumeration
<URL
> commonResources
= themeBundle
.findEntries(styleCssPath
, "*.css", true);
152 if (commonResources
== null)
154 while (commonResources
.hasMoreElements()) {
155 String resource
= commonResources
.nextElement().getPath();
156 // remove first '/' so that RWT registers it
157 resource
= resource
.substring(1);
158 if (!resource
.endsWith("/")) {
165 void loadFontsUrl(URL url
) {
166 try (BufferedReader in
= new BufferedReader(new InputStreamReader(url
.openStream(), UTF_8
))) {
168 while ((line
= in
.readLine()) != null) {
170 if (!line
.equals("") && !line
.startsWith("#")) {
174 } catch (IOException e
) {
175 throw new IllegalArgumentException("Cannot load URL " + url
, e
);
179 void addResources(String pattern
) {
180 Enumeration
<URL
> themeResources
= themeBundle
.findEntries(basePath
, pattern
, true);
181 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("/")) {
188 // if (resources.containsKey(resource))
189 // log.warn("Overriding " + resource + " from " + themeBundle.getSymbolicName());
190 // resources.put(resource, themeBRL);
191 imagesPaths
.add(resource
);
199 public InputStream
getResourceAsStream(String resourceName
) throws IOException
{
200 URL res
= themeBundle
.getEntry(resourceName
);
202 res
= themeBundle
.getResource(resourceName
);
205 // throw new IllegalArgumentException(
206 // "Resource " + resourceName + " not found in bundle " + themeBundle.getSymbolicName());
208 return res
.openStream();
211 public String
getThemeId() {
215 // public void setThemeId(String themeId) {
216 // this.themeId = themeId;
219 // public String getBasePath() {
223 // public void setBasePath(String basePath) {
224 // this.basePath = basePath;
227 // public String getRapCssPath() {
228 // return rapCssPath;
231 // public void setRapCssPath(String cssPath) {
232 // this.rapCssPath = cssPath;
236 public Set
<String
> getWebCssPaths() {
241 public Set
<String
> getRapCssPaths() {
246 public Set
<String
> getSwtCssPaths() {
251 public Set
<String
> getImagesPaths() {
256 public InputStream
loadPath(String path
) throws IOException
{
257 URL url
= themeBundle
.getResource(path
);
259 throw new IllegalArgumentException(
260 "Path " + path
+ " not found in bundle " + themeBundle
.getSymbolicName());
261 return url
.openStream();
264 private static Bundle
findThemeBundle(BundleContext bundleContext
, String themeId
) {
268 // TODO deal with multiple versions
269 Bundle themeBundle
= null;
270 if (themeId
!= null) {
271 for (Bundle bundle
: bundleContext
.getBundles())
272 if (themeId
.equals(bundle
.getSymbolicName())) {
273 themeBundle
= bundle
;