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