X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms.ui%2Fsrc%2Forg%2Fargeo%2Fcms%2Fui%2Futil%2FBundleCmsTheme.java;h=6b997e6a35c4f0e691b412b1d9129a2759b4ede7;hb=958c7142baa531d444d56725609c43c6a1ebd762;hp=c86ba3601950022cd5367e172236dc7adb4728df;hpb=fea7cd546f9c04c00f961918919dd6307c32cc8e;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms.ui/src/org/argeo/cms/ui/util/BundleCmsTheme.java b/org.argeo.cms.ui/src/org/argeo/cms/ui/util/BundleCmsTheme.java index c86ba3601..6b997e6a3 100644 --- a/org.argeo.cms.ui/src/org/argeo/cms/ui/util/BundleCmsTheme.java +++ b/org.argeo.cms.ui/src/org/argeo/cms/ui/util/BundleCmsTheme.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; @@ -15,6 +16,7 @@ import java.util.Set; import java.util.TreeSet; import java.util.stream.Collectors; +import org.apache.commons.io.IOUtils; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; @@ -33,6 +35,10 @@ public class BundleCmsTheme extends AbstractCmsTheme { public final static String CMS_THEME_PROPERTY = "argeo.cms.theme"; public final static String CMS_THEME_BUNDLE_PROPERTY = "argeo.cms.theme.bundle"; + private final static String HEADER_CSS = "header.css"; + private final static String FONTS_TXT = "fonts.txt"; + private final static String BODY_HTML = "body.html"; + // private final static Log log = LogFactory.getLog(BundleCmsTheme.class); private String themeId; @@ -40,10 +46,13 @@ public class BundleCmsTheme extends AbstractCmsTheme { private Set rapCssPaths = new TreeSet<>(); private Set swtCssPaths = new TreeSet<>(); private Set imagesPaths = new TreeSet<>(); + private Set fontsPaths = new TreeSet<>(); private String headerCss; private List fonts = new ArrayList<>(); + private String bodyHtml=""; + private String basePath; private String styleCssPath; // private String webCssPath; @@ -91,29 +100,41 @@ public class BundleCmsTheme extends AbstractCmsTheme { webCssPaths = addCss(themeBundle, "/css/"); rapCssPaths = addCss(themeBundle, "/rap/"); swtCssPaths = addCss(themeBundle, "/swt/"); - addResources("*.png"); - addResources("*.gif"); - addResources("*.jpg"); - addResources("*.jpeg"); - addResources("*.svg"); - addResources("*.ico"); + addImages("*.png"); + addImages("*.gif"); + addImages("*.jpg"); + addImages("*.jpeg"); + addImages("*.svg"); + addImages("*.ico"); + + addFonts("*.woff"); + addFonts("*.woff2"); // fonts - URL fontsUrl = themeBundle.getEntry(basePath + "fonts.txt"); + URL fontsUrl = themeBundle.getEntry(basePath + FONTS_TXT); if (fontsUrl != null) { loadFontsUrl(fontsUrl); } // common CSS header (plain CSS) - URL headerCssUrl = themeBundle.getEntry(basePath + "header.css"); + URL headerCssUrl = themeBundle.getEntry(basePath + HEADER_CSS); if (headerCssUrl != null) { + // added to plain Web CSS + webCssPaths.add(basePath + HEADER_CSS); + // and it will also be used by RAP: try (BufferedReader buffer = new BufferedReader(new InputStreamReader(headerCssUrl.openStream(), UTF_8))) { headerCss = buffer.lines().collect(Collectors.joining("\n")); } catch (IOException e) { throw new IllegalArgumentException("Cannot read " + headerCssUrl, e); } } - } + + // body + URL bodyUrl = themeBundle.getEntry(basePath + BODY_HTML); + if (bodyUrl != null) { + loadBodyHtml(bodyUrl); + } +} public String getHtmlHeaders() { StringBuilder sb = new StringBuilder(); @@ -132,6 +153,13 @@ public class BundleCmsTheme extends AbstractCmsTheme { else return sb.toString(); } + + + + @Override + public String getBodyHtml() { + return bodyHtml; + } Set addCss(Bundle themeBundle, String path) { Set paths = new TreeSet<>(); @@ -178,7 +206,15 @@ public class BundleCmsTheme extends AbstractCmsTheme { } } - void addResources(String pattern) { + void loadBodyHtml(URL url) { + try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), UTF_8))) { + bodyHtml= IOUtils.toString(url,StandardCharsets.UTF_8); + } catch (IOException e) { + throw new IllegalArgumentException("Cannot load URL " + url, e); + } + } + + void addImages(String pattern) { Enumeration themeResources = themeBundle.findEntries(basePath, pattern, true); if (themeResources == null) return; @@ -197,6 +233,25 @@ public class BundleCmsTheme extends AbstractCmsTheme { } + void addFonts(String pattern) { + Enumeration themeResources = themeBundle.findEntries(basePath, pattern, true); + if (themeResources == null) + return; + while (themeResources.hasMoreElements()) { + String resource = themeResources.nextElement().getPath(); + // remove first '/' so that RWT registers it + resource = resource.substring(1); + if (!resource.endsWith("/")) { +// if (resources.containsKey(resource)) +// log.warn("Overriding " + resource + " from " + themeBundle.getSymbolicName()); +// resources.put(resource, themeBRL); + fontsPaths.add(resource); + } + + } + + } + @Override public InputStream getResourceAsStream(String resourceName) throws IOException { URL res = themeBundle.getEntry(resourceName); @@ -254,6 +309,11 @@ public class BundleCmsTheme extends AbstractCmsTheme { return imagesPaths; } + @Override + public Set getFontsPaths() { + return fontsPaths; + } + @Override public InputStream loadPath(String path) throws IOException { URL url = themeBundle.getResource(path);