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.tools.plugin.javadoc;
20  
21  import java.util.Objects;
22  import java.util.Optional;
23  import java.util.regex.Matcher;
24  import java.util.regex.Pattern;
25  
26  /**
27   * Describes a code reference used in javadoc tags {@code see}, {@code link} and {@code linkplain}.
28   * The format of the reference given as string is {@code module/package.class#member label}.
29   * Members must be separated with a {@code #} to be detected.
30   * Targets either module, package, class or field/method/constructor in class.
31   * This class does not know whether the second part part refers to a package, class or both,
32   * as they use the same alphabet and separators.
33   * @see <a href="https://docs.oracle.com/en/java/javase/17/docs/specs/javadoc/doc-comment-spec.html#link">link tag specification</a>
34   */
35  public class JavadocReference {
36      private final Optional<String> moduleName;
37  
38      private final Optional<String> packageNameClassName;
39  
40      private final Optional<String> member; // optional, but may appear with both className and packageName being null
41  
42      private final Optional<String> label;
43  
44      /*
45       * Test at https://regex101.com/r/eDzWNx
46       * Captures several groups: module name (1), package name and/or class name (2), member (3), label (4)
47       */
48      private static final Pattern REFERENCE_VALUE_PATTERN =
49              Pattern.compile("^\\s*(?:(.+)/)??([^#\\s/]+)?(?:#([^\\s\\(]+(?:\\([^\\)]*\\))?))?(?: +(.*))?$");
50  
51      private static final int GROUP_INDEX_MODULE = 1;
52  
53      private static final int GROUP_INDEX_PACKAGECLASS = 2;
54  
55      private static final int GROUP_INDEX_MEMBER = 3;
56  
57      private static final int GROUP_INDEX_LABEL = 4;
58  
59      /**
60       *
61       * @param reference the reference value to parse
62       * @return the created {@link JavadocReference}
63       * @throws IllegalArgumentException in case the reference has an invalid format
64       */
65      public static JavadocReference parse(String reference) {
66          Matcher matcher = REFERENCE_VALUE_PATTERN.matcher(reference);
67          if (!matcher.matches()) {
68              throw new IllegalArgumentException("Invalid format of javadoc reference: " + reference);
69          }
70          final Optional<String> moduleName = getOptionalGroup(matcher, GROUP_INDEX_MODULE);
71          final Optional<String> packageNameClassName = getOptionalGroup(matcher, GROUP_INDEX_PACKAGECLASS);
72          final Optional<String> member = getOptionalGroup(matcher, GROUP_INDEX_MEMBER);
73          final Optional<String> label = getOptionalGroup(matcher, GROUP_INDEX_LABEL);
74          return new JavadocReference(moduleName, packageNameClassName, member, label);
75      }
76  
77      private static Optional<String> getOptionalGroup(Matcher matcher, int index) {
78          String group = matcher.group(index);
79          if (group != null && !group.isEmpty()) {
80              return Optional.of(group);
81          } else {
82              return Optional.empty();
83          }
84      }
85  
86      JavadocReference(
87              Optional<String> moduleName,
88              Optional<String> packageNameClassName,
89              Optional<String> member,
90              Optional<String> label) {
91          this.moduleName = moduleName;
92          this.packageNameClassName = packageNameClassName;
93          this.member = member;
94          this.label = label;
95      }
96  
97      public Optional<String> getModuleName() {
98          return moduleName;
99      }
100 
101     /**
102      *
103      * @return a package name, a class name or a package name followed by a class name
104      */
105     public Optional<String> getPackageNameClassName() {
106         return packageNameClassName;
107     }
108 
109     public Optional<String> getMember() {
110         return member;
111     }
112 
113     public Optional<String> getLabel() {
114         return label;
115     }
116 
117     @Override
118     public String toString() {
119         return "JavadocReference [moduleName=" + moduleName + ", packageNameClassName=" + packageNameClassName
120                 + ", member=" + member + ", label=" + label + "]";
121     }
122 
123     @Override
124     public int hashCode() {
125         return Objects.hash(label, member, packageNameClassName, moduleName);
126     }
127 
128     @Override
129     public boolean equals(Object obj) {
130         if (this == obj) {
131             return true;
132         }
133         if (obj == null) {
134             return false;
135         }
136         if (getClass() != obj.getClass()) {
137             return false;
138         }
139         JavadocReference other = (JavadocReference) obj;
140         return Objects.equals(label, other.label)
141                 && Objects.equals(member, other.member)
142                 && Objects.equals(packageNameClassName, other.packageNameClassName)
143                 && Objects.equals(moduleName, other.moduleName);
144     }
145 }