1 package org
.argeo
.cms
.ui
.util
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.ByteArrayOutputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
8 import java
.util
.LinkedHashMap
;
11 import org
.apache
.commons
.io
.IOUtils
;
12 import org
.argeo
.cms
.CmsException
;
13 import org
.eclipse
.rap
.rwt
.service
.ResourceLoader
;
14 import org
.osgi
.framework
.Bundle
;
16 /** {@link ResourceLoader} caching stylesheets. */
17 public class StyleSheetResourceLoader
implements ResourceLoader
{
18 private Bundle themeBundle
;
19 private Map
<String
, StyleSheet
> stylesheets
= new LinkedHashMap
<String
, StyleSheet
>();
21 public StyleSheetResourceLoader(Bundle themeBundle
) {
22 this.themeBundle
= themeBundle
;
26 public InputStream
getResourceAsStream(String resourceName
) throws IOException
{
27 if (!stylesheets
.containsKey(resourceName
)) {
28 // TODO deal with other bundles
29 // Bundle bundle = bundleContext.getBundle();
31 // bundle.getLocation().substring("initial@reference:".length());
32 // if (location.startsWith("file:")) {
35 // path = Paths.get(new URI(location));
36 // } catch (URISyntaxException e) {
37 // e.printStackTrace();
39 // if (path != null) {
40 // Path resourcePath = path.resolve(resourceName);
41 // if (Files.exists(resourcePath))
42 // return Files.newInputStream(resourcePath);
46 URL res
= themeBundle
.getEntry(resourceName
);
48 throw new CmsException(
49 "Entry " + resourceName
+ " not found in bundle " + themeBundle
.getSymbolicName());
50 ByteArrayOutputStream out
= new ByteArrayOutputStream();
51 IOUtils
.copy(res
.openStream(), out
);
52 stylesheets
.put(resourceName
, new StyleSheet(out
.toByteArray()));
54 return new ByteArrayInputStream(stylesheets
.get(resourceName
).getData());
55 // return res.openStream();
58 private class StyleSheet
{
61 public StyleSheet(byte[] data
) {
66 public byte[] getData() {