View Javadoc
1   package org.codehaus.mojo.taglist.tags;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.Locale;
25  
26  import org.codehaus.mojo.taglist.beans.TagReport;
27  
28  
29  /**
30   * Class that define a classification of tags.
31   * 
32   * Each tag class contains 1 or more tags.  This allows a user to define
33   * one tag "name" for display purposes, while still checking the files for
34   * multiple tag rules.
35   * 
36   *  Example
37   *  <pre>
38   *   &lt;tagClass&gt;
39   *    &lt;displayName&gt;Action Items&lt;/displayName&gt;
40   *    &lt;tags&gt;
41   *     &lt;tag&gt;
42   *      &lt;matchString&gt;todo&lt;/matchString&gt;
43   *      &lt;matchType&gt;ignoreCase&lt;/matchType&gt;
44   *     &lt;/tag&gt;
45   *    &lt;/tags&gt;
46   *   &lt;/tagClass&gt;
47   *  </pre>
48   * 
49   */
50  public class TagClass
51  {
52      /** The tag class's name. */
53      private String classDisplayName = null;
54      
55      /** The tag report for this tag class. */
56      private TagReport classTagReport = null;
57      
58      /** The container of tags that make up this tag class. */
59      private ArrayList tags = new ArrayList();
60      
61      /** The int value for no tag match found. */
62      public static final int NO_MATCH = AbsTag.NO_MATCH;
63      
64      /** The last tag to successfully match. */
65      private AbsTag lastSuccessfulTagMatch = null;
66      
67      /** A unique ID counter for the tag classes. */
68      private static int uniqueTcCounter = 1;
69      
70      /** The unique id for this tag class. */
71      private int uniqueId = 0;
72     
73      /**
74       * Constructor.
75       * 
76       * @param displayName the string to display as the name for this tag class.
77       */
78      public TagClass( final String displayName )
79      {
80          classDisplayName = displayName;
81          
82          // Assign a unique ID for this tag class and update the global counter.
83          uniqueId = uniqueTcCounter++;
84          
85          classTagReport = new TagReport( displayName, "tag_class_" + String.valueOf( uniqueId ) );
86      }
87  
88      /** Access the tag report for this tag class.
89       * 
90       * @return the tag class's tag report.
91       */
92      public TagReport getTagReport ()
93      {
94          return ( classTagReport );
95      }
96      
97      /** Add a tag to this tag class.
98       *  
99       * @param tag the tag to add to the tag class.
100      */
101     public void addTag ( AbsTag tag )
102     {
103         if ( tag != null )
104         {
105             tags.add( tag );
106         
107             classTagReport.addTagString( tag.tagString );
108         }            
109     }
110     
111     /** Get the index of the first tag contained from within a string. 
112      *  
113      *  The tag class will check each for its tags until a match is found
114      *  within the specified string.  If no match is found, this function will
115      *  return TagClass.NO_MATCH for the index. 
116      *  
117      *  @param currentLine the string for the current line being scanned.
118      *  @param locale the Locale of the currentLine.
119      *  @return the index within the string of the matched tag, or TagClass.NO_MATCH
120      *  if not match was found.
121      */
122     public int tagMatchContains ( final String currentLine, final Locale locale )
123     {
124         int index = NO_MATCH;
125         
126         // Reset the last tag match
127         lastSuccessfulTagMatch = null;
128         
129         Iterator itr = tags.iterator();      
130         while ( itr.hasNext() )
131         {
132             AbsTag tag = (AbsTag) itr.next();
133             
134             // Check if the string contain this tag
135             index = tag.contains( currentLine, locale );
136             
137             if ( index != NO_MATCH )
138             {
139                 // Store the last match
140                 lastSuccessfulTagMatch = tag;
141                 
142                 // Stop checking
143                 break;
144             }
145         }
146         
147         return index;
148     }
149     
150     /** Check if a string starts with a tag from this tag class.
151      *  
152      *  The tag class will check each of its tags until the start of the string
153      *  matched one of the tags.  If not match if found, false is returned.
154      *  
155      *  @param currentLine the string for the current line being scanned.
156      *  @param locale the Locale of the currentLine.
157      *  @return true if the string starts with a tag within this tag class.
158      *  Otherwise false is returned.
159      */
160     public boolean tagMatchStartsWith ( final String currentLine, final Locale locale )
161     {
162         boolean match = false;
163         
164         Iterator itr = tags.iterator();
165         
166         // Loop while there are more tags and there has not been a match.
167         while ( itr.hasNext() && !match )
168         {
169             AbsTag tag = (AbsTag) itr.next();
170             
171             // Check if the string starts with this tag
172             match = tag.startsWith( currentLine, locale );
173         }
174         
175         return match;
176     }
177 
178    /** Return the tag string for the last successfully matched tag.
179     * 
180     * @return string of the last matched tag.
181     */
182    public String getLastTagMatchString()
183    {
184        if ( lastSuccessfulTagMatch == null )
185        {
186            return ( "" );
187        }
188        else
189        {
190            return ( lastSuccessfulTagMatch.tagString );
191        }
192    }
193    
194    /** Return the length of the last matched tag.
195     * 
196     * Normally this is the length of the tag; however, some tags
197     * are dynamic.  For example a regular expression tag might be
198     * 10 characters; however, the matched string may only be 5.
199     * 
200     * Calling this function allows the tag object to return the
201     * correct length for the last matched tag.
202     *  
203     * @return the length of the last matched tag.
204     */
205    public int getLastTagMatchStringLength()
206    {
207        if ( lastSuccessfulTagMatch == null )
208        {
209            return ( 0 );
210        }
211        else
212        {
213            return ( lastSuccessfulTagMatch.getLastTagMatchLength() );
214        }
215    }
216    
217    /** Get the display name of this tag class.
218     * 
219     * @return the tag class display name.
220     */
221    public String getDisplayName ()
222    {
223        return ( classDisplayName );
224    }
225 
226 }