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;
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 *
035 */
036public class PerforceEditConsumer
037    extends AbstractPerforceConsumer
038    implements StreamConsumer
039{
040
041    private static final String PATTERN = "^([^#]+)#\\d+ - (.*)";
042
043    private static final String FILE_BEGIN_TOKEN = "//";
044
045    private List<ScmFile> edits = new ArrayList<ScmFile>();
046
047    private RE revisionRegexp;
048
049    private boolean errors = false;
050    private StringBuilder errorMessage = new StringBuilder();
051
052    public PerforceEditConsumer()
053    {
054        try
055        {
056            revisionRegexp = new RE( PATTERN );
057        }
058        catch ( RESyntaxException ignored )
059        {
060            ignored.printStackTrace();
061        }
062    }
063
064    public List<ScmFile> getEdits()
065    {
066        return edits;
067    }
068
069    /** {@inheritDoc} */
070    public void consumeLine( String line )
071    {
072        if ( line.startsWith( "... " ) )
073        {
074            //Should we log this somehow?
075            //System.out.println("Perforce: " + line);
076            return;
077        }
078
079        if ( !line.startsWith( FILE_BEGIN_TOKEN ) )
080        {
081            error( line );
082        }
083
084        if ( !revisionRegexp.match( line ) )
085        {
086            error( line );
087        }
088
089        edits.add( new ScmFile( revisionRegexp.getParen( 1 ), ScmFileStatus.EDITED ) );
090    }
091
092    private void error( String line )
093    {
094        errors = true;
095        output.println( line );
096        if ( errorMessage.length() > 0 )
097        {
098            errorMessage.append( System.getProperty( "line.separator" ) );
099        }
100        errorMessage.append( line );
101    }
102
103    public boolean isSuccess()
104    {
105        return !errors;
106    }
107
108    public String getErrorMessage()
109    {
110        return errorMessage.toString();
111    }
112
113}