1 package org.apache.maven.scm.provider.starteam.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.log.ScmLogger;
25 import org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils;
26 import org.codehaus.plexus.util.cli.StreamConsumer;
27
28 import java.io.File;
29 import java.util.ArrayList;
30 import java.util.List;
31
32
33
34
35
36
37 public class StarteamCheckOutConsumer
38 implements StreamConsumer
39 {
40 private ScmLogger logger;
41
42 private String workingDirectory;
43
44 private String currentDir = "";
45
46 private List<ScmFile> files = new ArrayList<ScmFile>();
47
48
49
50
51 private static final String DIR_MARKER = "(working dir: ";
52
53
54
55
56 private static final String CHECKOUT_MARKER = ": checked out";
57
58
59
60
61 private static final String SKIPPED_MARKER = ": skipped";
62
63 public StarteamCheckOutConsumer( ScmLogger logger, File workingDirectory )
64 {
65 this.logger = logger;
66
67 this.workingDirectory = workingDirectory.getPath().replace( '\\', '/' );
68 }
69
70
71 public void consumeLine( String line )
72 {
73 if ( logger.isDebugEnabled() )
74 {
75 logger.debug( line );
76 }
77
78 int pos = 0;
79
80 if ( ( pos = line.indexOf( CHECKOUT_MARKER ) ) != -1 )
81 {
82 processCheckedOutFile( line, pos );
83 }
84 else if ( ( pos = line.indexOf( DIR_MARKER ) ) != -1 )
85 {
86 processDirectory( line, pos );
87 }
88 else if ( ( pos = line.indexOf( CHECKOUT_MARKER ) ) != -1 )
89 {
90 processCheckedOutFile( line, pos );
91 }
92 else if ( ( pos = line.indexOf( SKIPPED_MARKER ) ) != -1 )
93 {
94 processSkippedFile( line, pos );
95 }
96 else
97 {
98 if ( logger.isWarnEnabled() )
99 {
100 logger.warn( "Unknown checkout ouput: " + line );
101 }
102 }
103 }
104
105 public List<ScmFile> getCheckedOutFiles()
106 {
107 return files;
108 }
109
110 private void processDirectory( String line, int pos )
111 {
112 String dirPath = line.substring( pos + DIR_MARKER.length(), line.length() - 1 ).replace( '\\', '/' );
113
114 try
115 {
116 this.currentDir = StarteamCommandLineUtils.getRelativeChildDirectory( this.workingDirectory, dirPath );
117 }
118 catch ( IllegalStateException e )
119 {
120 String error = "Working and checkout directories are not on the same tree";
121
122 if ( logger.isErrorEnabled() )
123 {
124 logger.error( error );
125 logger.error( "Working directory: " + workingDirectory );
126 logger.error( "Checked out directory: " + dirPath );
127 }
128
129 throw new IllegalStateException( error );
130 }
131
132 }
133
134 private void processCheckedOutFile( String line, int pos )
135 {
136 String checkedOutFilePath = this.currentDir + "/" + line.substring( 0, pos );
137
138 this.files.add( new ScmFile( checkedOutFilePath, ScmFileStatus.CHECKED_OUT ) );
139
140 if ( logger.isInfoEnabled() )
141 {
142 logger.info( "Checked out: " + checkedOutFilePath );
143 }
144 }
145
146 private void processSkippedFile( String line, int pos )
147 {
148 String skippedFilePath = this.currentDir + "/" + line.substring( 0, pos );
149
150 if ( logger.isDebugEnabled() )
151 {
152 logger.debug( "Skipped: " + skippedFilePath );
153 }
154 }
155
156 }