1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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       * @param id the HTML tag name.
52       * @param lineNo the source line number of this tag.
53       * @param position the position within the text of this tag.
54       * @param closedTag if this tag is self-closed (XHTML style)
55       * @param incomplete is the tag is incomplete.
56       * @param text the line of comment text for this tag.
57       */
58      /* package */ HtmlTag(String id, int lineNo, int position, boolean closedTag,
59              boolean incomplete, String text) {
60          this.id = id;
61          this.lineNo = lineNo;
62          this.position = position;
63          this.text = text;
64          this.closedTag = closedTag;
65          incompleteTag = incomplete;
66      }
67  
68      /**
69       * Returns the id (name) of this tag.
70       * @return a String id.
71       */
72      public String getId() {
73          return id;
74      }
75  
76      /**
77       * Indicates if this tag is a close (end) tag.
78       * @return {@code true} is this is a close tag.
79       */
80      public boolean isCloseTag() {
81          return position != text.length() - 1 && text.charAt(position + 1) == '/';
82      }
83  
84      /**
85       * Indicates if this tag is a self-closed XHTML style.
86       * @return {@code true} is this is a self-closed tag.
87       */
88      public boolean isClosedTag() {
89          return closedTag;
90      }
91  
92      /**
93       * Indicates if this tag is incomplete (has no close >).
94       * @return {@code true} if the tag is incomplete.
95       */
96      public boolean isIncompleteTag() {
97          return incompleteTag;
98      }
99  
100     /**
101      * Returns the source line number where this tag was found.
102      * Used for displaying a Checkstyle error.
103      * @return an int line number.
104      */
105     public int getLineNo() {
106         return lineNo;
107     }
108 
109     /**
110      * Returns the position with in the comment line where this tag
111      * was found.  Used for displaying a Checkstyle error.
112      * @return an int relative to zero.
113      */
114     public int getPosition() {
115         return position;
116     }
117 
118     @Override
119     public String toString() {
120         return "HtmlTag[id='" + id + '\''
121                 + ", lineNo=" + lineNo
122                 + ", position=" + position
123                 + ", text='" + text + '\''
124                 + ", closedTag=" + closedTag
125                 + ", incompleteTag=" + incompleteTag + ']';
126     }
127 
128     /**
129      * Returns the comment line of text where this tag appears.
130      * @return text of the tag
131      */
132     public String getText() {
133         final int startOfText = position;
134         final int endOfText = Math.min(startOfText + MAX_TEXT_LEN, text.length());
135         return text.substring(startOfText, endOfText);
136     }
137 
138 }