1 | |
package org.apache.maven.scm.provider.tfs.command.consumer; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.text.DateFormat; |
23 | |
import java.text.ParseException; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.Date; |
26 | |
import java.util.List; |
27 | |
import java.util.Locale; |
28 | |
import java.util.TimeZone; |
29 | |
import java.util.regex.Matcher; |
30 | |
import java.util.regex.Pattern; |
31 | |
|
32 | |
import org.apache.maven.scm.ChangeFile; |
33 | |
import org.apache.maven.scm.ChangeSet; |
34 | |
import org.apache.maven.scm.log.ScmLogger; |
35 | |
import org.apache.maven.scm.util.AbstractConsumer; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class TfsChangeLogConsumer |
42 | |
extends AbstractConsumer |
43 | |
{ |
44 | |
|
45 | |
private static final String PATTERN = |
46 | |
"^[^:]*:[ \t]([0-9]*)\n" + "[^:]*:[ \t](.*)\n[^:]*:[ \t](.*)\n" |
47 | |
+ "[^:]*:((?:\n.*)*)\n\n[^\n :]*:(?=\n )((?:\n[ \t]+.*)*)"; |
48 | |
|
49 | |
private static final String PATTERN_ITEM = "\n ([^$]+) (\\$/.*)"; |
50 | |
|
51 | 0 | private List<ChangeSet> logs = new ArrayList<ChangeSet>(); |
52 | |
|
53 | 0 | private String buffer = ""; |
54 | |
|
55 | 0 | boolean fed = false; |
56 | |
|
57 | |
public TfsChangeLogConsumer( ScmLogger logger ) |
58 | |
{ |
59 | 0 | super( logger ); |
60 | 0 | } |
61 | |
|
62 | |
public void consumeLine( String line ) |
63 | |
{ |
64 | 0 | fed = true; |
65 | 0 | if ( line.startsWith( "-----" ) ) |
66 | |
{ |
67 | 0 | addChangeLog(); |
68 | |
} |
69 | 0 | buffer += line + "\n"; |
70 | 0 | } |
71 | |
|
72 | |
public List<ChangeSet> getLogs() |
73 | |
{ |
74 | 0 | addChangeLog(); |
75 | 0 | return logs; |
76 | |
} |
77 | |
|
78 | |
private void addChangeLog() |
79 | |
{ |
80 | 0 | if ( !buffer.equals( "" ) ) |
81 | |
{ |
82 | 0 | Pattern p = Pattern.compile( PATTERN ); |
83 | 0 | Matcher m = p.matcher( buffer ); |
84 | 0 | if ( m.find() ) |
85 | |
{ |
86 | 0 | String revision = m.group( 1 ).trim(); |
87 | 0 | String username = m.group( 2 ).trim(); |
88 | 0 | String dateString = m.group( 3 ).trim(); |
89 | 0 | String comment = m.group( 4 ).trim(); |
90 | 0 | Pattern itemPattern = Pattern.compile( PATTERN_ITEM ); |
91 | 0 | Matcher itemMatcher = itemPattern.matcher( m.group( 5 ) ); |
92 | 0 | List<ChangeFile> files = new ArrayList<ChangeFile>(); |
93 | 0 | while ( itemMatcher.find() ) |
94 | |
{ |
95 | 0 | ChangeFile file = new ChangeFile( itemMatcher.group( 2 ).trim(), revision ); |
96 | 0 | files.add( file ); |
97 | 0 | } |
98 | |
Date date; |
99 | |
try |
100 | |
{ |
101 | 0 | date = parseDate( dateString ); |
102 | |
} |
103 | 0 | catch ( ParseException e ) |
104 | |
{ |
105 | 0 | getLogger().error( "Date parse error", e ); |
106 | 0 | throw new RuntimeException( e ); |
107 | 0 | } |
108 | |
|
109 | 0 | ChangeSet change = new ChangeSet( date, comment, username, files ); |
110 | 0 | logs.add( change ); |
111 | |
} |
112 | 0 | buffer = ""; |
113 | |
} |
114 | 0 | } |
115 | |
|
116 | |
public boolean hasBeenFed() |
117 | |
{ |
118 | 0 | return fed; |
119 | |
} |
120 | |
|
121 | |
@SuppressWarnings( "deprecation" ) |
122 | |
protected static Date parseDate( String dateString ) |
123 | |
throws ParseException |
124 | |
{ |
125 | 0 | Date date = null; |
126 | |
try |
127 | |
{ |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | 0 | date = new Date( Date.parse( dateString ) ); |
134 | |
} |
135 | 0 | catch ( IllegalArgumentException e ) |
136 | |
{ |
137 | |
|
138 | 0 | } |
139 | 0 | if ( date == null ) |
140 | |
{ |
141 | |
|
142 | |
|
143 | |
|
144 | 0 | DateFormat[] formats = createDateFormatsForLocaleAndTimeZone( null, null ); |
145 | 0 | return parseWithFormats( dateString, formats ); |
146 | |
} |
147 | 0 | return date; |
148 | |
} |
149 | |
|
150 | |
private static Date parseWithFormats( String input, DateFormat[] formats ) |
151 | |
throws ParseException |
152 | |
{ |
153 | 0 | ParseException parseException = null; |
154 | 0 | for ( int i = 0; i < formats.length; i++ ) |
155 | |
{ |
156 | |
try |
157 | |
{ |
158 | 0 | return formats[i].parse( input ); |
159 | |
} |
160 | 0 | catch ( ParseException ex ) |
161 | |
{ |
162 | 0 | parseException = ex; |
163 | |
} |
164 | |
} |
165 | |
|
166 | 0 | throw parseException; |
167 | |
} |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
private static DateFormat[] createDateFormatsForLocaleAndTimeZone( Locale locale, TimeZone timeZone ) |
173 | |
{ |
174 | 0 | if ( locale == null ) |
175 | |
{ |
176 | 0 | locale = Locale.getDefault(); |
177 | |
} |
178 | |
|
179 | 0 | if ( timeZone == null ) |
180 | |
{ |
181 | 0 | timeZone = TimeZone.getDefault(); |
182 | |
} |
183 | |
|
184 | 0 | List<DateFormat> formats = new ArrayList<DateFormat>(); |
185 | |
|
186 | 0 | for ( int dateStyle = DateFormat.FULL; dateStyle <= DateFormat.SHORT; dateStyle++ ) |
187 | |
{ |
188 | 0 | for ( int timeStyle = DateFormat.FULL; timeStyle <= DateFormat.SHORT; timeStyle++ ) |
189 | |
{ |
190 | 0 | DateFormat df = DateFormat.getDateTimeInstance( dateStyle, timeStyle, locale ); |
191 | 0 | if ( timeZone != null ) |
192 | |
{ |
193 | 0 | df.setTimeZone( timeZone ); |
194 | |
} |
195 | 0 | formats.add( df ); |
196 | |
} |
197 | |
} |
198 | |
|
199 | 0 | for ( int dateStyle = DateFormat.FULL; dateStyle <= DateFormat.SHORT; dateStyle++ ) |
200 | |
{ |
201 | 0 | DateFormat df = DateFormat.getDateInstance( dateStyle, locale ); |
202 | 0 | df.setTimeZone( timeZone ); |
203 | 0 | formats.add( df ); |
204 | |
} |
205 | |
|
206 | 0 | return (DateFormat[]) formats.toArray( new DateFormat[formats.size()] ); |
207 | |
} |
208 | |
|
209 | |
} |