From 9e53a184e6ab6e119a73cb06af019d01b3358869 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Sat, 23 Jan 2021 20:06:42 +0100 Subject: [PATCH] Added CSV export. --- .../devinsy/statoolinfos/core/Categories.java | 29 ++ .../statoolinfos/htmlize/ExportsPage.java | 193 +++----------- .../statoolinfos/htmlize/Htmlizer.java | 10 +- .../statoolinfos/htmlize/exports.xhtml | 8 +- src/fr/devinsy/statoolinfos/io/CSVFile.java | 250 ++++++++++++++++++ src/fr/devinsy/statoolinfos/io/JSONFile.java | 192 ++++++++++++++ .../properties/PathProperties.java | 4 +- 7 files changed, 523 insertions(+), 163 deletions(-) create mode 100644 src/fr/devinsy/statoolinfos/io/CSVFile.java create mode 100644 src/fr/devinsy/statoolinfos/io/JSONFile.java diff --git a/src/fr/devinsy/statoolinfos/core/Categories.java b/src/fr/devinsy/statoolinfos/core/Categories.java index b09cd3f..93b61c7 100644 --- a/src/fr/devinsy/statoolinfos/core/Categories.java +++ b/src/fr/devinsy/statoolinfos/core/Categories.java @@ -22,6 +22,8 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; +import fr.devinsy.strings.StringList; + /** * The Class Categories. */ @@ -149,4 +151,31 @@ public class Categories extends ArrayList // return result; } + + /** + * To string. + * + * @return the string + */ + @Override + public String toString() + { + String result; + + StringList buffer = new StringList(); + + for (Category category : this) + { + buffer.append(category.getName()).append(","); + } + if (!buffer.isEmpty()) + { + buffer.removeLast(); + } + + result = buffer.toString(); + + // + return result; + } } diff --git a/src/fr/devinsy/statoolinfos/htmlize/ExportsPage.java b/src/fr/devinsy/statoolinfos/htmlize/ExportsPage.java index a265607..bf0e0c2 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/ExportsPage.java +++ b/src/fr/devinsy/statoolinfos/htmlize/ExportsPage.java @@ -19,22 +19,20 @@ package fr.devinsy.statoolinfos.htmlize; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import org.apache.commons.io.FileUtils; -import org.apache.commons.text.StringEscapeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import fr.devinsy.statoolinfos.HtmlizerContext; 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.properties.PathPropertyUtils; -import fr.devinsy.strings.StringList; -import fr.devinsy.strings.StringsUtils; +import fr.devinsy.statoolinfos.io.CSVFile; +import fr.devinsy.statoolinfos.io.JSONFile; import fr.devinsy.xidyn.XidynException; import fr.devinsy.xidyn.presenters.PresenterUtils; @@ -53,162 +51,53 @@ public class ExportsPage * @throws IOException * Signals that an I/O exception has occurred. */ - public static void build() throws StatoolInfosException, IOException - { - File htmlizeDirectory = HtmlizerContext.instance().getHtmlizeDirectory(); - - // - logger.info("EXPORTS JSON."); - ExportsPage.buildFederationExport(); - ExportsPage.buildOrganizationsExport(); - ExportsPage.buildServicesExport(); - - // - String page = htmlize(); - FileUtils.write(new File(htmlizeDirectory, "exports.xhtml"), page, StandardCharsets.UTF_8); - } - - /** - * Builds the federation export. - * - * @throws StatoolInfosException - * the statool infos exception - * @throws IOException - * Signals that an I/O exception has occurred. - */ - public static void buildFederationExport() throws StatoolInfosException, IOException + public static void build() { Federation federation = HtmlizerContext.instance().getFederation(); File htmlizeDirectory = HtmlizerContext.instance().getHtmlizeDirectory(); - StringList lines = new StringList(); - - lines.appendln("{ \"federation\" : "); - - lines.addAll(PathPropertyUtils.toJSON(federation)); - - lines.removeLast(); - lines.appendln(","); - lines.appendln("\"organizations\" : ["); - - for (Organization organization : federation.getOrganizations()) + // + try { - lines.addAll(PathPropertyUtils.toJSON(organization)); - lines.removeLast(); - - lines.appendln(","); - lines.append("\"services\" : ["); - - for (Service service : organization.getServices()) - { - lines.addAll(PathPropertyUtils.toJSON(service)); - lines.appendln(","); - } - if (!organization.getServices().isEmpty()) - { - lines.removeLast(2); - } - lines.appendln(); - lines.appendln("]"); - lines.append("}"); - lines.appendln(","); + logger.info("EXPORTS CSV."); + CSVFile.save(new File(htmlizeDirectory, "organizations.csv"), federation.getOrganizations()); + CSVFile.save(new File(htmlizeDirectory, "services.csv"), federation.getAllServices()); } - if (!federation.getOrganizations().isEmpty()) + catch (UnsupportedEncodingException | FileNotFoundException exception) { - lines.removeLast(2); - lines.appendln(); + logger.error("Error during CSV export: " + exception.getMessage(), exception); } - lines.appendln("]"); - - lines.appendln("}"); - lines.appendln("}"); - - logger.info("Htmlize federation JSON Export pages."); - StringsUtils.writeToFile(new File(htmlizeDirectory, "federation.json"), lines); - } - - /** - * Builds the organizations export. - * - * @throws StatoolInfosException - * the statool infos exception - * @throws IOException - * Signals that an I/O exception has occurred. - */ - public static void buildOrganizationsExport() throws StatoolInfosException, IOException - { - Federation federation = HtmlizerContext.instance().getFederation(); - File htmlizeDirectory = HtmlizerContext.instance().getHtmlizeDirectory(); - - StringList lines = new StringList(); - - lines.appendln("{ \"organizations\" : ["); - - for (Organization organization : federation.getOrganizations()) - { - lines.addAll(PathPropertyUtils.toJSON(organization)); - lines.append(","); - } - if (!federation.getOrganizations().isEmpty()) - { - lines.removeLast(); - } - - lines.appendln("] }"); - - logger.info("Htmlize organizations JSON Export pages."); - StringsUtils.writeToFile(new File(htmlizeDirectory, "organizations.json"), lines); - } - - /** - * Builds the services export. - * - * @throws StatoolInfosException - * the statool infos exception - * @throws IOException - * Signals that an I/O exception has occurred. - */ - public static void buildServicesExport() throws StatoolInfosException, IOException - { - Federation federation = HtmlizerContext.instance().getFederation(); - File htmlizeDirectory = HtmlizerContext.instance().getHtmlizeDirectory(); - - StringList lines = new StringList(); - - lines.appendln("{ \"services\" : ["); - - for (Service service : federation.getAllServices()) - { - lines.addAll(PathPropertyUtils.toJSON(service)); - lines.append(","); - } - if (!federation.getAllServices().isEmpty()) - { - lines.removeLast(); - } - - lines.appendln("] }"); - - logger.info("Htmlize services JSON Export pages."); - StringsUtils.writeToFile(new File(htmlizeDirectory, "services.json"), lines); - } - - /** - * Escape. - * - * @param source - * the source - * @return the string - */ - public static String escapeJSON(final String source) - { - String result; - - result = StringEscapeUtils.escapeJson(source); - // - return result; + try + { + logger.info("EXPORTS JSON."); + JSONFile.save(new File(htmlizeDirectory, "federation.json"), federation); + JSONFile.save(new File(htmlizeDirectory, "organizations.json"), federation.getOrganizations()); + JSONFile.save(new File(htmlizeDirectory, "services.json"), federation.getAllServices()); + } + catch (UnsupportedEncodingException | FileNotFoundException exception) + { + logger.error("Error during JSON export: " + exception.getMessage(), exception); + } + + // + logger.info("EXPORTS ODS."); + // ODSFile.save(new File(htmlizeDirectory, "organizations.ods"), + // federation.getOrganizations()); + // ODSFile.save(new File(htmlizeDirectory, "services.ods"), + // federation.getAllServices()); + + // + try + { + String page = htmlize(); + FileUtils.write(new File(htmlizeDirectory, "exports.xhtml"), page, StandardCharsets.UTF_8); + } + catch (StatoolInfosException | IOException exception) + { + logger.error("Error building export page: " + exception.getMessage(), exception); + } } /** diff --git a/src/fr/devinsy/statoolinfos/htmlize/Htmlizer.java b/src/fr/devinsy/statoolinfos/htmlize/Htmlizer.java index 8756abe..9882b37 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/Htmlizer.java +++ b/src/fr/devinsy/statoolinfos/htmlize/Htmlizer.java @@ -180,22 +180,22 @@ public class Htmlizer copyCategoriesStuff(HtmlizerContext.instance().getConfiguration().get("conf.htmlize.categories.icons"), htmlizeDirectory); AboutPage.build(); + CategoriesPage.build(); + CategoryPage.buildAll(); EditoPage.build(); + ExportsPage.build(); FederationPage.build(); FederationStatsPage.build(); OrganizationPage.buildAll(); OrganizationStatsPage.buildAll(); - ServicePage.buildAll(); - ServicesPage.build(); PropertyFileCheckPage.buildAll(); PropertiesFilesPage.build(); PropertyStatsPage.buildAll(); - CategoriesPage.build(); - CategoryPage.buildAll(); + ServicePage.buildAll(); + ServicesPage.build(); SoftwaresPage.build(); SoftwarePage.buildAll(); SocialNetworksPage.buildAll(); - ExportsPage.build(); } /** diff --git a/src/fr/devinsy/statoolinfos/htmlize/exports.xhtml b/src/fr/devinsy/statoolinfos/htmlize/exports.xhtml index a847c54..fec50ba 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/exports.xhtml +++ b/src/fr/devinsy/statoolinfos/htmlize/exports.xhtml @@ -24,16 +24,16 @@
Les organisations :
-
+
-
+

Les services :
-
+
-
+
diff --git a/src/fr/devinsy/statoolinfos/io/CSVFile.java b/src/fr/devinsy/statoolinfos/io/CSVFile.java new file mode 100644 index 0000000..9e0ba71 --- /dev/null +++ b/src/fr/devinsy/statoolinfos/io/CSVFile.java @@ -0,0 +1,250 @@ +/* + * Copyright (C) 2021 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.io; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.text.StringEscapeUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import fr.devinsy.statoolinfos.HtmlizerContext; +import fr.devinsy.statoolinfos.core.Categories; +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.properties.PathProperty; +import fr.devinsy.strings.StringList; +import fr.devinsy.strings.StringSet; + +/** + * The Class CSVFile. + */ +public class CSVFile +{ + private static final Logger logger = LoggerFactory.getLogger(CSVFile.class); + + public static final String DEFAULT_CHARSET_NAME = "UTF-8"; + public static final int MAX_LINE_SIZE = 4096; + public static final String SEPARATOR = ";"; + + /** + * Escape. + * + * @param string + * the string + * @return the string + */ + public static String escape(final String string) + { + String result; + + result = StringEscapeUtils.escapeCsv(string); + + // + return result; + } + + /** + * Save. + * + * @param file + * the file + * @param source + * the source + * @throws UnsupportedEncodingException + * the unsupported encoding exception + * @throws FileNotFoundException + * the file not found exception + */ + public static void save(final File file, final Organizations source) throws UnsupportedEncodingException, FileNotFoundException + { + PrintWriter out = null; + try + { + out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); + + write(out, source); + } + finally + { + IOUtils.closeQuietly(out); + } + } + + /** + * Save. + * + * @param file + * the file + * @param source + * the source + * @throws UnsupportedEncodingException + * the unsupported encoding exception + * @throws FileNotFoundException + * the file not found exception + */ + public static void save(final File file, final Services source) throws UnsupportedEncodingException, FileNotFoundException + { + PrintWriter out = null; + try + { + out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); + + write(out, source); + } + finally + { + IOUtils.closeQuietly(out); + } + } + + /** + * Write. + * + * @param out + * the out + * @param source + * the source + */ + public static void write(final PrintWriter out, final Organizations organizations) + { + // Build label list. + StringList labels = new StringList(100); + StringSet dejavu = new StringSet(100); + StringList prefixes = new StringList("file", "organization", "crawl"); + for (String prefix : prefixes) + { + for (Organization organization : organizations) + { + for (PathProperty property : organization.getByPrefix(prefix)) + { + if (!dejavu.contains(property.getPath())) + { + labels.add(property.getPath()); + dejavu.add(property.getPath()); + } + } + } + } + dejavu.clear(); + + // Write label line. + StringList line = new StringList(); + for (String label : labels) + { + line.append(escape(label)).append(SEPARATOR); + } + line.removeLast(); + out.println(line.toString()); + + // Write organization lines. + for (Organization organization : organizations) + { + line = new StringList(100); + for (String label : labels) + { + String value = organization.get(label); + line.append(escape(value)).append(SEPARATOR); + } + line.removeLast(); + out.println(line.toString()); + } + } + + /** + * Write. + * + * @param out + * the out + * @param source + * the source + */ + public static void write(final PrintWriter out, final Services services) + { + // Build label list. + StringList labels = new StringList(100); + StringSet dejavu = new StringSet(100); + StringList prefixes = new StringList("file", "service", "host", "software", "crawl"); + for (String prefix : prefixes) + { + for (Service service : services) + { + for (PathProperty property : service.getByPrefix(prefix)) + { + if (!dejavu.contains(property.getPath())) + { + labels.add(property.getPath()); + dejavu.add(property.getPath()); + } + } + } + } + dejavu.clear(); + int index = labels.indexOf("service.name"); + labels.add(index, "organization.name"); + index = labels.indexOf("software.name"); + labels.add(index, "software.categories"); + + // Write label line. + StringList line = new StringList(); + for (String label : labels) + { + line.append(escape(label)).append(SEPARATOR); + } + line.removeLast(); + out.println(line.toString()); + + Categories categories = HtmlizerContext.instance().getCategories(); + + // Write service lines. + for (Service service : services) + { + line = new StringList(100); + for (String label : labels) + { + String value; + if (StringUtils.equals(label, "organization.name")) + { + value = service.getOrganization().getName(); + } + else if (StringUtils.equals(label, "software.categories")) + { + value = categories.findBySoftware(service.getSoftwareName()).toString(); + } + else + { + value = service.get(label); + } + + line.append(escape(value)).append(SEPARATOR); + } + line.removeLast(); + out.println(line.toString()); + } + } +} diff --git a/src/fr/devinsy/statoolinfos/io/JSONFile.java b/src/fr/devinsy/statoolinfos/io/JSONFile.java new file mode 100644 index 0000000..8bbdc0d --- /dev/null +++ b/src/fr/devinsy/statoolinfos/io/JSONFile.java @@ -0,0 +1,192 @@ +/* + * Copyright (C) 2021 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.io; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.UnsupportedEncodingException; + +import org.apache.commons.text.StringEscapeUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +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.properties.PathPropertyUtils; +import fr.devinsy.strings.StringList; +import fr.devinsy.strings.StringsUtils; + +/** + * The Class JSONFile. + */ +public class JSONFile +{ + private static final Logger logger = LoggerFactory.getLogger(JSONFile.class); + + public static final String DEFAULT_CHARSET_NAME = "UTF-8"; + + /** + * Escape. + * + * @param source + * the source + * @return the string + */ + public static String escapeJSON(final String source) + { + String result; + + result = StringEscapeUtils.escapeJson(source); + + // + return result; + } + + /** + * Save. + * + * @param file + * the file + * @param federation + * the federation + * @throws UnsupportedEncodingException + * the unsupported encoding exception + * @throws FileNotFoundException + * the file not found exception + */ + public static void save(final File file, final Federation federation) throws UnsupportedEncodingException, FileNotFoundException + { + StringList lines = new StringList(); + + lines.appendln("{ \"federation\" : "); + + lines.addAll(PathPropertyUtils.toJSON(federation)); + + lines.removeLast(); + lines.appendln(","); + lines.appendln("\"organizations\" : ["); + + for (Organization organization : federation.getOrganizations()) + { + lines.addAll(PathPropertyUtils.toJSON(organization)); + lines.removeLast(); + + lines.appendln(","); + lines.append("\"services\" : ["); + + for (Service service : organization.getServices()) + { + lines.addAll(PathPropertyUtils.toJSON(service)); + lines.appendln(","); + } + if (!organization.getServices().isEmpty()) + { + lines.removeLast(2); + } + lines.appendln(); + lines.appendln("]"); + lines.append("}"); + lines.appendln(","); + } + if (!federation.getOrganizations().isEmpty()) + { + lines.removeLast(2); + lines.appendln(); + } + + lines.appendln("]"); + + lines.appendln("}"); + lines.appendln("}"); + + logger.info("Htmlize federation JSON Export pages."); + StringsUtils.writeToFile(file, lines); + } + + /** + * Save. + * + * @param file + * the file + * @param organizations + * the organizations + * @throws UnsupportedEncodingException + * the unsupported encoding exception + * @throws FileNotFoundException + * the file not found exception + */ + public static void save(final File file, final Organizations organizations) throws UnsupportedEncodingException, FileNotFoundException + { + StringList lines = new StringList(); + + lines.appendln("{ \"organizations\" : ["); + + for (Organization organization : organizations) + { + lines.addAll(PathPropertyUtils.toJSON(organization)); + lines.append(","); + } + if (!organizations.isEmpty()) + { + lines.removeLast(); + } + + lines.appendln("] }"); + + logger.info("Htmlize organizations JSON Export pages."); + StringsUtils.writeToFile(file, lines); + } + + /** + * Save. + * + * @param file + * the file + * @param services + * the services + * @throws UnsupportedEncodingException + * the unsupported encoding exception + * @throws FileNotFoundException + * the file not found exception + */ + public static void save(final File file, final Services services) throws UnsupportedEncodingException, FileNotFoundException + { + StringList lines = new StringList(); + + lines.appendln("{ \"services\" : ["); + + for (Service service : services) + { + lines.addAll(PathPropertyUtils.toJSON(service)); + lines.append(","); + } + if (!services.isEmpty()) + { + lines.removeLast(); + } + + lines.appendln("] }"); + + logger.info("Htmlize services JSON Export pages."); + StringsUtils.writeToFile(file, lines); + } +} diff --git a/src/fr/devinsy/statoolinfos/properties/PathProperties.java b/src/fr/devinsy/statoolinfos/properties/PathProperties.java index fdb7ba7..5dd801c 100644 --- a/src/fr/devinsy/statoolinfos/properties/PathProperties.java +++ b/src/fr/devinsy/statoolinfos/properties/PathProperties.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Christian Pierre MOMON + * Copyright (C) 2020-2021 Christian Pierre MOMON * * This file is part of StatoolInfos, simple service statistics tool. * @@ -35,7 +35,7 @@ public interface PathProperties extends Iterable boolean add(PathProperty property); /** - * Gets the. + * h Gets the. * * @param path * the path