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         this.currentFile = this.currentDir + "/" + fileName;
123     }
124 
125     private void processStatus( String line, int pos )
126     {
127         String status = line.substring( pos + STATUS_MARKER.length(), line.length() );
128 
129         if ( status.equals( OUTDATE_MARKER ) )
130         {
131             changedFiles.add( new ScmFile( this.currentFile, ScmFileStatus.MODIFIED ) );
132 
133             if ( logger.isInfoEnabled() )
134             {
135                 logger.info( "Out of Date file: " + this.currentFile );
136             }
137         }
138         else if ( status.equals( MODIFIED_MARKER ) )
139         {
140             changedFiles.add( new ScmFile( this.currentFile, ScmFileStatus.MODIFIED ) );
141 
142             if ( logger.isInfoEnabled() )
143             {
144                 logger.info( "Modified file: " + this.currentFile );
145             }
146         }
147         else if ( status.equals( MISSING_MARKER ) )
148         {
149             changedFiles.add( new ScmFile( this.currentFile, ScmFileStatus.ADDED ) );
150 
151             if ( logger.isInfoEnabled() )
152             {
153                 logger.info( "Missing file: " + this.currentFile );
154             }
155         }
156         else if ( status.equals( MERGE_MARKER ) )
157         {
158             changedFiles.add( new ScmFile( this.currentFile, ScmFileStatus.CONFLICT ) );
159 
160             if ( logger.isInfoEnabled() )
161             {
162                 logger.info( "Conflict file: " + this.currentFile );
163             }
164         }
165         else if ( status.equals( CURRENT_MARKER ) )
166         {
167             //ignore
168         }
169         else
170         {
171             if ( logger.isWarnEnabled() )
172             {
173                 logger.warn( "status unknown (" + status + "): " + this.currentFile );
174             }
175         }
176     }
177 
178     public List<ScmFile> getChangedFiles()
179     {
180         return changedFiles;
181     }
182 
183 }