/* * Copyright (C) 2020 Christian Pierre MOMON * * This file is part of StatoolInfos, simple service statistics tool. * * StatoolInfos is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * StatoolInfos is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with StatoolInfos. If not, see . */ package fr.devinsy.statoolinfos.htmlize; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import fr.devinsy.statoolinfos.core.Categories; import fr.devinsy.statoolinfos.core.Category; import fr.devinsy.statoolinfos.core.Configuration; import fr.devinsy.statoolinfos.core.Factory; import fr.devinsy.statoolinfos.core.Federation; import fr.devinsy.statoolinfos.core.Organization; import fr.devinsy.statoolinfos.core.Organizations; import fr.devinsy.statoolinfos.core.Service; import fr.devinsy.statoolinfos.core.Services; import fr.devinsy.statoolinfos.core.SocialNetworks; import fr.devinsy.statoolinfos.core.Software; import fr.devinsy.statoolinfos.core.Softwares; import fr.devinsy.statoolinfos.core.StatoolInfosException; import fr.devinsy.statoolinfos.core.StatoolInfosUtils; import fr.devinsy.statoolinfos.crawl.CrawlCache; import fr.devinsy.statoolinfos.crawl.CrawlCache.DefaultLogoGenerator; import fr.devinsy.statoolinfos.stats.StatAgent; import fr.devinsy.statoolinfos.stats.categories.CategoryStats; import fr.devinsy.statoolinfos.stats.properties.PropertyStats; import fr.devinsy.statoolinfos.stats.propertyfiles.PropertiesFileStats; import fr.devinsy.statoolinfos.stats.softwares.SoftwareStats; /** * The Class Htmlizer. */ public class Htmlizer { private static Logger logger = LoggerFactory.getLogger(Htmlizer.class); /** * Instantiates a new htmlizer. */ private Htmlizer() { } /** * Clear. * * @param configuration * the configuration */ public static void clear(final Configuration configuration) { logger.info("Htmlize directory setting: {}", configuration.getHtmlizeDirectoryPath()); String htmlDirectoryPath = configuration.getHtmlizeDirectoryPath(); if (StringUtils.isBlank(htmlDirectoryPath)) { logger.warn("Undefined htmlize directory."); } else if (!new File(htmlDirectoryPath).exists()) { logger.warn("Htmlize directory does not exist: {}.", htmlDirectoryPath); } else { File htmlizeDirectory = configuration.getHtmlizeDirectory(); for (File file : htmlizeDirectory.listFiles()) { if ((file.isFile()) && (StringUtils.endsWithAny(file.getName(), ".properties", ".js", ".html", ".ico", ".css", ".jpg", ".png", ".xhtml"))) { logger.info("Deleting " + file.getName()); file.delete(); } } } } /** * Copy stuff. * * @param target * the target * @throws IOException */ private static void copyStuff(final File targetDirectory) throws IOException { // Copy commons files (index, images, favicon, css…). if (!new File(targetDirectory, "index.html").exists()) { final String source = "/fr/devinsy/statoolinfos/htmlize/stuff/"; StatoolInfosUtils.copyRessource(source + "index.html", targetDirectory); StatoolInfosUtils.copyRessource(source + "statoolinfos.css", targetDirectory); StatoolInfosUtils.copyRessource(source + "Chart.bundle.min.js", targetDirectory); StatoolInfosUtils.copyRessource(source + "sorttable.js", targetDirectory); StatoolInfosUtils.copyRessource(source + "datatables.min.css", targetDirectory); StatoolInfosUtils.copyRessource(source + "datatables.min.js", targetDirectory); StatoolInfosUtils.copyRessource(source + "statoolinfos-logo.jpg", targetDirectory); StatoolInfosUtils.copyRessource(source + "statoolinfos-logo.ico", targetDirectory); StatoolInfosUtils.copyRessource(source + "statoolinfos-logo-name.jpg", targetDirectory); StatoolInfosUtils.copyRessource(source + "statoolinfos-logo.jpg", new File(targetDirectory, "logo.jpg")); StatoolInfosUtils.copyRessource(source + "statoolinfos-logo.ico", new File(targetDirectory, "favicon.ico")); // StatoolInfosUtils.copyRessource(source + "status-ok.png", targetDirectory); StatoolInfosUtils.copyRessource(source + "status-warning.png", targetDirectory); StatoolInfosUtils.copyRessource(source + "status-alert.png", targetDirectory); StatoolInfosUtils.copyRessource(source + "status-error.png", targetDirectory); StatoolInfosUtils.copyRessource(source + "status-over.png", targetDirectory); StatoolInfosUtils.copyRessource(source + "status-void.png", targetDirectory); // StatoolInfosUtils.copyRessource(source + "diaspora-logo.png", targetDirectory); StatoolInfosUtils.copyRessource(source + "mastodon-logo.png", targetDirectory); // File color = new File(targetDirectory, "circle-icons/color"); color.mkdirs(); File mono = new File(targetDirectory, "circle-icons/mono"); mono.mkdirs(); StatoolInfosUtils.copyRessource(source + "circle-icons/color/booklet.png", color); StatoolInfosUtils.copyRessource(source + "circle-icons/mono/booklet.png", mono); StatoolInfosUtils.copyRessource(source + "circle-icons/color/bookshelf.png", color); StatoolInfosUtils.copyRessource(source + "circle-icons/mono/bookshelf.png", mono); StatoolInfosUtils.copyRessource(source + "circle-icons/color/clipboard.png", color); StatoolInfosUtils.copyRessource(source + "circle-icons/mono/clipboard.png", mono); StatoolInfosUtils.copyRessource(source + "circle-icons/color/contacts.png", color); StatoolInfosUtils.copyRessource(source + "circle-icons/mono/contacts.png", mono); StatoolInfosUtils.copyRessource(source + "circle-icons/color/cruise.png", color); StatoolInfosUtils.copyRessource(source + "circle-icons/mono/cruise.png", mono); StatoolInfosUtils.copyRessource(source + "circle-icons/color/dev.png", color); StatoolInfosUtils.copyRessource(source + "circle-icons/mono/dev.png", mono); StatoolInfosUtils.copyRessource(source + "circle-icons/color/document.png", color); StatoolInfosUtils.copyRessource(source + "circle-icons/mono/document.png", mono); StatoolInfosUtils.copyRessource(source + "circle-icons/color/creditcard.png", color); StatoolInfosUtils.copyRessource(source + "circle-icons/mono/creditcard.png", mono); StatoolInfosUtils.copyRessource(source + "circle-icons/color/email.png", color); StatoolInfosUtils.copyRessource(source + "circle-icons/mono/email.png", mono); StatoolInfosUtils.copyRessource(source + "circle-icons/color/global.png", color); StatoolInfosUtils.copyRessource(source + "circle-icons/mono/global.png", mono); StatoolInfosUtils.copyRessource(source + "circle-icons/color/mail.png", color); StatoolInfosUtils.copyRessource(source + "circle-icons/mono/mail.png", mono); StatoolInfosUtils.copyRessource(source + "circle-icons/color/money.png", color); StatoolInfosUtils.copyRessource(source + "circle-icons/mono/money.png", mono); StatoolInfosUtils.copyRessource(source + "circle-icons/color/profile.png", color); StatoolInfosUtils.copyRessource(source + "circle-icons/mono/profile.png", mono); StatoolInfosUtils.copyRessource(source + "circle-icons/color/ribbon.png", color); StatoolInfosUtils.copyRessource(source + "circle-icons/mono/ribbon.png", mono); StatoolInfosUtils.copyRessource(source + "circle-icons/color/toolbox.png", color); StatoolInfosUtils.copyRessource(source + "circle-icons/mono/toolbox.png", mono); StatoolInfosUtils.copyRessource(source + "circle-icons/color/tools.png", color); StatoolInfosUtils.copyRessource(source + "circle-icons/mono/tools.png", mono); } } /** * Htmlize. * * @param configuration * the configuration * @throws IOException * Signals that an I/O exception has occurred. * @throws StatoolInfosException * the statool infos exception */ public static void htmlize(final Configuration configuration) throws IOException, StatoolInfosException { logger.info("Cache setting: {}", configuration.getCrawlCachePath()); logger.info("Htmlize input setting: {}", configuration.getHtmlizeInputPath()); logger.info("Htmlize directory setting: {}", configuration.getHtmlizeDirectoryPath()); File htmlizeInput = configuration.getHtmlizeInput(); File htmlizeDirectory = configuration.getHtmlizeDirectory(); if (htmlizeInput == null) { throw new IllegalArgumentException("Htmlize input undefined."); } else if (!htmlizeInput.exists()) { throw new IllegalArgumentException("Htmlize input is missing."); } else if (htmlizeInput.isDirectory()) { throw new IllegalArgumentException("Htmlize input is a directory."); } else if (htmlizeDirectory == null) { throw new IllegalArgumentException("Htmlize directory undefined."); } else if (!htmlizeDirectory.exists()) { throw new IllegalArgumentException("Htmlize directory is missing."); } else if (!htmlizeDirectory.isDirectory()) { throw new IllegalArgumentException("Htmlize directory is not a directory."); } else { if (configuration.isFederation()) { Htmlizer.htmlizeFederation(configuration); } else if (configuration.isOrganization()) { Htmlizer.htmlizeOrganisation(configuration); } else { logger.warn("No htmlize for this input: {}.", configuration.getClassName()); } } } /** * Htmlize. * * @param configurationFile * the configuration file * @throws StatoolInfosException * the statool infos exception * @throws IOException * Signals that an I/O exception has occurred. */ public static void htmlize(final File configurationFile) throws StatoolInfosException, IOException { logger.info("Htmlize {}", configurationFile.getAbsolutePath()); Configuration configuration = Factory.loadConfiguration(configurationFile); htmlize(configuration); } /** * Htmlize federation. * * @param configuration * the configuration * @throws IOException * Signals that an I/O exception has occurred. * @throws StatoolInfosException * the statool infos exception */ public static void htmlizeFederation(final Configuration configuration) throws IOException, StatoolInfosException { CrawlCache cache = configuration.getCrawlCache(); File htmlizeInput = configuration.getHtmlizeInput(); File htmlizeDirectory = configuration.getHtmlizeDirectory(); Federation federation = Factory.loadFederation(htmlizeInput, cache); Categories categories = Factory.loadCategories(configuration.getCategoryFile(), federation); copyStuff(htmlizeDirectory); // Manage the logo file. logger.info("Htmlize federation logo."); cache.restoreLogoTo(federation.getLogoURL(), new File(htmlizeDirectory, federation.getTechnicalName() + "-logo.png"), federation.getTechnicalName(), DefaultLogoGenerator.CAT); logger.info("Htmlize federation properties files."); FileUtils.copyFile(federation.getInputFile(), new File(htmlizeDirectory, federation.getTechnicalName() + ".properties")); // logger.info("Htmlize about page."); String page = AboutPage.build(); FileUtils.write(new File(htmlizeDirectory, "about.xhtml"), page, StandardCharsets.UTF_8); // logger.info("Htmlize federation page: {}.", federation.getName()); page = FederationPage.build(federation); FileUtils.write(new File(htmlizeDirectory, "index.xhtml"), page, StandardCharsets.UTF_8); for (Organization organization : federation.getOrganizations()) { // Manage the logo file. logger.info("Htmlize organization logo: {}.", organization.getName()); cache.restoreLogoTo(organization.getLogoURL(), new File(htmlizeDirectory, organization.getTechnicalName() + "-logo.png"), organization.getTechnicalName(), DefaultLogoGenerator.CAT); logger.info("Htmlize organization properties file: {}.", organization.getName()); FileUtils.copyFile(organization.getInputFile(), new File(htmlizeDirectory, organization.getTechnicalName() + ".properties")); // logger.info("Htmlize organization page: {}.", organization.getName()); page = OrganizationPage.build(organization); FileUtils.write(new File(htmlizeDirectory, organization.getTechnicalName() + ".xhtml"), page, StandardCharsets.UTF_8); // for (Service service : organization.getServices()) { // Manage the logo file. logger.info("Htmlize service logo: {}.", service.getName()); cache.restoreLogoTo(service.getLogoURL(), new File(htmlizeDirectory, organization.getTechnicalName() + "-" + service.getTechnicalName() + "-logo.png"), service.getTechnicalName(), DefaultLogoGenerator.BIRD); logger.info("Htmlize service properties file: {}.", service.getName()); FileUtils.copyFile(service.getInputFile(), new File(htmlizeDirectory, organization.getTechnicalName() + "-" + service.getTechnicalName() + ".properties")); logger.info("Htmlize service page: {}.", service.getName()); page = ServicePage.build(organization, service); FileUtils.write(new File(htmlizeDirectory, organization.getTechnicalName() + "-" + service.getTechnicalName() + ".xhtml"), page, StandardCharsets.UTF_8); } } // logger.info("Htmlize services page."); page = ServicesPage.build(federation.getAllServices()); FileUtils.write(new File(htmlizeDirectory, "services.xhtml"), page, StandardCharsets.UTF_8); // { logger.info("Htmlize propertiesFiles page."); PropertiesFileStats stats = StatAgent.statAllPropertiesFiles(federation, cache).sortByName(); page = PropertiesFilesPage.build(stats); FileUtils.write(new File(htmlizeDirectory, "propertiesFiles.xhtml"), page, StandardCharsets.UTF_8); } // { logger.info("Htmlize propertyStats page."); PropertyStats stats = StatAgent.statAllProperties(federation); page = PropertyStatsPage.build("Toutes les propriétés", stats); FileUtils.write(new File(htmlizeDirectory, "propertyStats.xhtml"), page, StandardCharsets.UTF_8); PropertyStats federationStats = StatAgent.statFederationProperties(federation); page = PropertyStatsPage.build("Les propriétés de la fédération", federationStats); FileUtils.write(new File(htmlizeDirectory, "propertyStats-federation.xhtml"), page, StandardCharsets.UTF_8); PropertyStats organizationsStats = StatAgent.statOrganizationsProperties(federation.getOrganizations()); page = PropertyStatsPage.build("Les propriétés des organizations", organizationsStats); FileUtils.write(new File(htmlizeDirectory, "propertyStats-organizations.xhtml"), page, StandardCharsets.UTF_8); PropertyStats servicesStats = StatAgent.statServicesProperties(federation.getAllServices()); page = PropertyStatsPage.build("Les propriétés des services", servicesStats); FileUtils.write(new File(htmlizeDirectory, "propertyStats-services.xhtml"), page, StandardCharsets.UTF_8); } // { logger.info("Htmlize categories page."); CategoryStats stats = StatAgent.statAllCategories(federation, categories); page = CategoriesPage.build(stats); FileUtils.write(new File(htmlizeDirectory, "categories.xhtml"), page, StandardCharsets.UTF_8); } // { logger.info("Htmlize category pages."); for (Category category : categories) { Services services = federation.getAllServices().getBy(category); page = CategoryPage.build(category, services); FileUtils.write(new File(htmlizeDirectory, "category-" + category.getTechnicalName() + ".xhtml"), page, StandardCharsets.UTF_8); } } // { logger.info("Htmlize softwares page."); SoftwareStats stats = StatAgent.statAllSoftwares(federation, categories); page = SoftwaresPage.build(stats); FileUtils.write(new File(htmlizeDirectory, "softwares.xhtml"), page, StandardCharsets.UTF_8); } // { logger.info("Htmlize software pages."); Softwares catalog = federation.getSoftwares(); for (Software software : catalog.values()) { Services services = federation.getAllServices().getBy(software); page = SoftwarePage.build(software, services); FileUtils.write(new File(htmlizeDirectory, "software-" + software.getTechnicalName() + ".xhtml"), page, StandardCharsets.UTF_8); } } // { logger.info("Htmlize social networks pages."); Organizations organizations = federation.getOrganizations().filterBySocialNetworks(); page = SocialNetworksPage.build("Tous", organizations); FileUtils.write(new File(htmlizeDirectory, "socialNetworks.xhtml"), page, StandardCharsets.UTF_8); organizations = federation.getOrganizations().filterBySocialNetwork(SocialNetworks.DIASPORA); page = SocialNetworksPage.build("Disapora*", organizations); FileUtils.write(new File(htmlizeDirectory, "socialNetworks-diaspora.xhtml"), page, StandardCharsets.UTF_8); organizations = federation.getOrganizations().filterBySocialNetwork(SocialNetworks.MASTODON); page = SocialNetworksPage.build("Mastodon", organizations); FileUtils.write(new File(htmlizeDirectory, "socialNetworks-mastodon.xhtml"), page, StandardCharsets.UTF_8); } } /** * Htmlize organisation. * * @param configuration * the configuration * @throws IOException * Signals that an I/O exception has occurred. * @throws StatoolInfosException * the statool infos exception */ public static void htmlizeOrganisation(final Configuration configuration) throws IOException, StatoolInfosException { CrawlCache cache = configuration.getCrawlCache(); File htmlizeInput = configuration.getHtmlizeInput(); File htmlizeDirectory = configuration.getHtmlizeDirectory(); Organization organization = Factory.loadOrganization(configuration.getBuildInput(), cache); copyStuff(htmlizeDirectory); // Manage the logo file. cache.restoreLogoTo(organization.getLogoURL(), new File(htmlizeDirectory, organization.getTechnicalName() + "-logo.jpg"), organization.getTechnicalName(), DefaultLogoGenerator.CAT); // logger.info("Htmlize about page."); String page = AboutPage.build(); FileUtils.write(new File(htmlizeDirectory, "about.xhtml"), page, StandardCharsets.UTF_8); // page = OrganizationPage.build(organization); FileUtils.write(new File(htmlizeDirectory, "index.xhtml"), page, StandardCharsets.UTF_8); for (Service service : organization.getServices()) { // Manage the logo file. cache.restoreLogoTo(service.getLogoURL(), new File(htmlizeDirectory, service.getTechnicalName() + "-logo.jpg"), service.getTechnicalName(), DefaultLogoGenerator.BIRD); // page = ServicePage.build(organization, service); FileUtils.write(new File(htmlizeDirectory, organization.getTechnicalName() + "-" + service.getTechnicalName() + ".xhtml"), page, StandardCharsets.UTF_8); } } }