diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index bfbfdc3..29d1414 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -23,3 +23,6 @@ jobs: build: name: Verify uses: apache/maven-gh-actions-shared/.github/workflows/maven-verify.yml@v4 + with: + ff-goal: 'install' # site use project version for reporting + verify-goal: 'install' # should be the same as for first build diff --git a/pom.xml b/pom.xml index 84f9722..69522eb 100644 --- a/pom.xml +++ b/pom.xml @@ -84,19 +84,17 @@ under the License. - + - org.apache.maven - maven-artifact - ${mavenVersion} - provided + commons-io + commons-io + 2.17.0 - org.apache.maven - maven-core - ${mavenVersion} - provided + org.codehaus.plexus + plexus-archiver + 4.10.0 @@ -108,15 +106,44 @@ under the License. ${mavenVersion} provided + + org.apache.maven + maven-core + ${mavenVersion} + provided + + + org.apache.maven + maven-model + ${mavenVersion} + provided + org.apache.maven.plugin-tools maven-plugin-annotations provided + + + org.apache.maven.doxia + doxia-sink-api + 2.0.0 + + + org.apache.maven.reporting + maven-reporting-api + 4.0.0 + org.apache.maven.reporting maven-reporting-impl - 3.2.0 + 4.0.0 + + + + org.codehaus.plexus + plexus-utils + 4.0.2 diff --git a/src/main/java/org/codehaus/mojo/l10n/L10NStatusReport.java b/src/main/java/org/codehaus/mojo/l10n/L10NStatusReport.java index 2799860..175f225 100644 --- a/src/main/java/org/codehaus/mojo/l10n/L10NStatusReport.java +++ b/src/main/java/org/codehaus/mojo/l10n/L10NStatusReport.java @@ -21,14 +21,13 @@ import java.io.BufferedInputStream; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; @@ -47,7 +46,6 @@ import org.apache.maven.reporting.AbstractMavenReportRenderer; import org.apache.maven.reporting.MavenReportException; import org.codehaus.plexus.util.DirectoryScanner; -import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.StringUtils; /** @@ -60,13 +58,6 @@ */ @Mojo(name = "report") public class L10NStatusReport extends AbstractMavenReport { - /** - * The projects in the reactor for aggregation report. - * - * @since 1.0.0 - */ - @Parameter(defaultValue = "${reactorProjects}", readonly = true) - protected List reactorProjects; /** * A list of locale strings that are to be watched for l10n status. @@ -104,6 +95,7 @@ public class L10NStatusReport extends AbstractMavenReport { private static final String[] EMPTY_STRING_ARRAY = {}; + @Override public boolean canGenerateReport() { if (aggregate && !project.isExecutionRoot()) { return false; @@ -114,14 +106,11 @@ public boolean canGenerateReport() { /** * Collects resource definitions from all projects in reactor. - * - * @return */ - protected Map constructResourceDirs() { - Map sourceDirs = new HashMap(); + protected Map> constructResourceDirs() { + Map> sourceDirs = new HashMap<>(); if (aggregate) { - for (Iterator i = reactorProjects.iterator(); i.hasNext(); ) { - MavenProject prj = (MavenProject) i.next(); + for (MavenProject prj : reactorProjects) { if (prj.getResources() != null && !prj.getResources().isEmpty()) { sourceDirs.put(prj, prj.getResources()); } @@ -137,15 +126,14 @@ protected Map constructResourceDirs() { /** * @see org.apache.maven.reporting.AbstractMavenReport#executeReport(java.util.Locale) */ + @Override protected void executeReport(Locale locale) throws MavenReportException { - Set included = new TreeSet(new WrapperComparator()); - Map res = constructResourceDirs(); - for (Iterator it = res.keySet().iterator(); it.hasNext(); ) { - MavenProject prj = (MavenProject) it.next(); - List lst = (List) res.get(prj); - for (Iterator i = lst.iterator(); i.hasNext(); ) { - Resource resource = (Resource) i.next(); - + Set included = new TreeSet<>(new WrapperComparator()); + Map> res = constructResourceDirs(); + for (Map.Entry> entry : res.entrySet()) { + MavenProject prj = entry.getKey(); + List lst = entry.getValue(); + for (Resource resource : lst) { File resourceDirectory = new File(resource.getDirectory()); if (!resourceDirectory.exists()) { @@ -156,7 +144,7 @@ protected void executeReport(Locale locale) throws MavenReportException { DirectoryScanner scanner = new DirectoryScanner(); scanner.setBasedir(resource.getDirectory()); - List allIncludes = new ArrayList(); + List allIncludes = new ArrayList<>(); if (resource.getIncludes() != null && !resource.getIncludes().isEmpty()) { allIncludes.addAll(resource.getIncludes()); } @@ -167,24 +155,23 @@ protected void executeReport(Locale locale) throws MavenReportException { if (allIncludes.isEmpty()) { scanner.setIncludes(DEFAULT_INCLUDES); } else { - scanner.setIncludes((String[]) allIncludes.toArray(EMPTY_STRING_ARRAY)); + scanner.setIncludes(allIncludes.toArray(EMPTY_STRING_ARRAY)); } - List allExcludes = new ArrayList(); + List allExcludes = new ArrayList<>(); if (resource.getExcludes() != null && !resource.getExcludes().isEmpty()) { allExcludes.addAll(resource.getExcludes()); } else if (excludes != null && !excludes.isEmpty()) { allExcludes.addAll(excludes); } - scanner.setExcludes((String[]) allExcludes.toArray(EMPTY_STRING_ARRAY)); + scanner.setExcludes(allExcludes.toArray(EMPTY_STRING_ARRAY)); scanner.addDefaultExcludes(); scanner.scan(); - List includedFiles = Arrays.asList(scanner.getIncludedFiles()); - for (Iterator j = includedFiles.iterator(); j.hasNext(); ) { - String name = (String) j.next(); + String[] includedFiles = scanner.getIncludedFiles(); + for (String name : includedFiles) { File source = new File(resource.getDirectory(), name); included.add(new Wrapper(name, source, prj)); } @@ -199,6 +186,7 @@ protected void executeReport(Locale locale) throws MavenReportException { /** * @see org.apache.maven.reporting.MavenReport#getDescription(java.util.Locale) */ + @Override public String getDescription(Locale locale) { return getBundle(locale).getString("report.l10n.description"); } @@ -206,6 +194,7 @@ public String getDescription(Locale locale) { /** * @see org.apache.maven.reporting.MavenReport#getName(java.util.Locale) */ + @Override public String getName(Locale locale) { return getBundle(locale).getString("report.l10n.name"); } @@ -213,6 +202,7 @@ public String getName(Locale locale) { /** * @see org.apache.maven.reporting.MavenReport#getOutputName() */ + @Override public String getOutputName() { return "l10n-status"; } @@ -234,11 +224,11 @@ class L10NStatusRenderer extends AbstractMavenReportRenderer { */ private final Locale rendererLocale; - private Set files; + private final Set files; - private Pattern localedPattern = Pattern.compile(".*_[a-zA-Z]{2}[_]?[a-zA-Z]{0,2}?\\.properties"); + private final Pattern localedPattern = Pattern.compile(".*_[a-zA-Z]{2}[_]?[a-zA-Z]{0,2}?\\.properties"); - public L10NStatusRenderer(Sink sink, ResourceBundle bundle, Set files, Locale rendererLocale) { + public L10NStatusRenderer(Sink sink, ResourceBundle bundle, Set files, Locale rendererLocale) { super(sink); this.bundle = bundle; @@ -273,14 +263,12 @@ public void renderBody() { String additionalKeysLabel = bundle.getString("report.l10n.additional"); String nontranslatedKeysLabel = bundle.getString("report.l10n.nontranslated"); String[] headers = new String[locales != null ? locales.size() + 2 : 2]; - Map localeDisplayNames = new HashMap(); + Map localeDisplayNames = new HashMap<>(); headers[0] = pathColumnName; headers[1] = defaultLocaleColumnName; if (locales != null) { - Iterator it = locales.iterator(); int ind = 2; - while (it.hasNext()) { - final String localeCode = (String) it.next(); + for (String localeCode : locales) { headers[ind] = localeCode; ind = ind + 1; @@ -296,11 +284,9 @@ public void renderBody() { tableHeader(headers); int[] count = new int[locales != null ? locales.size() + 1 : 1]; Arrays.fill(count, 0); - Iterator it = files.iterator(); MavenProject lastPrj = null; - Set usedFiles = new TreeSet(new WrapperComparator()); - while (it.hasNext()) { - Wrapper wr = (Wrapper) it.next(); + Set usedFiles = new TreeSet<>(new WrapperComparator()); + for (Wrapper wr : files) { if (reactorProjects.size() > 1 && (lastPrj == null || lastPrj != wr.getProject())) { lastPrj = wr.getProject(); sink.tableRow(); @@ -318,75 +304,77 @@ public void renderBody() { sink.tableRow(); tableCell(wr.getPath()); Properties props = new Properties(); - BufferedInputStream in = null; - try { - in = new BufferedInputStream(new FileInputStream(wr.getFile())); + try (BufferedInputStream in = new BufferedInputStream( + Files.newInputStream(wr.getFile().toPath()))) { props.load(in); wr.getProperties().put(Wrapper.DEFAULT_LOCALE, props); tableCell("" + props.size(), true); count[0] = count[0] + props.size(); if (locales != null) { - Iterator it2 = locales.iterator(); int i = 1; - while (it2.hasNext()) { - String loc = (String) it2.next(); + for (String loc : locales) { String nm = wr.getFile().getName(); String fn = nm.substring(0, nm.length() - ".properties".length()); File locFile = new File(wr.getFile().getParentFile(), fn + "_" + loc + ".properties"); if (locFile.exists()) { - BufferedInputStream in2 = null; Properties props2 = new Properties(); - try { - in2 = new BufferedInputStream(new FileInputStream(locFile)); + try (BufferedInputStream in2 = + new BufferedInputStream(Files.newInputStream(locFile.toPath()))) { props2.load(in2); wr.getProperties().put(loc, props2); - Set missing = new HashSet(props.keySet()); - missing.removeAll(props2.keySet()); - Set additional = new HashSet(props2.keySet()); - additional.removeAll(props.keySet()); - Set nonTranslated = new HashSet(); - Iterator itx = props.keySet().iterator(); - while (itx.hasNext()) { - String k = (String) itx.next(); + Set missing = new HashSet<>(props.stringPropertyNames()); + missing.removeAll(props2.stringPropertyNames()); + Set additional = new HashSet<>(props2.stringPropertyNames()); + additional.removeAll(props.stringPropertyNames()); + Set nonTranslated = new HashSet<>(); + for (String k : props.stringPropertyNames()) { String val1 = props.getProperty(k); String val2 = props2.getProperty(k); - if (val2 != null && val1.equals(val2)) { + if (val1.equals(val2)) { nonTranslated.add(k); } } count[i] = count[i] + (props.size() - missing.size() - nonTranslated.size()); - StringBuffer statusRows = new StringBuffer(); - if (missing.size() != 0) { - statusRows.append("" + missingKeysLabel + "" - + missing.size() + ""); + StringBuilder statusRows = new StringBuilder(); + if (!missing.isEmpty()) { + statusRows + .append("") + .append(missingKeysLabel) + .append("") + .append(missing.size()) + .append(""); } else { statusRows.append("  "); } - if (additional.size() != 0) { - statusRows.append("" + additionalKeysLabel + "" - + additional.size() + ""); + if (!additional.isEmpty()) { + statusRows + .append("") + .append(additionalKeysLabel) + .append("") + .append(additional.size()) + .append(""); } else { statusRows.append("  "); } - if (nonTranslated.size() != 0) { - statusRows.append("" + nontranslatedKeysLabel + "" - + nonTranslated.size() + ""); + if (!nonTranslated.isEmpty()) { + statusRows + .append("") + .append(nontranslatedKeysLabel) + .append("") + .append(nonTranslated.size()) + .append(""); } tableCell(wrapInTable(okLabel, statusRows.toString()), true); - } finally { - IOUtil.close(in2); } } else { tableCell(missingFileLabel); - count[i] = count[i] + 0; + count[i] += 0; } i = i + 1; } } } catch (IOException ex) { getLog().error(ex); - } finally { - IOUtil.close(in); } sink.tableRow_(); } @@ -403,9 +391,9 @@ public void renderBody() { sink.tableRow_(); endTable(); - sink.paragraph(); - text(bundle.getString("report.l10n.legend")); - sink.paragraph_(); + + paragraph(bundle.getString("report.l10n.legend")); + sink.list(); sink.listItem(); text(bundle.getString("report.l10n.list1")); @@ -423,19 +411,15 @@ public void renderBody() { endSection(); if (locales != null) { - Iterator itx = locales.iterator(); sink.list(); - while (itx.hasNext()) { - String x = (String) itx.next(); + for (String x : locales) { sink.listItem(); link("#" + x, x + " - " + localeDisplayNames.get(x)); sink.listItem_(); } sink.list_(); - itx = locales.iterator(); - while (itx.hasNext()) { - String x = (String) itx.next(); + for (String x : locales) { startSection(x + " - " + localeDisplayNames.get(x)); sink.anchor(x); sink.anchor_(); @@ -447,48 +431,43 @@ public void renderBody() { bundle.getString("report.l10n.tableheader3"), bundle.getString("report.l10n.tableheader4") }); - Iterator usedIter = usedFiles.iterator(); - while (usedIter.hasNext()) { + + for (Wrapper wr : usedFiles) { sink.tableRow(); - Wrapper wr = (Wrapper) usedIter.next(); tableCell(wr.getPath()); - Properties defs = (Properties) wr.getProperties().get(Wrapper.DEFAULT_LOCALE); - Properties locals = (Properties) wr.getProperties().get(x); + Properties defs = wr.getProperties().get(Wrapper.DEFAULT_LOCALE); + Properties locals = wr.getProperties().get(x); if (locals == null) { locals = new Properties(); } - Set missing = new TreeSet(defs.keySet()); - missing.removeAll(locals.keySet()); + Set missing = new TreeSet<>(defs.stringPropertyNames()); + missing.removeAll(locals.stringPropertyNames()); String cell = ""; - Iterator ms = missing.iterator(); - while (ms.hasNext()) { - cell = cell + "" + ms.next() + ""; + for (String s : missing) { + cell = cell + "" + s + ""; } tableCell(wrapInTable(okLabel, cell), true); - Set additional = new TreeSet(locals.keySet()); - additional.removeAll(defs.keySet()); - Iterator ex = additional.iterator(); + Set additional = new TreeSet<>(locals.stringPropertyNames()); + additional.removeAll(defs.stringPropertyNames()); cell = ""; - while (ex.hasNext()) { - cell = cell + "" + ex.next() + ""; + for (String ex : additional) { + cell = cell + "" + ex + ""; } tableCell(wrapInTable(okLabel, cell), true); - Set nonTranslated = new TreeSet(); - Iterator itnt = defs.keySet().iterator(); - while (itnt.hasNext()) { - String k = (String) itnt.next(); + Set nonTranslated = new TreeSet<>(); + for (String k : defs.stringPropertyNames()) { String val1 = defs.getProperty(k); String val2 = locals.getProperty(k); - if (val2 != null && val1.equals(val2)) { + if (val1.equals(val2)) { nonTranslated.add(k); } } - Iterator nt = nonTranslated.iterator(); + cell = ""; - while (nt.hasNext()) { - String n = (String) nt.next(); + for (String n : nonTranslated) { cell = cell + "" + n + "\"" + defs.getProperty(n) + "\""; } + tableCell(wrapInTable(okLabel, cell), true); sink.tableRow_(); @@ -521,7 +500,7 @@ private Locale createLocale(String localeCode) { } private String wrapInTable(String okLabel, String cell) { - if (cell.length() == 0) { + if (cell.isEmpty()) { cell = okLabel; } else { cell = "" + cell + "
"; @@ -530,15 +509,15 @@ private String wrapInTable(String okLabel, String cell) { } } - private static class Wrapper { + static class Wrapper { - private String path; + private final String path; - private File file; + private final File file; - private MavenProject proj; + private final MavenProject proj; - private Map properties; + private final Map properties; static final String DEFAULT_LOCALE = "Default"; @@ -546,7 +525,7 @@ public Wrapper(String p, File f, MavenProject prj) { path = p; file = f; proj = prj; - properties = new HashMap(); + properties = new HashMap<>(); } public File getFile() { @@ -561,16 +540,14 @@ public MavenProject getProject() { return proj; } - public Map getProperties() { + public Map getProperties() { return properties; } } - private static class WrapperComparator implements Comparator { + private static class WrapperComparator implements Comparator { - public int compare(Object o1, Object o2) { - Wrapper wr1 = (Wrapper) o1; - Wrapper wr2 = (Wrapper) o2; + public int compare(Wrapper wr1, Wrapper wr2) { int comp1 = wr1.getProject().getBasedir().compareTo(wr2.getProject().getBasedir()); if (comp1 != 0) { return comp1;