View Javadoc
1   package org.apache.maven.scm.provider.perforce.command.checkin;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.codehaus.plexus.util.cli.StreamConsumer;
23  
24  import java.io.PrintWriter;
25  import java.io.StringWriter;
26  import java.util.regex.Pattern;
27  
28  /**
29   * @author Mike Perham
30   * @version $Id: PerforceChangeLogConsumer.java 331276 2005-11-07 15:04:54Z
31   *          evenisse $
32   */
33  public class PerforceCheckInConsumer
34      implements StreamConsumer
35  {
36      private static final Pattern CREATED_PATTERN = Pattern.compile( "^Change \\d+ created .+$" );
37  
38      private static final Pattern SUBMITTING_PATTERN = Pattern.compile( "^Submitting change \\d+\\.$" );
39  
40      private static final Pattern LOCKING_PATTERN = Pattern.compile( "^Locking \\d+ files \\.\\.\\.$" );
41  
42      private static final Pattern OPERATION_PATTERN = Pattern.compile( "^[a-z]+ //[^#]+#\\d+$" );
43  
44      // SCM-181 Two possible messages:
45      // "Change 94821 renamed change 94823 and submitted."
46      // "Change 94821 submitted."
47      private static final Pattern COMPLETE_PATTERN = Pattern.compile( "^Change \\d+ .*submitted.$" );
48  
49      public static final int STATE_CREATED = 1;
50  
51      public static final int STATE_SUBMITTING = 2;
52  
53      public static final int STATE_LOCKING = 3;
54  
55      public static final int STATE_OP = 4;
56  
57      public static final int STATE_COMPLETE = 5;
58  
59      public static final int STATE_ERROR = 6;
60  
61      private StringWriter errors = new StringWriter();
62  
63      private PrintWriter errorOutput = new PrintWriter( errors );
64  
65      private int currentState = STATE_CREATED;
66  
67      /*
68       * Change 80835 created with 1 open file(s). Submitting change 80835.
69       * Locking 1 files ... add //depot/modules/cordoba/runtime-ear/foo.xml#1
70       * Change 80835 submitted.
71       */
72      /*
73       * Submitting change 80837. Locking 1 files ... edit
74       * //depot/modules/cordoba/runtime-ear/Foo.java#2 Submit validation failed --
75       * fix problems then use 'p4 submit -c 80837'. 'checkstyle' validation
76       * failed:
77       *
78       * depot/modules/cordoba/runtime-ear/Foo.java:3:1: Got an exception -
79       * expecting EOF, found '}'
80       */
81      /** {@inheritDoc} */
82      public void consumeLine( String line )
83      {
84          if ( line.startsWith( "... " ) )
85          {
86              //TODO log this somehow?
87              //System.out.println("Perforce: " + line);
88              return;
89          }
90  
91          switch ( currentState )
92          {
93              case STATE_CREATED:
94                  boolean created = CREATED_PATTERN.matcher( line ).matches();
95                  if ( created )
96                  {
97                      currentState++;
98                      break;
99                  }
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 }