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.scm.hg.command.list;
25
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31 import org.apache.maven.scm.ScmFileStatus;
32 import org.apache.maven.scm.log.ScmLogger;
33 import org.apache.maven.scm.provider.hg.command.HgConsumer;
34 import org.codehaus.mojo.scmchangelog.tags.Tag;
35
36 /**
37 * Consumer for the output of the command: <code>hg tags --verbose path</code>.
38 * @author ehsavoie
39 * @version $Id: HgTagsConsumer.java 8989 2009-02-05 08:51:11Z ehsavoie $
40 */
41 class HgTagsConsumer
42 extends HgConsumer
43 {
44
45 /**
46 * List of tags found in the Mercurial repository.
47 */
48 private final List repositoryStatus = new ArrayList();
49
50 /**
51 * The filter on the tag names to be used.
52 */
53 private Pattern filter;
54
55 /**
56 * Instantiate a new HgTagsConsumer.
57 * @param logger the logger.
58 * @param filter the filter on the tag names to be used.
59 */
60 HgTagsConsumer( ScmLogger logger , Pattern filter )
61 {
62 super( logger );
63 this.filter = filter;
64 }
65
66 /**
67 * Consume a line of the command output.
68 * @param status null.
69 * @param trimmedLine the line.
70 */
71 public void doConsume( ScmFileStatus status, String trimmedLine )
72 {
73 getLogger().debug( trimmedLine );
74
75 int startRevisionIndex = trimmedLine.lastIndexOf( ' ' );
76 int endRevisionIndex = trimmedLine.lastIndexOf( ':' );
77 String title = trimmedLine.substring( 0, startRevisionIndex );
78 String revisionId = trimmedLine.substring( startRevisionIndex + 1,
79 endRevisionIndex );
80 if ( isTagAccepted( title ) )
81 {
82 Tag tag = new Tag( title );
83 tag.setStartRevision( "0" );
84 tag.setEndRevision( revisionId );
85 repositoryStatus.add( tag );
86 }
87 }
88
89 /**
90 * Return the list of Tag.
91 * @return List<Tag>
92 * @see org.codehaus.mojo.scmchangelog.tags.Tag
93 */
94 List getStatus()
95 {
96 return repositoryStatus;
97 }
98
99 /**
100 * Checks if the tag name matches the filter.
101 * @param title the name of the tag to be checked.
102 * @return true if the tag matches - false otherwise.
103 */
104 protected boolean isTagAccepted( String title )
105 {
106 if ( filter != null )
107 {
108 Matcher matcher = filter.matcher( title );
109 getLogger().debug( "Filtering " + title + " against " + filter.pattern()
110 + " : " + matcher.matches() );
111 return matcher.matches();
112 }
113 return true;
114 }
115 }