001package org.apache.maven.scm.provider.perforce.command.add;
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.regexp.RE;
028import org.apache.regexp.RESyntaxException;
029import org.codehaus.plexus.util.cli.StreamConsumer;
030
031/**
032 * @author Mike Perham
033 * @version $Id: PerforceChangeLogConsumer.java 331276 2005-11-07 15:04:54Z
034 *          evenisse $
035 */
036public class PerforceAddConsumer
037    implements StreamConsumer
038{
039
040    private static final String PATTERN = "^([^#]+)#(\\d+) - (.*)";
041
042    private static final String FILE_BEGIN_TOKEN = "//";
043
044    private List<ScmFile> additions = new ArrayList<ScmFile>();
045
046    private RE revisionRegexp;
047
048    public PerforceAddConsumer()
049    {
050        try
051        {
052            revisionRegexp = new RE( PATTERN );
053        }
054        catch ( RESyntaxException ignored )
055        {
056            ignored.printStackTrace();
057        }
058    }
059
060    public List<ScmFile> getAdditions()
061    {
062        return additions;
063    }
064
065    /** {@inheritDoc} */
066    public void consumeLine( String line )
067    {
068        if ( line.startsWith( "... " ) )
069        {
070            //TODO log this somehow?
071            //System.out.println("Perforce: " + line);
072            return;
073        }
074
075        if ( !line.startsWith( FILE_BEGIN_TOKEN ) )
076        {
077            throw new IllegalStateException( "Unknown error: " + line );
078        }
079
080        if ( !revisionRegexp.match( line ) )
081        {
082            throw new IllegalStateException( "Unknown input: " + line );
083        }
084
085        additions.add( new ScmFile( revisionRegexp.getParen( 1 ), ScmFileStatus.ADDED ) );
086    }
087}