View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.javadoc;
21  
22  /**
23   * Used to keep track of a tag and the text that follows it.
24   *
25   */
26  class HtmlTag {
27  
28      /** The maximum length of text to display with this tag. */
29      private static final int MAX_TEXT_LEN = 60;
30  
31      /** The HTML tag name. */
32      private final String id;
33  
34      /** The line number in the source file where this tag was found. */
35      private final int lineNo;
36  
37      /** The position within the line where this tag was found. */
38      private final int position;
39  
40      /** The comment line of text where this tag appears. */
41      private final String text;
42  
43      /** If this tag is self-closed. */
44      private final boolean closedTag;
45  
46      /** If the tag is incomplete. */
47      private final boolean incompleteTag;
48  
49      /**
50       * Construct the HtmlTag.
51       *
52       * @param id the HTML tag name.
53       * @param lineNo the source line number of this tag.
54       * @param position the position within the text of this tag.
55       * @param closedTag if this tag is self-closed (XHTML style)
56       * @param incomplete is the tag is incomplete.
57       * @param text the line of comment text for this tag.
58       */
59      /* package */ HtmlTag(String id, int lineNo, int position, boolean closedTag,
60              boolean incomplete, String text) {
61          this.id = id;
62          this.lineNo = lineNo;
63          this.position = position;
64          this.text = text;
65          this.closedTag = closedTag;
66          incompleteTag = incomplete;
67      }
68  
69      /**
70       * Returns the id (name) of this tag.
71       *
72       * @return a String id.
73       */
74      public String getId() {
75          return id;
76      }
77  
78      /**
79       * Indicates if this tag is a close (end) tag.
80       *
81       * @return {@code true} is this is a close tag.
82       */
83      public boolean isCloseTag() {
84          return position != text.length() - 1 && text.charAt(position + 1) == '/';
85      }
86  
87      /**
88       * Indicates if this tag is a self-closed XHTML style.
89       *
90       * @return {@code true} is this is a self-closed tag.
91       */
92      public boolean isClosedTag() {
93          return closedTag;
94      }
95  
96      /**
97       * Indicates if this tag is incomplete (has no close >).
98       *
99       * @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 }