Skip to content

Commit

Permalink
Refactor SiteConversionConfigurationParser to reduce duplication and …
Browse files Browse the repository at this point in the history
…return only '<asciidoc>' configuration always
  • Loading branch information
abelsromero committed Nov 9, 2024
1 parent a92cabc commit d78dc28
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void parse(Reader reader, Sink sink, String reference) throws ParseExcept
}

final SiteConversionConfiguration conversionConfig = siteConfigParser.processAsciiDocConfig(mavenProject, ROLE_HINT);
final Xpp3Dom siteConfig = conversionConfig.getSiteConfig();
final Xpp3Dom asciidocConfig = conversionConfig.getAsciidocConfig();
final File siteDirectory = conversionConfig.getSiteBaseDir();

// Doxia handles a single instance of this class and invokes it multiple times.
Expand All @@ -86,7 +86,7 @@ public void parse(Reader reader, Sink sink, String reference) throws ParseExcept
requireLibrary(asciidoctor, require);
}

final LogHandler logHandler = logHandlerFactory.getConfiguration(siteConfig);
final LogHandler logHandler = logHandlerFactory.getConfiguration(asciidocConfig);
final MemoryLogHandler memoryLogHandler = logHandlerFactory.create(asciidoctor, siteDirectory, logger);

final Result headerMetadata = siteConverter.process(asciidoctor, source, conversionConfig.getOptions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
*/
public class LogHandlerFactory {

public LogHandler getConfiguration(Xpp3Dom siteConfig) {
Xpp3Dom asciidoc = siteConfig == null ? null : siteConfig.getChild("asciidoc");
return new SiteLogHandlerDeserializer().deserialize(asciidoc);
public LogHandler getConfiguration(Xpp3Dom asciidocConfig) {
return new SiteLogHandlerDeserializer().deserialize(asciidocConfig);
}

public MemoryLogHandler create(Asciidoctor asciidoctor, File siteDirectory, Logger logger) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@
import org.asciidoctor.Options;
import org.codehaus.plexus.util.xml.Xpp3Dom;

/**
* @author abelsromero
* @since 3.1.1
*/
public final class SiteConversionConfiguration {

private final Xpp3Dom siteConfig;
private final Xpp3Dom asciidocConfig;
private final File siteBaseDir;
private final Options options;
private final List<String> requires;

SiteConversionConfiguration(Xpp3Dom siteConfig,
SiteConversionConfiguration(Xpp3Dom asciidocConfig,
File siteBaseDir,
Options options,
List<String> requires) {
this.siteConfig = siteConfig;
this.asciidocConfig = asciidocConfig;
this.siteBaseDir = siteBaseDir;
this.options = options;
this.requires = requires;
Expand All @@ -27,8 +31,8 @@ public File getSiteBaseDir() {
return siteBaseDir;
}

public Xpp3Dom getSiteConfig() {
return siteConfig;
public Xpp3Dom getAsciidocConfig() {
return asciidocConfig;
}

public Options getOptions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -40,10 +41,6 @@ public SiteConversionConfigurationParser(SiteBaseDirResolver siteBaseDirResolver
this.siteBaseDirResolver = siteBaseDirResolver;
}

Xpp3Dom getSiteConfig(MavenProject project) {
return project.getGoalConfiguration("org.apache.maven.plugins", "maven-site-plugin", "site", "site");
}

public SiteConversionConfiguration processAsciiDocConfig(MavenProject mavenProject, String roleHint) {

final AttributesBuilder presetAttributes = defaultAttributes();
Expand All @@ -53,17 +50,13 @@ public SiteConversionConfiguration processAsciiDocConfig(MavenProject mavenProje
final File siteDir = siteBaseDirResolver.resolveBaseDir(mavenProject.getBasedir(), getSiteConfig(mavenProject));
final OptionsBuilder presetOptions = defaultOptions(siteDir, roleHint);

final Xpp3Dom siteConfig = getSiteConfig(mavenProject);
if (siteConfig == null) {
final Options options = presetOptions.attributes(attributes).build();
// TODO refactor this "always null"
return new SiteConversionConfiguration(siteConfig, siteDir, options, Collections.emptyList());
}
final Xpp3Dom asciidocConfig = Optional.ofNullable(getSiteConfig(mavenProject))
.map(node -> node.getChild("asciidoc"))
.orElse(null);

final Xpp3Dom asciidocConfig = siteConfig.getChild("asciidoc");
if (asciidocConfig == null) {
final Options options = presetOptions.attributes(attributes).build();
return new SiteConversionConfiguration(siteConfig, siteDir, options, Collections.emptyList());
return new SiteConversionConfiguration(null, siteDir, options, Collections.emptyList());
}

final List<String> gemsToRequire = new ArrayList<>();
Expand Down Expand Up @@ -108,7 +101,11 @@ public SiteConversionConfiguration processAsciiDocConfig(MavenProject mavenProje
}

final Options options = presetOptions.attributes(attributes).build();
return new SiteConversionConfiguration(siteConfig, siteDir, options, gemsToRequire);
return new SiteConversionConfiguration(asciidocConfig, siteDir, options, gemsToRequire);
}

private Xpp3Dom getSiteConfig(MavenProject project) {
return project.getGoalConfiguration("org.apache.maven.plugins", "maven-site-plugin", "site", "site");
}

private File resolveProjectDir(MavenProject project, String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void should_return_default_configuration_when_site_xml_is_null() {

// then
assertThat(configuration.getSiteBaseDir().getPath()).endsWith(defaultSiteSources());
assertThat(configuration.getSiteConfig()).isNull();
assertThat(configuration.getAsciidocConfig()).isNull();
assertContainsDefaultOptions(configuration);
assertContainsDefaultAttributes(configuration);
assertThat(configuration.getRequires()).isEmpty();
Expand All @@ -55,7 +55,7 @@ void should_return_default_configuration_when_asciidoc_xml_is_null() {

// then
assertThat(configuration.getSiteBaseDir().getPath()).endsWith(defaultSiteSources());
assertThat(configuration.getSiteConfig()).isNotNull();
assertThat(configuration.getAsciidocConfig()).isNull();
final Map<String, Object> optionsMap = assertContainsDefaultAttributes(configuration);
assertThat((String) optionsMap.get(BACKEND)).isEqualTo("xhtml");
assertThat((String) optionsMap.get(BASEDIR)).endsWith(defaultPluginSources());
Expand Down Expand Up @@ -86,7 +86,7 @@ void should_return_simple_single_requires() {

// then
assertThat(configuration.getSiteBaseDir().getPath()).endsWith(defaultSiteSources());
assertThat(configuration.getSiteConfig()).isNotNull();
assertThat(configuration.getAsciidocConfig()).isNotNull();
assertContainsDefaultOptions(configuration);
assertContainsDefaultAttributes(configuration);
assertThat(configuration.getRequires())
Expand All @@ -107,7 +107,7 @@ void should_return_multiple_requires() {

// then
assertThat(configuration.getSiteBaseDir().getPath()).endsWith(defaultSiteSources());
assertThat(configuration.getSiteConfig()).isNotNull();
assertThat(configuration.getAsciidocConfig()).isNotNull();
assertContainsDefaultOptions(configuration);
assertContainsDefaultAttributes(configuration);
assertThat(configuration.getRequires())
Expand All @@ -128,7 +128,7 @@ void should_return_multiple_requires_when_defined_in_single_element() {

// then
assertThat(configuration.getSiteBaseDir().getPath()).endsWith(defaultSiteSources());
assertThat(configuration.getSiteConfig()).isNotNull();
assertThat(configuration.getAsciidocConfig()).isNotNull();
assertContainsDefaultOptions(configuration);
assertContainsDefaultAttributes(configuration);
assertThat(configuration.getRequires())
Expand All @@ -149,7 +149,7 @@ void should_remove_empty_and_blank_requires() {

// then
assertThat(configuration.getSiteBaseDir().getPath()).endsWith(defaultSiteSources());
assertThat(configuration.getSiteConfig()).isNotNull();
assertThat(configuration.getAsciidocConfig()).isNotNull();
assertContainsDefaultOptions(configuration);
assertContainsDefaultAttributes(configuration);
assertThat(configuration.getRequires())
Expand All @@ -173,7 +173,7 @@ void should_return_attributes() {

// then
assertThat(configuration.getSiteBaseDir().getPath()).endsWith(defaultSiteSources());
assertThat(configuration.getSiteConfig()).isNotNull();
assertThat(configuration.getAsciidocConfig()).isNotNull();
assertContainsDefaultOptions(configuration);
assertThat(configuration.getRequires()).isEmpty();
Map attributes = (Map) configuration.getOptions()
Expand Down Expand Up @@ -203,7 +203,7 @@ void should_map_null_attributes_as_empty_string() {

// then
assertThat(configuration.getSiteBaseDir().getPath()).endsWith(defaultSiteSources());
assertThat(configuration.getSiteConfig()).isNotNull();
assertThat(configuration.getAsciidocConfig()).isNotNull();
assertContainsDefaultOptions(configuration);
assertThat(configuration.getRequires()).isEmpty();
Map attributes = (Map) configuration.getOptions()
Expand Down Expand Up @@ -231,7 +231,7 @@ void should_map_true_boolean_attribute_as_empty_string_value() {

// then
assertThat(configuration.getSiteBaseDir().getPath()).endsWith(defaultSiteSources());
assertThat(configuration.getSiteConfig()).isNotNull();
assertThat(configuration.getAsciidocConfig()).isNotNull();
assertContainsDefaultOptions(configuration);
assertThat(configuration.getRequires()).isEmpty();
Map attributes = (Map) configuration
Expand Down Expand Up @@ -259,7 +259,7 @@ void should_map_false_boolean_attribute_as_null_value() {

// then
assertThat(configuration.getSiteBaseDir().getPath()).endsWith(defaultSiteSources());
assertThat(configuration.getSiteConfig()).isNotNull();
assertThat(configuration.getAsciidocConfig()).isNotNull();
assertContainsDefaultOptions(configuration);
assertThat(configuration.getRequires()).isEmpty();
Map attributes = (Map) configuration
Expand Down Expand Up @@ -289,7 +289,7 @@ void should_return_template_dirs_when_defined_as_templateDirs_dir() {

// then
assertThat(configuration.getSiteBaseDir().getPath()).endsWith(defaultSiteSources());
assertThat(configuration.getSiteConfig()).isNotNull();
assertThat(configuration.getAsciidocConfig()).isNotNull();
assertContainsDefaultAttributes(configuration);
assertThat(configuration.getRequires()).isEmpty();

Expand Down Expand Up @@ -318,7 +318,7 @@ void should_return_template_dirs_when_defined_as_template_dirs_dir() {

// then
assertThat(configuration.getSiteBaseDir().getPath()).endsWith(defaultSiteSources());
assertThat(configuration.getSiteConfig()).isNotNull();
assertThat(configuration.getAsciidocConfig()).isNotNull();
assertContainsDefaultAttributes(configuration);
assertThat(configuration.getRequires()).isEmpty();

Expand Down Expand Up @@ -347,7 +347,7 @@ void should_not_return_empty_template_dirs() {

// then: BASE_DIR option is not even added
assertThat(configuration.getSiteBaseDir().getPath()).endsWith(defaultSiteSources());
assertThat(configuration.getSiteConfig()).isNotNull();
assertThat(configuration.getAsciidocConfig()).isNotNull();
assertContainsDefaultOptions(configuration);
assertContainsDefaultAttributes(configuration);
assertThat(configuration.getRequires()).isEmpty();
Expand All @@ -366,7 +366,7 @@ void should_return_baseDir_dirs_when_defined_as_template_dirs_dir() {

// then
assertThat(configuration.getSiteBaseDir().getPath()).endsWith(defaultSiteSources());
assertThat(configuration.getSiteConfig()).isNotNull();
assertThat(configuration.getAsciidocConfig()).isNotNull();
assertContainsDefaultAttributes(configuration);
assertThat(configuration.getRequires()).isEmpty();

Expand Down Expand Up @@ -395,7 +395,7 @@ void should_return_any_configuration_inside_asciidoc_node_as_option() {

// then
assertThat(configuration.getSiteBaseDir().getPath()).endsWith(defaultSiteSources());
assertThat(configuration.getSiteConfig()).isNotNull();
assertThat(configuration.getAsciidocConfig()).isNotNull();
assertContainsDefaultAttributes(configuration);
assertThat(configuration.getRequires()).isEmpty();

Expand Down Expand Up @@ -424,7 +424,7 @@ void should_return_and_format_any_maven_project_property_as_attribute_when_site_

// then
assertThat(configuration.getSiteBaseDir().getPath()).endsWith(defaultSiteSources());
assertThat(configuration.getSiteConfig()).isNull();
assertThat(configuration.getAsciidocConfig()).isNull();
assertContainsDefaultOptions(configuration);
assertThat(configuration.getRequires()).isEmpty();

Expand Down Expand Up @@ -455,7 +455,7 @@ void should_return_and_format_any_maven_project_property_as_attribute_when_site_

// then
assertThat(configuration.getSiteBaseDir().getPath()).endsWith(defaultSiteSources());
assertThat(configuration.getSiteConfig()).isNotNull();
assertThat(configuration.getAsciidocConfig()).isNotNull();
assertContainsDefaultOptions(configuration);
assertThat(configuration.getRequires()).isEmpty();

Expand Down

0 comments on commit d78dc28

Please sign in to comment.