This is an automated email from the ASF dual-hosted git repository. michaelo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-reporting-impl.git
The following commit(s) were added to refs/heads/master by this push: new 166edf2 [MSHARED-1186] Upgrade Parent to 39 166edf2 is described below commit 166edf2fbcca79fee8382dfda94db7aef6d7d430 Author: Michael Osipov <micha...@apache.org> AuthorDate: Sat Feb 11 12:39:43 2023 +0100 [MSHARED-1186] Upgrade Parent to 39 --- pom.xml | 6 +- .../maven/reporting/AbstractMavenReport.java | 231 ++++++------- .../reporting/AbstractMavenReportRenderer.java | 372 +++++++++------------ .../reporting/AbstractMavenReportRendererTest.java | 113 +++---- 4 files changed, 305 insertions(+), 417 deletions(-) diff --git a/pom.xml b/pom.xml index 7348adf..3ccc898 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,4 @@ -<?xml version='1.0' encoding='UTF-8'?> +<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file @@ -23,7 +23,7 @@ <parent> <groupId>org.apache.maven.shared</groupId> <artifactId>maven-shared-components</artifactId> - <version>36</version> + <version>39</version> <relativePath /> </parent> @@ -49,8 +49,8 @@ <scm> <connection>scm:git:https://gitbox.apache.org/repos/asf/maven-reporting-impl.git</connection> <developerConnection>scm:git:https://gitbox.apache.org/repos/asf/maven-reporting-impl.git</developerConnection> - <url>https://github.com/apache/maven-reporting-impl/tree/${project.scm.tag}</url> <tag>HEAD</tag> + <url>https://github.com/apache/maven-reporting-impl/tree/${project.scm.tag}</url> </scm> <issueManagement> <system>jira</system> diff --git a/src/main/java/org/apache/maven/reporting/AbstractMavenReport.java b/src/main/java/org/apache/maven/reporting/AbstractMavenReport.java index d20add7..c2acae8 100644 --- a/src/main/java/org/apache/maven/reporting/AbstractMavenReport.java +++ b/src/main/java/org/apache/maven/reporting/AbstractMavenReport.java @@ -1,5 +1,3 @@ -package org.apache.maven.reporting; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -9,7 +7,7 @@ package org.apache.maven.reporting; * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an @@ -18,10 +16,22 @@ package org.apache.maven.reporting; * specific language governing permissions and limitations * under the License. */ +package org.apache.maven.reporting; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import org.apache.maven.archiver.MavenArchiver; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.archiver.MavenArchiver; import org.apache.maven.doxia.sink.Sink; import org.apache.maven.doxia.sink.SinkFactory; import org.apache.maven.doxia.site.decoration.DecorationModel; @@ -42,17 +52,6 @@ import org.codehaus.plexus.util.ReaderFactory; import static org.apache.maven.shared.utils.logging.MessageUtils.buffer; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; - /** * The basis for a Maven report which can be generated both as part of a site generation or * as a direct standalone goal invocation. @@ -69,58 +68,55 @@ import java.util.Map; * from maven-reporting-api * @see #executeReport(Locale) <code>abstract executeReport( Locale )</code> */ -public abstract class AbstractMavenReport - extends AbstractMojo - implements MavenMultiPageReport -{ +public abstract class AbstractMavenReport extends AbstractMojo implements MavenMultiPageReport { /** * The output directory for the report. Note that this parameter is only evaluated if the goal is run directly from * the command line. If the goal is run indirectly as part of a site generation, the output directory configured in * the Maven Site Plugin is used instead. */ - @Parameter( defaultValue = "${project.reporting.outputDirectory}", readonly = true, required = true ) + @Parameter(defaultValue = "${project.reporting.outputDirectory}", readonly = true, required = true) protected File outputDirectory; /** * The Maven Project. */ - @Parameter( defaultValue = "${project}", readonly = true, required = true ) + @Parameter(defaultValue = "${project}", readonly = true, required = true) protected MavenProject project; /** * The reactor projects. */ - @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true ) + @Parameter(defaultValue = "${reactorProjects}", required = true, readonly = true) protected List<MavenProject> reactorProjects; /** * Specifies the input encoding. */ - @Parameter( property = "encoding", defaultValue = "${project.build.sourceEncoding}", readonly = true ) + @Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}", readonly = true) private String inputEncoding; /** * Specifies the output encoding. */ - @Parameter( property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}", readonly = true ) + @Parameter(property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}", readonly = true) private String outputEncoding; /** * The local repository. */ - @Parameter( defaultValue = "${localRepository}", readonly = true, required = true ) + @Parameter(defaultValue = "${localRepository}", readonly = true, required = true) protected ArtifactRepository localRepository; /** * Remote repositories used for the project. */ - @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true ) + @Parameter(defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true) protected List<ArtifactRepository> remoteRepositories; /** * Directory containing the <code>site.xml</code> file. */ - @Parameter( defaultValue = "${basedir}/src/site" ) + @Parameter(defaultValue = "${basedir}/src/site") protected File siteDirectory; /** @@ -130,8 +126,7 @@ public abstract class AbstractMavenReport * * @see SiteTool#getSiteLocales(String) */ - - @Parameter( defaultValue = "default" ) + @Parameter(defaultValue = "default") protected String locale; /** @@ -162,106 +157,91 @@ public abstract class AbstractMavenReport * @see org.apache.maven.plugin.Mojo#execute() */ @Override - public void execute() - throws MojoExecutionException - { - if ( !canGenerateReport() ) - { + public void execute() throws MojoExecutionException { + if (!canGenerateReport()) { return; } - File outputDirectory = new File( getOutputDirectory() ); + File outputDirectory = new File(getOutputDirectory()); String filename = getOutputName() + ".html"; Locale locale = getLocale(); - try - { - SiteRenderingContext siteContext = createSiteRenderingContext( locale ); + try { + SiteRenderingContext siteContext = createSiteRenderingContext(locale); // copy resources - getSiteRenderer().copyResources( siteContext, outputDirectory ); + getSiteRenderer().copyResources(siteContext, outputDirectory); // TODO Replace null with real value - RenderingContext docRenderingContext = new RenderingContext( outputDirectory, filename, null ); + RenderingContext docRenderingContext = new RenderingContext(outputDirectory, filename, null); - SiteRendererSink sink = new SiteRendererSink( docRenderingContext ); + SiteRendererSink sink = new SiteRendererSink(docRenderingContext); - generate( sink, null, locale ); + generate(sink, null, locale); - if ( !isExternalReport() ) // MSHARED-204: only render Doxia sink if not an external report + if (!isExternalReport()) // MSHARED-204: only render Doxia sink if not an external report { outputDirectory.mkdirs(); - try ( Writer writer = - new OutputStreamWriter( new FileOutputStream( new File( outputDirectory, filename ) ), - getOutputEncoding() ) ) - { + try (Writer writer = new OutputStreamWriter( + new FileOutputStream(new File(outputDirectory, filename)), getOutputEncoding())) { // render report - getSiteRenderer().mergeDocumentIntoSite( writer, sink, siteContext ); + getSiteRenderer().mergeDocumentIntoSite(writer, sink, siteContext); } } // copy generated resources also - getSiteRenderer().copyResources( siteContext, outputDirectory ); - } - catch ( RendererException | IOException | MavenReportException | SiteToolException e ) - { + getSiteRenderer().copyResources(siteContext, outputDirectory); + } catch (RendererException | IOException | MavenReportException | SiteToolException e) { throw new MojoExecutionException( - "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e ); + "An error has occurred in " + getName(Locale.ENGLISH) + " report generation.", e); } } - private SiteRenderingContext createSiteRenderingContext( Locale locale ) - throws MavenReportException, IOException, SiteToolException - { + private SiteRenderingContext createSiteRenderingContext(Locale locale) + throws MavenReportException, IOException, SiteToolException { DecorationModel decorationModel = siteTool.getDecorationModel( - siteDirectory, locale, project, reactorProjects, localRepository, remoteRepositories ); + siteDirectory, locale, project, reactorProjects, localRepository, remoteRepositories); Map<String, Object> templateProperties = new HashMap<>(); // We tell the skin that we are rendering in standalone mode - templateProperties.put( "standalone", Boolean.TRUE ); - templateProperties.put( "project", getProject() ); - templateProperties.put( "inputEncoding", getInputEncoding() ); - templateProperties.put( "outputEncoding", getOutputEncoding() ); + templateProperties.put("standalone", Boolean.TRUE); + templateProperties.put("project", getProject()); + templateProperties.put("inputEncoding", getInputEncoding()); + templateProperties.put("outputEncoding", getOutputEncoding()); // Put any of the properties in directly into the Velocity context - for ( Map.Entry<Object, Object> entry : getProject().getProperties().entrySet() ) - { - templateProperties.put( (String) entry.getKey(), entry.getValue() ); + for (Map.Entry<Object, Object> entry : getProject().getProperties().entrySet()) { + templateProperties.put((String) entry.getKey(), entry.getValue()); } SiteRenderingContext context; - try - { + try { Artifact skinArtifact = - siteTool.getSkinArtifactFromRepository( localRepository, remoteRepositories, decorationModel ); - - getLog().info( buffer().a( "Rendering content with " ).strong( skinArtifact.getId() - + " skin" ).a( '.' ).toString() ); - - context = siteRenderer.createContextForSkin( skinArtifact, templateProperties, decorationModel, - project.getName(), locale ); - } - catch ( SiteToolException e ) - { - throw new MavenReportException( "Failed to retrieve skin artifact", e ); - } - catch ( RendererException e ) - { - throw new MavenReportException( "Failed to create context for skin", e ); + siteTool.getSkinArtifactFromRepository(localRepository, remoteRepositories, decorationModel); + + getLog().info(buffer().a("Rendering content with ") + .strong(skinArtifact.getId() + " skin") + .a('.') + .toString()); + + context = siteRenderer.createContextForSkin( + skinArtifact, templateProperties, decorationModel, project.getName(), locale); + } catch (SiteToolException e) { + throw new MavenReportException("Failed to retrieve skin artifact", e); + } catch (RendererException e) { + throw new MavenReportException("Failed to create context for skin", e); } // Add publish date - String outputTimestamp = getProject().getProperties().getProperty( "project.build.outputTimestamp" ); - MavenArchiver.parseBuildOutputTimestamp( outputTimestamp ).ifPresent( v -> - { - context.setPublishDate( Date.from( v ) ); - } - ); + String outputTimestamp = getProject().getProperties().getProperty("project.build.outputTimestamp"); + MavenArchiver.parseBuildOutputTimestamp(outputTimestamp).ifPresent(v -> { + context.setPublishDate(Date.from(v)); + }); // Generate static site - context.setRootDirectory( project.getBasedir() ); + context.setRootDirectory(project.getBasedir()); return context; } @@ -276,10 +256,8 @@ public abstract class AbstractMavenReport */ @Deprecated @Override - public void generate( Sink sink, Locale locale ) - throws MavenReportException - { - generate( sink, null, locale ); + public void generate(Sink sink, Locale locale) throws MavenReportException { + generate(sink, null, locale); } /** @@ -291,13 +269,10 @@ public abstract class AbstractMavenReport * @throws MavenReportException */ @Override - public void generate( Sink sink, SinkFactory sinkFactory, Locale locale ) - throws MavenReportException - { - if ( !canGenerateReport() ) - { - getLog().info( "This report cannot be generated as part of the current build. " - + "The report name should be referenced in this line of output." ); + public void generate(Sink sink, SinkFactory sinkFactory, Locale locale) throws MavenReportException { + if (!canGenerateReport()) { + getLog().info("This report cannot be generated as part of the current build. " + + "The report name should be referenced in this line of output."); return; } @@ -305,7 +280,7 @@ public abstract class AbstractMavenReport this.sinkFactory = sinkFactory; - executeReport( locale ); + executeReport(locale); closeReport(); } @@ -314,41 +289,34 @@ public abstract class AbstractMavenReport * @return CATEGORY_PROJECT_REPORTS */ @Override - public String getCategoryName() - { + public String getCategoryName() { return CATEGORY_PROJECT_REPORTS; } @Override - public File getReportOutputDirectory() - { - if ( reportOutputDirectory == null ) - { - reportOutputDirectory = new File( getOutputDirectory() ); + public File getReportOutputDirectory() { + if (reportOutputDirectory == null) { + reportOutputDirectory = new File(getOutputDirectory()); } return reportOutputDirectory; } @Override - public void setReportOutputDirectory( File reportOutputDirectory ) - { + public void setReportOutputDirectory(File reportOutputDirectory) { this.reportOutputDirectory = reportOutputDirectory; this.outputDirectory = reportOutputDirectory; } - protected String getOutputDirectory() - { + protected String getOutputDirectory() { return outputDirectory.getAbsolutePath(); } - protected MavenProject getProject() - { + protected MavenProject getProject() { return project; } - protected Renderer getSiteRenderer() - { + protected Renderer getSiteRenderer() { return siteRenderer; } @@ -357,9 +325,8 @@ public abstract class AbstractMavenReport * * @return The input files encoding, never <code>null</code>. */ - protected String getInputEncoding() - { - return ( inputEncoding == null ) ? ReaderFactory.FILE_ENCODING : inputEncoding; + protected String getInputEncoding() { + return (inputEncoding == null) ? ReaderFactory.FILE_ENCODING : inputEncoding; } /** @@ -367,9 +334,8 @@ public abstract class AbstractMavenReport * * @return The effective reporting output file encoding, never <code>null</code>. */ - protected String getOutputEncoding() - { - return ( outputEncoding == null ) ? WriterFactory.UTF_8 : outputEncoding; + protected String getOutputEncoding() { + return (outputEncoding == null) ? WriterFactory.UTF_8 : outputEncoding; } /** @@ -377,32 +343,28 @@ public abstract class AbstractMavenReport * * @return the locale for this standalone report */ - protected Locale getLocale() - { - return siteTool.getSiteLocales( locale ).get( 0 ); + protected Locale getLocale() { + return siteTool.getSiteLocales(locale).get(0); } /** * Actions when closing the report. */ - protected void closeReport() - { + protected void closeReport() { getSink().close(); } /** * @return the sink used */ - public Sink getSink() - { + public Sink getSink() { return sink; } /** * @return the sink factory used */ - public SinkFactory getSinkFactory() - { + public SinkFactory getSinkFactory() { return sinkFactory; } @@ -411,14 +373,12 @@ public abstract class AbstractMavenReport * @return {@code false} by default. */ @Override - public boolean isExternalReport() - { + public boolean isExternalReport() { return false; } @Override - public boolean canGenerateReport() - { + public boolean canGenerateReport() { return true; } @@ -428,6 +388,5 @@ public abstract class AbstractMavenReport * @param locale the wanted locale to return the report's description, could be <code>null</code>. * @throws MavenReportException if any */ - protected abstract void executeReport( Locale locale ) - throws MavenReportException; + protected abstract void executeReport(Locale locale) throws MavenReportException; } diff --git a/src/main/java/org/apache/maven/reporting/AbstractMavenReportRenderer.java b/src/main/java/org/apache/maven/reporting/AbstractMavenReportRenderer.java index 2daf830..0f1a74e 100644 --- a/src/main/java/org/apache/maven/reporting/AbstractMavenReportRenderer.java +++ b/src/main/java/org/apache/maven/reporting/AbstractMavenReportRenderer.java @@ -1,7 +1,23 @@ -package org.apache.maven.reporting; - /* * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.reporting; +/* * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file @@ -19,11 +35,6 @@ package org.apache.maven.reporting; * under the License. */ -import org.apache.maven.doxia.markup.Markup; -import org.apache.maven.doxia.sink.Sink; -import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet; -import org.apache.maven.shared.utils.StringUtils; - import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; @@ -31,6 +42,11 @@ import java.util.List; import java.util.Map; import java.util.Properties; +import org.apache.maven.doxia.markup.Markup; +import org.apache.maven.doxia.sink.Sink; +import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet; +import org.apache.maven.shared.utils.StringUtils; + /** * <p>An abstract class to manage report generation, with many helper methods to ease the job: you just need to * implement getTitle() and renderBody().</p> @@ -46,9 +62,7 @@ import java.util.Properties; * @see #getTitle() * @see #renderBody() */ -public abstract class AbstractMavenReportRenderer - implements MavenReportRenderer -{ +public abstract class AbstractMavenReportRenderer implements MavenReportRenderer { /** The current sink to use */ protected Sink sink; @@ -60,19 +74,17 @@ public abstract class AbstractMavenReportRenderer * * @param sink the sink to use. */ - public AbstractMavenReportRenderer( Sink sink ) - { + public AbstractMavenReportRenderer(Sink sink) { this.sink = sink; } /** {@inheritDoc} */ @Override - public void render() - { + public void render() { sink.head(); sink.title(); - text( getTitle() ); + text(getTitle()); sink.title_(); sink.head_(); @@ -100,9 +112,8 @@ public abstract class AbstractMavenReportRenderer * @see Sink#sectionTitle(int, org.apache.maven.doxia.sink.SinkEventAttributes) * @see Sink#sectionTitle_(int) */ - protected void startSection( String name ) - { - startSection( name, name ); + protected void startSection(String name) { + startSection(name, name); } /** @@ -115,15 +126,14 @@ public abstract class AbstractMavenReportRenderer * @see Sink#sectionTitle(int, org.apache.maven.doxia.sink.SinkEventAttributes) * @see Sink#sectionTitle_(int) */ - protected void startSection( String name, String anchor ) - { + protected void startSection(String name, String anchor) { section++; - sink.section( section, null ); - sink.anchor( anchor ); + sink.section(section, null); + sink.anchor(anchor); sink.anchor_(); - sink.sectionTitle( section, null ); - text( name ); - sink.sectionTitle_( section ); + sink.sectionTitle(section, null); + text(name); + sink.sectionTitle_(section); } /** @@ -132,14 +142,12 @@ public abstract class AbstractMavenReportRenderer * @see Sink#section_(int) * @throws IllegalStateException if too many closing sections. */ - protected void endSection() - { - sink.section_( section ); + protected void endSection() { + sink.section_(section); section--; - if ( section < 0 ) - { - throw new IllegalStateException( "Too many closing sections" ); + if (section < 0) { + throw new IllegalStateException("Too many closing sections"); } } @@ -152,9 +160,8 @@ public abstract class AbstractMavenReportRenderer * * @see Sink#table() */ - protected void startTable() - { - startTable( null, false ); + protected void startTable() { + startTable(null, false); } /** @@ -167,10 +174,9 @@ public abstract class AbstractMavenReportRenderer * @see Sink#tableRows(int[],boolean) * @since 2.1 */ - protected void startTable( int[] justification, boolean grid ) - { + protected void startTable(int[] justification, boolean grid) { sink.table(); - sink.tableRows( justification, grid ); + sink.tableRows(justification, grid); } /** @@ -178,8 +184,7 @@ public abstract class AbstractMavenReportRenderer * * @see Sink#table_() */ - protected void endTable() - { + protected void endTable() { sink.tableRows_(); sink.table_(); } @@ -192,11 +197,10 @@ public abstract class AbstractMavenReportRenderer * @see Sink#tableHeaderCell() * @see Sink#tableHeaderCell_() */ - protected void tableHeaderCell( String text ) - { + protected void tableHeaderCell(String text) { sink.tableHeaderCell(); - text( text ); + text(text); sink.tableHeaderCell_(); } @@ -209,9 +213,8 @@ public abstract class AbstractMavenReportRenderer * @see #linkPatternedText(String) * @see #tableCell(String) */ - protected void tableCell( String text ) - { - tableCell( text, false ); + protected void tableCell(String text) { + tableCell(text, false); } /** @@ -226,17 +229,13 @@ public abstract class AbstractMavenReportRenderer * @see Sink#tableCell_() * @see Sink#rawText(String) */ - protected void tableCell( String text, boolean asHtml ) - { + protected void tableCell(String text, boolean asHtml) { sink.tableCell(); - if ( asHtml ) - { - sink.rawText( text ); - } - else - { - linkPatternedText( text ); + if (asHtml) { + sink.rawText(text); + } else { + linkPatternedText(text); } sink.tableCell_(); @@ -251,15 +250,12 @@ public abstract class AbstractMavenReportRenderer * @see Sink#tableRow() * @see Sink#tableRow_() */ - protected void tableRow( String[] content ) - { + protected void tableRow(String[] content) { sink.tableRow(); - if ( content != null ) - { - for ( int i = 0; i < content.length; i++ ) - { - tableCell( content[i] ); + if (content != null) { + for (int i = 0; i < content.length; i++) { + tableCell(content[i]); } } @@ -274,15 +270,12 @@ public abstract class AbstractMavenReportRenderer * @see Sink#tableRow() * @see Sink#tableRow_() */ - protected void tableHeader( String[] content ) - { + protected void tableHeader(String[] content) { sink.tableRow(); - if ( content != null ) - { - for ( int i = 0; i < content.length; i++ ) - { - tableHeaderCell( content[i] ); + if (content != null) { + for (int i = 0; i < content.length; i++) { + tableHeaderCell(content[i]); } } @@ -297,11 +290,10 @@ public abstract class AbstractMavenReportRenderer * @see Sink#tableCaption() * @see Sink#tableCaption_() */ - protected void tableCaption( String caption ) - { + protected void tableCaption(String caption) { sink.tableCaption(); - text( caption ); + text(caption); sink.tableCaption_(); } @@ -318,11 +310,10 @@ public abstract class AbstractMavenReportRenderer * @see Sink#paragraph() * @see Sink#paragraph_() */ - protected void paragraph( String paragraph ) - { + protected void paragraph(String paragraph) { sink.paragraph(); - text( paragraph ); + text(paragraph); sink.paragraph_(); } @@ -336,11 +327,10 @@ public abstract class AbstractMavenReportRenderer * @see Sink#link(String) * @see Sink#link_() */ - protected void link( String href, String name ) - { - sink.link( href ); + protected void link(String href, String name) { + sink.link(href); - text( name ); + text(name); sink.link_(); } @@ -352,15 +342,12 @@ public abstract class AbstractMavenReportRenderer * @param text a text, could be null. * @see Sink#text(String) */ - protected void text( String text ) - { - if ( StringUtils.isEmpty( text ) ) // Take care of spaces + protected void text(String text) { + if (StringUtils.isEmpty(text)) // Take care of spaces { - sink.text( "-" ); - } - else - { - sink.text( text ); + sink.text("-"); + } else { + sink.text(text); } } @@ -372,11 +359,10 @@ public abstract class AbstractMavenReportRenderer * @see Sink#verbatim(org.apache.maven.doxia.sink.SinkEventAttributes) * @see Sink#verbatim_() */ - protected void verbatimText( String text ) - { - sink.verbatim( null ); + protected void verbatimText(String text) { + sink.verbatim(null); - text( text ); + text(text); sink.verbatim_(); } @@ -391,17 +377,13 @@ public abstract class AbstractMavenReportRenderer * @see Sink#verbatim(org.apache.maven.doxia.sink.SinkEventAttributes) * @see Sink#verbatim_() */ - protected void verbatimLink( String text, String href ) - { - if ( StringUtils.isEmpty( href ) ) - { - verbatimText( text ); - } - else - { - sink.verbatim( null ); + protected void verbatimLink(String text, String href) { + if (StringUtils.isEmpty(href)) { + verbatimText(text); + } else { + sink.verbatim(null); - link( href, text ); + link(href, text); sink.verbatim_(); } @@ -415,11 +397,10 @@ public abstract class AbstractMavenReportRenderer * @see Sink#verbatim(org.apache.maven.doxia.sink.SinkEventAttributes) * @see Sink#verbatim_() */ - protected void verbatimSource( String source ) - { - sink.verbatim( SinkEventAttributeSet.SOURCE ); + protected void verbatimSource(String source) { + sink.verbatim(SinkEventAttributeSet.SOURCE); - text( source ); + text(source); sink.verbatim_(); } @@ -430,9 +411,8 @@ public abstract class AbstractMavenReportRenderer * @param jsCode a string of Javascript * @see Sink#rawText(String) */ - protected void javaScript( String jsCode ) - { - sink.rawText( Markup.EOL + "<script>" + Markup.EOL + jsCode + Markup.EOL + "</script>" + Markup.EOL ); + protected void javaScript(String jsCode) { + sink.rawText(Markup.EOL + "<script>" + Markup.EOL + jsCode + Markup.EOL + "</script>" + Markup.EOL); } /** @@ -445,34 +425,23 @@ public abstract class AbstractMavenReportRenderer * @see #link(String, String) * @see #applyPattern(String) */ - public void linkPatternedText( String text ) - { - if ( StringUtils.isEmpty( text ) ) - { - text( text ); - } - else - { - List<String> segments = applyPattern( text ); - - if ( segments == null ) - { - text( text ); - } - else - { - for ( Iterator<String> it = segments.iterator(); it.hasNext(); ) - { + public void linkPatternedText(String text) { + if (StringUtils.isEmpty(text)) { + text(text); + } else { + List<String> segments = applyPattern(text); + + if (segments == null) { + text(text); + } else { + for (Iterator<String> it = segments.iterator(); it.hasNext(); ) { String name = it.next(); String href = it.next(); - if ( href == null ) - { - text( name ); - } - else - { - link( href, name ); + if (href == null) { + text(name); + } else { + link(href, name); } } } @@ -489,15 +458,12 @@ public abstract class AbstractMavenReportRenderer * @return a link pattern * @see #linkPatternedText(String) */ - protected static String createLinkPatternedText( String text, String href ) - { - if ( text == null ) - { + protected static String createLinkPatternedText(String text, String href) { + if (text == null) { return text; } - if ( href == null ) - { + if (href == null) { return text; } @@ -510,23 +476,19 @@ public abstract class AbstractMavenReportRenderer * @param props the properties to display. * @return the properties object as comma separated String */ - protected static String propertiesToString( Properties props ) - { - if ( props == null || props.isEmpty() ) - { + protected static String propertiesToString(Properties props) { + if (props == null || props.isEmpty()) { return ""; } StringBuilder sb = new StringBuilder(); - for ( Map.Entry<?, ?> entry : props.entrySet() ) - { - if ( sb.length() > 0 ) - { - sb.append( ", " ); + for (Map.Entry<?, ?> entry : props.entrySet()) { + if (sb.length() > 0) { + sb.append(", "); } - sb.append( entry.getKey() ).append( "=" ).append( entry.getValue() ); + sb.append(entry.getKey()).append("=").append(entry.getValue()); } return sb.toString(); @@ -543,10 +505,8 @@ public abstract class AbstractMavenReportRenderer * @param text a text with or without the pattern <code>{text, url}</code> * @return a map of text/href */ - private static List<String> applyPattern( String text ) - { - if ( StringUtils.isEmpty( text ) ) - { + private static List<String> applyPattern(String text) { + if (StringUtils.isEmpty(text)) { return null; } @@ -555,19 +515,15 @@ public abstract class AbstractMavenReportRenderer List<String> segments = new ArrayList<>(); // TODO Special case http://jira.codehaus.org/browse/MEV-40 - if ( text.indexOf( "${" ) != -1 ) - { - int lastComma = text.lastIndexOf( "," ); - int lastSemi = text.lastIndexOf( "}" ); - if ( lastComma != -1 && lastSemi != -1 && lastComma < lastSemi ) - { - segments.add( text.substring( lastComma + 1, lastSemi ).trim() ); - segments.add( null ); - } - else - { - segments.add( text ); - segments.add( null ); + if (text.indexOf("${") != -1) { + int lastComma = text.lastIndexOf(","); + int lastSemi = text.lastIndexOf("}"); + if (lastComma != -1 && lastSemi != -1 && lastComma < lastSemi) { + segments.add(text.substring(lastComma + 1, lastSemi).trim()); + segments.add(null); + } else { + segments.add(text); + segments.add(null); } return segments; @@ -577,38 +533,28 @@ public abstract class AbstractMavenReportRenderer int braceStack = 0; int lastOffset = 0; - for ( int i = 0; i < text.length(); i++ ) - { - char ch = text.charAt( i ); + for (int i = 0; i < text.length(); i++) { + char ch = text.charAt(i); - if ( ch == '\'' && !inQuote && braceStack == 0 ) - { + if (ch == '\'' && !inQuote && braceStack == 0) { // handle: '' - if ( i + 1 < text.length() && text.charAt( i + 1 ) == '\'' ) - { + if (i + 1 < text.length() && text.charAt(i + 1) == '\'') { i++; - segments.add( text.substring( lastOffset, i ) ); - segments.add( null ); + segments.add(text.substring(lastOffset, i)); + segments.add(null); lastOffset = i + 1; - } - else - { + } else { inQuote = true; } - } - else - { - switch ( ch ) - { + } else { + switch (ch) { case '{': - if ( !inQuote ) - { - if ( braceStack == 0 ) - { - if ( i != lastOffset ) // handle { at first character + if (!inQuote) { + if (braceStack == 0) { + if (i != lastOffset) // handle { at first character { - segments.add( text.substring( lastOffset, i ) ); - segments.add( null ); + segments.add(text.substring(lastOffset, i)); + segments.add(null); } lastOffset = i + 1; } @@ -616,24 +562,21 @@ public abstract class AbstractMavenReportRenderer } break; case '}': - if ( !inQuote ) - { + if (!inQuote) { braceStack--; - if ( braceStack == 0 ) - { - String subString = text.substring( lastOffset, i ); + if (braceStack == 0) { + String subString = text.substring(lastOffset, i); lastOffset = i + 1; - int lastComma = subString.lastIndexOf( "," ); - if ( lastComma != -1 ) - { - segments.add( subString.substring( 0, lastComma ).trim() ); - segments.add( subString.substring( lastComma + 1 ).trim() ); - } - else - { - segments.add( subString ); - segments.add( null ); + int lastComma = subString.lastIndexOf(","); + if (lastComma != -1) { + segments.add( + subString.substring(0, lastComma).trim()); + segments.add( + subString.substring(lastComma + 1).trim()); + } else { + segments.add(subString); + segments.add(null); } } } @@ -647,24 +590,21 @@ public abstract class AbstractMavenReportRenderer } } - if ( !StringUtils.isEmpty( text.substring( lastOffset ) ) ) - { - segments.add( text.substring( lastOffset ) ); - segments.add( null ); + if (!StringUtils.isEmpty(text.substring(lastOffset))) { + segments.add(text.substring(lastOffset)); + segments.add(null); } - if ( braceStack != 0 ) - { - throw new IllegalArgumentException( "Unmatched braces in the pattern." ); + if (braceStack != 0) { + throw new IllegalArgumentException("Unmatched braces in the pattern."); } - if ( inQuote ) - { - //throw new IllegalArgumentException( "Unmatched quote in the pattern." ); - //TODO: warning... + if (inQuote) { + // throw new IllegalArgumentException( "Unmatched quote in the pattern." ); + // TODO: warning... } - return Collections.unmodifiableList( segments ); + return Collections.unmodifiableList(segments); } // ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/maven/reporting/AbstractMavenReportRendererTest.java b/src/test/java/org/apache/maven/reporting/AbstractMavenReportRendererTest.java index 8835476..4f29331 100644 --- a/src/test/java/org/apache/maven/reporting/AbstractMavenReportRendererTest.java +++ b/src/test/java/org/apache/maven/reporting/AbstractMavenReportRendererTest.java @@ -1,5 +1,3 @@ -package org.apache.maven.reporting; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -9,7 +7,7 @@ package org.apache.maven.reporting; * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an @@ -18,6 +16,7 @@ package org.apache.maven.reporting; * specific language governing permissions and limitations * under the License. */ +package org.apache.maven.reporting; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -32,46 +31,35 @@ import static org.junit.jupiter.api.Assertions.fail; /** * Test case for some public method in AbstractMavenReportRenderer. */ -public class AbstractMavenReportRendererTest -{ - private static List<String> applyPattern( String pattern ) - throws Throwable - { - try - { - Method method = AbstractMavenReportRenderer.class.getDeclaredMethod( "applyPattern", String.class ); - method.setAccessible( true ); - return (List<String>) method.invoke( null, pattern ); - } catch ( InvocationTargetException ite ) - { +public class AbstractMavenReportRendererTest { + private static List<String> applyPattern(String pattern) throws Throwable { + try { + Method method = AbstractMavenReportRenderer.class.getDeclaredMethod("applyPattern", String.class); + method.setAccessible(true); + return (List<String>) method.invoke(null, pattern); + } catch (InvocationTargetException ite) { throw ite.getTargetException(); } } - private static void checkPattern( String pattern, String[] expectedResult ) throws Throwable - { - List<String> result = applyPattern( pattern ); - assertEquals( expectedResult.length, result.size(), "result size" ); + private static void checkPattern(String pattern, String[] expectedResult) throws Throwable { + List<String> result = applyPattern(pattern); + assertEquals(expectedResult.length, result.size(), "result size"); int i = 0; - for ( Iterator<String> it = result.iterator(); it.hasNext(); ) - { + for (Iterator<String> it = result.iterator(); it.hasNext(); ) { String name = it.next(); String href = it.next(); - assertEquals( expectedResult[i], name ); - assertEquals( expectedResult[i + 1], href ); + assertEquals(expectedResult[i], name); + assertEquals(expectedResult[i + 1], href); i += 2; } } - private static void checkPatternIllegalArgument( String cause, String pattern ) throws Throwable - { - try - { - applyPattern( pattern ); - fail( cause + " should throw an IllegalArgumentException" ); - } - catch ( IllegalArgumentException iae ) - { + private static void checkPatternIllegalArgument(String cause, String pattern) throws Throwable { + try { + applyPattern(pattern); + fail(cause + " should throw an IllegalArgumentException"); + } catch (IllegalArgumentException iae) { // ok } } @@ -80,60 +68,61 @@ public class AbstractMavenReportRendererTest * @throws Throwable if any */ @Test - public void testApplyPattern() throws Throwable - { + public void testApplyPattern() throws Throwable { // the most simple test - checkPattern( "test {text,url}", new String[] { "test ", null, "text", "url" } ); + checkPattern("test {text,url}", new String[] {"test ", null, "text", "url"}); // check that link content is trimmed, and no problem if 2 text values are the same - checkPattern( "test{ text , url }test", new String[] { "test", null, "text", "url", "test", null } ); + checkPattern("test{ text , url }test", new String[] {"test", null, "text", "url", "test", null}); // check brace stacking - checkPattern( "test{ {text} , url }", new String[] { "test", null, "{text}", "url" } ); + checkPattern("test{ {text} , url }", new String[] {"test", null, "{text}", "url"}); // check quoting - checkPatternIllegalArgument( "unmatched brace", "{" ); - checkPattern( "'{'", new String[] { "'{'", null } ); - checkPattern( " ' { '.", new String[] { " ' { '.", null } ); + checkPatternIllegalArgument("unmatched brace", "{"); + checkPattern("'{'", new String[] {"'{'", null}); + checkPattern(" ' { '.", new String[] {" ' { '.", null}); // unmatched quote: the actual behavior is to ignore that they are unmatched - checkPattern( " '", new String[] { " '", null } ); + checkPattern(" '", new String[] {" '", null}); // but shouldn't it be different and throw an IllegalArgumentException? // checkPatternIllegalArgument( "unmatched quote", " ' " ); // checkPatternIllegalArgument( "unmatched quote", " '" ); // impact is too important to make the change for the moment // check double quoting - checkPattern( " ''", new String[] { " '", null } ); - checkPattern( " '' ", new String[] { " '", null } ); - checkPattern( " '' ", new String[] { " '", null } ); + checkPattern(" ''", new String[] {" '", null}); + checkPattern(" '' ", new String[] {" '", null}); + checkPattern(" '' ", new String[] {" '", null}); // real world cases with quote - checkPattern( "project''s info", new String[] { "project'", null, "s info", null } ); - checkPattern( "it''s a question of {chance, http://en.wikipedia.org/wiki/Chance}", - new String[] { "it'", null, "s a question of ", null, - "chance", "http://en.wikipedia.org/wiki/Chance" } ); - checkPattern( "{s'inscrire,m...@mail.com}", new String[] { "s'inscrire", "m...@mail.com" } ); + checkPattern("project''s info", new String[] {"project'", null, "s info", null}); + checkPattern( + "it''s a question of {chance, http://en.wikipedia.org/wiki/Chance}", + new String[] {"it'", null, "s a question of ", null, "chance", "http://en.wikipedia.org/wiki/Chance"}); + checkPattern("{s'inscrire,m...@mail.com}", new String[] {"s'inscrire", "m...@mail.com"}); // throwing an IllegalArgumentException in case of unmatched quote would avoid the following: - checkPattern( "it's a question of {chance, http://en.wikipedia.org/wiki/Chance}", - new String[] { "it's a question of {chance, http://en.wikipedia.org/wiki/Chance}", null } ); + checkPattern( + "it's a question of {chance, http://en.wikipedia.org/wiki/Chance}", + new String[] {"it's a question of {chance, http://en.wikipedia.org/wiki/Chance}", null}); - checkPattern( "{}test,", new String[] { "", null, "test,", null } ); - checkPattern( "Hi ${name}. How is it going, sir?", new String[] { "Hi ${name}. How is it going, sir?", null } ); + checkPattern("{}test,", new String[] {"", null, "test,", null}); + checkPattern("Hi ${name}. How is it going, sir?", new String[] {"Hi ${name}. How is it going, sir?", null}); // MSHARED-392 multiple links - checkPattern( "{Indiana University Extreme! Lab Software License, vesion 1.1.1," + checkPattern( + "{Indiana University Extreme! Lab Software License, vesion 1.1.1," + "http://www.extreme.indiana.edu/viewcvs/~checkout~/XPP3/java/LICENSE.txt}" + "{Public Domain,http://creativecommons.org/licenses/publicdomain}" + "{Apache Software License, version 1.1,http://www.apache.org/licenses/LICENSE-1.1}", - new String[]{"Indiana University Extreme! Lab Software License, vesion 1.1.1", - "http://www.extreme.indiana.edu/viewcvs/~checkout~/XPP3/java/LICENSE.txt", - "Public Domain", - "http://creativecommons.org/licenses/publicdomain", - "Apache Software License, version 1.1", - "http://www.apache.org/licenses/LICENSE-1.1"}); - - + new String[] { + "Indiana University Extreme! Lab Software License, vesion 1.1.1", + "http://www.extreme.indiana.edu/viewcvs/~checkout~/XPP3/java/LICENSE.txt", + "Public Domain", + "http://creativecommons.org/licenses/publicdomain", + "Apache Software License, version 1.1", + "http://www.apache.org/licenses/LICENSE-1.1" + }); } }