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.BiFunction;
24  
25  import org.apache.maven.model.transform.pull.NodeBufferingParser;
26  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
27  
28  /**
29   * Will apply the version if the dependency is part of the reactor
30   *
31   * @author Robert Scholte
32   * @author Guillaume Nodet
33   * @since 4.0.0
34   */
35  public class ReactorDependencyXMLFilter extends NodeBufferingParser
36  {
37      private final BiFunction<String, String, String> reactorVersionMapper;
38  
39      public ReactorDependencyXMLFilter( XmlPullParser xmlPullParser,
40                                         BiFunction<String, String, String> reactorVersionMapper )
41      {
42          super( xmlPullParser, "dependency" );
43          this.reactorVersionMapper = reactorVersionMapper;
44      }
45  
46      protected void process( List<Event> buffer )
47      {
48          // whiteSpace after <dependency>, to be used to position <version>
49          String dependencyWhitespace = "";
50          boolean hasVersion = false;
51          String groupId = null;
52          String artifactId = null;
53          String tagName = null;
54          for ( int i = 0; i < buffer.size(); i++ )
55          {
56              Event event = buffer.get( i );
57              if ( event.event == START_TAG )
58              {
59                  tagName = event.name;
60                  hasVersion |= "version".equals( tagName );
61              }
62              else if ( event.event == TEXT )
63              {
64                  if ( event.text.matches( "\\s+" ) )
65                  {
66                      if ( dependencyWhitespace.isEmpty() )
67                      {
68                          dependencyWhitespace = event.text;
69                      }
70                  }
71                  else if ( "groupId".equals( tagName ) )
72                  {
73                      groupId = nullSafeAppend( groupId, event.text );
74                  }
75                  else if ( "artifactId".equals( tagName ) )
76                  {
77                      artifactId = nullSafeAppend( artifactId, event.text );
78                  }
79              }
80              else if ( event.event == END_TAG && "dependency".equals( event.name ) )
81              {
82                  String version = reactorVersionMapper.apply( groupId, artifactId  );
83                  if ( !hasVersion && version != null )
84                  {
85                      int pos = buffer.get( i - 1 ).event == TEXT ? i - 1  : i;
86                      Event e = new Event();
87                      e.event = TEXT;
88                      e.text = dependencyWhitespace;
89                      buffer.add( pos++, e );
90                      e = new Event();
91                      e.event = START_TAG;
92                      e.namespace = buffer.get( 0 ).namespace;
93                      e.prefix = buffer.get( 0 ).prefix;
94                      e.name = "version";
95                      buffer.add( pos++, e );
96                      e = new Event();
97                      e.event = TEXT;
98                      e.text = version;
99                      buffer.add( pos++, e );
100                     e = new Event();
101                     e.event = END_TAG;
102                     e.name = "version";
103                     e.namespace = buffer.get( 0 ).namespace;
104                     e.prefix = buffer.get( 0 ).prefix;
105                     buffer.add( pos++, e );
106                 }
107                 break;
108             }
109         }
110         buffer.forEach( this::pushEvent );
111     }
112 
113 }