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