View Javadoc

1   package org.apache.maven.scm.provider.starteam.command.status;
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.ScmFile;
23  import org.apache.maven.scm.ScmFileStatus;
24  import org.apache.maven.scm.log.ScmLogger;
25  import org.codehaus.plexus.util.cli.StreamConsumer;
26  
27  import java.io.File;
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  /**
32   * @author <a href="mailto:dantran@gmail.com">Dan T. Tran</a>
33   *
34   */
35  public class StarteamStatusConsumer
36      implements StreamConsumer
37  {
38      private ScmLogger logger;
39  
40      private String workingDirectory;
41  
42      private List<ScmFile> changedFiles = new ArrayList<ScmFile>();
43  
44      /**
45       * Marks current directory data
46       */
47      private static final String DIR_MARKER = "(working dir: ";
48  
49      /**
50       * Marks current file data
51       */
52      private static final String FILE_MARKER = "History for: ";
53  
54      /**
55       * Marks current file status
56       */
57      private static final String STATUS_MARKER = "Status: ";
58  
59      /**
60       * Marks current file status
61       */
62      private static final String OUTDATE_MARKER = "Out of Date";
63  
64      private static final String MISSING_MARKER = "Missing";
65  
66      private static final String CURRENT_MARKER = "Current";
67  
68      private static final String MERGE_MARKER = "Merge";
69  
70      private static final String MODIFIED_MARKER = "Modified";
71  
72      private String currentDir = "";
73  
74      private String currentFile = "";
75  
76      public StarteamStatusConsumer( ScmLogger logger, File basedir )
77      {
78          this.logger = logger;
79  
80          this.workingDirectory = basedir.getPath().replace( '\\', '/' );
81      }
82  
83      /** {@inheritDoc} */
84      public void consumeLine( String line )
85      {
86          if ( logger.isDebugEnabled() )
87          {
88              logger.debug( line );
89          }
90  
91          int pos = 0;
92  
93          if ( ( pos = line.indexOf( DIR_MARKER ) ) != -1 )
94          {
95              processGetDir( line, pos );
96          }
97          else if ( ( pos = line.indexOf( FILE_MARKER ) ) != -1 )
98          {
99              processGetFile( line, pos );
100         }
101         else if ( ( pos = line.indexOf( STATUS_MARKER ) ) != -1 )
102         {
103             processStatus( line, pos );
104         }
105         else
106         {
107             //do nothing
108         }
109     }
110 
111     private void processGetDir( String line, int pos )
112     {
113         String dirPath = line.substring( pos + DIR_MARKER.length(), line.length() - 1 ).replace( '\\', '/' );
114 
115         this.currentDir = "." + dirPath.substring( workingDirectory.length() );
116     }
117 
118     private void processGetFile( String line, int pos )
119     {
120         String fileName = line.substring( pos + FILE_MARKER.length(), line.length() );
121 
122         String checkedOutFilePath = this.currentDir + "/" + fileName;
123 
124         this.currentFile = checkedOutFilePath;
125     }
126 
127     private void processStatus( String line, int pos )
128     {
129         String status = line.substring( pos + STATUS_MARKER.length(), line.length() );
130 
131         if ( status.equals( OUTDATE_MARKER ) )
132         {
133             changedFiles.add( new ScmFile( this.currentFile, ScmFileStatus.MODIFIED ) );
134 
135             if ( logger.isInfoEnabled() )
136             {
137                 logger.info( "Out of Date file: " + this.currentFile );
138             }
139         }
140         else if ( status.equals( MODIFIED_MARKER ) )
141         {
142             changedFiles.add( new ScmFile( this.currentFile, ScmFileStatus.MODIFIED ) );
143 
144             if ( logger.isInfoEnabled() )
145             {
146                 logger.info( "Modified file: " + this.currentFile );
147             }
148         }
149         else if ( status.equals( MISSING_MARKER ) )
150         {
151             changedFiles.add( new ScmFile( this.currentFile, ScmFileStatus.ADDED ) );
152 
153             if ( logger.isInfoEnabled() )
154             {
155                 logger.info( "Missing file: " + this.currentFile );
156             }
157         }
158         else if ( status.equals( MERGE_MARKER ) )
159         {
160             changedFiles.add( new ScmFile( this.currentFile, ScmFileStatus.CONFLICT ) );
161 
162             if ( logger.isInfoEnabled() )
163             {
164                 logger.info( "Conflict file: " + this.currentFile );
165             }
166         }
167         else if ( status.equals( CURRENT_MARKER ) )
168         {
169             //ignore
170         }
171         else
172         {
173             if ( logger.isWarnEnabled() )
174             {
175                 logger.warn( "status unknown (" + status + "): " + this.currentFile );
176             }
177         }
178     }
179 
180     public List<ScmFile> getChangedFiles()
181     {
182         return changedFiles;
183     }
184 
185 }