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 public class PerforceCheckOutConsumer
37 extends AbstractPerforceConsumer
38 implements StreamConsumer
39 {
40 public static final int STATE_CLIENTSPEC = 0;
41
42 public static final int STATE_NORMAL = 1;
43
44 public static final int STATE_ERROR = 2;
45
46 private int currentState = STATE_CLIENTSPEC;
47
48 private Pattern fileRegexp = Pattern.compile( "([^#]+)#\\d+ - ([a-z]+)" );
49
50 private List<ScmFile> checkedout = new ArrayList<ScmFile>();
51
52 private String repo = null;
53
54 private String specname = null;
55
56 public PerforceCheckOutConsumer( String clientspec, String repoPath )
57 {
58 repo = repoPath;
59 specname = clientspec;
60 }
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 public void consumeLine( String line )
86 {
87 if ( currentState == STATE_CLIENTSPEC
88 && ( line.startsWith( "Client " + specname + " saved." ) || line.startsWith( "Client " + specname
89 + " not changed." ) ) )
90 {
91 currentState = STATE_NORMAL;
92 return;
93 }
94
95
96 if ( currentState == STATE_NORMAL && line.indexOf( "ile(s) up-to-date" ) != -1 )
97 {
98 return;
99 }
100
101 Matcher matcher;
102 if ( currentState != STATE_ERROR && ( matcher = fileRegexp.matcher( line ) ).find() )
103 {
104 String location = matcher.group( 1 );
105 if ( location.startsWith( repo ) )
106 {
107 location = location.substring( repo.length() + 1 );
108 }
109 ScmFileStatus status = PerforceVerbMapper.toStatus( matcher.group( 2 ) );
110 if ( status != null )
111 {
112
113
114
115 checkedout.add( new ScmFile( location, status ) );
116 }
117 return;
118 }
119
120 error( line );
121 }
122
123 private void error( String line )
124 {
125 currentState = STATE_ERROR;
126 output.println( line );
127 }
128
129 public boolean isSuccess()
130 {
131 return currentState == STATE_NORMAL;
132 }
133
134 public List<ScmFile> getCheckedout()
135 {
136 return checkedout;
137 }
138 }