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.regex.Matcher;
23  import java.util.regex.Pattern;
24  import java.util.Locale;
25  
26  /** The regular expression tag class.
27   * 
28   *  This class defines a regular express tag search.  This tag
29   *  will match if the regular express string listed is found within
30   *  the scanned lines.
31   *  
32   *  Example POM:
33   *  <pre>
34   *   &lt;project&gt;
35   *       ...
36   *       &lt;matchString&gt;fixme[0-9]&lt;/matchString&gt;
37   *       &lt;matchType&gt;regEx&lt;/matchType&gt;
38   *       ...
39   *   &lt;/project&gt;
40   *  </pre>
41   *  
42   *  Example Java code with match:
43   *  <pre>
44   *    fixme1 this will match (fixme followed by one digit.)
45   *  </pre>
46   *  
47   *  Example Java code without match:
48   *  <pre>
49   *     fixme this will NOT match (no digit after fixme)
50   *  </pre>
51   *       
52   */
53  public class RegExTag extends AbsTag
54  {
55      /** The regular expression pattern to pre-compile. */
56      private Pattern pattern = null;
57      
58      /** The length of the last regEx comment tag match */
59      private int lastMatchedCommentTagLength = 0;
60  
61      /** Check to see if the string contains this tag.  
62       *  If there is a match, return the index within the string; otherwise,
63       *  return NO_MATCH.
64       *  
65       *  @param currentLine the string for the current line being scanned.
66       *  @param locale the Locale of the currentLine.
67       *  @return the index within the string of the matched tag, or TagClass.NO_MATCH
68       *  if not match was found.
69       */
70      public int contains( final String currentLine, final Locale locale )
71      {
72          int result = AbsTag.NO_MATCH;
73          
74          if ( currentLine != null )
75          {
76              // Get index match or -1 if no match
77              Matcher m = pattern.matcher( currentLine );
78              if ( m.find() )
79              {
80                  result = m.start();
81                  
82                  // Store the length of the comment tag.
83                  lastMatchedCommentTagLength = m.end() - m.start();
84              }
85          }
86          
87          return ( result );
88      }
89      
90      /** Check to see if the string starts with this tag.  
91       *  
92       *  @param currentLine the string for the current line being scanned.
93       *  @param locale the Locale of the currentLine.
94       *  @return true if the string starts with this tag.
95       */
96      public boolean startsWith( final String currentLine, final Locale locale )
97      {
98          boolean result = false;
99          
100         if ( currentLine != null )
101         {
102             Matcher m = pattern.matcher( currentLine );
103             if ( m.find() )
104             {
105                 // Was the match at the first character?
106                 result = m.start() == 0;
107             }
108         }
109         
110         return ( result );
111     }
112     
113     /** Return the length of the last matched tag.
114      * 
115      * A regular expression tag might be 10 characters; however, 
116      * the matched string may only be 5.
117      * 
118      * Example:
119      *    regEx tag:       a*b    (tag length = 3)
120      *    
121      *    matched comment: aaabbb (comment length = 6)
122      *    
123      * In the above example, this function will return 6.
124      *  
125      * @return the length of the last matched tag.
126      */
127     public int getLastTagMatchLength()
128     {
129         return ( lastMatchedCommentTagLength );
130     }
131 
132     /** Constructor.
133      * 
134      * @param tagString the string to match against for this tag.
135      */
136     public RegExTag ( final String tagString )
137     {
138         super( tagString );
139         
140         // Pre-compile the regular expression
141         pattern = Pattern.compile( tagString );
142 
143     }
144 
145 }