View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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   * @author Chris Stillwell
26   */
27  class HtmlTag {
28  
29      /** The maximum length of text to display with this tag. */
30      private static final int MAX_TEXT_LEN = 60;
31  
32      /** The HTML tag name. */
33      private final String id;
34  
35      /** The line number in the source file where this tag was found. */
36      private final int lineNo;
37  
38      /** The position within the line where this tag was found. */
39      private final int position;
40  
41      /** The comment line of text where this tag appears. */
42      private final String text;
43  
44      /** If this tag is self-closed. */
45      private final boolean closedTag;
46  
47      /** If the tag is incomplete. */
48      private final boolean incompleteTag;
49  
50      /**
51       * Construct the HtmlTag.
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      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       * @return a String id.
72       */
73      public String getId() {
74          return id;
75      }
76  
77      /**
78       * Indicates if this tag is a close (end) tag.
79       * @return {@code true} is this is a close tag.
80       */
81      public boolean isCloseTag() {
82          return position != text.length() - 1 && text.charAt(position + 1) == '/';
83      }
84  
85      /**
86       * Indicates if this tag is a self-closed XHTML style.
87       * @return {@code true} is this is a self-closed tag.
88       */
89      public boolean isClosedTag() {
90          return closedTag;
91      }
92  
93      /**
94       * Indicates if this tag is incomplete (has no close >).
95       * @return {@code true} if the tag is incomplete.
96       */
97      public boolean isIncompleteTag() {
98          return incompleteTag;
99      }
100 
101     /**
102      * Returns the source line number where this tag was found.
103      * Used for displaying a Checkstyle error.
104      * @return an int line number.
105      */
106     public int getLineNo() {
107         return lineNo;
108     }
109 
110     /**
111      * Returns the position with in the comment line where this tag
112      * was found.  Used for displaying a Checkstyle error.
113      * @return an int relative to zero.
114      */
115     public int getPosition() {
116         return position;
117     }
118 
119     @Override
120     public String toString() {
121         return "HtmlTag[id='" + id + '\''
122                 + ", lineNo=" + lineNo
123                 + ", position=" + position
124                 + ", text='" + text + '\''
125                 + ", closedTag=" + closedTag
126                 + ", incompleteTag=" + incompleteTag + ']';
127     }
128 
129     /**
130      * Returns the comment line of text where this tag appears.
131      * @return text of the tag
132      */
133     public String getText() {
134         final int startOfText = position;
135         final int endOfText = Math.min(startOfText + MAX_TEXT_LEN, text.length());
136         return text.substring(startOfText, endOfText);
137     }
138 
139 }