001package org.apache.maven.scm.provider.cvslib.command.status;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.maven.scm.ScmFile;
027import org.apache.maven.scm.ScmFileStatus;
028import org.apache.maven.scm.log.ScmLogger;
029import org.codehaus.plexus.util.StringUtils;
030import org.codehaus.plexus.util.cli.StreamConsumer;
031
032/**
033 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
034 *
035 */
036public class CvsStatusConsumer
037    implements StreamConsumer
038{
039    private ScmLogger logger;
040
041    private File workingDirectory;
042
043    private List<ScmFile> changedFiles = new ArrayList<ScmFile>();
044
045    // ----------------------------------------------------------------------
046    //
047    // ----------------------------------------------------------------------
048
049    public CvsStatusConsumer( ScmLogger logger, File workingDirectory )
050    {
051        this.logger = logger;
052
053        this.workingDirectory = workingDirectory;
054    }
055
056    // ----------------------------------------------------------------------
057    // StreamConsumer Implementation
058    // ----------------------------------------------------------------------
059
060    /** {@inheritDoc} */
061    public void consumeLine( String line )
062    {
063        if ( logger.isDebugEnabled() )
064        {
065            logger.debug( line );
066        }
067
068        if ( line.length() < 3 )
069        {
070            if ( StringUtils.isNotEmpty( line ) )
071            {
072                if ( logger.isWarnEnabled() )
073                {
074                    logger.warn( "Unable to parse output from command: line length must be bigger than 3. ("
075                        + line + ")." );
076                }
077            }
078            return;
079        }
080
081        String statusString = line.substring( 0, 1 );
082
083        String file = line.substring( 2 );
084
085        ScmFileStatus status;
086
087        if ( statusString.equals( "A" ) )
088        {
089            status = ScmFileStatus.ADDED;
090        }
091        else if ( statusString.equals( "M" ) )
092        {
093            status = ScmFileStatus.MODIFIED;
094        }
095        else if ( statusString.equals( "D" ) )
096        {
097            status = ScmFileStatus.DELETED;
098        }
099        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}