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.scm.svn;
25  
26  import org.codehaus.mojo.scmchangelog.AbstractBufferedConsumer;
27  import org.codehaus.mojo.scmchangelog.tags.Tag;
28  import org.codehaus.mojo.scmchangelog.scm.svn.xml.CommitDocument.Commit;
29  import org.codehaus.mojo.scmchangelog.scm.svn.xml.EntryDocument.Entry;
30  import org.codehaus.mojo.scmchangelog.scm.svn.xml.ListDocument.List;
31  import org.codehaus.mojo.scmchangelog.scm.svn.xml.ListsDocument;
32  import org.codehaus.mojo.scmchangelog.scm.svn.xml.ListsDocument.Factory;
33  
34  import org.apache.xmlbeans.XmlException;
35  
36  
37  import java.io.IOException;
38  import java.io.StringReader;
39  
40  import java.util.ArrayList;
41  import java.util.Collections;
42  import java.util.Iterator;
43  import java.util.regex.Matcher;
44  import java.util.regex.Pattern;
45  
46  
47  /**
48   * Parses the output of the command and returns a list of Tag.
49   * @author ehsavoie
50   * @version $Id: SvnListConsumer.java 8989 2009-02-05 08:51:11Z ehsavoie $
51   */
52  public class SvnListConsumer
53      extends AbstractBufferedConsumer
54  {
55  
56    /**
57     * The elements result of the parsing.
58     */
59    private java.util.List elements = new ArrayList();
60  
61  
62    /**
63     * The filter on the tag names to be used.
64     */
65    private Pattern filter;
66  
67    /**
68     * Create new instance of the SvnListConsumer.
69     * @param filter the filter on the tag names to be used.
70     */
71    public SvnListConsumer ( Pattern filter )
72    {
73      this.filter = filter;
74    }
75  
76    /**
77     * Parses the output of the command and returns a list of elements.
78     * @return a list of Tag.
79     * @see org.codehaus.mojo.scmchangelog.tags.Tag
80     */
81    public java.util.List analyse()
82    {
83      getLogger().debug( "Receiving this line " + getOutput() );
84  
85      try
86      {
87        ListsDocument doc = Factory.parse( new StringReader( getOutput() ) );
88  
89        for ( int i = 0; i < doc.getLists().getListArray().length; i++ )
90        {
91          List list = doc.getLists().getListArray()[i];
92  
93          for ( int j = 0; j < list.getEntryArray().length; j++ )
94          {
95            Entry entry = list.getEntryArray()[j];
96            getLogger().debug( entry.getName() );
97            if ( isTagAccepted( entry.getName() ) )
98            {
99              getLogger().debug( "Creating new Tag" );
100             Tag tag = new Tag( entry.getName() );
101             Commit commit = entry.getCommit();
102             tag.setDate( commit.getDate().getTime() );
103             tag.setEndRevision( commit.getRevision().toString() );
104             tag.setAuthor( commit.getAuthor() );
105             elements.add( tag );
106           }
107         }
108       }
109       Collections.sort( elements );
110       Tag oldTag = new Tag( "" );
111       oldTag.setEndRevision( "0" );
112       Iterator iter = elements.iterator();
113       while ( iter.hasNext() )
114       {
115         Tag tag = ( Tag ) iter.next();
116         tag.setStartRevision( oldTag.getEndRevision() );
117         oldTag = tag;
118       }
119       return this.elements;
120     }
121     catch ( XmlException ex )
122     {
123       getLogger().error( getOutput(), ex );
124       throw new RuntimeException( ex );
125     }
126     catch ( IOException ioe )
127     {
128       getLogger().error( getOutput(), ioe );
129       throw new RuntimeException( ioe );
130     }
131   }
132 
133   /**
134    * Checks if the tag name matches the filter.
135    * @param title the name of the tag to be checked.
136    * @return true if the tag matches - false otherwise.
137    */
138   protected boolean isTagAccepted( String title )
139   {
140     if ( "tags".equalsIgnoreCase( title ) )
141     {
142       return false;
143     }
144     if ( filter != null )
145     {
146       Matcher matcher = filter.matcher( title );
147       getLogger().debug( "Filtering " + title + " against " + filter.pattern()
148               + " : " +  matcher.matches() );
149       return matcher.matches();
150     }
151     return true;
152   }
153 }