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.apache.regexp.RE;
23 import org.apache.regexp.RESyntaxException;
24 import org.codehaus.plexus.util.cli.StreamConsumer;
25
26 import java.io.PrintWriter;
27 import java.io.StringWriter;
28
29 /**
30 * @author Mike Perham
31 * @version $Id: PerforceChangeLogConsumer.java 331276 2005-11-07 15:04:54Z
32 * evenisse $
33 */
34 public class PerforceCheckInConsumer
35 implements StreamConsumer
36 {
37 private static final String CREATED_PATTERN = "^Change \\d+ created .+$";
38
39 private static final String SUBMITTING_PATTERN = "^Submitting change \\d+\\.$";
40
41 private static final String LOCKING_PATTERN = "^Locking \\d+ files \\.\\.\\.$";
42
43 private static final String OP_PATTERN = "^[a-z]+ //[^#]+#\\d+$";
44
45 // SCM-181 Two possible messages:
46 // "Change 94821 renamed change 94823 and submitted."
47 // "Change 94821 submitted."
48 private static final String COMPLETE_PATTERN = "^Change \\d+ .*submitted.$";
49
50 public static final int STATE_CREATED = 1;
51
52 public static final int STATE_SUBMITTING = 2;
53
54 public static final int STATE_LOCKING = 3;
55
56 public static final int STATE_OP = 4;
57
58 public static final int STATE_COMPLETE = 5;
59
60 public static final int STATE_ERROR = 6;
61
62 private StringWriter errors = new StringWriter();
63
64 private PrintWriter errorOutput = new PrintWriter( errors );
65
66 private int currentState = STATE_CREATED;
67
68 private RE opRegexp;
69
70 public PerforceCheckInConsumer()
71 {
72 try
73 {
74 opRegexp = new RE( OP_PATTERN );
75 }
76 catch ( RESyntaxException ignored )
77 {
78 ignored.printStackTrace();
79 }
80 }
81
82 /*
83 * Change 80835 created with 1 open file(s). Submitting change 80835.
84 * Locking 1 files ... add //depot/modules/cordoba/runtime-ear/foo.xml#1
85 * Change 80835 submitted.
86 */
87 /*
88 * Submitting change 80837. Locking 1 files ... edit
89 * //depot/modules/cordoba/runtime-ear/Foo.java#2 Submit validation failed --
90 * fix problems then use 'p4 submit -c 80837'. 'checkstyle' validation
91 * failed:
92 *
93 * depot/modules/cordoba/runtime-ear/Foo.java:3:1: Got an exception -
94 * expecting EOF, found '}'
95 */
96 /** {@inheritDoc} */
97 public void consumeLine( String line )
98 {
99 if ( line.startsWith( "... " ) )
100 {
101 //TODO log this somehow?
102 //System.out.println("Perforce: " + line);
103 return;
104 }
105
106 switch ( currentState )
107 {
108 case STATE_CREATED:
109 boolean created = new RE( CREATED_PATTERN ).match( line );
110 if ( created )
111 {
112 currentState++;
113 break;
114 }
115 error( line );
116 break;
117 case STATE_SUBMITTING:
118 boolean submitting = new RE( SUBMITTING_PATTERN ).match( line );
119 if ( submitting )
120 {
121 currentState++;
122 break;
123 }
124 error( line );
125 break;
126 case STATE_LOCKING:
127 boolean locked = new RE( LOCKING_PATTERN ).match( line );
128 if ( locked )
129 {
130 currentState++;
131 break;
132 }
133 error( line );
134 break;
135 case STATE_OP:
136 boolean operation = opRegexp.match( line );
137 if ( operation )
138 {
139 break;
140 }
141 else if ( new RE( COMPLETE_PATTERN ).match( line ) )
142 {
143 currentState++;
144 break;
145 }
146 error( line );
147 break;
148 case STATE_ERROR:
149 error( line );
150 break;
151 default:
152 }
153 }
154
155 private void error( String line )
156 {
157 // if (currentState != STATE_ERROR) {
158 // System.out.println("Unable to match: " + line + " State: " +
159 // currentState);
160 // new Exception().printStackTrace();
161 // }
162 currentState = STATE_ERROR;
163 errorOutput.println( line );
164 }
165
166 public boolean isSuccess()
167 {
168 return currentState == STATE_COMPLETE;
169 }
170
171 public String getOutput()
172 {
173 errorOutput.flush();
174 errors.flush();
175 return errors.toString();
176 }
177 }