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 import java.util.regex.Pattern;
30 import org.codehaus.mojo.scmchangelog.changelog.log.Issue;
31 import org.codehaus.mojo.scmchangelog.changelog.log.Message;
32 import org.codehaus.mojo.scmchangelog.changelog.log.OperationTypeEnum;
33
34 /**
35 * Simple grammar which recognizes bug references in the format: bug ####.
36 * For more information, refer to the "Autolinkification" section of the Bugzilla manual:
37 * http://www.bugzilla.org/docs/3.2/en/html/hintsandtips.html
38 *
39 * @author Matthew Beermann <matthew.beermann@cerner.com>
40 */
41 public class BugzillaScmGrammar extends AbstractScmGrammar
42 {
43
44 /**
45 * Bugzilla pattern.
46 */
47 private static final Pattern BUGZILLA_PATTERN =
48 Pattern.compile( "bug\\s+#?([1-9][0-9]*)", Pattern.CASE_INSENSITIVE );
49
50 /**
51 * Extract a Message from a comment.
52 * @param content <html>
53 * <head>
54 *
55 * </head>
56 * <body>
57 * <p style="margin-top: 0">
58 * the comment to be parsed and from which a Message will be extracted
59 * </p>
60 * </body>
61 * </html>
62 *
63 * @return <html>
64 * <head>
65 *
66 * </head>
67 * <body>
68 * <p style="margin-top: 0">
69 * the Message extracted from the content
70 * </p>
71 * </body>
72 * </html>
73 */
74 public Message extractMessage( String content )
75 {
76 List issues = new ArrayList();
77 Matcher matcher = BUGZILLA_PATTERN.matcher( content );
78 while ( matcher.find() )
79 {
80 issues.add( new Issue( matcher.group( 1 ), OperationTypeEnum.FIX ) );
81 }
82 return new Message( content, issues );
83 }
84
85 /**
86 * Returns the String to be inserted between each issue comment. It may be replaced
87 * when generating the report.
88 * @return the String to be inserted between each issue comment.
89 */
90 public String getIssueSeparator()
91 {
92 return NEW_LINE;
93 }
94
95 /**
96 * Indicates if the content has a Message to be extracted.
97 * @param content <html>
98 * <head>
99 *
100 * </head>
101 * <body>
102 * <p style="margin-top: 0">
103 * the content to be tested
104 * </p>
105 * </body>
106 * </html>
107 *
108 * @return <html>
109 * <head>
110 *
111 * </head>
112 * <body>
113 * <p style="margin-top: 0">
114 * true if a Message can be extracted - false otherwise
115 * </p>
116 * </body>
117 * </html>
118 */
119 public boolean hasMessage( String content )
120 {
121 Matcher matcher = BUGZILLA_PATTERN.matcher( content );
122 return matcher.find();
123 }
124 }