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