| 1 | /* |
|---|---|
| 2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
| 3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
| 4 | * |
| 5 | * This file is part of JavaParser. |
| 6 | * |
| 7 | * JavaParser can be used either under the terms of |
| 8 | * a) the GNU Lesser General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * b) the terms of the Apache License |
| 12 | * |
| 13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
| 14 | * LICENCE.APACHE. Please refer to those files for details. |
| 15 | * |
| 16 | * JavaParser is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU Lesser General Public License for more details. |
| 20 | */ |
| 21 | |
| 22 | package com.github.javaparser.javadoc; |
| 23 | |
| 24 | import com.github.javaparser.javadoc.description.JavadocDescription; |
| 25 | |
| 26 | import java.util.Optional; |
| 27 | |
| 28 | import static com.github.javaparser.utils.Utils.nextWord; |
| 29 | import static com.github.javaparser.utils.Utils.screamingToCamelCase; |
| 30 | |
| 31 | /** |
| 32 | * A block tag. |
| 33 | * <p> |
| 34 | * Typically they are found at the end of Javadoc comments. |
| 35 | * <p> |
| 36 | * Examples: |
| 37 | * {@code @see AnotherClass} |
| 38 | * {@code @since v0.0.1} |
| 39 | * {@code @author Jim O'Java} |
| 40 | */ |
| 41 | public class JavadocBlockTag { |
| 42 | |
| 43 | /** |
| 44 | * The type of tag: it could either correspond to a known tag (param, return, etc.) or represent |
| 45 | * an unknown tag. |
| 46 | */ |
| 47 | public enum Type { |
| 48 | AUTHOR, |
| 49 | DEPRECATED, |
| 50 | EXCEPTION, |
| 51 | PARAM, |
| 52 | RETURN, |
| 53 | SEE, |
| 54 | SERIAL, |
| 55 | SERIAL_DATA, |
| 56 | SERIAL_FIELD, |
| 57 | SINCE, |
| 58 | THROWS, |
| 59 | VERSION, |
| 60 | UNKNOWN; |
| 61 | |
| 62 | Type() { |
| 63 | this.keyword = screamingToCamelCase(name()); |
| 64 | } |
| 65 | |
| 66 | private String keyword; |
| 67 | |
| 68 | boolean hasName() { |
| 69 | return this == PARAM || this == EXCEPTION || this == THROWS; |
| 70 | } |
| 71 | |
| 72 | static Type fromName(String tagName) { |
| 73 | for (Type t : Type.values()) { |
| 74 | if (t.keyword.equals(tagName)) { |
| 75 | return t; |
| 76 | } |
| 77 | } |
| 78 | return UNKNOWN; |
| 79 | } |
| 80 | |
| 81 | } |
| 82 | |
| 83 | private Type type; |
| 84 | private JavadocDescription content; |
| 85 | private Optional<String> name = Optional.empty(); |
| 86 | private String tagName; |
| 87 | |
| 88 | public JavadocBlockTag(Type type, String content) { |
| 89 | this.type = type; |
| 90 | this.tagName = type.keyword; |
| 91 | if (type.hasName()) { |
| 92 | this.name = Optional.of(nextWord(content)); |
| 93 | content = content.substring(this.name.get().length()).trim(); |
| 94 | } |
| 95 | this.content = JavadocDescription.parseText(content); |
| 96 | } |
| 97 | |
| 98 | public JavadocBlockTag(String tagName, String content) { |
| 99 | this(Type.fromName(tagName), content); |
| 100 | this.tagName = tagName; |
| 101 | } |
| 102 | |
| 103 | public static JavadocBlockTag createParamBlockTag(String paramName, String content) { |
| 104 | return new JavadocBlockTag(Type.PARAM, paramName + " " + content); |
| 105 | } |
| 106 | |
| 107 | public Type getType() { |
| 108 | return type; |
| 109 | } |
| 110 | |
| 111 | public JavadocDescription getContent() { |
| 112 | return content; |
| 113 | } |
| 114 | |
| 115 | public Optional<String> getName() { |
| 116 | return name; |
| 117 | } |
| 118 | |
| 119 | public String getTagName() { |
| 120 | return tagName; |
| 121 | } |
| 122 | |
| 123 | public String toText() { |
| 124 | StringBuilder sb = new StringBuilder(); |
| 125 | sb.append("@"); |
| 126 | sb.append(tagName); |
| 127 | name.ifPresent(s -> sb.append(" ").append(s)); |
| 128 | if (!content.isEmpty()) { |
| 129 | sb.append(" "); |
| 130 | sb.append(content.toText()); |
| 131 | } |
| 132 | return sb.toString(); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public boolean equals(Object o) { |
| 137 | if (this == o) return true; |
| 138 | if (o == null || getClass() != o.getClass()) return false; |
| 139 | |
| 140 | JavadocBlockTag that = (JavadocBlockTag) o; |
| 141 | |
| 142 | if (type != that.type) return false; |
| 143 | if (!content.equals(that.content)) return false; |
| 144 | return name.equals(that.name); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public int hashCode() { |
| 149 | int result = type.hashCode(); |
| 150 | result = 31 * result + content.hashCode(); |
| 151 | result = 31 * result + name.hashCode(); |
| 152 | return result; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public String toString() { |
| 157 | return "JavadocBlockTag{" + |
| 158 | "type=" + type + |
| 159 | ", content='" + content + '\'' + |
| 160 | ", name=" + name + |
| 161 | '}'; |
| 162 | } |
| 163 | } |
| 164 |
Members