001package org.apache.maven.scm.provider.cvslib.command.update;
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 org.apache.maven.scm.ScmFile;
023import org.apache.maven.scm.ScmFileStatus;
024import org.apache.maven.scm.log.ScmLogger;
025import org.codehaus.plexus.util.cli.StreamConsumer;
026import org.codehaus.plexus.util.StringUtils;
027
028import java.util.ArrayList;
029import java.util.List;
030
031/**
032 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
033 *
034 */
035public class CvsUpdateConsumer
036    implements StreamConsumer
037{
038    private ScmLogger logger;
039
040    private List<ScmFile> files = new ArrayList<ScmFile>();
041
042    public CvsUpdateConsumer( ScmLogger logger )
043    {
044        this.logger = logger;
045    }
046
047    /** {@inheritDoc} */
048    public void consumeLine( String line )
049    {
050        if ( logger.isDebugEnabled() )
051        {
052            logger.debug( line );
053        }
054
055        if ( line.length() < 3 )
056        {
057            if ( StringUtils.isNotEmpty( line ) )
058            {
059                if ( logger.isWarnEnabled() )
060                {
061                    logger.warn( "Unable to parse output from command: line length must be bigger than 3. ("
062                        + line + ")." );
063                }
064            }
065            return;
066        }
067
068        String status = line.substring( 0, 2 );
069
070        String file = line.substring( 2 );
071
072        if ( status.equals( "U " ) )
073        {
074            files.add( new ScmFile( file, ScmFileStatus.UPDATED ) );
075        }
076        else if ( status.equals( "P " ) )
077        {
078            files.add( new ScmFile( file, ScmFileStatus.PATCHED ) );
079        }
080        else if ( status.equals( "A " ) )
081        {
082            files.add( new ScmFile( file, ScmFileStatus.ADDED ) );
083        }
084        else if ( status.equals( "C " ) )
085        {
086            files.add( new ScmFile( file, ScmFileStatus.CONFLICT ) );
087        }
088        else if ( status.equals( "M " ) )
089        {
090            files.add( new ScmFile( file, ScmFileStatus.MODIFIED ) );
091        }
092        else if ( status.equals( "? " ) )
093        {
094            files.add( new ScmFile( file, ScmFileStatus.UNKNOWN ) );
095        }
096        else
097        {
098            if ( logger.isDebugEnabled() )
099            {
100                logger.warn( "Unknown status: '" + status + "' for file '" + file + "'." );
101            }
102        }
103    }
104
105    public List<ScmFile> getUpdatedFiles()
106    {
107        return files;
108    }
109}