001package org.apache.maven.scm.provider.svn.svnexe.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.commons.lang.StringUtils;
023import org.apache.maven.scm.ScmFile;
024import org.apache.maven.scm.ScmFileStatus;
025import org.apache.maven.scm.log.ScmLogger;
026import org.apache.maven.scm.provider.svn.svnexe.command.AbstractFileCheckingConsumer;
027
028import java.io.File;
029import java.util.ArrayList;
030import java.util.List;
031
032/**
033 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
034 * @author Olivier Lamy
035 *
036 */
037public class SvnCheckOutConsumer
038    extends AbstractFileCheckingConsumer
039{
040    private static final String CHECKED_OUT_REVISION_TOKEN = "Checked out revision";
041
042    private List<ScmFile> files = new ArrayList<ScmFile>();
043
044    public SvnCheckOutConsumer( ScmLogger logger, File workingDirectory )
045    {
046        super( logger, workingDirectory );
047    }
048
049    /**
050     * {@inheritDoc}
051     */
052    protected void parseLine( String line )
053    {
054        String statusString = line.substring( 0, 1 );
055
056        String file = line.substring( 3 ).trim();
057        //[SCM-368]
058        if ( file.startsWith( getWorkingDirectory().getAbsolutePath() ) )
059        {
060            file = StringUtils.substring( file, getWorkingDirectory().getAbsolutePath().length() + 1 );
061        }
062
063        ScmFileStatus status;
064
065        if ( line.startsWith( CHECKED_OUT_REVISION_TOKEN ) )
066        {
067            String revisionString = line.substring( CHECKED_OUT_REVISION_TOKEN.length() + 1, line.length() - 1 );
068
069            revision = parseInt( revisionString );
070
071            return;
072        }
073        else if ( statusString.equals( "A" ) )
074        {
075            status = ScmFileStatus.ADDED;
076        }
077        else if ( statusString.equals( "U" ) )
078        {
079            status = ScmFileStatus.UPDATED;
080        }
081        else
082        {
083            //Do nothing
084
085            return;
086        }
087
088        addFile( new ScmFile( file, status ) );
089    }
090
091    // ----------------------------------------------------------------------
092    //
093    // ----------------------------------------------------------------------
094
095    public List<ScmFile> getCheckedOutFiles()
096    {
097        return getFiles();
098    }
099
100    protected void addFile( ScmFile file )
101    {
102        files.add( file );
103    }
104
105    protected List<ScmFile> getFiles()
106    {
107        List<ScmFile> onlyFiles = new ArrayList<ScmFile>();
108        for ( ScmFile file : files )
109        {
110            // second part is for svn 1.7 as the co output is now relative not a full path as for svn 1.7-
111            if ( !( !file.getStatus().equals( ScmFileStatus.DELETED ) && !new File( getWorkingDirectory(),
112                                                                                    file.getPath() ).isFile() ) || !(
113                !file.getStatus().equals( ScmFileStatus.DELETED ) && !new File( getWorkingDirectory().getParent(),
114                                                                                file.getPath() ).isFile() ) )
115            {
116                onlyFiles.add( file );
117            }
118        }
119
120        return onlyFiles;
121    }
122
123}