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