001package org.apache.maven.scm.provider.perforce.command.unedit;
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;
024import java.util.regex.Matcher;
025import java.util.regex.Pattern;
026
027import org.apache.maven.scm.ScmFile;
028import org.apache.maven.scm.ScmFileStatus;
029import org.apache.maven.scm.provider.perforce.command.AbstractPerforceConsumer;
030import org.codehaus.plexus.util.cli.StreamConsumer;
031
032/**
033 * @author Mike Perham
034 * @author Olivier Lamy
035 * @version $Id: PerforceChangeLogConsumer.java 331276 2005-11-07 15:04:54Z
036 *          evenisse $
037 */
038public class PerforceUnEditConsumer
039    extends AbstractPerforceConsumer
040    implements StreamConsumer
041{
042
043    private static final Pattern REVISION_PATTERN = Pattern.compile( "^([^#]+)#\\d+ - (.*)" );
044
045    private static final int STATE_NORMAL = 1;
046
047    private static final int STATE_ERROR = 2;
048
049    private int currentState = STATE_NORMAL;
050
051    private List<ScmFile> edits = new ArrayList<ScmFile>();
052
053    public List<ScmFile> getEdits()
054    {
055        return edits;
056    }
057
058    /** {@inheritDoc} */
059    public void consumeLine( String line )
060    {
061        Matcher matcher = REVISION_PATTERN.matcher( line );
062        if ( currentState != STATE_ERROR && matcher.matches() )
063        {
064            edits.add( new ScmFile( matcher.group( 1 ), ScmFileStatus.UNKNOWN ) );
065            return;
066        }
067
068        error( line );
069    }
070
071    private void error( String line )
072    {
073        currentState = STATE_ERROR;
074        output.println( line );
075    }
076
077    public boolean isSuccess()
078    {
079        return currentState == STATE_NORMAL;
080    }
081}