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