1 package org.apache.maven.scm.provider.starteam.command.add;
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 public class StarteamAddConsumer
37 implements StreamConsumer
38 {
39 private ScmLogger logger;
40
41 private String workingDirectory;
42
43
44
45
46 private String currentDir;
47
48 private List<ScmFile> files = new ArrayList<ScmFile>();
49
50
51
52
53 private static final String DIR_MARKER = "(working dir: ";
54
55
56
57
58 private static final String ADDED_MARKER = ": added";
59
60
61
62
63 private static final String LINKTO_MARKER = ": linked to";
64
65 public StarteamAddConsumer( ScmLogger logger, File basedir )
66 {
67 this.logger = logger;
68
69 this.workingDirectory = basedir.getPath().replace( '\\', '/' );
70 }
71
72
73 public void consumeLine( String line )
74 {
75 if ( logger.isDebugEnabled() )
76 {
77 logger.debug( line );
78 }
79
80 int pos = 0;
81
82 if ( ( pos = line.indexOf( DIR_MARKER ) ) != -1 )
83 {
84 processDirectory( line, pos );
85 }
86 else if ( ( pos = line.indexOf( ADDED_MARKER ) ) != -1 )
87 {
88 processAddedFile( line, pos );
89 }
90 else if ( ( pos = line.indexOf( LINKTO_MARKER ) ) != -1 )
91 {
92
93 }
94 else
95 {
96 if ( logger.isWarnEnabled() )
97 {
98 this.logger.warn( "Unknown add ouput: " + line );
99 }
100 }
101 }
102
103 public List<ScmFile> getAddedFiles()
104 {
105 return files;
106 }
107
108 private void processDirectory( String line, int pos )
109 {
110 String dirPath = line.substring( pos + DIR_MARKER.length(), line.length() - 1 ).replace( '\\', '/' );
111
112 try
113 {
114 this.currentDir = StarteamCommandLineUtils.getRelativeChildDirectory( this.workingDirectory, dirPath );
115 }
116 catch ( IllegalStateException e )
117 {
118 String error = "Working and checkout directories are not on the same tree";
119
120 if ( logger.isErrorEnabled() )
121 {
122 logger.error( error );
123 logger.error( "Working directory: " + workingDirectory );
124 logger.error( "Checked out directory: " + dirPath );
125 }
126
127 throw new IllegalStateException( error );
128 }
129 }
130
131 private void processAddedFile( String line, int pos )
132 {
133 String addedFilePath = this.currentDir + "/" + line.substring( 0, pos );
134
135 this.files.add( new ScmFile( addedFilePath, ScmFileStatus.ADDED ) );
136
137 if ( logger.isInfoEnabled() )
138 {
139 logger.info( "Added: " + addedFilePath );
140 }
141 }
142
143 }