View Javadoc

1   package org.apache.maven.scm.provider.cvslib.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 java.io.File;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.maven.scm.ScmFile;
27  import org.apache.maven.scm.ScmFileStatus;
28  import org.apache.maven.scm.log.ScmLogger;
29  import org.codehaus.plexus.util.StringUtils;
30  import org.codehaus.plexus.util.cli.StreamConsumer;
31  
32  /**
33   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
34   *
35   */
36  public class CvsStatusConsumer
37      implements StreamConsumer
38  {
39      private ScmLogger logger;
40  
41      private File workingDirectory;
42  
43      private List<ScmFile> changedFiles = new ArrayList<ScmFile>();
44  
45      // ----------------------------------------------------------------------
46      //
47      // ----------------------------------------------------------------------
48  
49      public CvsStatusConsumer( ScmLogger logger, File workingDirectory )
50      {
51          this.logger = logger;
52  
53          this.workingDirectory = workingDirectory;
54      }
55  
56      // ----------------------------------------------------------------------
57      // StreamConsumer Implementation
58      // ----------------------------------------------------------------------
59  
60      /** {@inheritDoc} */
61      public void consumeLine( String line )
62      {
63          if ( logger.isDebugEnabled() )
64          {
65              logger.debug( line );
66          }
67  
68          if ( line.length() < 3 )
69          {
70              if ( StringUtils.isNotEmpty( line ) )
71              {
72                  if ( logger.isWarnEnabled() )
73                  {
74                      logger.warn( "Unable to parse output from command: line length must be bigger than 3. ("
75                          + line + ")." );
76                  }
77              }
78              return;
79          }
80  
81          String statusString = line.substring( 0, 1 );
82  
83          String file = line.substring( 2 );
84  
85          ScmFileStatus status;
86  
87          if ( statusString.equals( "A" ) )
88          {
89              status = ScmFileStatus.ADDED;
90          }
91          else if ( statusString.equals( "M" ) )
92          {
93              status = ScmFileStatus.MODIFIED;
94          }
95          else if ( statusString.equals( "D" ) )
96          {
97              status = ScmFileStatus.DELETED;
98          }
99          else if ( statusString.equals( "C" ) )
100         {
101             status = ScmFileStatus.CONFLICT;
102         }
103         else if ( statusString.equals( "?" ) )
104         {
105             status = ScmFileStatus.UNKNOWN;
106         }
107         else if ( statusString.equals( "U" ) || statusString.equals( "P" ) )
108         {
109             // skip remote changes
110             return;
111         }
112         else
113         {
114             if ( logger.isInfoEnabled() )
115             {
116                 logger.info( "Unknown file status: '" + statusString + "'." );
117             }
118 
119             return;
120         }
121 
122         // If the file isn't a file; don't add it.
123         if ( !status.equals( ScmFileStatus.DELETED ) && !new File( workingDirectory, file ).isFile() )
124         {
125             return;
126         }
127 
128         changedFiles.add( new ScmFile( file, status ) );
129     }
130 
131     public List<ScmFile> getChangedFiles()
132     {
133         return changedFiles;
134     }
135 }