View Javadoc
1   package org.apache.maven.model.transform;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.List;
23  import java.util.function.Function;
24  
25  import org.apache.maven.model.transform.pull.NodeBufferingParser;
26  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
27  
28  /**
29   * Resolves all ci-friendly properties occurrences between version-tags
30   *
31   * @author Robert Scholte
32   * @author Guillaume Nodet
33   * @since 4.0.0
34   */
35  class CiFriendlyXMLFilter
36      extends NodeBufferingParser
37  {
38      private final boolean replace;
39  
40      private Function<String, String> replaceChain = Function.identity();
41  
42      CiFriendlyXMLFilter( XmlPullParser xmlPullParser, boolean replace )
43      {
44          super( xmlPullParser, "version" );
45          this.replace = replace;
46      }
47  
48      public CiFriendlyXMLFilter setChangelist( String changelist )
49      {
50          replaceChain = replaceChain.andThen( t -> t.replace( "${changelist}", changelist ) );
51          return this;
52      }
53  
54      public CiFriendlyXMLFilter setRevision( String revision )
55      {
56          replaceChain = replaceChain.andThen( t -> t.replace( "${revision}", revision ) );
57          return this;
58      }
59  
60      public CiFriendlyXMLFilter setSha1( String sha1 )
61      {
62          replaceChain = replaceChain.andThen( t -> t.replace( "${sha1}", sha1 ) );
63          return this;
64      }
65  
66      /**
67       * @return {@code true} is any of the ci properties is set, otherwise {@code false}
68       */
69      public boolean isSet()
70      {
71          return !replaceChain.equals( Function.identity() );
72      }
73  
74      @Override
75      protected void process( List<Event> buffer )
76      {
77          for ( Event event : buffer )
78          {
79              if ( event.event == TEXT && replace && event.text.contains( "${" ) )
80              {
81                  event.text = replaceChain.apply( event.text );
82              }
83              pushEvent( event );
84          }
85      }
86  
87  }