in app/src/main/java/org/apache/roller/weblogger/business/themes/SharedThemeFromDir.java [177:396]
private void loadThemeFromDisk() throws ThemeInitializationException {
log.debug("Parsing theme descriptor for " + this.themeDir);
ThemeMetadata themeMetadata;
try {
// lookup theme descriptor and parse it
ThemeMetadataParser parser = new ThemeMetadataParser();
InputStream is = new FileInputStream(this.themeDir + File.separator
+ "theme.xml");
themeMetadata = parser.unmarshall(is);
} catch (Exception ex) {
throw new ThemeInitializationException(
"Unable to parse theme.xml for theme " + this.themeDir, ex);
}
log.debug("Loading Theme " + themeMetadata.getName());
// use parsed theme descriptor to load Theme data
setId(themeMetadata.getId());
setName(themeMetadata.getName());
if (StringUtils.isNotEmpty(themeMetadata.getDescription())) {
setDescription(themeMetadata.getDescription());
} else {
setDescription(" ");
}
setAuthor(themeMetadata.getAuthor());
setLastModified(null);
setEnabled(true);
// load resource representing preview image
File previewFile = new File(this.themeDir + File.separator + themeMetadata.getPreviewImage());
if (!previewFile.exists() || !previewFile.canRead()) {
log.warn("Couldn't read theme [" + this.getName()
+ "] preview image file ["
+ themeMetadata.getPreviewImage() + "]");
} else {
this.previewImage = new SharedThemeResourceFromDir(
themeMetadata.getPreviewImage(), previewFile);
}
// available types with Roller
List<RenditionType> availableTypesList = new ArrayList<>();
availableTypesList.add(RenditionType.STANDARD);
if (themeMetadata.getDualTheme()) {
availableTypesList.add(RenditionType.MOBILE);
}
// load stylesheet if possible
if (themeMetadata.getStylesheet() != null) {
ThemeMetadataTemplate stylesheetTmpl = themeMetadata
.getStylesheet();
// getting the template codes for available types
ThemeMetadataTemplateRendition standardTemplateCode = stylesheetTmpl
.getTemplateRenditionTable().get(RenditionType.STANDARD);
ThemeMetadataTemplateRendition mobileTemplateCode = stylesheetTmpl
.getTemplateRenditionTable().get(RenditionType.MOBILE);
// standardTemplateCode required
if (standardTemplateCode == null) {
throw new ThemeInitializationException(
"Error in getting template codes for template");
} else if (mobileTemplateCode == null && themeMetadata.getDualTheme()) {
// clone the standard template code if no mobile is present
mobileTemplateCode = new ThemeMetadataTemplateRendition();
mobileTemplateCode.setContentsFile(standardTemplateCode
.getContentsFile());
mobileTemplateCode.setTemplateLang(standardTemplateCode
.getTemplateLang());
mobileTemplateCode.setType(RenditionType.MOBILE);
stylesheetTmpl.addTemplateRendition(mobileTemplateCode);
}
// construct File object from path
// we are getting the file path from standard as the default and
// load it to initially.
File templateFile = new File(this.themeDir + File.separator
+ standardTemplateCode.getContentsFile());
// read stylesheet contents
String contents = loadTemplateFile(templateFile);
if (contents == null) {
// if we don't have any contents then skip this one
log.error("Couldn't load stylesheet theme [" + this.getName()
+ "] template file [" + templateFile + "]");
} else {
// construct ThemeTemplate representing this file
// here we set content and template language from standard
// template code assuming it is the default
SharedThemeTemplate themeTemplate = new SharedThemeTemplate(
themeMetadata.getId() + ":"
+ stylesheetTmpl.getName(),
stylesheetTmpl.getAction(), stylesheetTmpl.getName(),
stylesheetTmpl.getDescription(), contents,
stylesheetTmpl.getLink(), new Date(
templateFile.lastModified()), false, false);
for (RenditionType type : availableTypesList) {
SharedThemeTemplateRendition rendition = createRendition(
themeTemplate.getId(),
stylesheetTmpl.getTemplateRendition(type));
themeTemplate.addTemplateRendition(rendition);
// Set Last Modified
Date lstModified = rendition.getLastModified();
if (getLastModified() == null
|| lstModified.after(getLastModified())) {
setLastModified(lstModified);
}
}
// store it
this.stylesheet = themeTemplate;
// Update last modified
themeTemplate.setLastModified(getLastModified());
addTemplate(themeTemplate);
}
}
// go through static resources and add them to the theme
for (String resourcePath : themeMetadata.getResources()) {
// construct ThemeResource object from resource
File resourceFile = new File(this.themeDir + File.separator
+ resourcePath);
// Continue reading theme even if problem encountered with one file
if (!resourceFile.exists() || !resourceFile.canRead()) {
log.warn("Couldn't read theme [" + this.getName()
+ "] resource file [" + resourcePath + "]");
continue;
}
// add it to the theme
setResource(resourcePath, new SharedThemeResourceFromDir(
resourcePath, resourceFile));
// Set Last Modified
Date lstModified = new Date(resourceFile.lastModified());
if (getLastModified() == null
|| lstModified.after(getLastModified())) {
setLastModified(lstModified);
}
}
// go through templates and read in contents to a ThemeTemplate
SharedThemeTemplate themeTemplate;
for (ThemeMetadataTemplate templateMetadata : themeMetadata.getTemplates()) {
// getting the template codes for available types
ThemeMetadataTemplateRendition standardTemplateCode = templateMetadata
.getTemplateRenditionTable().get(RenditionType.STANDARD);
ThemeMetadataTemplateRendition mobileTemplateCode = templateMetadata
.getTemplateRenditionTable().get(RenditionType.MOBILE);
// If no template code present for any type
if (standardTemplateCode == null) {
throw new ThemeInitializationException(
"Error in getting template codes for template");
} else if (mobileTemplateCode == null && themeMetadata.getDualTheme()) {
// cloning the standard template code if no mobile is present
mobileTemplateCode = new ThemeMetadataTemplateRendition();
mobileTemplateCode.setContentsFile(standardTemplateCode
.getContentsFile());
mobileTemplateCode.setTemplateLang(standardTemplateCode
.getTemplateLang());
mobileTemplateCode.setType(RenditionType.MOBILE);
templateMetadata.addTemplateRendition(mobileTemplateCode);
}
// construct File object from path
File templateFile = new File(this.themeDir + File.separator
+ standardTemplateCode.getContentsFile());
String contents = loadTemplateFile(templateFile);
if (contents == null) {
// if we don't have any contents then skip this one
throw new ThemeInitializationException("Couldn't load theme ["
+ this.getName() + "] template file [" + templateFile
+ "]");
}
// construct ThemeTemplate representing this file
themeTemplate = new SharedThemeTemplate(
themeMetadata.getId() + ":" + templateMetadata.getName(),
templateMetadata.getAction(), templateMetadata.getName(),
templateMetadata.getDescription(), contents,
templateMetadata.getLink(), new Date(
templateFile.lastModified()),
templateMetadata.isHidden(), templateMetadata.isNavbar());
for (RenditionType type : availableTypesList) {
SharedThemeTemplateRendition templateCode = createRendition(
themeTemplate.getId(),
templateMetadata.getTemplateRendition(type));
themeTemplate.addTemplateRendition(templateCode);
// Set Last Modified
Date lstModified = templateCode.getLastModified();
if (getLastModified() == null
|| lstModified.after(getLastModified())) {
setLastModified(lstModified);
}
}
themeTemplate.setLastModified(getLastModified());
// add it to the theme
addTemplate(themeTemplate);
}
}