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