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