Skip to content

Commit 70c192d

Browse files
committed
Adding withAllResourceBundlesFromClasspath
Signed-off-by: Florian Dupuy <[email protected]>
1 parent d5c62b7 commit 70c192d

File tree

6 files changed

+74
-2
lines changed

6 files changed

+74
-2
lines changed

commons/src/main/java/com/powsybl/commons/report/PowsyblCoreReportResourceBundle.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
*/
88
package com.powsybl.commons.report;
99

10+
import com.google.auto.service.AutoService;
11+
1012
/**
1113
* @author Alice Caron {@literal <alice.caron at rte-france.com>}
1214
*/
13-
public final class PowsyblCoreReportResourceBundle {
15+
@AutoService(ReportResourceBundle.class)
16+
public final class PowsyblCoreReportResourceBundle implements ReportResourceBundle {
1417

1518
public static final String BASE_NAME = "com.powsybl.commons.reports";
1619

17-
private PowsyblCoreReportResourceBundle() {
20+
public String getBaseName() {
21+
return BASE_NAME;
1822
}
1923
}

commons/src/main/java/com/powsybl/commons/report/ReportNodeBuilder.java

+15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.time.format.DateTimeFormatter;
1111
import java.util.Locale;
12+
import java.util.ServiceLoader;
1213

1314
/**
1415
* A builder to create a {@link ReportNode} object.
@@ -34,6 +35,20 @@ public interface ReportNodeBuilder extends ReportNodeAdderOrBuilder<ReportNodeBu
3435
*/
3536
ReportNodeBuilder withLocale(Locale locale);
3637

38+
/**
39+
* Sets the {@link MessageTemplateProvider} for the whole tree, unless overridden, to the {@link MultiBundleMessageTemplateProvider}
40+
* corresponding to all the bundle base names gathered by the {@link java.util.ServiceLoader} of {@link ReportResourceBundle}
41+
* implementations.
42+
* @return a reference to this object
43+
*/
44+
default ReportNodeBuilder withAllResourceBundlesFromClasspath() {
45+
String[] bundleBaseNames = ServiceLoader.load(ReportResourceBundle.class).stream()
46+
.map(ServiceLoader.Provider::get)
47+
.map(ReportResourceBundle::getBaseName)
48+
.toArray(String[]::new);
49+
return withMessageTemplateProvider(new MultiBundleMessageTemplateProvider(bundleBaseNames));
50+
}
51+
3752
/**
3853
* Build the corresponding {@link ReportNode}.
3954
* @return the new {@link ReportNode} corresponding to current <code>ReportNodeBuilder</code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright (c) 2025, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
* SPDX-License-Identifier: MPL-2.0
7+
*/
8+
package com.powsybl.commons.report;
9+
10+
/**
11+
* Interface used to load all the report ResourceBundles which are in the classpath.
12+
* @author Florian Dupuy {@literal <florian.dupuy at rte-france.com>}
13+
*/
14+
public interface ReportResourceBundle {
15+
String getBaseName();
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright (c) 2025, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
* SPDX-License-Identifier: MPL-2.0
7+
*/
8+
package com.powsybl.commons.report;
9+
10+
import com.google.auto.service.AutoService;
11+
12+
/**
13+
* @author Florian Dupuy {@literal <florian.dupuy at rte-france.com>}
14+
*/
15+
@AutoService(ReportResourceBundle.class)
16+
public final class MockReportResourceBundle implements ReportResourceBundle {
17+
18+
public String getBaseName() {
19+
return "i18n.mock.test";
20+
}
21+
}

commons/src/test/java/com/powsybl/commons/report/ReportNodeTest.java

+15
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,21 @@ void testLocaleAndi18n() {
373373
assertEquals(Locale.FRANCE, rootReportFrance.getTreeContext().getLocale());
374374
}
375375

376+
@Test
377+
void testAllBundlesFromClasspath() {
378+
ReportNode root = ReportNode.newRootReportNode()
379+
.withAllResourceBundlesFromClasspath()
380+
.withMessageTemplate("core.iidm.modification.voltageLevelRemoved")
381+
.withTypedValue("vlId", "vl1", TypedValue.ID)
382+
.build();
383+
assertEquals("Voltage level vl1 removed", root.getMessage());
384+
385+
ReportNode child = root.newReportNode()
386+
.withMessageTemplate("mocking.key")
387+
.add();
388+
assertEquals("Mocking message template", child.getMessage());
389+
}
390+
376391
private static void assertHasNoTimeStamp(ReportNode root1) {
377392
assertFalse(root1.getValues().containsKey(ReportConstants.TIMESTAMP_KEY));
378393
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mocking.key = Mocking message template

0 commit comments

Comments
 (0)