1 package org.apache.maven.scm.provider.starteam.command.unedit;
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.codehaus.plexus.util.cli.StreamConsumer;
26
27 import java.io.File;
28 import java.util.ArrayList;
29 import java.util.List;
30
31
32
33
34
35
36 public class StarteamUnEditConsumer
37 implements StreamConsumer
38 {
39 private String workingDirectory;
40
41 private ScmLogger logger;
42
43 private List<ScmFile> files = new ArrayList<ScmFile>();
44
45
46
47
48 private String currentDir = "";
49
50
51
52
53 private static final String DIR_MARKER = "(working dir: ";
54
55
56
57
58 private static final String UNLOCKED_MARKER = ": unlocked";
59
60
61 public StarteamUnEditConsumer( ScmLogger logger, File basedir )
62 {
63 this.logger = logger;
64 this.workingDirectory = basedir.getPath().replace( '\\', '/' );
65 }
66
67
68 public void consumeLine( String line )
69 {
70 if ( logger.isDebugEnabled() )
71 {
72 logger.debug( line );
73 }
74
75 int pos = 0;
76
77 if ( ( pos = line.indexOf( DIR_MARKER ) ) != -1 )
78 {
79 processDirectory( line, pos );
80 }
81 else if ( ( pos = line.indexOf( UNLOCKED_MARKER ) ) != -1 )
82 {
83 processUnLockedFile( line, pos );
84 }
85 else
86 {
87 if ( logger.isWarnEnabled() )
88 {
89 logger.warn( "Unknown unedit ouput: " + line );
90 }
91 }
92 }
93
94 public List<ScmFile> getUnEditFiles()
95 {
96 return files;
97 }
98
99 private void processDirectory( String line, int pos )
100 {
101 String dirPath = line.substring( pos + DIR_MARKER.length(), line.length() - 1 ).replace( '\\', '/' );
102
103 if ( !dirPath.startsWith( workingDirectory ) )
104 {
105 if ( logger.isInfoEnabled() )
106 {
107 logger.info( "Working directory: " + workingDirectory );
108 logger.info( "unedit directory: " + dirPath );
109 }
110
111 throw new IllegalStateException( "Working and unedit directories are not on the same tree" );
112 }
113
114 this.currentDir = "." + dirPath.substring( workingDirectory.length() );
115 }
116
117 private void processUnLockedFile( String line, int pos )
118 {
119 String lockedFilePath = this.currentDir + "/" + line.substring( 0, pos );
120
121 this.files.add( new ScmFile( lockedFilePath, ScmFileStatus.UNKNOWN ) );
122
123 if ( logger.isInfoEnabled() )
124 {
125 logger.info( "Unlocked: " + lockedFilePath );
126 }
127 }
128
129
130 }