001package org.apache.maven.scm.provider.perforce.command.checkin;
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 org.codehaus.plexus.util.cli.StreamConsumer;
023
024import java.io.PrintWriter;
025import java.io.StringWriter;
026import java.util.regex.Pattern;
027
028/**
029 * @author Mike Perham
030 * @version $Id: PerforceChangeLogConsumer.java 331276 2005-11-07 15:04:54Z
031 *          evenisse $
032 */
033public class PerforceCheckInConsumer
034    implements StreamConsumer
035{
036    private static final Pattern CREATED_PATTERN = Pattern.compile( "^Change \\d+ created .+$" );
037
038    private static final Pattern SUBMITTING_PATTERN = Pattern.compile( "^Submitting change \\d+\\.$" );
039
040    private static final Pattern LOCKING_PATTERN = Pattern.compile( "^Locking \\d+ files \\.\\.\\.$" );
041
042    private static final Pattern OPERATION_PATTERN = Pattern.compile( "^[a-z]+ //[^#]+#\\d+$" );
043
044    // SCM-181 Two possible messages:
045    // "Change 94821 renamed change 94823 and submitted."
046    // "Change 94821 submitted."
047    private static final Pattern COMPLETE_PATTERN = Pattern.compile( "^Change \\d+ .*submitted.$" );
048
049    public static final int STATE_CREATED = 1;
050
051    public static final int STATE_SUBMITTING = 2;
052
053    public static final int STATE_LOCKING = 3;
054
055    public static final int STATE_OP = 4;
056
057    public static final int STATE_COMPLETE = 5;
058
059    public static final int STATE_ERROR = 6;
060
061    private StringWriter errors = new StringWriter();
062
063    private PrintWriter errorOutput = new PrintWriter( errors );
064
065    private int currentState = STATE_CREATED;
066
067    /*
068     * Change 80835 created with 1 open file(s). Submitting change 80835.
069     * Locking 1 files ... add //depot/modules/cordoba/runtime-ear/foo.xml#1
070     * Change 80835 submitted.
071     */
072    /*
073     * Submitting change 80837. Locking 1 files ... edit
074     * //depot/modules/cordoba/runtime-ear/Foo.java#2 Submit validation failed --
075     * fix problems then use 'p4 submit -c 80837'. 'checkstyle' validation
076     * failed:
077     *
078     * depot/modules/cordoba/runtime-ear/Foo.java:3:1: Got an exception -
079     * expecting EOF, found '}'
080     */
081    /** {@inheritDoc} */
082    public void consumeLine( String line )
083    {
084        if ( line.startsWith( "... " ) )
085        {
086            //TODO log this somehow?
087            //System.out.println("Perforce: " + line);
088            return;
089        }
090
091        switch ( currentState )
092        {
093            case STATE_CREATED:
094                boolean created = CREATED_PATTERN.matcher( line ).matches();
095                if ( created )
096                {
097                    currentState++;
098                    break;
099                }
100                error( line );
101                break;
102            case STATE_SUBMITTING:
103                boolean submitting = SUBMITTING_PATTERN.matcher( line ).matches();
104                if ( submitting )
105                {
106                    currentState++;
107                    break;
108                }
109                error( line );
110                break;
111            case STATE_LOCKING:
112                boolean locked = LOCKING_PATTERN.matcher( line ).matches();
113                if ( locked )
114                {
115                    currentState++;
116                    break;
117                }
118                error( line );
119                break;
120            case STATE_OP:
121                boolean operation = OPERATION_PATTERN.matcher( line ).matches();
122                if ( operation )
123                {
124                    break;
125                }
126                else if ( COMPLETE_PATTERN.matcher( line ).matches() )
127                {
128                    currentState++;
129                    break;
130                }
131                error( line );
132                break;
133            case STATE_ERROR:
134                error( line );
135                break;
136            default:
137        }
138    }
139
140    private void error( String line )
141    {
142        //        if (currentState != STATE_ERROR) {
143        //            System.out.println("Unable to match: " + line + " State: " +
144        // currentState);
145        //            new Exception().printStackTrace();
146        //        }
147        currentState = STATE_ERROR;
148        errorOutput.println( line );
149    }
150
151    public boolean isSuccess()
152    {
153        return currentState == STATE_COMPLETE;
154    }
155
156    public String getOutput()
157    {
158        errorOutput.flush();
159        errors.flush();
160        return errors.toString();
161    }
162}