001package org.apache.maven.scm.provider.perforce.command.edit;
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 *
035 */
036public class PerforceEditConsumer
037    extends AbstractPerforceConsumer
038    implements StreamConsumer
039{
040
041    private static final Pattern PATTERN = Pattern.compile( "^([^#]+)#\\d+ - (.*)" );
042
043    private static final String FILE_BEGIN_TOKEN = "//";
044
045    private List<ScmFile> edits = new ArrayList<ScmFile>();
046
047    private boolean errors = false;
048    private StringBuilder errorMessage = new StringBuilder();
049
050    public List<ScmFile> getEdits()
051    {
052        return edits;
053    }
054
055    /** {@inheritDoc} */
056    public void consumeLine( String line )
057    {
058        if ( line.startsWith( "... " ) )
059        {
060            //Should we log this somehow?
061            //System.out.println("Perforce: " + line);
062            return;
063        }
064
065        if ( !line.startsWith( FILE_BEGIN_TOKEN ) )
066        {
067            error( line );
068        }
069
070        Matcher matcher = PATTERN.matcher( line );
071        if ( !matcher.matches() )
072        {
073            error( line );
074        }
075
076        edits.add( new ScmFile( matcher.group( 1 ), ScmFileStatus.EDITED ) );
077    }
078
079    private void error( String line )
080    {
081        errors = true;
082        output.println( line );
083        if ( errorMessage.length() > 0 )
084        {
085            errorMessage.append( System.getProperty( "line.separator" ) );
086        }
087        errorMessage.append( line );
088    }
089
090    public boolean isSuccess()
091    {
092        return !errors;
093    }
094
095    public String getErrorMessage()
096    {
097        return errorMessage.toString();
098    }
099
100}