001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.javadoc;
021
022/**
023 * Used to keep track of a tag and the text that follows it.
024 *
025 */
026class HtmlTag {
027
028    /** The maximum length of text to display with this tag. */
029    private static final int MAX_TEXT_LEN = 60;
030
031    /** The HTML tag name. */
032    private final String id;
033
034    /** The line number in the source file where this tag was found. */
035    private final int lineNo;
036
037    /** The position within the line where this tag was found. */
038    private final int position;
039
040    /** The comment line of text where this tag appears. */
041    private final String text;
042
043    /** If this tag is self-closed. */
044    private final boolean closedTag;
045
046    /** If the tag is incomplete. */
047    private final boolean incompleteTag;
048
049    /**
050     * Construct the HtmlTag.
051     *
052     * @param id the HTML tag name.
053     * @param lineNo the source line number of this tag.
054     * @param position the position within the text of this tag.
055     * @param closedTag if this tag is self-closed (XHTML style)
056     * @param incomplete is the tag is incomplete.
057     * @param text the line of comment text for this tag.
058     */
059    /* package */ HtmlTag(String id, int lineNo, int position, boolean closedTag,
060            boolean incomplete, String text) {
061        this.id = id;
062        this.lineNo = lineNo;
063        this.position = position;
064        this.text = text;
065        this.closedTag = closedTag;
066        incompleteTag = incomplete;
067    }
068
069    /**
070     * Returns the id (name) of this tag.
071     *
072     * @return a String id.
073     */
074    public String getId() {
075        return id;
076    }
077
078    /**
079     * Indicates if this tag is a close (end) tag.
080     *
081     * @return {@code true} is this is a close tag.
082     */
083    public boolean isCloseTag() {
084        return position != text.length() - 1 && text.charAt(position + 1) == '/';
085    }
086
087    /**
088     * Indicates if this tag is a self-closed XHTML style.
089     *
090     * @return {@code true} is this is a self-closed tag.
091     */
092    public boolean isClosedTag() {
093        return closedTag;
094    }
095
096    /**
097     * Indicates if this tag is incomplete (has no close >).
098     *
099     * @return {@code true} if the tag is incomplete.
100     */
101    public boolean isIncompleteTag() {
102        return incompleteTag;
103    }
104
105    /**
106     * Returns the source line number where this tag was found.
107     * Used for displaying a Checkstyle violation.
108     *
109     * @return an int line number.
110     */
111    public int getLineNo() {
112        return lineNo;
113    }
114
115    /**
116     * Returns the position with in the comment line where this tag
117     * was found.  Used for displaying a Checkstyle violation.
118     *
119     * @return an int relative to zero.
120     */
121    public int getPosition() {
122        return position;
123    }
124
125    @Override
126    public String toString() {
127        return "HtmlTag[id='" + id + '\''
128                + ", lineNo=" + lineNo
129                + ", position=" + position
130                + ", text='" + text + '\''
131                + ", closedTag=" + closedTag
132                + ", incompleteTag=" + incompleteTag + ']';
133    }
134
135    /**
136     * Returns the comment line of text where this tag appears.
137     *
138     * @return text of the tag
139     */
140    public String getText() {
141        final int startOfText = position;
142        final int endOfText = Math.min(startOfText + MAX_TEXT_LEN, text.length());
143        return text.substring(startOfText, endOfText);
144    }
145
146}