001package org.apache.maven.scm.provider.perforce.command.remove;
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.util.ArrayList;
023import java.util.List;
024
025import org.apache.maven.scm.ScmFile;
026import org.apache.maven.scm.ScmFileStatus;
027import org.apache.maven.scm.provider.perforce.command.AbstractPerforceConsumer;
028import org.apache.regexp.RE;
029import org.apache.regexp.RESyntaxException;
030import org.codehaus.plexus.util.cli.StreamConsumer;
031
032/**
033 * @author Mike Perham
034 * @author Olivier Lamy
035 *
036 */
037public class PerforceRemoveConsumer
038    extends AbstractPerforceConsumer
039    implements StreamConsumer
040{
041    private static final String FILE_BEGIN_TOKEN = "//";
042
043    private static final String PATTERN = "^([^#]+)#\\d+ - (.*)";
044
045    private List<ScmFile> removals = new ArrayList<ScmFile>();
046
047    private RE revisionRegexp;
048
049    private boolean error = false;
050
051    public PerforceRemoveConsumer()
052    {
053        try
054        {
055            revisionRegexp = new RE( PATTERN );
056        }
057        catch ( RESyntaxException ignored )
058        {
059            ignored.printStackTrace();
060        }
061    }
062
063    public List<ScmFile> getRemovals()
064    {
065        return removals;
066    }
067
068    /** {@inheritDoc} */
069    public void consumeLine( String line )
070    {
071        if ( line.startsWith( "... " ) )
072        {
073            return;
074        }
075
076        if ( !line.startsWith( FILE_BEGIN_TOKEN ) )
077        {
078            error( line );
079        }
080
081        if ( !revisionRegexp.match( line ) )
082        {
083            error( line );
084        }
085
086        removals.add( new ScmFile(revisionRegexp.getParen( 1 ), ScmFileStatus.DELETED ) );
087    }
088
089    private void error( String line )
090    {
091        error = true;
092        output.println( line );
093    }
094
095    public boolean isSuccess()
096    {
097        return !error;
098    }
099}