1 package org.apache.maven.scm.provider.perforce.command.checkout;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.scm.ScmFile;
23 import org.apache.maven.scm.ScmFileStatus;
24 import org.apache.maven.scm.provider.perforce.command.AbstractPerforceConsumer;
25 import org.apache.maven.scm.provider.perforce.command.PerforceVerbMapper;
26 import org.codehaus.plexus.util.cli.StreamConsumer;
27
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32
33
34
35
36
37
38 public class PerforceCheckOutConsumer
39 extends AbstractPerforceConsumer
40 implements StreamConsumer
41 {
42 public static final int STATE_CLIENTSPEC = 0;
43
44 public static final int STATE_NORMAL = 1;
45
46 public static final int STATE_ERROR = 2;
47
48 private int currentState = STATE_CLIENTSPEC;
49
50 private Pattern fileRegexp = Pattern.compile( "([^#]+)#\\d+ - ([a-z]+)" );
51
52 private List<ScmFile> checkedout = new ArrayList<ScmFile>();
53
54 private String repo = null;
55
56 private String specname = null;
57
58 public PerforceCheckOutConsumer( String clientspec, String repoPath )
59 {
60 repo = repoPath;
61 specname = clientspec;
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 public void consumeLine( String line )
88 {
89 if ( currentState == STATE_CLIENTSPEC
90 && ( line.startsWith( "Client " + specname + " saved." ) || line.startsWith( "Client " + specname
91 + " not changed." ) ) )
92 {
93 currentState = STATE_NORMAL;
94 return;
95 }
96
97
98 if ( currentState == STATE_NORMAL && line.indexOf( "ile(s) up-to-date" ) != -1 )
99 {
100 return;
101 }
102
103 Matcher matcher;
104 if ( currentState != STATE_ERROR && ( matcher = fileRegexp.matcher( line ) ).find() )
105 {
106 String location = matcher.group( 1 );
107 if ( location.startsWith( repo ) )
108 {
109 location = location.substring( repo.length() + 1 );
110 }
111 ScmFileStatus status = PerforceVerbMapper.toStatus( matcher.group( 2 ) );
112 if ( status != null )
113 {
114
115
116
117 checkedout.add( new ScmFile( location, status ) );
118 }
119 return;
120 }
121
122 error( line );
123 }
124
125 private void error( String line )
126 {
127 currentState = STATE_ERROR;
128 output.println( line );
129 }
130
131 public boolean isSuccess()
132 {
133 return currentState == STATE_NORMAL;
134 }
135
136 public List<ScmFile> getCheckedout()
137 {
138 return checkedout;
139 }
140 }