1 | |
package org.apache.maven.plugin.changelog; |
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.ChangeFile; |
23 | |
import org.apache.maven.scm.ChangeSet; |
24 | |
import org.apache.maven.scm.ScmTag; |
25 | |
import org.apache.maven.scm.command.changelog.ChangeLogSet; |
26 | |
import org.xml.sax.Attributes; |
27 | |
import org.xml.sax.SAXException; |
28 | |
import org.xml.sax.helpers.DefaultHandler; |
29 | |
|
30 | |
import java.text.ParseException; |
31 | |
import java.text.SimpleDateFormat; |
32 | |
import java.util.Collection; |
33 | |
import java.util.Date; |
34 | |
import java.util.LinkedList; |
35 | |
import java.util.TimeZone; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public class ChangeLogHandler |
44 | |
extends DefaultHandler |
45 | |
{ |
46 | |
private Collection changeSets; |
47 | |
|
48 | 44 | private String bufData = ""; |
49 | |
|
50 | |
private ChangeFile bufFile; |
51 | |
|
52 | |
private ChangeSet bufEntry; |
53 | |
|
54 | |
private LinkedList bufEntries; |
55 | |
|
56 | |
private ChangeLogSet bufSet; |
57 | |
|
58 | |
private String currentPattern; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public ChangeLogHandler( Collection changeSets ) |
66 | 44 | { |
67 | 44 | this.changeSets = changeSets; |
68 | 44 | } |
69 | |
|
70 | |
|
71 | |
public void characters( char[] ch, int start, int length ) |
72 | |
throws SAXException |
73 | |
{ |
74 | 1424 | bufData += new String( ch, start, length ); |
75 | 1424 | } |
76 | |
|
77 | |
|
78 | |
public void endElement( String uri, String localName, String qName ) |
79 | |
throws SAXException |
80 | |
{ |
81 | 712 | if ( "changeset".equals( qName ) ) |
82 | |
{ |
83 | 60 | changeSets.add( bufSet ); |
84 | |
} |
85 | |
|
86 | 712 | if ( "changelog-entry".equals( qName ) ) |
87 | |
{ |
88 | 64 | bufEntries.add( bufEntry ); |
89 | |
} |
90 | |
|
91 | 712 | if ( "file".equals( qName ) ) |
92 | |
{ |
93 | 96 | bufEntry.addFile( bufFile ); |
94 | |
} |
95 | 616 | else if ( "date".equals( qName ) ) |
96 | |
{ |
97 | |
try |
98 | |
{ |
99 | 64 | long ms = 0; |
100 | 64 | if ( bufEntry.getDate() != null ) |
101 | |
{ |
102 | 32 | ms = bufEntry.getDate().getTime(); |
103 | |
} |
104 | 64 | bufEntry.setDate( new Date( ms + new SimpleDateFormat( currentPattern ).parse( bufData ).getTime() ) ); |
105 | |
} |
106 | 0 | catch ( ParseException e ) |
107 | |
{ |
108 | 0 | throw new SAXException( e ); |
109 | 64 | } |
110 | |
} |
111 | 552 | else if ( "time".equals( qName ) ) |
112 | |
{ |
113 | |
try |
114 | |
{ |
115 | 64 | long ms = 0; |
116 | 64 | if ( bufEntry.getDate() != null ) |
117 | |
{ |
118 | 32 | ms = bufEntry.getDate().getTime(); |
119 | |
} |
120 | 64 | bufEntry.setDate( new Date( ms + new SimpleDateFormat( currentPattern ).parse( bufData ).getTime() |
121 | |
+ TimeZone.getDefault().getRawOffset() ) ); |
122 | |
} |
123 | 0 | catch ( ParseException e ) |
124 | |
{ |
125 | 0 | throw new SAXException( e ); |
126 | 64 | } |
127 | |
} |
128 | 488 | else if ( "author".equals( qName ) ) |
129 | |
{ |
130 | 64 | bufEntry.setAuthor( bufData ); |
131 | |
} |
132 | 424 | else if ( "msg".equals( qName ) ) |
133 | |
{ |
134 | 64 | bufEntry.setComment( bufData ); |
135 | |
} |
136 | |
|
137 | 712 | if ( "revision".equals( qName ) ) |
138 | |
{ |
139 | 96 | bufFile.setRevision( bufData ); |
140 | |
} |
141 | 616 | else if ( "name".equals( qName ) ) |
142 | |
{ |
143 | 96 | bufFile.setName( bufData.replaceFirst( " \\(from [^:]+:\\d+\\)", "" ) ); |
144 | |
} |
145 | 712 | } |
146 | |
|
147 | |
|
148 | |
public void startElement( String uri, String localName, String qName, Attributes attributes ) |
149 | |
throws SAXException |
150 | |
{ |
151 | 712 | bufData = ""; |
152 | |
|
153 | 712 | if ( "file".equals( qName ) ) |
154 | |
{ |
155 | 96 | bufFile = new ChangeFile( "" ); |
156 | |
} |
157 | 616 | else if ( "changelog-entry".equals( qName ) ) |
158 | |
{ |
159 | 64 | bufEntry = new ChangeSet(); |
160 | |
} |
161 | 552 | else if ( "date".equals( qName ) ) |
162 | |
{ |
163 | 64 | currentPattern = attributes.getValue( "pattern" ); |
164 | 64 | if ( currentPattern == null ) |
165 | |
{ |
166 | 64 | currentPattern = "yyyy-MM-dd"; |
167 | |
} |
168 | |
} |
169 | 488 | else if ( "time".equals( qName ) ) |
170 | |
{ |
171 | 64 | currentPattern = attributes.getValue( "pattern" ); |
172 | 64 | if ( currentPattern == null ) |
173 | |
{ |
174 | 64 | currentPattern = "HH:mm:ss"; |
175 | |
} |
176 | |
} |
177 | 424 | else if ( "changeset".equals( qName ) ) |
178 | |
{ |
179 | 60 | bufEntries = new LinkedList(); |
180 | |
|
181 | 60 | currentPattern = attributes.getValue( "datePattern" ); |
182 | 60 | if ( currentPattern == null ) |
183 | |
{ |
184 | 32 | currentPattern = "yyyy-MM-dd"; |
185 | |
} |
186 | |
|
187 | 60 | SimpleDateFormat formatter = new SimpleDateFormat( currentPattern ); |
188 | |
|
189 | 60 | String start = attributes.getValue( "start" ); |
190 | |
|
191 | 60 | String end = attributes.getValue( "end" ); |
192 | |
|
193 | 60 | Date startDate = null; |
194 | |
|
195 | 60 | Date endDate = null; |
196 | |
|
197 | 60 | if ( start != null ) |
198 | |
{ |
199 | |
try |
200 | |
{ |
201 | 60 | startDate = formatter.parse( start ); |
202 | |
} |
203 | 0 | catch ( ParseException e ) |
204 | |
{ |
205 | 0 | throw new SAXException( "Can't parse start date '" + start + "'.", e ); |
206 | 60 | } |
207 | |
} |
208 | |
|
209 | 60 | if ( end != null ) |
210 | |
{ |
211 | |
try |
212 | |
{ |
213 | 60 | endDate = formatter.parse( end ); |
214 | |
} |
215 | 0 | catch ( ParseException e ) |
216 | |
{ |
217 | 0 | throw new SAXException( "Can't parse end date '" + end + "'.", e ); |
218 | 60 | } |
219 | |
} |
220 | |
|
221 | 60 | bufSet = new ChangeLogSet( bufEntries, startDate, endDate ); |
222 | 60 | bufSet.setStartVersion( new ScmTag( attributes.getValue( "startTag" ) ) ); |
223 | 60 | bufSet.setEndVersion( new ScmTag( attributes.getValue( "endTag" ) ) ); |
224 | |
} |
225 | 712 | } |
226 | |
} |