/* * 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.Configuration; import fr.devinsy.statoolinfos.core.Factory; import fr.devinsy.statoolinfos.core.Federation; import fr.devinsy.statoolinfos.core.Organization; import fr.devinsy.statoolinfos.core.Service; import fr.devinsy.statoolinfos.core.StatoolInfosException; import fr.devinsy.statoolinfos.core.StatoolInfosUtils; import fr.devinsy.statoolinfos.crawl.CrawlCache; import fr.devinsy.statoolinfos.stats.StatAgent; import fr.devinsy.statoolinfos.stats.properties.PropertyStats; import fr.devinsy.statoolinfos.stats.propertyfiles.PropertiesFileStats; /** * 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", ".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()) { StatoolInfosUtils.copyRessource("/fr/devinsy/statoolinfos/htmlize/stuff/index.html", targetDirectory); StatoolInfosUtils.copyRessource("/fr/devinsy/statoolinfos/htmlize/stuff/statoolinfos.css", targetDirectory); StatoolInfosUtils.copyRessource("/fr/devinsy/statoolinfos/htmlize/stuff/Chart.bundle.min.js", targetDirectory); StatoolInfosUtils.copyRessource("/fr/devinsy/statoolinfos/htmlize/stuff/sorttable.js", targetDirectory); StatoolInfosUtils.copyRessource("/fr/devinsy/statoolinfos/htmlize/stuff/statoolinfos-logo.jpg", targetDirectory); StatoolInfosUtils.copyRessource("/fr/devinsy/statoolinfos/htmlize/stuff/statoolinfos-logo.ico", targetDirectory); StatoolInfosUtils.copyRessource("/fr/devinsy/statoolinfos/htmlize/stuff/statoolinfos-logo-name.jpg", targetDirectory); StatoolInfosUtils.copyRessource("/fr/devinsy/statoolinfos/htmlize/stuff/statoolinfos-logo.jpg", new File(targetDirectory, "logo.jpg")); StatoolInfosUtils.copyRessource("/fr/devinsy/statoolinfos/htmlize/stuff/statoolinfos-logo.ico", new File(targetDirectory, "favicon.ico")); } } /** * 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); copyStuff(htmlizeDirectory); // Manage the logo file. logger.info("Htmlize federation logo."); cache.restoreLogoTo(federation.getLogoURL(), new File(htmlizeDirectory, federation.getTechnicalName() + "-logo.png"), federation.getTechnicalName()); 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()); 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()); 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); PropertyStats federationStats = StatAgent.statFederationProperties(federation); PropertyStats organizationsStats = StatAgent.statOrganizationsProperties(federation.getOrganizations()); PropertyStats servicesStats = StatAgent.statServicesProperties(federation.getAllServices()); page = PropertyStatsPage.build(stats, federationStats, organizationsStats, servicesStats); FileUtils.write(new File(htmlizeDirectory, "propertyStats.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()); // 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()); // page = ServicePage.build(organization, service); FileUtils.write(new File(htmlizeDirectory, organization.getTechnicalName() + "-" + service.getTechnicalName() + ".xhtml"), page, StandardCharsets.UTF_8); } } }