You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by mi...@apache.org on 2022/11/06 22:21:39 UTC
[maven-doxia-sitetools] 01/01: [DOXIASITETOOLS-271] Overhaul locale support and make telescopic
This is an automated email from the ASF dual-hosted git repository.
michaelo pushed a commit to branch DOXIASITETOOLS-271
in repository https://gitbox.apache.org/repos/asf/maven-doxia-sitetools.git
commit fd70c43312d2acbfcda2d387198dae8ecc6af04d
Author: Michael Osipov <mi...@apache.org>
AuthorDate: Sun Nov 6 23:20:06 2022 +0100
[DOXIASITETOOLS-271] Overhaul locale support and make telescopic
---
.../apache/maven/doxia/tools/DefaultSiteTool.java | 107 +++++++++++----------
.../org/apache/maven/doxia/tools/SiteTool.java | 20 ++--
.../org/apache/maven/doxia/tools/SiteToolTest.java | 20 ++--
.../doxia/siterenderer/SiteRenderingContext.java | 2 +-
.../siterenderer/DefaultSiteRendererTest.java | 6 +-
5 files changed, 80 insertions(+), 75 deletions(-)
diff --git a/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java b/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java
index ca364f5..deac8d6 100644
--- a/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java
+++ b/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java
@@ -335,14 +335,34 @@ public class DefaultSiteTool
public File getSiteDescriptor( File siteDirectory, Locale locale )
{
Objects.requireNonNull( siteDirectory, "siteDirectory cannot be null" );
- final Locale llocale = ( locale == null ) ? new Locale( "" ) : locale;
+ Objects.requireNonNull( locale, "locale cannot be null" );
- File siteDescriptor = new File( siteDirectory, "site_" + llocale.getLanguage() + ".xml" );
+ String language = locale.getLanguage();
+ String country = locale.getCountry();
+ String variant = locale.getVariant();
- if ( !siteDescriptor.isFile() )
+ File siteDescriptor = null;
+
+ if ( !variant.isEmpty() )
+ {
+ siteDescriptor = new File( siteDirectory, "site_" + language + "_" + country + "_" + variant + ".xml" );
+ }
+
+ if ( ( siteDescriptor == null || !siteDescriptor.isFile() ) && !country.isEmpty() )
+ {
+ siteDescriptor = new File( siteDirectory, "site_" + language + "_" + country + ".xml" );
+ }
+
+ if ( ( siteDescriptor == null || !siteDescriptor.isFile() ) && !language.isEmpty() )
+ {
+ siteDescriptor = new File( siteDirectory, "site_" + language + "_" + ".xml" );
+ }
+
+ if ( siteDescriptor == null || !siteDescriptor.isFile() )
{
siteDescriptor = new File( siteDirectory, "site.xml" );
}
+
return siteDescriptor;
}
@@ -352,8 +372,8 @@ public class DefaultSiteTool
* @param project the Maven project, not null.
* @param localRepository the Maven local repository, not null.
* @param repositories the Maven remote repositories, not null.
- * @param locale the locale wanted for the site descriptor. If not null, searching for
- * <code>site_<i>localeLanguage</i>.xml</code>, otherwise searching for <code>site.xml</code>.
+ * @param locale the locale wanted for the site descriptor, not null.
+ * See {@link #getSiteDescriptor(File, Locale)} for details.
* @return the site descriptor into the local repository after download of it from repositories or null if not
* found in repositories.
* @throws SiteToolException if any
@@ -365,12 +385,11 @@ public class DefaultSiteTool
Objects.requireNonNull( project, "project cannot be null" );
Objects.requireNonNull( localRepository, "localRepository cannot be null" );
Objects.requireNonNull( repositories, "repositories cannot be null" );
-
- final Locale llocale = ( locale == null ) ? new Locale( "" ) : locale;
+ Objects.requireNonNull( locale, "locale cannot be null" );
try
{
- return resolveSiteDescriptor( project, localRepository, repositories, llocale );
+ return resolveSiteDescriptor( project, localRepository, repositories, locale );
}
catch ( ArtifactNotFoundException e )
{
@@ -393,17 +412,16 @@ public class DefaultSiteTool
List<ArtifactRepository> repositories )
throws SiteToolException
{
+ Objects.requireNonNull( locale, "locale cannot be null" );
Objects.requireNonNull( project, "project cannot be null" );
Objects.requireNonNull( reactorProjects, "reactorProjects cannot be null" );
Objects.requireNonNull( localRepository, "localRepository cannot be null" );
Objects.requireNonNull( repositories, "repositories cannot be null" );
- final Locale llocale = ( locale == null ) ? Locale.getDefault() : locale;
-
- LOGGER.debug( "Computing decoration model of " + project.getId() + " for locale " + llocale );
+ LOGGER.debug( "Computing decoration model of " + project.getId() + " for locale " + locale );
Map.Entry<DecorationModel, MavenProject> result =
- getDecorationModel( 0, siteDirectory, llocale, project, reactorProjects, localRepository, repositories );
+ getDecorationModel( 0, siteDirectory, locale, project, reactorProjects, localRepository, repositories );
DecorationModel decorationModel = result.getKey();
MavenProject parentProject = result.getValue();
@@ -423,12 +441,12 @@ public class DefaultSiteTool
if ( parentProject != null )
{
- populateParentMenu( decorationModel, llocale, project, parentProject, true );
+ populateParentMenu( decorationModel, locale, project, parentProject, true );
}
try
{
- populateModulesMenu( decorationModel, llocale, project, reactorProjects, localRepository, true );
+ populateModulesMenu( decorationModel, locale, project, reactorProjects, localRepository, true );
}
catch ( IOException e )
{
@@ -506,7 +524,7 @@ public class DefaultSiteTool
* if used through <code><menu ref="parent"/></code>.
*
* @param decorationModel the Doxia Sitetools DecorationModel, not null.
- * @param locale the locale used for the i18n in DecorationModel. If null, using the default locale in the jvm.
+ * @param locale the locale used for the i18n in DecorationModel, not null.
* @param project a Maven project, not null.
* @param parentProject a Maven parent project, not null.
* @param keepInheritedRefs used for inherited references.
@@ -515,6 +533,7 @@ public class DefaultSiteTool
MavenProject parentProject, boolean keepInheritedRefs )
{
Objects.requireNonNull( decorationModel, "decorationModel cannot be null" );
+ Objects.requireNonNull( locale, "locale cannot be null" );
Objects.requireNonNull( project, "project cannot be null" );
Objects.requireNonNull( parentProject, "parentProject cannot be null" );
@@ -530,8 +549,6 @@ public class DefaultSiteTool
return;
}
- final Locale llocale = ( locale == null ) ? Locale.getDefault() : locale;
-
String parentUrl = getDistMgmntSiteUrl( parentProject );
if ( parentUrl != null )
@@ -570,7 +587,7 @@ public class DefaultSiteTool
{
if ( menu.getName() == null )
{
- menu.setName( i18n.getString( "site-tool", llocale, "decorationModel.menu.parentproject" ) );
+ menu.setName( i18n.getString( "site-tool", locale, "decorationModel.menu.parentproject" ) );
}
MenuItem item = new MenuItem();
@@ -585,7 +602,7 @@ public class DefaultSiteTool
* if used through <code><menu ref="modules"/></code>.
*
* @param decorationModel the Doxia Sitetools DecorationModel, not null.
- * @param locale the locale used for the i18n in DecorationModel. If null, using the default locale in the jvm.
+ * @param locale the locale used for the i18n in DecorationModel, not null.
* @param project a Maven project, not null.
* @param reactorProjects the Maven reactor projects, not null.
* @param localRepository the Maven local repository, not null.
@@ -598,10 +615,11 @@ public class DefaultSiteTool
boolean keepInheritedRefs )
throws SiteToolException, IOException
{
+ Objects.requireNonNull( decorationModel, "decorationModel cannot be null" );
+ Objects.requireNonNull( locale, "locale cannot be null" );
Objects.requireNonNull( project, "project cannot be null" );
Objects.requireNonNull( reactorProjects, "reactorProjects cannot be null" );
Objects.requireNonNull( localRepository, "localRepository cannot be null" );
- Objects.requireNonNull( decorationModel, "decorationModel cannot be null" );
Menu menu = decorationModel.getMenuRef( "modules" );
@@ -615,14 +633,12 @@ public class DefaultSiteTool
return;
}
- final Locale llocale = ( locale == null ) ? Locale.getDefault() : locale ;
-
// we require child modules and reactors to process module menu
if ( project.getModules().size() > 0 )
{
if ( menu.getName() == null )
{
- menu.setName( i18n.getString( "site-tool", llocale, "decorationModel.menu.projectmodules" ) );
+ menu.setName( i18n.getString( "site-tool", locale, "decorationModel.menu.projectmodules" ) );
}
for ( String module : (List<String>) project.getModules() )
@@ -699,6 +715,7 @@ public class DefaultSiteTool
Map<String, List<MavenReport>> categories )
{
Objects.requireNonNull( decorationModel, "decorationModel cannot be null" );
+ Objects.requireNonNull( locale, "locale cannot be null" );
Objects.requireNonNull( categories, "categories cannot be null" );
Menu menu = decorationModel.getMenuRef( "reports" );
@@ -708,11 +725,9 @@ public class DefaultSiteTool
return;
}
- final Locale llocale = ( locale == null ) ? Locale.getDefault() : locale;
-
if ( menu.getName() == null )
{
- menu.setName( i18n.getString( "site-tool", llocale, "decorationModel.menu.projectdocumentation" ) );
+ menu.setName( i18n.getString( "site-tool", locale, "decorationModel.menu.projectdocumentation" ) );
}
boolean found = false;
@@ -722,9 +737,9 @@ public class DefaultSiteTool
if ( !isEmptyList( categoryReports ) )
{
MenuItem item = createCategoryMenu(
- i18n.getString( "site-tool", llocale,
+ i18n.getString( "site-tool", locale,
"decorationModel.menu.projectinformation" ),
- "/project-info.html", categoryReports, llocale );
+ "/project-info.html", categoryReports, locale );
menu.getItems().add( item );
found = true;
}
@@ -733,8 +748,8 @@ public class DefaultSiteTool
if ( !isEmptyList( categoryReports ) )
{
MenuItem item =
- createCategoryMenu( i18n.getString( "site-tool", llocale, "decorationModel.menu.projectreports" ),
- "/project-reports.html", categoryReports, llocale );
+ createCategoryMenu( i18n.getString( "site-tool", locale, "decorationModel.menu.projectreports" ),
+ "/project-reports.html", categoryReports, locale );
menu.getItems().add( item );
found = true;
}
@@ -777,10 +792,8 @@ public class DefaultSiteTool
continue;
}
- // Default bundles are in English
- if ( ( !locale.getLanguage().equals( DEFAULT_LOCALE.getLanguage() ) )
- && ( !i18n.getBundle( "site-tool", locale ).getLocale().getLanguage()
- .equals( locale.getLanguage() ) ) )
+ if ( !i18n.getBundle( "site-tool", locale ).getLocale().equals( locale )
+ || !i18n.getBundle( "site-tool", locale ).getLocale().getLanguage().equals( locale.getLanguage() ) )
{
if ( LOGGER.isWarnEnabled() )
{
@@ -810,6 +823,7 @@ public class DefaultSiteTool
* object.
* <p>If localeCode = <code>default</code>, return the current value of the default locale for this instance
* of the Java Virtual Machine.</p>
+ * <p>If localeCode = <code>root</code>, return the root locale.</p>
*
* @param localeCode the locale code string.
* @return a java.util.Locale object instanced or null if errors occurred
@@ -827,6 +841,11 @@ public class DefaultSiteTool
return Locale.getDefault();
}
+ if ( "root".equalsIgnoreCase( localeCode ) )
+ {
+ return Locale.ROOT;
+ }
+
String language = "";
String country = "";
String variant = "";
@@ -891,6 +910,7 @@ public class DefaultSiteTool
* @throws ArtifactResolutionException if any
* @throws ArtifactNotFoundException if any
*/
+ // TODO here we need to telescope
private File resolveSiteDescriptor( MavenProject project, ArtifactRepository localRepository,
List<ArtifactRepository> repositories, Locale locale )
throws IOException, ArtifactResolutionException, ArtifactNotFoundException
@@ -1326,26 +1346,9 @@ public class DefaultSiteTool
{
if ( distMgmnt != null && distMgmnt.getSite() != null && distMgmnt.getSite().getUrl() != null )
{
- return urlEncode( distMgmnt.getSite().getUrl() );
+ return distMgmnt.getSite().getUrl();
}
return null;
}
-
- private static String urlEncode( final String url )
- {
- if ( url == null )
- {
- return null;
- }
-
- try
- {
- return new File( url ).toURI().toURL().toExternalForm();
- }
- catch ( MalformedURLException ex )
- {
- return url; // this will then throw somewhere else
- }
- }
}
diff --git a/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/SiteTool.java b/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/SiteTool.java
index b5454ce..b697f03 100644
--- a/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/SiteTool.java
+++ b/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/SiteTool.java
@@ -40,9 +40,9 @@ public interface SiteTool
{
/**
* The locale by default for a Maven Site
- * @see Locale#ENGLISH
+ * @see Locale#ROOT
*/
- Locale DEFAULT_LOCALE = Locale.ENGLISH;
+ Locale DEFAULT_LOCALE = Locale.ROOT;
/**
* Get a skin artifact from one of the repositories.
@@ -63,8 +63,9 @@ public interface SiteTool
* Get a site descriptor from the project's site directory.
*
* @param siteDirectory the site directory, not null
- * @param locale the locale wanted for the site descriptor. If not null, searching for
- * <code>site_<i>localeLanguage</i>.xml</code>, otherwise searching for <code>site.xml</code>.
+ * @param locale the locale wanted for the site descriptor, not null. Telescoping lookup for
+ * <code>site_language_country_variant.xml</code>, <code>site_language_country.xml</code>,
+ * <code>site_language_country.xml}</code>, or pass {@link Locale#ROOT} <code>site.xml</code>.
* @return the site descriptor file
*/ // used by maven-pdf-plugin (should not?)
File getSiteDescriptor( File siteDirectory, Locale locale );
@@ -107,7 +108,8 @@ public interface SiteTool
* Get a decoration model for a project.
*
* @param siteDirectory the site directory, may be null if project from repository
- * @param locale the locale used for the i18n in DecorationModel. If null, using the default locale in the jvm.
+ * @param locale the locale used for the i18n in DecorationModel, not null.
+ * See {@link #getSiteDescriptor(File, Locale)} for details.
* @param project the Maven project, not null.
* @param reactorProjects the Maven reactor projects, not null.
* @param localRepository the Maven local repository, not null.
@@ -128,7 +130,8 @@ public interface SiteTool
* 2 separate menus: "Project Information" and "Project Reports".
*
* @param decorationModel the Doxia Sitetools DecorationModel, not null.
- * @param locale the locale used for the i18n in DecorationModel. If null, using the default locale in the jvm.
+ * @param locale the locale used for the i18n in DecorationModel, not null.
+ * See {@link #getSiteDescriptor(File, Locale)} for details.
* @param reportsPerCategory reports per category to put in "Reports" or "Information" menus, not null.
* @see MavenReport#CATEGORY_PROJECT_INFORMATION
* @see MavenReport#CATEGORY_PROJECT_REPORTS
@@ -138,11 +141,10 @@ public interface SiteTool
/**
* Extracts from a comma-separated list the locales that are available in <code>site-tool</code>
- * resource bundle. Notice that <code>default</code> value will be changed to the default locale of
- * the JVM.
+ * resource bundle.
*
* @param locales A comma separated list of locales
- * @return a list of <code>Locale</code>, which at least contains the Maven default locale which is english
+ * @return a list of <code>Locale</code>s.
* @since 1.7, was previously getAvailableLocales(String)
*/
List<Locale> getSiteLocales( String locales );
diff --git a/doxia-integration-tools/src/test/java/org/apache/maven/doxia/tools/SiteToolTest.java b/doxia-integration-tools/src/test/java/org/apache/maven/doxia/tools/SiteToolTest.java
index 071ba27..3e8454d 100644
--- a/doxia-integration-tools/src/test/java/org/apache/maven/doxia/tools/SiteToolTest.java
+++ b/doxia-integration-tools/src/test/java/org/apache/maven/doxia/tools/SiteToolTest.java
@@ -212,12 +212,12 @@ public class SiteToolTest
assertNotNull( tool );
SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "site-tool-test" );
- assertEquals( tool.getSiteDescriptor( new File( project.getBasedir(), "src/site" ), null ).toString(),
+ assertEquals( tool.getSiteDescriptor( new File( project.getBasedir(), "src/site" ), SiteTool.DEFAULT_LOCALE ).toString(),
project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml" );
assertEquals( tool.getSiteDescriptor( new File( project.getBasedir(), "src/site" ), Locale.ENGLISH ).toString(),
project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml" );
String siteDir = "src/blabla";
- assertEquals( tool.getSiteDescriptor( new File( project.getBasedir(), siteDir ), null ).toString(),
+ assertEquals( tool.getSiteDescriptor( new File( project.getBasedir(), siteDir ), SiteTool.DEFAULT_LOCALE ).toString(),
project.getBasedir() + File.separator + "src" + File.separator + "blabla" + File.separator + "site.xml" );
}
@@ -257,7 +257,7 @@ public class SiteToolTest
// model from current local build
DecorationModel model =
- tool.getDecorationModel( new File( project.getBasedir(), "src/site" ), Locale.getDefault(), project,
+ tool.getDecorationModel( new File( project.getBasedir(), "src/site" ), SiteTool.DEFAULT_LOCALE, project,
reactorProjects, getLocalRepo(), project.getRemoteArtifactRepositories() );
assertNotNull( model );
assertNotNull( model.getBannerLeft() );
@@ -276,7 +276,7 @@ public class SiteToolTest
project.setArtifactId( "maven-site" );
project.setVersion( "1.0" );
DecorationModel modelFromRepo =
- tool.getDecorationModel( null, Locale.getDefault(), project, reactorProjects, getLocalRepo(),
+ tool.getDecorationModel( null, SiteTool.DEFAULT_LOCALE, project, reactorProjects, getLocalRepo(),
project.getRemoteArtifactRepositories() );
assertNotNull( modelFromRepo );
assertNotNull( modelFromRepo.getBannerLeft() );
@@ -303,7 +303,7 @@ public class SiteToolTest
List<MavenProject> reactorProjects = new ArrayList<MavenProject>();
DecorationModel model =
- tool.getDecorationModel( new File( project.getBasedir(), siteDirectory ), Locale.getDefault(), project,
+ tool.getDecorationModel( new File( project.getBasedir(), siteDirectory ), SiteTool.DEFAULT_LOCALE, project,
reactorProjects, getLocalRepo(), project.getRemoteArtifactRepositories() );
assertNotNull( model );
}
@@ -312,10 +312,10 @@ public class SiteToolTest
public void testGetAvailableLocales()
throws Exception
{
- assertEquals( Collections.singletonList( SiteTool.DEFAULT_LOCALE ), tool.getSiteLocales( "en" ) );
+ assertEquals( Collections.singletonList( SiteTool.DEFAULT_LOCALE ), tool.getSiteLocales( "root" ) );
assertEquals( Arrays.asList( SiteTool.DEFAULT_LOCALE, Locale.FRENCH, Locale.ITALIAN ),
- tool.getSiteLocales( "en,fr,it" ) );
+ tool.getSiteLocales( "root,fr,it" ) );
// by default, only DEFAULT_LOCALE
assertEquals( Collections.singletonList( SiteTool.DEFAULT_LOCALE ), tool.getSiteLocales( "" ) );
@@ -358,16 +358,16 @@ public class SiteToolTest
assertNotNull( tool );
SiteToolMavenProjectStub parentProject = new SiteToolMavenProjectStub( "interpolation-parent-test" );
- parentProject.setDistgributionManagementSiteUrl( "dav:https://davs.codehaus.org/site" );
+ parentProject.setDistgributionManagementSiteUrl( "dav+https://davs.codehaus.org/site" );
SiteToolMavenProjectStub childProject = new SiteToolMavenProjectStub( "interpolation-child-test" );
childProject.setParent( parentProject );
- childProject.setDistgributionManagementSiteUrl( "dav:https://davs.codehaus.org/site/child" );
+ childProject.setDistgributionManagementSiteUrl( "dav+https://davs.codehaus.org/site/child" );
List<MavenProject> reactorProjects = Collections.<MavenProject>singletonList( parentProject );
DecorationModel model = tool.getDecorationModel( new File( childProject.getBasedir(), "src/site" ),
- Locale.getDefault(), childProject, reactorProjects,
+ Locale.ROOT, childProject, reactorProjects,
getLocalRepo(), childProject.getRemoteArtifactRepositories() );
assertNotNull( model );
diff --git a/doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/SiteRenderingContext.java b/doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/SiteRenderingContext.java
index 434f971..1b4c57b 100644
--- a/doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/SiteRenderingContext.java
+++ b/doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/SiteRenderingContext.java
@@ -50,7 +50,7 @@ public class SiteRenderingContext
private Map<String, ?> templateProperties;
- private Locale locale = Locale.getDefault();
+ private Locale locale = Locale.ROOT;
private List<Locale> siteLocales = new ArrayList<Locale>();
diff --git a/doxia-site-renderer/src/test/java/org/apache/maven/doxia/siterenderer/DefaultSiteRendererTest.java b/doxia-site-renderer/src/test/java/org/apache/maven/doxia/siterenderer/DefaultSiteRendererTest.java
index e3eb8b4..936709e 100644
--- a/doxia-site-renderer/src/test/java/org/apache/maven/doxia/siterenderer/DefaultSiteRendererTest.java
+++ b/doxia-site-renderer/src/test/java/org/apache/maven/doxia/siterenderer/DefaultSiteRendererTest.java
@@ -142,7 +142,7 @@ public class DefaultSiteRendererTest
}
oldLocale = Locale.getDefault();
- Locale.setDefault( Locale.ENGLISH );
+ Locale.setDefault( Locale.ROOT );
}
/**
@@ -345,7 +345,7 @@ public class DefaultSiteRendererTest
skin.setFile( skinFile );
SiteRenderingContext siteRenderingContext =
renderer.createContextForSkin( skin, attributes, new DecorationModel(), "defaultWindowTitle",
- Locale.ENGLISH );
+ Locale.ROOT );
RenderingContext context = new RenderingContext( new File( "" ), "document.html", "generator" );
SiteRendererSink sink = new SiteRendererSink( context );
renderer.mergeDocumentIntoSite( writer, sink, siteRenderingContext );
@@ -379,7 +379,7 @@ public class DefaultSiteRendererTest
skin.setFile( skinFile );
SiteRenderingContext siteRenderingContext =
renderer.createContextForSkin( skin, attributes,decoration, "defaultWindowTitle",
- Locale.ENGLISH );
+ Locale.ROOT );
siteRenderingContext.addSiteDirectory( getTestFile( siteDir ) );
siteRenderingContext.setValidate( validate );