View Javadoc
1   package org.apache.maven.plugin.jdeps.consumers;
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.HashMap;
23  import java.util.Map;
24  import java.util.regex.Matcher;
25  import java.util.regex.Pattern;
26  
27  import org.codehaus.plexus.util.cli.CommandLineUtils;
28  import org.codehaus.plexus.util.cli.StreamConsumer;
29  
30  /**
31   * Consumes the output of the jdeps tool
32   *  
33   * @author Robert Scholte
34   *
35   */
36  public class JDepsConsumer
37      extends CommandLineUtils.StringStreamConsumer
38      implements StreamConsumer
39  {
40  
41      /**
42       * JDK8: JDK internal API (rt.jar)
43       * JDK9: JDK internal API (java.base)
44       */
45      private static final Pattern JDKINTERNALAPI = Pattern.compile( "\\s+->\\s([a-z\\.]+)\\s+(JDK internal API .+)" );
46  
47      /**
48       * <dl>
49       *  <dt>key</dt><dd>The offending package</dd>
50       *  <dt>value</dt><dd>Offending details</dd>
51       * </dl>
52       */
53      private Map<String, String> offendingPackages = new HashMap<String, String>();
54  
55      private static final Pattern PROFILE = Pattern.compile( "\\s+->\\s([a-z\\.]+)\\s+(\\S+)" );
56  
57      /**
58       * <dl>
59       *  <dt>key</dt><dd>The package</dd>
60       *  <dt>value</dt><dd>The profile</dd>
61       * </dl>
62       */
63      private Map<String, String> profiles = new HashMap<String, String>();
64  
65      
66      public void consumeLine( String line )
67      {
68          super.consumeLine( line );
69          Matcher matcher;
70          
71          matcher = JDKINTERNALAPI.matcher( line );
72          if ( matcher.matches() )
73          {
74              offendingPackages.put( matcher.group( 1 ), matcher.group( 2 ) );
75              return;
76          }
77          
78          matcher = PROFILE.matcher( line );
79          if ( matcher.matches() )
80          {
81              profiles.put( matcher.group( 1 ), matcher.group( 2 ) );
82              return;
83          }
84      }
85  
86      public Map<String, String> getOffendingPackages()
87      {
88          return offendingPackages;
89      }
90      
91      public Map<String, String> getProfiles()
92      {
93          return profiles;
94      }
95  
96  }