001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.tools.plugin.javadoc;
020
021import java.util.Objects;
022import java.util.Optional;
023import java.util.regex.Matcher;
024import java.util.regex.Pattern;
025
026/**
027 * Describes a code reference used in javadoc tags {@code see}, {@code link} and {@code linkplain}.
028 * The format of the reference given as string is {@code module/package.class#member label}.
029 * Members must be separated with a {@code #} to be detected.
030 * Targets either module, package, class or field/method/constructor in class.
031 * This class does not know whether the second part part refers to a package, class or both,
032 * as they use the same alphabet and separators.
033 * @see <a href="https://docs.oracle.com/en/java/javase/17/docs/specs/javadoc/doc-comment-spec.html#link">link tag specification</a>
034 */
035public class JavadocReference {
036    private final Optional<String> moduleName;
037
038    private final Optional<String> packageNameClassName;
039
040    private final Optional<String> member; // optional, but may appear with both className and packageName being null
041
042    private final Optional<String> label;
043
044    /*
045     * Test at https://regex101.com/r/eDzWNx
046     * Captures several groups: module name (1), package name and/or class name (2), member (3), label (4)
047     */
048    private static final Pattern REFERENCE_VALUE_PATTERN =
049            Pattern.compile("^\\s*(?:(.+)/)??([^#\\s/]+)?(?:#([^\\s\\(]+(?:\\([^\\)]*\\))?))?(?: +(.*))?$");
050
051    private static final int GROUP_INDEX_MODULE = 1;
052
053    private static final int GROUP_INDEX_PACKAGECLASS = 2;
054
055    private static final int GROUP_INDEX_MEMBER = 3;
056
057    private static final int GROUP_INDEX_LABEL = 4;
058
059    /**
060     *
061     * @param reference the reference value to parse
062     * @return the created {@link JavadocReference}
063     * @throws IllegalArgumentException in case the reference has an invalid format
064     */
065    public static JavadocReference parse(String reference) {
066        Matcher matcher = REFERENCE_VALUE_PATTERN.matcher(reference);
067        if (!matcher.matches()) {
068            throw new IllegalArgumentException("Invalid format of javadoc reference: " + reference);
069        }
070        final Optional<String> moduleName = getOptionalGroup(matcher, GROUP_INDEX_MODULE);
071        final Optional<String> packageNameClassName = getOptionalGroup(matcher, GROUP_INDEX_PACKAGECLASS);
072        final Optional<String> member = getOptionalGroup(matcher, GROUP_INDEX_MEMBER);
073        final Optional<String> label = getOptionalGroup(matcher, GROUP_INDEX_LABEL);
074        return new JavadocReference(moduleName, packageNameClassName, member, label);
075    }
076
077    private static Optional<String> getOptionalGroup(Matcher matcher, int index) {
078        String group = matcher.group(index);
079        if (group != null && !group.isEmpty()) {
080            return Optional.of(group);
081        } else {
082            return Optional.empty();
083        }
084    }
085
086    JavadocReference(
087            Optional<String> moduleName,
088            Optional<String> packageNameClassName,
089            Optional<String> member,
090            Optional<String> label) {
091        this.moduleName = moduleName;
092        this.packageNameClassName = packageNameClassName;
093        this.member = member;
094        this.label = label;
095    }
096
097    public Optional<String> getModuleName() {
098        return moduleName;
099    }
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}