1 | |
package org.apache.maven.scm.provider.perforce.command.checkin; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
31 | |
|
32 | |
|
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 | |
|
46 | |
|
47 | |
|
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 | 3 | private StringWriter errors = new StringWriter(); |
63 | |
|
64 | 3 | private PrintWriter errorOutput = new PrintWriter( errors ); |
65 | |
|
66 | 3 | private int currentState = STATE_CREATED; |
67 | |
|
68 | |
private RE opRegexp; |
69 | |
|
70 | |
public PerforceCheckInConsumer() |
71 | 3 | { |
72 | |
try |
73 | |
{ |
74 | 3 | opRegexp = new RE( OP_PATTERN ); |
75 | |
} |
76 | 0 | catch ( RESyntaxException ignored ) |
77 | |
{ |
78 | 0 | ignored.printStackTrace(); |
79 | 3 | } |
80 | 3 | } |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public void consumeLine( String line ) |
98 | |
{ |
99 | 18 | if ( line.startsWith( "... " ) ) |
100 | |
{ |
101 | |
|
102 | |
|
103 | 0 | return; |
104 | |
} |
105 | |
|
106 | 18 | switch ( currentState ) |
107 | |
{ |
108 | |
case STATE_CREATED: |
109 | 3 | boolean created = new RE( CREATED_PATTERN ).match( line ); |
110 | 3 | if ( created ) |
111 | |
{ |
112 | 3 | currentState++; |
113 | 3 | break; |
114 | |
} |
115 | 0 | error( line ); |
116 | 0 | break; |
117 | |
case STATE_SUBMITTING: |
118 | 3 | boolean submitting = new RE( SUBMITTING_PATTERN ).match( line ); |
119 | 3 | if ( submitting ) |
120 | |
{ |
121 | 3 | currentState++; |
122 | 3 | break; |
123 | |
} |
124 | 0 | error( line ); |
125 | 0 | break; |
126 | |
case STATE_LOCKING: |
127 | 3 | boolean locked = new RE( LOCKING_PATTERN ).match( line ); |
128 | 3 | if ( locked ) |
129 | |
{ |
130 | 3 | currentState++; |
131 | 3 | break; |
132 | |
} |
133 | 0 | error( line ); |
134 | 0 | break; |
135 | |
case STATE_OP: |
136 | 6 | boolean operation = opRegexp.match( line ); |
137 | 6 | if ( operation ) |
138 | |
{ |
139 | 3 | break; |
140 | |
} |
141 | 3 | else if ( new RE( COMPLETE_PATTERN ).match( line ) ) |
142 | |
{ |
143 | 2 | currentState++; |
144 | 2 | break; |
145 | |
} |
146 | 1 | error( line ); |
147 | 1 | break; |
148 | |
case STATE_ERROR: |
149 | 3 | error( line ); |
150 | 3 | break; |
151 | |
default: |
152 | |
} |
153 | 18 | } |
154 | |
|
155 | |
private void error( String line ) |
156 | |
{ |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | 4 | currentState = STATE_ERROR; |
163 | 4 | errorOutput.println( line ); |
164 | 4 | } |
165 | |
|
166 | |
public boolean isSuccess() |
167 | |
{ |
168 | 3 | return currentState == STATE_COMPLETE; |
169 | |
} |
170 | |
|
171 | |
public String getOutput() |
172 | |
{ |
173 | 7 | errorOutput.flush(); |
174 | 7 | errors.flush(); |
175 | 7 | return errors.toString(); |
176 | |
} |
177 | |
} |