1 package org.apache.maven.scm.provider.cvslib.command.diff;
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.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33
34
35
36
37
38
39 public class CvsDiffConsumer
40 implements StreamConsumer
41 {
42
43
44
45
46
47
48
49
50
51
52
53 private static final String RCS_TOKEN = "RCS file: ";
54
55 private static final String RETRIEVING_TOKEN = "retrieving revision ";
56
57 private static final String DIFF_TOKEN = "diff ";
58
59 private static final String INDEX_TOKEN = "Index: ";
60
61 private static final String FILE_SEPARATOR_TOKEN = "===";
62
63 private static final String START_REVISION_TOKEN = "---";
64
65 private static final String END_REVISION_TOKEN = "+++";
66
67 private static final String ADDED_LINE_TOKEN = "+";
68
69 private static final String REMOVED_LINE_TOKEN = "-";
70
71 private static final String UNCHANGED_LINE_TOKEN = " ";
72
73 private static final String CHANGE_SEPARATOR_TOKEN = "@@";
74
75 private static final String NO_NEWLINE_TOKEN = "\\ No newline at end of file";
76
77 private ScmLogger logger;
78
79 private String currentFile;
80
81 private StringBuilder currentDifference;
82
83 private List<ScmFile> changedFiles = new ArrayList<ScmFile>();
84
85 private Map<String, CharSequence> differences = new HashMap<String, CharSequence>();
86
87 private StringBuilder patch = new StringBuilder();
88
89
90
91
92
93 public CvsDiffConsumer( ScmLogger logger, File workingDirectory )
94 {
95 this.logger = logger;
96 }
97
98
99
100
101
102
103 public void consumeLine( String line )
104 {
105 if ( logger.isDebugEnabled() )
106 {
107 logger.debug( line );
108 }
109
110 if ( line.startsWith( INDEX_TOKEN ) )
111 {
112
113 currentFile = line.substring( INDEX_TOKEN.length() );
114
115 changedFiles.add( new ScmFile( currentFile, ScmFileStatus.MODIFIED ) );
116
117 currentDifference = new StringBuilder();
118
119 differences.put( currentFile, currentDifference );
120
121 patch.append( line ).append( "\n" );
122
123 return;
124 }
125
126 if ( currentFile == null )
127 {
128 if ( logger.isWarnEnabled() )
129 {
130 logger.warn( "Unparseable line: '" + line + "'" );
131 }
132 patch.append( line ).append( "\n" );
133 return;
134 }
135
136 if ( line.startsWith( FILE_SEPARATOR_TOKEN ) )
137 {
138
139 patch.append( line ).append( "\n" );
140 }
141 else if ( line.startsWith( START_REVISION_TOKEN ) )
142 {
143
144 patch.append( line ).append( "\n" );
145 }
146 else if ( line.startsWith( END_REVISION_TOKEN ) )
147 {
148
149 patch.append( line ).append( "\n" );
150 }
151 else if ( line.startsWith( RCS_TOKEN ) )
152 {
153
154 }
155 else if ( line.startsWith( RETRIEVING_TOKEN ) )
156 {
157
158 }
159 else if ( line.startsWith( DIFF_TOKEN ) )
160 {
161
162 }
163 else if ( line.startsWith( ADDED_LINE_TOKEN ) || line.startsWith( REMOVED_LINE_TOKEN )
164 || line.startsWith( UNCHANGED_LINE_TOKEN ) || line.startsWith( CHANGE_SEPARATOR_TOKEN )
165 || line.equals( NO_NEWLINE_TOKEN ) )
166 {
167
168 currentDifference.append( line ).append( "\n" );
169 patch.append( line ).append( "\n" );
170 }
171 else
172 {
173 if ( logger.isWarnEnabled() )
174 {
175 logger.warn( "Unparseable line: '" + line + "'" );
176 }
177 patch.append( line ).append( "\n" );
178
179 currentFile = null;
180 currentDifference = null;
181 }
182 }
183
184 public List<ScmFile> getChangedFiles()
185 {
186 return changedFiles;
187 }
188
189 public Map<String, CharSequence> getDifferences()
190 {
191 return differences;
192 }
193
194 public String getPatch()
195 {
196 return patch.toString();
197 }
198
199 }