JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/javadoc/description/JavadocDescription.java
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
22package com.github.javaparser.javadoc.description;
23
24import com.github.javaparser.utils.Pair;
25
26import java.util.LinkedList;
27import 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 */
34public class JavadocDescription {
35
36    private List<JavadocDescriptionElementelements;
37
38    public static JavadocDescription parseText(String text) {
39        JavadocDescription instance = new JavadocDescription();
40        int index = 0;
41        Pair<IntegerIntegernextInlineTagPos;
42        while ((nextInlineTagPos = indexOfNextInlineTag(textindex)) != null) {
43            if (nextInlineTagPos.a != index) {
44                instance.addElement(new JavadocSnippet(text.substring(indexnextInlineTagPos.a)));
45            }
46            instance.addElement(JavadocInlineTag.fromText(text.substring(nextInlineTagPos.anextInlineTagPos.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<IntegerIntegerindexOfNextInlineTag(String textint 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<>(indexcloseIndex);
66    }
67
68    public JavadocDescription() {
69        elements = new LinkedList<>();
70    }
71
72    public JavadocDescription(List<JavadocDescriptionElementelements) {
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<JavadocDescriptionElementgetElements() {
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 = (JavadocDescriptiono;
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
MembersX
JavadocDescription:parseText
JavadocDescription:indexOfNextInlineTag
JavadocDescription:equals
JavadocDescription:parseText:Block:index
JavadocDescription:indexOfNextInlineTag:Block:closeIndex
JavadocDescription:toText:Block:sb
JavadocDescription:JavadocDescription
JavadocDescription:parseText:Block:instance
JavadocDescription:equals:Block:that
JavadocDescription:hashCode
JavadocDescription:toString
JavadocDescription:indexOfNextInlineTag:Block:index
JavadocDescription:getElements
JavadocDescription:addElement
JavadocDescription:parseText:Block:nextInlineTagPos
JavadocDescription:elements
JavadocDescription:toText
JavadocDescription:isEmpty
Members
X