/* * 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.slf4j.Logger; import org.slf4j.LoggerFactory; import fr.devinsy.statoolinfos.core.Configuration; import fr.devinsy.statoolinfos.core.CrawlCache; 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; /** * The Class Htmlizer. */ public class Htmlizer { private static Logger logger = LoggerFactory.getLogger(Htmlizer.class); /** * Instantiates a new htmlizer. */ private Htmlizer() { } /** * 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/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")); StatoolInfosUtils.copyRessource("/fr/devinsy/statoolinfos/htmlize/stuff/about.xhtml", targetDirectory); } } /** * Htmlize federation. * * @param federation * the federation * @param cache * the cache * @throws IOException * @throws StatoolInfosException */ 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); cache.restoreLogoTo(federation.getLogoURL(), new File(htmlizeDirectory, federation.getTechnicalName() + "-logo.jpg")); // String page = FederationPage.build(federation); FileUtils.write(new File(htmlizeDirectory, "index.xhtml"), page, StandardCharsets.UTF_8); for (Organization organization : federation.getOrganizations()) { page = OrganizationPage.build(organization); FileUtils.write(new File(htmlizeDirectory, organization.getTechnicalName() + ".xhtml"), page, StandardCharsets.UTF_8); for (Service service : organization.getServices()) { page = ServicePage.build(service); FileUtils.write(new File(htmlizeDirectory, organization.getTechnicalName() + "-" + service.getTechnicalName() + ".xhtml"), page, StandardCharsets.UTF_8); } } // Download federation stuff (favicon, logo…). // Build the federation page. // For each organization // Download organization stuff (favicon, logo…). // Build organization page. // for each service // Download service stuff (favicon, logo…). // Build service page. } /** * Htmlize organization. * * @param federation * the federation * @param targetDirectory * the target directory * @throws IOException * @throws StatoolInfosException */ public static void htmlizeOrganisation(final Configuration configuration) throws IOException, StatoolInfosException { CrawlCache cache = configuration.getCrawlCache(); Organization organization = Factory.loadOrganization(configuration.getBuildInput(), cache); File targetDirectory = new File(organization.get("conf.htmlize.directory")); logger.info("Htmlize target directory: {}", targetDirectory.getAbsoluteFile()); if (!targetDirectory.exists()) { throw new IllegalArgumentException("Htmlize target directory is missing."); } else if (!targetDirectory.isDirectory()) { throw new IllegalArgumentException("Htmlize target directory is not a directory."); } else { copyStuff(targetDirectory); // String page = OrganizationPage.build(organization); FileUtils.write(new File(targetDirectory, "index.xhtml"), page, StandardCharsets.UTF_8); for (Service service : organization.getServices()) { page = ServicePage.build(service); FileUtils.write(new File(targetDirectory, organization.getTechnicalName() + "-" + service.getTechnicalName() + ".xhtml"), page, StandardCharsets.UTF_8); } // Download federation stuff (favicon, logo…). // Build the federation page. // For each organization // Download organization stuff (favicon, logo…). // Build organization page. // for each service // Download service stuff (favicon, logo…). // Build service page. } } }