001package org.apache.maven.scm.provider.cvslib.command.checkout;
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.StringUtils;
026import org.codehaus.plexus.util.cli.StreamConsumer;
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 * @author Olivier Lamy
034 *
035 */
036public class CvsCheckOutConsumer
037    implements StreamConsumer
038{
039    private ScmLogger logger;
040
041    private List<ScmFile> files = new ArrayList<ScmFile>();
042
043    public CvsCheckOutConsumer( ScmLogger logger )
044    {
045        this.logger = logger;
046    }
047
048    /** {@inheritDoc} */
049    public void consumeLine( String line )
050    {
051        if ( logger.isDebugEnabled() )
052        {
053            logger.debug( line );
054        }
055
056        if ( line.length() < 3 )
057        {
058            if ( StringUtils.isNotEmpty( line ) )
059            {
060                if ( logger.isWarnEnabled() )
061                {
062                    logger.warn( "Unable to parse output from command: line length must be bigger than 3. ("
063                        + line + ")." );
064                }
065            }
066            return;
067        }
068
069        String status = line.substring( 0, 2 );
070
071        String file = line.substring( 2 );
072
073        file = file.substring( file.indexOf( '/' ) );
074
075        if ( status.equals( "U " ) )
076        {
077            files.add( new ScmFile( file, ScmFileStatus.UPDATED ) );
078        }
079        else if ( status.equals( "P " ) )
080        {
081            files.add( new ScmFile( file, ScmFileStatus.PATCHED ) );
082        }
083        else if ( status.equals( "C " ) )
084        {
085            files.add( new ScmFile( file, ScmFileStatus.CONFLICT ) );
086        }
087        else
088        {
089            if ( logger.isWarnEnabled() )
090            {
091                logger.warn( "Unknown status: '" + status + "'." );
092            }
093        }
094    }
095
096    public List<ScmFile> getCheckedOutFiles()
097    {
098        return files;
099    }
100}