View Javadoc

1   package org.apache.maven.scm.provider.clearcase.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 org.apache.maven.scm.ChangeFile;
23  import org.apache.maven.scm.ChangeSet;
24  import org.apache.maven.scm.log.ScmLogger;
25  import org.apache.maven.scm.util.AbstractConsumer;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  /**
31   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
32   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
33   * @version $Id: ClearCaseChangeLogConsumer.java 483105 2006-12-06 15:07:54Z evenisse $
34   */
35  public class ClearCaseChangeLogConsumer
36      extends AbstractConsumer
37  {
38      /**
39       * Formatter used to parse Clearcase date/timestamp.
40       */
41      private static final String CLEARCASE_TIMESTAMP_PATTERN = "yyyyMMdd.HHmmss";
42  
43      private static final String NAME_TAG = "NAME:";
44  
45      private static final String USER_TAG = "USER:";
46  
47      private static final String DATE_TAG = "DATE:";
48  
49      private static final String COMMENT_TAG = "COMM:";
50  
51      private List entries = new ArrayList();
52  
53      // state machine constants for reading clearcase lshistory command output
54  
55      /**
56       * expecting file information
57       */
58      private static final int GET_FILE = 1;
59  
60      /**
61       * expecting date
62       */
63      private static final int GET_DATE = 2;
64  
65      /**
66       * expecting comments
67       */
68      private static final int GET_COMMENT = 3;
69  
70      /**
71       * current status of the parser
72       */
73      private int status = GET_FILE;
74  
75      /**
76       * the current log entry being processed by the parser
77       */
78      private ChangeSet currentChange = null;
79  
80      /**
81       * the current file being processed by the parser
82       */
83      private ChangeFile currentFile = null;
84  
85      private String userDatePattern;
86  
87      // ----------------------------------------------------------------------
88      //
89      // ----------------------------------------------------------------------
90  
91      public ClearCaseChangeLogConsumer( ScmLogger logger, String userDatePattern )
92      {
93          super( logger );
94  
95          this.userDatePattern = userDatePattern;
96      }
97  
98      // ----------------------------------------------------------------------
99      //
100     // ----------------------------------------------------------------------
101 
102     public List getModifications()
103     {
104         return entries;
105     }
106 
107     // ----------------------------------------------------------------------
108     // StreamConsumer Implementation
109     // ----------------------------------------------------------------------
110 
111     public void consumeLine( String line )
112     {
113         switch ( getStatus() )
114         {
115             case GET_FILE:
116                 processGetFile( line );
117                 break;
118             case GET_DATE:
119                 processGetDate( line );
120                 break;
121             case GET_COMMENT:
122                 processGetCommentAndUser( line );
123                 break;
124             default:
125                 getLogger().warn( "Unknown state: " + status );
126         }
127     }
128 
129     // ----------------------------------------------------------------------
130     //
131     // ----------------------------------------------------------------------
132 
133     /**
134      * Process the current input line in the Get File state.
135      *
136      * @param line a line of text from the clearcase log output
137      */
138     private void processGetFile( String line )
139     {
140         if ( line.startsWith( NAME_TAG ) )
141         {
142             setCurrentChange( new ChangeSet() );
143             setCurrentFile( new ChangeFile( line.substring( NAME_TAG.length(), line.length() ) ) );
144             setStatus( GET_DATE );
145         }
146     }
147 
148     /**
149      * Process the current input line in the Get Date state.
150      *
151      * @param line a line of text from the clearcase log output
152      */
153     private void processGetDate( String line )
154     {
155         if ( line.startsWith( DATE_TAG ) )
156         {
157             getCurrentChange().setDate(
158                 parseDate( line.substring( DATE_TAG.length() ), userDatePattern, CLEARCASE_TIMESTAMP_PATTERN ) );
159 
160             setStatus( GET_COMMENT );
161         }
162     }
163 
164     /**
165      * Process the current input line in the Get Comment state.
166      *
167      * @param line a line of text from the clearcase log output
168      */
169     private void processGetCommentAndUser( String line )
170     {
171         if ( line.startsWith( COMMENT_TAG ) )
172         {
173             String comm = line.substring( COMMENT_TAG.length() );
174 
175             getCurrentChange().setComment( getCurrentChange().getComment() + comm + "\n" );
176         }
177         else if ( line.startsWith( USER_TAG ) )
178         {
179             getCurrentChange().setAuthor( line.substring( USER_TAG.length() ) );
180 
181             // add entry, and set state to get file
182             getCurrentChange().addFile( getCurrentFile() );
183 
184             entries.add( getCurrentChange() );
185 
186             setStatus( GET_FILE );
187         }
188         else
189         {
190             // keep gathering comments
191             getCurrentChange().setComment( getCurrentChange().getComment() + line + "\n" );
192         }
193     }
194 
195     /**
196      * Getter for property currentFile.
197      *
198      * @return Value of property currentFile.
199      */
200     private ChangeFile getCurrentFile()
201     {
202         return currentFile;
203     }
204 
205     /**
206      * Setter for property currentFile.
207      *
208      * @param currentFile New value of property currentFile.
209      */
210     private void setCurrentFile( ChangeFile currentFile )
211     {
212         this.currentFile = currentFile;
213     }
214 
215     /**
216      * Getter for property currentChange.
217      *
218      * @return Value of property currentChange.
219      */
220     private ChangeSet getCurrentChange()
221     {
222         return currentChange;
223     }
224 
225     /**
226      * Setter for property currentLogEntry.
227      *
228      * @param currentChange New value of property currentLogEntry.
229      */
230     private void setCurrentChange( ChangeSet currentChange )
231     {
232         this.currentChange = currentChange;
233     }
234 
235     /**
236      * Getter for property status.
237      *
238      * @return Value of property status.
239      */
240     private int getStatus()
241     {
242         return status;
243     }
244 
245     /**
246      * Setter for property status.
247      *
248      * @param status New value of property status.
249      */
250     private void setStatus( int status )
251     {
252         this.status = status;
253     }
254 }