|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.maven.plugins.site.render; |
| 20 | + |
| 21 | +import java.io.Closeable; |
| 22 | +import java.io.IOException; |
| 23 | +import java.nio.file.FileSystem; |
| 24 | +import java.nio.file.FileSystems; |
| 25 | +import java.nio.file.Path; |
| 26 | +import java.nio.file.PathMatcher; |
| 27 | +import java.util.Collection; |
| 28 | +import java.util.LinkedHashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Optional; |
| 32 | +import java.util.stream.Collectors; |
| 33 | + |
| 34 | +import org.apache.maven.doxia.parser.Parser; |
| 35 | +import org.apache.maven.doxia.siterenderer.ParserConfigurator; |
| 36 | +import org.apache.maven.plugin.descriptor.MojoDescriptor; |
| 37 | +import org.codehaus.plexus.PlexusContainer; |
| 38 | +import org.codehaus.plexus.component.configurator.ComponentConfigurationException; |
| 39 | +import org.codehaus.plexus.component.configurator.ComponentConfigurator; |
| 40 | +import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; |
| 41 | +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; |
| 42 | +import org.codehaus.plexus.configuration.PlexusConfiguration; |
| 43 | + |
| 44 | +/** |
| 45 | + * Configures a parser based on a {@link PlexusConfiguration} for a particular parser id and optionally matching one of multiple patterns. |
| 46 | + * It internally leverages the {@link ComponentConfigurator} for calling the right methods inside the parser implementation. |
| 47 | + */ |
| 48 | +public class ParserConfiguratorImpl implements ParserConfigurator, Closeable { |
| 49 | + |
| 50 | + private static final class ParserConfigurationKey { |
| 51 | + |
| 52 | + ParserConfigurationKey(String parserId, PlexusConfiguration patternsConfiguration) { |
| 53 | + this(parserId, PlexusConfigurationUtils.getStringArrayValues(patternsConfiguration)); |
| 54 | + } |
| 55 | + |
| 56 | + ParserConfigurationKey(String parserId, Collection<String> patterns) { |
| 57 | + this.parserId = parserId; |
| 58 | + // lazily populate all matchers |
| 59 | + matchers = patterns.stream() |
| 60 | + .map(p -> FileSystems.getDefault().getPathMatcher(p)) |
| 61 | + .collect(Collectors.toList()); |
| 62 | + } |
| 63 | + |
| 64 | + private final String parserId; |
| 65 | + |
| 66 | + /** |
| 67 | + * List of {@link PathMatcher}s for all of the patterns passed to the constructor |
| 68 | + */ |
| 69 | + private List<PathMatcher> matchers; |
| 70 | + |
| 71 | + /** |
| 72 | + * Returns {@code true} the given file path matches one of the {@link #patterns} given via {@link #addPattern(String)} |
| 73 | + * @param filePath the file path to check |
| 74 | + * @return {@code true} if the given file path matches at least one of the patterns, {@code false} otherwise. |
| 75 | + * @throws IllegalArgumentException |
| 76 | + * If one of the patterns does not comply with the form: {@code syntax:pattern} |
| 77 | + * @throws java.util.regex.PatternSyntaxException |
| 78 | + * If one of the regex patterns is invalid |
| 79 | + * @throws UnsupportedOperationException |
| 80 | + * If one of the patterns syntax prefix is not known to the implementation |
| 81 | + * @see FileSystem#getPathMatcher(String) |
| 82 | + */ |
| 83 | + public boolean matches(String parserId, Path filePath) { |
| 84 | + if (this.parserId.equals(parserId)) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + if (matchers.isEmpty()) { |
| 88 | + return true; // no patterns mean always match |
| 89 | + } |
| 90 | + return matchers.stream().anyMatch(m -> m.matches(filePath)); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private final org.codehaus.plexus.classworlds.realm.ClassRealm pluginClassRealm; |
| 95 | + private final PlexusContainer plexusContainer; |
| 96 | + private final ComponentConfigurator componentConfigurator; |
| 97 | + |
| 98 | + private final Map<ParserConfigurationKey, PlexusConfiguration> parserConfigurations; |
| 99 | + |
| 100 | + public ParserConfiguratorImpl( |
| 101 | + Map<String, List<PlexusConfiguration>> parserConfigurations, |
| 102 | + PlexusContainer plexusContainer, |
| 103 | + MojoDescriptor mojoDescriptor) |
| 104 | + throws ComponentLookupException { |
| 105 | + this.parserConfigurations = new LinkedHashMap<>(); |
| 106 | + for (Map.Entry<String, List<PlexusConfiguration>> parserConfigurationPerId : parserConfigurations.entrySet()) { |
| 107 | + String parserId = parserConfigurationPerId.getKey(); |
| 108 | + for (PlexusConfiguration configuration : parserConfigurationPerId.getValue()) { |
| 109 | + ParserConfigurationKey key = new ParserConfigurationKey(parserId, configuration.getChild("patterns")); |
| 110 | + this.parserConfigurations.put(key, configuration); |
| 111 | + } |
| 112 | + } |
| 113 | + pluginClassRealm = mojoDescriptor.getRealm(); |
| 114 | + this.plexusContainer = plexusContainer; |
| 115 | + componentConfigurator = getComponentConfigurator(mojoDescriptor.getComponentConfigurator()); |
| 116 | + } |
| 117 | + |
| 118 | + ComponentConfigurator getComponentConfigurator(String configuratorId) throws ComponentLookupException { |
| 119 | + // logic copied from |
| 120 | + // https://github.com/apache/maven/blob/267de063eec17111688fd1a27d4e3aae6c8d0c51/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java#L696C9-L700C10 |
| 121 | + if (configuratorId == null || configuratorId.isEmpty()) { |
| 122 | + configuratorId = "basic"; // TODO: support v4 |
| 123 | + } |
| 124 | + return plexusContainer.lookup(ComponentConfigurator.class, configuratorId); |
| 125 | + } |
| 126 | + |
| 127 | + public void configureParser(PlexusConfiguration configuration, Parser parser) |
| 128 | + throws ComponentConfigurationException { |
| 129 | + componentConfigurator.configureComponent(parser, configuration, pluginClassRealm); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void close() throws IOException { |
| 134 | + try { |
| 135 | + plexusContainer.release(componentConfigurator); |
| 136 | + } catch (ComponentLifecycleException e) { |
| 137 | + throw new IOException(e); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public boolean configure(String parserId, Path filePath, Parser parser) { |
| 143 | + Optional<PlexusConfiguration> config = parserConfigurations.entrySet().stream() |
| 144 | + .filter(c -> c.getKey().matches(parserId, filePath)) |
| 145 | + .findFirst() |
| 146 | + .map(Map.Entry::getValue); |
| 147 | + config.ifPresent(c -> { |
| 148 | + try { |
| 149 | + configureParser(c, parser); |
| 150 | + } catch (ComponentConfigurationException e) { |
| 151 | + throw new IllegalStateException("Could not configure parser " + parser, e); |
| 152 | + } |
| 153 | + }); |
| 154 | + return config.isPresent(); |
| 155 | + } |
| 156 | +} |
0 commit comments