View Javadoc
1   package org.apache.maven.scm.provider.hg.command.changelog;
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.ArrayList;
23  import java.util.Date;
24  import java.util.List;
25  import java.util.Locale;
26  
27  import org.apache.maven.scm.ChangeFile;
28  import org.apache.maven.scm.ChangeSet;
29  import org.apache.maven.scm.ScmFileStatus;
30  import org.apache.maven.scm.provider.hg.command.HgConsumer;
31  
32  /**
33   * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
34   * @author <a href="mailto:hr.mohr@gmail.com">Mads Mohr Christensen</a>
35   */
36  public class HgChangeLogConsumer
37      extends HgConsumer
38  {
39  
40      private static final String TIME_PATTERN = "yyyy-MM-dd HH:mm:ss Z";
41  
42      private static final String REVNO_TAG = "changeset:";
43  
44      private static final String TAG_TAG = "tag:";
45  
46      private static final String BRANCH_TAG = "branch:";
47  
48      private static final String AUTHOR_TAG = "user:";
49  
50      private static final String TIME_STAMP_TOKEN = "date:";
51  
52      private static final String MESSAGE_TOKEN = "description:";
53  
54      private static final String FILES_TOKEN = "files:";
55  
56      private List<ChangeSet> logEntries = new ArrayList<ChangeSet>();
57  
58      private ChangeSet currentChange;
59  
60      private String currentRevision;
61  
62      @SuppressWarnings( "unused" )
63      private String currentBranch; // don't know what to do with this
64  
65      private String userDatePattern;
66  
67      public HgChangeLogConsumer( String userDatePattern )
68      {
69          this.userDatePattern = userDatePattern;
70      }
71  
72      public List<ChangeSet> getModifications()
73      {
74          return logEntries;
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      public void consumeLine( String line )
81      {
82          // override default behaviour which tries to pick through things for some standard messages.  that
83          // does not apply here
84          String trimmedLine = line.trim();
85          doConsume( null, trimmedLine );
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      public void doConsume( ScmFileStatus status, String line )
92      {
93          String tmpLine;
94  
95          // new changeset
96          if ( line.startsWith( REVNO_TAG ) )
97          {
98              //Init a new changeset
99              currentChange = new ChangeSet();
100             currentChange.setFiles( new ArrayList<ChangeFile>( 0 ) );
101             logEntries.add( currentChange );
102 
103             // parse revision
104             tmpLine = line.substring( REVNO_TAG.length() ).trim();
105             currentRevision = tmpLine.substring( tmpLine.indexOf( ':' ) + 1 );
106             currentChange.setRevision( currentRevision );
107         }
108         else if ( line.startsWith( BRANCH_TAG ) )
109         {
110             tmpLine = line.substring( BRANCH_TAG.length() ).trim();
111             currentBranch = tmpLine;
112         }
113         else if ( line.startsWith( AUTHOR_TAG ) )
114         {
115             tmpLine = line.substring( AUTHOR_TAG.length() ).trim();
116             currentChange.setAuthor( tmpLine );
117         }
118         else if ( line.startsWith( TIME_STAMP_TOKEN ) )
119         {
120             tmpLine = line.substring( TIME_STAMP_TOKEN.length() ).trim();
121             Date date = parseDate( tmpLine, userDatePattern, TIME_PATTERN, Locale.ENGLISH );
122             currentChange.setDate( date );
123         }
124         else if ( line.startsWith( TAG_TAG ) )
125         {
126             tmpLine = line.substring( TAG_TAG.length() ).trim();
127             currentChange.addTag( tmpLine );
128         }
129         else if ( line.startsWith( FILES_TOKEN ) )
130         {
131             tmpLine = line.substring( FILES_TOKEN.length() ).trim();
132             String[] files = tmpLine.split( " " );
133             for ( int i = 0; i < files.length; i++ )
134             {
135                 String file = files[i];
136                 ChangeFile changeFile = new ChangeFile( file, currentRevision );
137                 currentChange.addFile( changeFile );
138             }
139         }
140         else if ( line.startsWith( MESSAGE_TOKEN ) )
141         {
142             currentChange.setComment( "" );
143         }
144         else
145         {
146             StringBuilder comment = new StringBuilder( currentChange.getComment() );
147             comment.append( line );
148             currentChange.setComment( comment.toString() );
149         }
150     }
151 }