001package org.apache.maven.scm.provider.perforce.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.provider.perforce.command.AbstractPerforceConsumer;
025import org.apache.maven.scm.provider.perforce.command.PerforceVerbMapper;
026import org.apache.regexp.RE;
027import org.codehaus.plexus.util.cli.StreamConsumer;
028
029import java.util.ArrayList;
030import java.util.List;
031
032/**
033 * @author Mike Perham
034 * @version $Id: PerforceChangeLogConsumer.java 331276 2005-11-07 15:04:54Z
035 *          evenisse $
036 */
037public class PerforceCheckOutConsumer
038    extends AbstractPerforceConsumer
039    implements StreamConsumer
040{
041    public static final int STATE_CLIENTSPEC = 0;
042
043    public static final int STATE_NORMAL = 1;
044
045    public static final int STATE_ERROR = 2;
046
047    private int currentState = STATE_CLIENTSPEC;
048
049    private RE fileRegexp = new RE( "([^#]+)#\\d+ - ([a-z]+)" );
050
051    private List<ScmFile> checkedout = new ArrayList<ScmFile>();
052
053    private String repo = null;
054
055    private String specname = null;
056
057    public PerforceCheckOutConsumer( String clientspec, String repoPath )
058    {
059        repo = repoPath;
060        specname = clientspec;
061    }
062
063    /*
064     * Client mperham-mikeperham-dt-maven saved.
065     */
066    /*
067     * //depot/modules/cordoba/runtime-ear/.j2ee#1 - deleted as
068     * d:\perforce\depot\modules\cordoba\runtime-ear\.j2ee
069     * //depot/modules/cordoba/runtime-ear/.project#1 - deleted as
070     * d:\perforce\depot\modules\cordoba\runtime-ear\.project
071     * //depot/modules/cordoba/runtime-ear/.runtime#1 - deleted as
072     * d:\perforce\depot\modules\cordoba\runtime-ear\.runtime
073     * //depot/modules/cordoba/runtime-ear/Foo.java#1 - deleted as
074     * d:\perforce\depot\modules\cordoba\runtime-ear\Foo.java
075     * //depot/modules/cordoba/runtime-ear/META-INF/.modulemaps#1 - deleted as
076     * d:\perforce\depot\modules\cordoba\runtime-ear\META-INF\.modulemaps
077     * //depot/modules/cordoba/runtime-ear/META-INF/application.xml#1 - deleted
078     * as d:\perforce\depot\modules\cordoba\runtime-ear\META-INF\application.xml
079     * //depot/modules/cordoba/runtime-ear/pom.xml#4 - deleted as
080     * d:\perforce\depot\modules\cordoba\runtime-ear\pom.xml
081     */
082    /*
083     * Invalid changelist/client/label/date '@somelabel'.
084     */
085    /** {@inheritDoc} */
086    public void consumeLine( String line )
087    {
088        if ( currentState == STATE_CLIENTSPEC
089            && ( line.startsWith( "Client " + specname + " saved." ) || line.startsWith( "Client " + specname
090                + " not changed." ) ) )
091        {
092            currentState = STATE_NORMAL;
093            return;
094        }
095
096        // Handle case where the clientspec is current
097        if ( currentState == STATE_NORMAL && line.indexOf( "ile(s) up-to-date" ) != -1 )
098        {
099            return;
100        }
101
102        if ( currentState != STATE_ERROR && fileRegexp.match( line ) )
103        {
104            String location = fileRegexp.getParen( 1 );
105            if ( location.startsWith( repo ) )
106            {
107                location = location.substring( repo.length() + 1 );
108            }
109            ScmFileStatus status = PerforceVerbMapper.toStatus( fileRegexp.getParen( 2 ) );
110            if ( status != null )
111            {
112                // there are cases where Perforce prints out something but the file did not
113                // actually change (especially when force syncing).  Those files will have
114                // a null status.
115                checkedout.add( new ScmFile( location, status ) );
116            }
117            return;
118        }
119
120        error( line );
121    }
122
123    private void error( String line )
124    {
125        currentState = STATE_ERROR;
126        output.println( line );
127    }
128
129    public boolean isSuccess()
130    {
131        return currentState == STATE_NORMAL;
132    }
133
134    public List<ScmFile> getCheckedout()
135    {
136        return checkedout;
137    }
138}