1 package org
.argeo
.cms
.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
;
15 import org
.osgi
.framework
.BundleContext
;
17 /** {@link ResourceLoader} caching stylesheets. */
18 public class StyleSheetResourceLoader
implements ResourceLoader
{
19 private final BundleContext bundleContext
;
21 private Map
<String
, StyleSheet
> stylesheets
= new LinkedHashMap
<String
, StyleSheet
>();
23 public StyleSheetResourceLoader(BundleContext bundleContext
) {
24 this.bundleContext
= bundleContext
;
28 public InputStream
getResourceAsStream(String resourceName
)
30 if (!stylesheets
.containsKey(resourceName
)) {
31 // TODO deal with other bundles
32 Bundle bundle
= bundleContext
.getBundle();
34 // bundle.getLocation().substring("initial@reference:".length());
35 // if (location.startsWith("file:")) {
38 // path = Paths.get(new URI(location));
39 // } catch (URISyntaxException e) {
40 // e.printStackTrace();
42 // if (path != null) {
43 // Path resourcePath = path.resolve(resourceName);
44 // if (Files.exists(resourcePath))
45 // return Files.newInputStream(resourcePath);
48 URL res
= bundle
.getResource(resourceName
);
50 throw new CmsException("Resource " + resourceName
51 + " not found in bundle " + bundle
.getSymbolicName());
52 ByteArrayOutputStream out
= new ByteArrayOutputStream();
53 IOUtils
.copy(res
.openStream(), out
);
54 stylesheets
.put(resourceName
, new StyleSheet(out
.toByteArray()));
56 return new ByteArrayInputStream(stylesheets
.get(resourceName
).getData());
57 // return res.openStream();
60 private class StyleSheet
{
63 public StyleSheet(byte[] data
) {
68 public byte[] getData() {