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   */
31  public class PerforceCheckInConsumer
32      implements StreamConsumer
33  {
34      private static final Pattern CREATED_PATTERN = Pattern.compile( "^Change \\d+ created .+$" );
35  
36      private static final Pattern SUBMITTING_PATTERN = Pattern.compile( "^Submitting change \\d+\\.$" );
37  
38      private static final Pattern LOCKING_PATTERN = Pattern.compile( "^Locking \\d+ files \\.\\.\\.$" );
39  
40      private static final Pattern OPERATION_PATTERN = Pattern.compile( "^[a-z]+ //[^#]+#\\d+$" );
41  
42      // SCM-181 Two possible messages:
43      // "Change 94821 renamed change 94823 and submitted."
44      // "Change 94821 submitted."
45      private static final Pattern COMPLETE_PATTERN = Pattern.compile( "^Change \\d+ .*submitted.$" );
46  
47      public static final int STATE_CREATED = 1;
48  
49      public static final int STATE_SUBMITTING = 2;
50  
51      public static final int STATE_LOCKING = 3;
52  
53      public static final int STATE_OP = 4;
54  
55      public static final int STATE_COMPLETE = 5;
56  
57      public static final int STATE_ERROR = 6;
58  
59      private StringWriter errors = new StringWriter();
60  
61      private PrintWriter errorOutput = new PrintWriter( errors );
62  
63      private int currentState = STATE_CREATED;
64  
65      /*
66       * Change 80835 created with 1 open file(s). Submitting change 80835.
67       * Locking 1 files ... add //depot/modules/cordoba/runtime-ear/foo.xml#1
68       * Change 80835 submitted.
69       */
70      /*
71       * Submitting change 80837. Locking 1 files ... edit
72       * //depot/modules/cordoba/runtime-ear/Foo.java#2 Submit validation failed --
73       * fix problems then use 'p4 submit -c 80837'. 'checkstyle' validation
74       * failed:
75       *
76       * depot/modules/cordoba/runtime-ear/Foo.java:3:1: Got an exception -
77       * expecting EOF, found '}'
78       */
79      /** {@inheritDoc} */
80      public void consumeLine( String line )
81      {
82          if ( line.startsWith( "... " ) )
83          {
84              //TODO log this somehow?
85              //System.out.println("Perforce: " + line);
86              return;
87          }
88  
89          switch ( currentState )
90          {
91              case STATE_CREATED:
92                  boolean created = CREATED_PATTERN.matcher( line ).matches();
93                  if ( created )
94                  {
95                      currentState++;
96                      break;
97                  }
98                  error( line );
99                  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 }