diff --git a/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java b/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java index 6e8e5da..174b61d 100644 --- a/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java +++ b/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java @@ -28,6 +28,7 @@ import org.slf4j.LoggerFactory; import fr.devinsy.statoolinfos.core.StatoolInfos; import fr.devinsy.statoolinfos.util.BuildInformation; import fr.devinsy.statoolinfos.util.Files; +import fr.devinsy.statoolinfos.util.FilesUtils; import fr.devinsy.strings.StringList; /** @@ -101,11 +102,13 @@ public final class StatoolInfosCLI message.appendln("Usage:"); message.appendln(" statoolinfos [ -h | -help | --help ]"); message.appendln(" statoolinfos [ -v | -version | --version ]"); - message.appendln(" statoolinfos clear [ directory | file ]"); - message.appendln(" statoolinfos probe [ directory | file ]"); - message.appendln(" statoolinfos build [ directory | file ]"); - message.appendln(" statoolinfos crawl [ directory | file ]"); - message.appendln(" statoolinfos htmlize [ directory | file ]"); + message.appendln(" statoolinfos build build property files from conf and input"); + message.appendln(" statoolinfos clear remove property files from conf"); + message.appendln(" statoolinfos crawl crawl all file from conf and input"); + message.appendln(" statoolinfos format format in tiny way"); + message.appendln(" statoolinfos htmlize generate web pages from conf"); + message.appendln(" statoolinfos probe generate metrics files from conf"); + message.appendln(" statoolinfos tagdate update the file.datetime file"); System.out.println(message.toString()); } @@ -273,6 +276,22 @@ public final class StatoolInfosCLI } } } + else if (isMatching(args, "format", "\\s*.+\\s*")) + { + Files inputs = FilesUtils.searchEndingWith(new File(args[1]), ".properties"); + for (File input : inputs) + { + try + { + StatoolInfos.format(input); + } + catch (Exception exception) + { + logger.error("Error with [{}]: {}", input.getAbsoluteFile(), exception.getMessage()); + exception.printStackTrace(); + } + } + } else if (isMatching(args, "htmlize", "\\s*.+\\s*")) { Files inputs = convertPath(StringUtils.trim(args[1])); @@ -305,6 +324,22 @@ public final class StatoolInfosCLI } } } + else if (isMatching(args, "tagdate", "\\s*.+\\s*")) + { + Files inputs = FilesUtils.searchEndingWith(new File(args[1]), ".properties"); + for (File input : inputs) + { + try + { + StatoolInfos.tagDate(input); + } + catch (Exception exception) + { + logger.error("Error with [{}]: {}", input.getAbsoluteFile(), exception.getMessage()); + exception.printStackTrace(); + } + } + } else { System.out.println("Bad usage."); diff --git a/src/fr/devinsy/statoolinfos/core/StatoolInfos.java b/src/fr/devinsy/statoolinfos/core/StatoolInfos.java index 9a73d4c..ed9b010 100644 --- a/src/fr/devinsy/statoolinfos/core/StatoolInfos.java +++ b/src/fr/devinsy/statoolinfos/core/StatoolInfos.java @@ -20,7 +20,10 @@ package fr.devinsy.statoolinfos.core; import java.io.File; import java.io.IOException; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -28,6 +31,10 @@ import fr.devinsy.statoolinfos.build.Builder; import fr.devinsy.statoolinfos.crawl.Crawler; import fr.devinsy.statoolinfos.htmlize.Htmlizer; import fr.devinsy.statoolinfos.metrics.Prober; +import fr.devinsy.statoolinfos.properties.PathProperties; +import fr.devinsy.statoolinfos.properties.PathPropertyUtils; +import fr.devinsy.strings.StringList; +import fr.devinsy.strings.StringsUtils; /** * The Class StatoolInfos. @@ -86,6 +93,43 @@ public class StatoolInfos Crawler.crawl(configurationFile); } + /** + * Format. + * + * @param inputFile + * the input file + * @throws StatoolInfosException + * the statool infos exception + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static void format(final File inputFile) throws StatoolInfosException, IOException + { + logger.info("Format {}", inputFile); + + if (inputFile == null) + { + throw new StatoolInfosException("Input is undefined."); + } + else if (!inputFile.exists()) + { + throw new StatoolInfosException("Input does not exist."); + } + else if (!inputFile.isFile()) + { + throw new StatoolInfosException("Input is not a file."); + } + else + { + // Load input properties. + PathProperties inputs = PathPropertyUtils.load(inputFile); + + // Save input properties. + inputFile.renameTo(new File(inputFile.getParentFile(), inputFile.getName() + ".bak")); + PathPropertyUtils.save(inputFile, inputs); + } + } + /** * Htmlize. * @@ -115,4 +159,50 @@ public class StatoolInfos { Prober.probe(configurationFile); } + + /** + * Tag date. + * + * @param inputFile + * the source + * @throws StatoolInfosException + * the statool infos exception + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static void tagDate(final File inputFile) throws StatoolInfosException, IOException + { + System.out.println("TagDate " + inputFile); + + if (inputFile == null) + { + throw new StatoolInfosException("Null parameter [source]."); + } + else if (!inputFile.exists()) + { + throw new StatoolInfosException("Input does not exist."); + } + else if (!inputFile.isFile()) + { + throw new StatoolInfosException("Input is not a file."); + } + else + { + StringList lines = StringsUtils.load(inputFile); + + for (int lineIndex = 0; lineIndex < lines.size(); lineIndex++) + { + String line = lines.get(lineIndex); + + if (StringUtils.startsWith(line, "file.datetime=")) + { + lines.set(lineIndex, "file.datetime=" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("YYYY-MM-dd'T'HH:mm:ss"))); + } + } + + inputFile.renameTo(new File(inputFile.getParentFile(), inputFile.getName() + ".bak")); + StringsUtils.save(inputFile, lines); + } + } + } diff --git a/src/fr/devinsy/statoolinfos/crawl/Crawler.java b/src/fr/devinsy/statoolinfos/crawl/Crawler.java index 060e2e4..8ad199a 100644 --- a/src/fr/devinsy/statoolinfos/crawl/Crawler.java +++ b/src/fr/devinsy/statoolinfos/crawl/Crawler.java @@ -41,7 +41,7 @@ import fr.devinsy.statoolinfos.properties.PathPropertyList; import fr.devinsy.statoolinfos.properties.PathPropertyUtils; /** - * The Class StatoolInfos. + * The Class Crawler. */ public class Crawler { diff --git a/src/fr/devinsy/statoolinfos/util/CompareUtils.java b/src/fr/devinsy/statoolinfos/util/CompareUtils.java index d65a64a..5aece3d 100644 --- a/src/fr/devinsy/statoolinfos/util/CompareUtils.java +++ b/src/fr/devinsy/statoolinfos/util/CompareUtils.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. * @@ -19,6 +19,8 @@ package fr.devinsy.statoolinfos.util; import java.time.LocalDateTime; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; @@ -235,6 +237,49 @@ public class CompareUtils return result; } + /** + * Compare natural. + * + * @param alpha + * the alpha + * @param bravo + * the bravo + * @return the int + */ + public static int compareNatural(final String alpha, final String bravo) + { + int result; + + Pattern pattern = Pattern.compile("(?.*)(?\\d+)(?.*)"); + + Matcher matcherA = pattern.matcher(alpha); + Matcher matcherB = pattern.matcher(bravo); + if ((matcherA.find()) && (matcherB.find())) + { + result = StringUtils.compare(matcherA.group("left"), matcherB.group("left")); + + if (result == 0) + { + Long a = Long.valueOf(matcherA.group("digits")); + Long b = Long.valueOf(matcherB.group("digits")); + + result = compare(a, b); + + if (result == 0) + { + result = compareNatural(matcherA.group("right"), matcherB.group("right")); + } + } + } + else + { + result = StringUtils.compare(alpha, bravo); + } + + // + return result; + } + /** * Compare reverse. * diff --git a/src/fr/devinsy/statoolinfos/util/FileComparator.java b/src/fr/devinsy/statoolinfos/util/FileComparator.java new file mode 100644 index 0000000..b0e89de --- /dev/null +++ b/src/fr/devinsy/statoolinfos/util/FileComparator.java @@ -0,0 +1,124 @@ +/* + * Copyright (C) 2020-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.util; + +import java.io.File; +import java.util.Comparator; + +/** + * The Class FileComparator. + */ +public class FileComparator implements Comparator +{ + public enum Sorting + { + NAME + } + + private Sorting sorting; + + /** + * Instantiates a new organization comparator. + * + * @param sorting + * the sorting + */ + public FileComparator(final Sorting sorting) + { + this.sorting = sorting; + } + + /** + * Compare. + * + * @param alpha + * the alpha + * @param bravo + * the bravo + * @return the int + */ + @Override + public int compare(final File alpha, final File bravo) + { + int result; + + result = compare(alpha, bravo, this.sorting); + + // + return result; + } + + /** + * Compare. + * + * @param alpha + * the alpha + * @param bravo + * the bravo + * @param sorting + * the sorting + * @return the int + */ + public static int compare(final File alpha, final File bravo, final Sorting sorting) + { + int result; + + if (sorting == null) + { + result = 0; + } + else + { + switch (sorting) + { + default: + case NAME: + result = CompareUtils.compareNatural(getName(alpha), getName(bravo)); + break; + } + } + + // + return result; + } + + /** + * Gets the name. + * + * @param source + * the source + * @return the name + */ + public static String getName(final File source) + { + String result; + + if (source == null) + { + result = null; + } + else + { + result = source.getName(); + } + + // + return result; + } +} diff --git a/src/fr/devinsy/statoolinfos/util/Files.java b/src/fr/devinsy/statoolinfos/util/Files.java index 07191a3..757b1ac 100644 --- a/src/fr/devinsy/statoolinfos/util/Files.java +++ b/src/fr/devinsy/statoolinfos/util/Files.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. * @@ -20,6 +20,8 @@ package fr.devinsy.statoolinfos.util; import java.io.File; import java.util.ArrayList; +import java.util.Iterator; +import java.util.regex.Pattern; /** * The Class Files. @@ -46,4 +48,229 @@ public class Files extends ArrayList { super(initialCapacity); } + + /** + * Keep. + * + * @param regex + * the regex + * @return the files + */ + public Files keep(final String regex) + { + Files result; + + Pattern pattern = Pattern.compile(regex); + + Iterator iterator = iterator(); + while (iterator.hasNext()) + { + File file = iterator.next(); + + if (!pattern.matcher(file.getName()).matches()) + { + iterator.remove(); + } + } + + result = this; + + // + return result; + } + + /** + * Keep directories. + * + * @return the files + */ + public Files keepDirectoryType() + { + Files result; + + Iterator iterator = iterator(); + while (iterator.hasNext()) + { + File file = iterator.next(); + if (!file.isDirectory()) + { + iterator.remove(); + } + } + + result = this; + + // + return result; + } + + /** + * Keep file type. + * + * @return the files + */ + public Files keepFileType() + { + Files result; + + Iterator iterator = iterator(); + while (iterator.hasNext()) + { + File file = iterator.next(); + if (!file.isFile()) + { + iterator.remove(); + } + } + + result = this; + + // + return result; + } + + /** + * Removes the containing. + * + * @param token + * the token + * @return the files + */ + public Files removeContaining(final String token) + { + Files result; + + Iterator iterator = iterator(); + while (iterator.hasNext()) + { + File file = iterator.next(); + if (file.getName().contains(token)) + { + iterator.remove(); + } + } + + result = this; + + // + return result; + } + + /** + * Removes the file type. + * + * @return the files + */ + public Files removeFileType() + { + Files result; + + Iterator iterator = iterator(); + while (iterator.hasNext()) + { + File file = iterator.next(); + if (file.isFile()) + { + iterator.remove(); + } + } + + result = this; + + // + return result; + } + + /** + * Removes the hidden. + * + * @return the files + */ + public Files removeHidden() + { + Files result; + + Iterator iterator = iterator(); + while (iterator.hasNext()) + { + File file = iterator.next(); + if (file.getName().startsWith(".")) + { + iterator.remove(); + } + } + + result = this; + + // + return result; + } + + /** + * Sort. + * + * @param sorting + * the sorting + * @return the files + */ + public Files sort(final FileComparator.Sorting sorting) + { + Files result; + + sort(new FileComparator(sorting)); + + result = this; + + // + return result; + } + + /** + * Sort by name. + * + * @return the files + */ + public Files sortByName() + { + Files result; + + result = sort(FileComparator.Sorting.NAME); + + // + return result; + } + + public Files sortByPathname() + { + Files result; + + result = sort(FileComparator.Sorting.NAME); + + // + return result; + } + + /** + * Of. + * + * @param source + * the source + * @return the files + */ + public static Files of(final File source) + { + Files result; + + result = new Files(); + if (source != null) + { + for (File file : source.listFiles()) + { + result.add(file); + } + } + + // + return result; + } } diff --git a/src/fr/devinsy/statoolinfos/util/FilesUtils.java b/src/fr/devinsy/statoolinfos/util/FilesUtils.java new file mode 100644 index 0000000..56fc5b8 --- /dev/null +++ b/src/fr/devinsy/statoolinfos/util/FilesUtils.java @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2020-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.util; + +import java.io.File; +import java.util.regex.Pattern; + +import org.apache.commons.lang3.StringUtils; + +/** + * The Class FilesUtils. + */ +public class FilesUtils +{ + + /** + * Instantiates a new files utils. + */ + private FilesUtils() + { + } + + /** + * List recursively. + * + * @param source + * the source + * @return the files + */ + public static Files listRecursively(final File source) + { + Files result; + + result = new Files(); + if ((source != null) && (source.exists())) + { + if (source.isFile()) + { + result.add(source); + } + else + { + for (File file : source.listFiles()) + { + if (file.isDirectory()) + { + result.addAll(listRecursively(file)); + } + else + { + result.add(file); + } + } + } + } + + // + return result; + } + + /** + * Search recursively. + * + * @param source + * the source + * @param regex + * the regex + * @return the files + */ + public static Files search(final File source, final String regex) + { + Files result; + + result = new Files(); + + Pattern pattern = Pattern.compile(regex); + + Files full = listRecursively(source); + for (File file : full) + { + if (pattern.matcher(file.getName()).matches()) + { + result.add(file); + } + } + + // + return result; + } + + /** + * List recursively. + * + * @param source + * the source + * @param extensions + * the extensions + * @return the files + */ + public static Files searchEndingWith(final File source, final String... extensions) + { + Files result; + + result = new Files(); + + Files full = listRecursively(source); + for (File file : full) + { + if (StringUtils.endsWithAny(file.getName(), extensions)) + { + result.add(file); + } + } + + // + return result; + } +}