View Javadoc

1   /*
2   The MIT License
3   
4   Copyright (c) 2004, The Codehaus
5   
6   Permission is hereby granted, free of charge, to any person obtaining a copy of
7   this software and associated documentation files (the "Software"), to deal in
8   the Software without restriction, including without limitation the rights to
9   use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10  of the Software, and to permit persons to whom the Software is furnished to do
11  so, subject to the following conditions:
12  
13  The above copyright notice and this permission notice shall be included in all
14  copies or substantial portions of the Software.
15  
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  SOFTWARE.
23   */
24  package org.codehaus.mojo.scmchangelog.changelog.log.grammar;
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.regex.Matcher;
29  
30  
31  import org.codehaus.mojo.scmchangelog.changelog.log.Issue;
32  import org.codehaus.mojo.scmchangelog.changelog.log.Message;
33  import org.codehaus.mojo.scmchangelog.changelog.log.OperationTypeEnum;
34  
35  /**
36   * Abstract grammar to be used as an helper class.
37   * @author ehsavoie
38   * @version $Id: AbstractScmGrammar.java 9280 2009-03-26 19:21:50Z ehsavoie $
39   */
40  public abstract class AbstractRegexpScmGrammar extends AbstractScmGrammar
41  {
42      /**
43     * Extracts the message (list of issues)from the comment.
44     * @param content the comment of the log entry.
45     * @return the message
46     * @see  org.codehaus.mojo.scmchangelog.changelog.log.Message
47     */
48    public Message extractMessage( String content )
49    {
50      String expression = content;
51      List issues = new ArrayList();
52      expression = computeIssues( OperationTypeEnum.FIX, expression, issues );
53      expression = computeIssues( OperationTypeEnum.ADD, expression, issues );
54      expression = computeIssues( OperationTypeEnum.UPDATE, expression, issues );
55      expression = computeIssues( OperationTypeEnum.REMOVE, expression, issues );
56  
57      if ( expression.startsWith( getIssueSeparator() ) )
58      {
59        expression = expression.substring( 2 );
60      }
61  
62      return new Message( expression.trim(), issues );
63    }
64  
65    /**
66     * Indicates if the content respects the grammar used to extract issues.
67     * @param content the content to be checked
68     * @return true if the content uses the grammar - false otherwise.
69     */
70    public boolean hasMessage( String content )
71    {
72      return ( getAddCleaner( content ).find()
73          || getFixCleaner( content ).find()
74          || getRemoveCleaner( content ).find()
75          || getUpdateCleaner( content ).find() );
76    }
77  
78    /**
79     * Extract all issues of same type from the SVN comment.
80     * @param type : type of issue to be extracted.
81     * @param expression : svn comment.
82     * @param issues : the list of issues already found.
83     * @return the comment after extraction.
84     */
85    protected String computeIssues( OperationTypeEnum type, String expression,
86        List issues )
87    {
88      String tempExpression = expression;
89      Matcher matcher = getFinder( type, expression );
90      boolean hasMore = matcher.find();
91  
92      while ( hasMore )
93      {
94        String element = matcher.group();
95        Matcher cleaner = getCleaner( type, element );
96        cleaner.find();
97  
98        Issue issue = new Issue( element.substring( cleaner.end(),
99            element.length() - 1 ), type );
100       issues.add( issue );
101       tempExpression = tempExpression.substring( 0, matcher.start() )
102           + getIssueSeparator()
103           + tempExpression.substring( matcher.end(), tempExpression.length() );
104       matcher = getFinder( type, tempExpression );
105       hasMore = matcher.find();
106     }
107     return tempExpression;
108   }
109 
110   /**
111    * Return the regexp used for removing grammar elements from the comments.
112    * @param type : type of issue to be extracted.
113    * @param expression : svn comment.
114    * @return the regexp matcher for 'cleaning' the comments.
115    */
116   protected Matcher getCleaner( OperationTypeEnum type, String expression )
117   {
118     if ( OperationTypeEnum.ADD.equals( type ) )
119     {
120       return getAddCleaner( expression );
121     }
122     else if ( OperationTypeEnum.FIX.equals( type ) )
123     {
124       return getFixCleaner( expression );
125     }
126     else if ( OperationTypeEnum.UPDATE.equals( type ) )
127     {
128       return getUpdateCleaner( expression );
129     }
130     else if ( OperationTypeEnum.REMOVE.equals( type ) )
131     {
132       return getRemoveCleaner( expression );
133     }
134     return getRemoveCleaner( expression );
135   }
136 
137   /**
138    * Return the regexp used for finding grammar elements from the comments.
139    * @param type : type of issue to be extracted.
140    * @param expression : svn comment.
141    * @return the regexp matcher for finding the comments.
142    */
143   protected Matcher getFinder( OperationTypeEnum type, String expression )
144   {
145     if ( OperationTypeEnum.ADD.equals( type ) )
146     {
147       return getAddMatcher( expression );
148     }
149     else if ( OperationTypeEnum.FIX.equals( type ) )
150     {
151       return getFixMatcher( expression );
152     }
153     else if ( OperationTypeEnum.UPDATE.equals( type ) )
154     {
155       return getUpdateMatcher( expression );
156     }
157     else if ( OperationTypeEnum.REMOVE.equals( type ) )
158     {
159       return getRemoveMatcher( expression );
160     }
161     return getRemoveMatcher( expression );
162   }
163 
164   /**
165    * Returns the String to be inserted between each issue comment. It may be replaced
166    * when generating the report.
167    * @return the String to be inserted between each issue comment.
168    */
169   public String getIssueSeparator()
170   {
171     return NEW_LINE;
172   }
173 
174   /**
175    * Getter for the regexp matcher to clean the comments of the FIX issues.
176    * @param expression the regexp for cleaning the comments of the FIX issues.
177    * @return a Matcher to clean the comments of FIX issues.
178    */
179   public abstract Matcher getFixCleaner( String expression );
180 
181   /**
182    * Getter for the regexp matcher to clean the comments of the UPDATE issues.
183    * @param expression the regexp for cleaning the comments of the UPDATE issues.
184    * @return a regexp matcher to clean the comments of UPDATE issues.
185    */
186   public abstract Matcher getUpdateCleaner( String expression );
187 
188   /**
189    * Getter for the regexp matcher to clean the comments of the ADD issues.
190    * @param expression the regexp for cleaning the comments of the ADD issues.
191    * @return a regexp matcher to clean the comments of ADD issues.
192    */
193   public abstract Matcher getAddCleaner( String expression );
194 
195   /**
196    * Getter for the regexp matcher to clean the comments of the REMOVE issues.
197    * @param expression the regexp for cleaning the comments of the REMOVE issues.
198    * @return a regexp matcher to clean the comments of REMOVE issues.
199    */
200   public abstract Matcher getRemoveCleaner( String expression );
201 
202   /**
203    * Getter for the regexp matcher to find the comments of the FIX issues.
204    * @param expression the regexp for finding the comments of the FIX issues.
205    * @return a regexp matcher to find the comments of FIX issues.
206    */
207   public abstract Matcher getFixMatcher( String expression );
208 
209   /**
210    * Getter for the regexp matcher to find the comments of the ADD issues.
211    * @param expression the regexp for finding the comments of the ADD issues.
212    * @return a regexp matcher to find the comments of ADD issues.
213    */
214   public abstract Matcher getAddMatcher( String expression );
215 
216   /**
217    * Getter for the regexp matcher to find the comments of the REMOVE issues.
218    * @param expression the regexp for finding the comments of the REMOVE issues.
219    * @return a regexp matcher to find the comments of REMOVE issues.
220    */
221   public abstract Matcher getRemoveMatcher( String expression );
222 
223   /**
224    * Getter for the regexp matcher to find the comments of the UPDATE issues.
225    * @param expression the regexp for finding the comments of the UPDATE issues.
226    * @return a regexp matcher to find the comments of UPDATE issues.
227    */
228   public abstract Matcher getUpdateMatcher( String expression );
229 }