| 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.description; |
| 23 | |
| 24 | import com.github.javaparser.utils.Pair; |
| 25 | |
| 26 | import java.util.LinkedList; |
| 27 | import java.util.List; |
| 28 | |
| 29 | /** |
| 30 | * A javadoc text, potentially containing inline tags. |
| 31 | * |
| 32 | * For example <code>This class is totally unrelated to {@link com.github.javaparser.Range}</code> |
| 33 | */ |
| 34 | public class JavadocDescription { |
| 35 | |
| 36 | private List<JavadocDescriptionElement> elements; |
| 37 | |
| 38 | public static JavadocDescription parseText(String text) { |
| 39 | JavadocDescription instance = new JavadocDescription(); |
| 40 | int index = 0; |
| 41 | Pair<Integer, Integer> nextInlineTagPos; |
| 42 | while ((nextInlineTagPos = indexOfNextInlineTag(text, index)) != null) { |
| 43 | if (nextInlineTagPos.a != index) { |
| 44 | instance.addElement(new JavadocSnippet(text.substring(index, nextInlineTagPos.a))); |
| 45 | } |
| 46 | instance.addElement(JavadocInlineTag.fromText(text.substring(nextInlineTagPos.a, nextInlineTagPos.b + 1))); |
| 47 | index = nextInlineTagPos.b + 1; |
| 48 | } |
| 49 | if (index < text.length()) { |
| 50 | instance.addElement(new JavadocSnippet(text.substring(index))); |
| 51 | } |
| 52 | return instance; |
| 53 | } |
| 54 | |
| 55 | private static Pair<Integer, Integer> indexOfNextInlineTag(String text, int start) { |
| 56 | int index = text.indexOf("{@", start); |
| 57 | if (index == -1) { |
| 58 | return null; |
| 59 | } |
| 60 | // we are interested only in complete inline tags |
| 61 | int closeIndex = text.indexOf("}", index); |
| 62 | if (closeIndex == -1) { |
| 63 | return null; |
| 64 | } |
| 65 | return new Pair<>(index, closeIndex); |
| 66 | } |
| 67 | |
| 68 | public JavadocDescription() { |
| 69 | elements = new LinkedList<>(); |
| 70 | } |
| 71 | |
| 72 | public JavadocDescription(List<JavadocDescriptionElement> elements) { |
| 73 | this(); |
| 74 | |
| 75 | this.elements.addAll(elements); |
| 76 | } |
| 77 | |
| 78 | public boolean addElement(JavadocDescriptionElement element) { |
| 79 | return this.elements.add(element); |
| 80 | } |
| 81 | |
| 82 | public List<JavadocDescriptionElement> getElements() { |
| 83 | return this.elements; |
| 84 | } |
| 85 | |
| 86 | public String toText() { |
| 87 | StringBuilder sb = new StringBuilder(); |
| 88 | elements.forEach(e -> sb.append(e.toText())); |
| 89 | return sb.toString(); |
| 90 | } |
| 91 | |
| 92 | public boolean isEmpty() { |
| 93 | return toText().isEmpty(); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public boolean equals(Object o) { |
| 98 | if (this == o) return true; |
| 99 | if (o == null || getClass() != o.getClass()) return false; |
| 100 | |
| 101 | JavadocDescription that = (JavadocDescription) o; |
| 102 | |
| 103 | return elements.equals(that.elements); |
| 104 | |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public int hashCode() { |
| 109 | return elements.hashCode(); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public String toString() { |
| 114 | return "JavadocDescription{" + |
| 115 | "elements=" + elements + |
| 116 | '}'; |
| 117 | } |
| 118 | |
| 119 | } |
| 120 |
Members