Skip to content

Commit

Permalink
[DOXIASITETOOLS-271] Overhaul locale support and make telescopic
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-o committed Nov 12, 2022
1 parent 107c5d0 commit 4aa1e6e
Show file tree
Hide file tree
Showing 14 changed files with 503 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,34 @@ private static String getRelativeFilePath( final String oldPath, final String ne
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;
}

Expand All @@ -352,8 +372,8 @@ public File getSiteDescriptor( File siteDirectory, Locale locale )
* @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
Expand All @@ -365,12 +385,11 @@ File getSiteDescriptorFromRepository( MavenProject project, ArtifactRepository l
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 )
{
Expand All @@ -393,17 +412,16 @@ public DecorationModel getDecorationModel( File siteDirectory, Locale locale, Ma
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();

Expand All @@ -423,12 +441,12 @@ public DecorationModel getDecorationModel( File siteDirectory, Locale locale, Ma

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 )
{
Expand Down Expand Up @@ -506,7 +524,7 @@ public MavenProject getParentProject( MavenProject aProject, List<MavenProject>
* if used through <code>&lt;menu ref="parent"/&gt;</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.
Expand All @@ -515,6 +533,7 @@ private void populateParentMenu( DecorationModel decorationModel, Locale locale,
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" );

Expand All @@ -530,8 +549,6 @@ private void populateParentMenu( DecorationModel decorationModel, Locale locale,
return;
}

final Locale llocale = ( locale == null ) ? Locale.getDefault() : locale;

String parentUrl = getDistMgmntSiteUrl( parentProject );

if ( parentUrl != null )
Expand Down Expand Up @@ -570,7 +587,7 @@ private void populateParentMenu( DecorationModel decorationModel, Locale locale,
{
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();
Expand All @@ -585,7 +602,7 @@ private void populateParentMenu( DecorationModel decorationModel, Locale locale,
* if used through <code>&lt;menu ref="modules"/&gt;</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.
Expand All @@ -598,10 +615,11 @@ private void populateModulesMenu( DecorationModel decorationModel, Locale locale
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" );

Expand All @@ -615,14 +633,12 @@ private void populateModulesMenu( DecorationModel decorationModel, Locale locale
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() )
Expand Down Expand Up @@ -699,6 +715,7 @@ public void populateReportsMenu( DecorationModel decorationModel, Locale locale,
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" );
Expand All @@ -708,11 +725,9 @@ public void populateReportsMenu( DecorationModel decorationModel, Locale locale,
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;
Expand All @@ -722,9 +737,9 @@ public void populateReportsMenu( DecorationModel decorationModel, Locale locale,
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;
}
Expand All @@ -733,8 +748,8 @@ public void populateReportsMenu( DecorationModel decorationModel, Locale locale,
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;
}
Expand Down Expand Up @@ -777,10 +792,8 @@ public List<Locale> getSiteLocales( String locales )
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() )
{
Expand Down Expand Up @@ -810,10 +823,12 @@ public List<Locale> getSiteLocales( String locales )
* 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
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Locale.html">java.util.Locale#getDefault()</a>
* @see Locale#getDefault()
* @see Locale#ROOT
*/
private Locale codeToLocale( String localeCode )
{
Expand All @@ -827,6 +842,11 @@ private Locale codeToLocale( String localeCode )
return Locale.getDefault();
}

if ( "root".equalsIgnoreCase( localeCode ) )
{
return Locale.ROOT;
}

String language = "";
String country = "";
String variant = "";
Expand Down Expand Up @@ -891,6 +911,7 @@ protected static String getNormalizedPath( String path )
* @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
Expand Down Expand Up @@ -1326,26 +1347,9 @@ private static String getDistMgmntSiteUrl( DistributionManagement distMgmnt )
{
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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -63,8 +63,9 @@ Artifact getSkinArtifactFromRepository( ArtifactRepository localRepository,
* 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.xml}</code>, or <code>site.xml</code> as last resort for {@link Locale#ROOT}.
* @return the site descriptor file
*/ // used by maven-pdf-plugin (should not?)
File getSiteDescriptor( File siteDirectory, Locale locale );
Expand Down Expand Up @@ -107,7 +108,8 @@ String getInterpolatedSiteDescriptorContent( Map<String, String> props, MavenPro
* 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.
Expand All @@ -128,7 +130,8 @@ DecorationModel getDecorationModel( File siteDirectory, Locale locale, MavenProj
* 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
Expand All @@ -138,11 +141,10 @@ void populateReportsMenu( DecorationModel decorationModel, Locale locale,

/**
* 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 );
Expand Down

0 comments on commit 4aa1e6e

Please sign in to comment.