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;
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.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 Pattern PATTERN = Pattern.compile( "^([^#]+)#(\\d+) - (.*)" );
041
042    private static final String FILE_BEGIN_TOKEN = "//";
043
044    private List<ScmFile> additions = new ArrayList<ScmFile>();
045
046    public List<ScmFile> getAdditions()
047    {
048        return additions;
049    }
050
051    /** {@inheritDoc} */
052    public void consumeLine( String line )
053    {
054        if ( line.startsWith( "... " ) )
055        {
056            //TODO log this somehow?
057            //System.out.println("Perforce: " + line);
058            return;
059        }
060
061        if ( !line.startsWith( FILE_BEGIN_TOKEN ) )
062        {
063            throw new IllegalStateException( "Unknown error: " + line );
064        }
065
066        Matcher matcher = PATTERN.matcher( line );
067        if ( !matcher.find() )
068        {
069            throw new IllegalStateException( "Unknown input: " + line );
070        }
071
072        additions.add( new ScmFile( matcher.group( 1 ), ScmFileStatus.ADDED ) );
073    }
074}