View Javadoc
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.model.transform;
20  
21  import java.nio.file.Path;
22  import java.util.Optional;
23  import java.util.function.BiFunction;
24  import java.util.function.Function;
25  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
26  
27  /**
28   * Base implementation for providing the BuildToRawPomXML.
29   *
30   * @author Robert Scholte
31   * @since 4.0.0
32   */
33  public class BuildToRawPomXMLFilterFactory {
34      private final boolean consume;
35  
36      public BuildToRawPomXMLFilterFactory() {
37          this(false);
38      }
39  
40      public BuildToRawPomXMLFilterFactory(boolean consume) {
41          this.consume = consume;
42      }
43  
44      /**
45       *
46       * @param projectFile will be used by ConsumerPomXMLFilter to get the right filter
47       */
48      public final XmlPullParser get(XmlPullParser orgParser, Path projectFile) {
49  
50          // Ensure that xs:any elements aren't touched by next filters
51          XmlPullParser parser = orgParser instanceof FastForwardFilter ? orgParser : new FastForwardFilter(orgParser);
52  
53          if (getDependencyKeyToVersionMapper() != null) {
54              parser = new ReactorDependencyXMLFilter(parser, getDependencyKeyToVersionMapper());
55          }
56  
57          if (getRelativePathMapper() != null) {
58              parser = new ParentXMLFilter(parser, getRelativePathMapper(), projectFile.getParent());
59          }
60  
61          CiFriendlyXMLFilter ciFriendlyFilter = new CiFriendlyXMLFilter(parser, consume);
62          getChangelist().ifPresent(ciFriendlyFilter::setChangelist);
63          getRevision().ifPresent(ciFriendlyFilter::setRevision);
64          getSha1().ifPresent(ciFriendlyFilter::setSha1);
65          parser = ciFriendlyFilter;
66  
67          return parser;
68      }
69  
70      /**
71       * @return the mapper or {@code null} if relativePaths don't need to be mapped
72       */
73      protected Function<Path, Optional<RelativeProject>> getRelativePathMapper() {
74          return null;
75      }
76  
77      protected BiFunction<String, String, String> getDependencyKeyToVersionMapper() {
78          return null;
79      }
80  
81      // getters for the 3 magic properties of CIFriendly versions ( https://maven.apache.org/maven-ci-friendly.html )
82  
83      protected Optional<String> getChangelist() {
84          return Optional.empty();
85      }
86  
87      protected Optional<String> getRevision() {
88          return Optional.empty();
89      }
90  
91      protected Optional<String> getSha1() {
92          return Optional.empty();
93      }
94  }