-
Notifications
You must be signed in to change notification settings - Fork 49
[MSITE-1000] Introduce parser configuration parameter #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
kwin
wants to merge
1
commit into
master
Choose a base branch
from
feature/configure-parser-with-plexus-configurator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
src/main/java/org/apache/maven/plugins/site/render/ParserConfiguratorImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| /* | ||
| * 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.plugins.site.render; | ||
|
|
||
| import java.io.Closeable; | ||
| import java.io.IOException; | ||
| import java.nio.file.FileSystem; | ||
| import java.nio.file.FileSystems; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.PathMatcher; | ||
| import java.util.Collection; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.apache.maven.doxia.parser.Parser; | ||
| import org.apache.maven.doxia.siterenderer.ParserConfigurator; | ||
| import org.apache.maven.plugin.descriptor.MojoDescriptor; | ||
| import org.codehaus.plexus.PlexusContainer; | ||
| import org.codehaus.plexus.component.configurator.ComponentConfigurationException; | ||
| import org.codehaus.plexus.component.configurator.ComponentConfigurator; | ||
| import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; | ||
| import org.codehaus.plexus.component.repository.exception.ComponentLookupException; | ||
| import org.codehaus.plexus.configuration.PlexusConfiguration; | ||
|
|
||
| /** | ||
| * Configures a parser based on a {@link PlexusConfiguration} for a particular parser id and optionally matching one of multiple patterns. | ||
| * It internally leverages the {@link ComponentConfigurator} for calling the right methods inside the parser implementation. | ||
| */ | ||
| public class ParserConfiguratorImpl implements ParserConfigurator, Closeable { | ||
|
|
||
| private static final class ParserConfigurationKey { | ||
|
|
||
| ParserConfigurationKey(String parserId, PlexusConfiguration patternsConfiguration) { | ||
| this(parserId, PlexusConfigurationUtils.getStringArrayValues(patternsConfiguration)); | ||
| } | ||
|
|
||
| ParserConfigurationKey(String parserId, Collection<String> patterns) { | ||
| this.parserId = parserId; | ||
| // lazily populate all matchers | ||
| matchers = patterns.stream() | ||
| .map(p -> FileSystems.getDefault().getPathMatcher(p)) | ||
| .collect(Collectors.toList()); | ||
michael-o marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private final String parserId; | ||
|
|
||
| /** | ||
| * List of {@link PathMatcher}s for all of the patterns passed to the constructor | ||
| */ | ||
| private List<PathMatcher> matchers; | ||
|
|
||
| /** | ||
| * Returns {@code true} the given file path matches one of the {@link #patterns} given via {@link #addPattern(String)} | ||
| * @param filePath the file path to check | ||
| * @return {@code true} if the given file path matches at least one of the patterns, {@code false} otherwise. | ||
| * @throws IllegalArgumentException | ||
| * If one of the patterns does not comply with the form: {@code syntax:pattern} | ||
| * @throws java.util.regex.PatternSyntaxException | ||
| * If one of the regex patterns is invalid | ||
| * @throws UnsupportedOperationException | ||
| * If one of the patterns syntax prefix is not known to the implementation | ||
| * @see FileSystem#getPathMatcher(String) | ||
| */ | ||
| public boolean matches(String parserId, Path filePath) { | ||
| if (this.parserId.equals(parserId)) { | ||
| return false; | ||
| } | ||
| if (matchers.isEmpty()) { | ||
| return true; // no patterns mean always match | ||
| } | ||
| return matchers.stream().anyMatch(m -> m.matches(filePath)); | ||
| } | ||
| } | ||
|
|
||
| private final org.codehaus.plexus.classworlds.realm.ClassRealm pluginClassRealm; | ||
| private final PlexusContainer plexusContainer; | ||
| private final ComponentConfigurator componentConfigurator; | ||
|
|
||
| private final Map<ParserConfigurationKey, PlexusConfiguration> parserConfigurations; | ||
|
|
||
| public ParserConfiguratorImpl( | ||
| Map<String, List<PlexusConfiguration>> parserConfigurations, | ||
| PlexusContainer plexusContainer, | ||
| MojoDescriptor mojoDescriptor) | ||
| throws ComponentLookupException { | ||
| this.parserConfigurations = new LinkedHashMap<>(); | ||
| if (parserConfigurations != null) { | ||
| for (Map.Entry<String, List<PlexusConfiguration>> parserConfigurationPerId : | ||
| parserConfigurations.entrySet()) { | ||
| String parserId = parserConfigurationPerId.getKey(); | ||
| for (PlexusConfiguration configuration : parserConfigurationPerId.getValue()) { | ||
| ParserConfigurationKey key = | ||
| new ParserConfigurationKey(parserId, configuration.getChild("patterns")); | ||
| this.parserConfigurations.put(key, configuration); | ||
| } | ||
| } | ||
| } | ||
| pluginClassRealm = mojoDescriptor.getRealm(); | ||
| this.plexusContainer = plexusContainer; | ||
| componentConfigurator = getComponentConfigurator(mojoDescriptor.getComponentConfigurator()); | ||
| } | ||
|
|
||
| ComponentConfigurator getComponentConfigurator(String configuratorId) throws ComponentLookupException { | ||
| // logic copied from | ||
| // https://github.com/apache/maven/blob/267de063eec17111688fd1a27d4e3aae6c8d0c51/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java#L696C9-L700C10 | ||
| if (configuratorId == null || configuratorId.isEmpty()) { | ||
| configuratorId = "basic"; // TODO: support v4 | ||
| } | ||
| return plexusContainer.lookup(ComponentConfigurator.class, configuratorId); | ||
| } | ||
|
|
||
| public void configureParser(PlexusConfiguration configuration, Parser parser) | ||
| throws ComponentConfigurationException { | ||
| componentConfigurator.configureComponent(parser, configuration, pluginClassRealm); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| try { | ||
| plexusContainer.release(componentConfigurator); | ||
| } catch (ComponentLifecycleException e) { | ||
| throw new IOException(e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean configure(String parserId, Path filePath, Parser parser) { | ||
| Optional<PlexusConfiguration> config = parserConfigurations.entrySet().stream() | ||
| .filter(c -> c.getKey().matches(parserId, filePath)) | ||
| .findFirst() | ||
| .map(Map.Entry::getValue); | ||
| config.ifPresent(c -> { | ||
| try { | ||
| configureParser(c, parser); | ||
| } catch (ComponentConfigurationException e) { | ||
| throw new IllegalStateException("Could not configure parser " + parser, e); | ||
| } | ||
| }); | ||
| return config.isPresent(); | ||
| } | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
src/main/java/org/apache/maven/plugins/site/render/PlexusConfigurationUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * 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.plugins.site.render; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import org.codehaus.plexus.configuration.PlexusConfiguration; | ||
|
|
||
| public class PlexusConfigurationUtils { | ||
|
|
||
| private PlexusConfigurationUtils() { | ||
| // not supposed to be instantiated | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves all string values from the children of the given {@link PlexusConfiguration} | ||
| * @param arrayContainer the configuration containing the array container (may be {@code null}) | ||
| * @return the list of string values | ||
| */ | ||
| static List<String> getStringArrayValues(PlexusConfiguration arrayContainer) { | ||
| if (arrayContainer == null) { | ||
| return Collections.emptyList(); | ||
| } | ||
| List<String> stringValues = new ArrayList<>(); | ||
| for (PlexusConfiguration item : arrayContainer.getChildren()) { | ||
| stringValues.add(item.getValue()); | ||
| } | ||
| return stringValues; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.