1 | |
package org.apache.maven.plugin.jira; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.FileInputStream; |
24 | |
import java.io.FileNotFoundException; |
25 | |
import java.io.InputStream; |
26 | |
import java.text.ParseException; |
27 | |
import java.text.SimpleDateFormat; |
28 | |
import java.util.ArrayList; |
29 | |
import java.util.Collections; |
30 | |
import java.util.List; |
31 | |
import java.util.Locale; |
32 | |
|
33 | |
import javax.xml.parsers.SAXParser; |
34 | |
import javax.xml.parsers.SAXParserFactory; |
35 | |
|
36 | |
import org.apache.maven.plugin.MojoExecutionException; |
37 | |
import org.apache.maven.plugin.issues.Issue; |
38 | |
import org.apache.maven.plugin.logging.Log; |
39 | |
import org.codehaus.plexus.util.IOUtil; |
40 | |
import org.xml.sax.Attributes; |
41 | |
import org.xml.sax.InputSource; |
42 | |
import org.xml.sax.SAXException; |
43 | |
import org.xml.sax.helpers.DefaultHandler; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public class JiraXML |
52 | |
extends DefaultHandler |
53 | |
{ |
54 | |
private final List<Issue> issueList; |
55 | |
|
56 | 1 | private final StringBuffer currentElement = new StringBuffer( 1024 ); |
57 | |
|
58 | 1 | private String currentParent = ""; |
59 | |
|
60 | |
private final String datePattern; |
61 | |
|
62 | |
private Issue issue; |
63 | |
|
64 | 1 | private String jiraVersion = null; |
65 | |
|
66 | |
private final Log log; |
67 | |
|
68 | 1 | private SimpleDateFormat sdf = null; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public JiraXML( Log log, String datePattern ) |
76 | 1 | { |
77 | 1 | this.log = log; |
78 | 1 | this.datePattern = datePattern; |
79 | |
|
80 | 1 | if ( datePattern == null ) |
81 | |
{ |
82 | 1 | sdf = null; |
83 | |
} |
84 | |
else |
85 | |
{ |
86 | |
|
87 | 0 | sdf = new SimpleDateFormat( datePattern, Locale.ENGLISH ); |
88 | |
} |
89 | |
|
90 | 1 | this.issueList = new ArrayList<Issue>( 16 ); |
91 | 1 | } |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public void parseXML( File xmlPath ) |
101 | |
throws MojoExecutionException |
102 | |
{ |
103 | 0 | InputStream xmlStream = null; |
104 | |
try |
105 | |
{ |
106 | 0 | xmlStream = new FileInputStream( xmlPath ); |
107 | 0 | InputSource inputSource = new InputSource( xmlStream ); |
108 | 0 | parse( inputSource ); |
109 | |
} |
110 | 0 | catch ( FileNotFoundException e ) |
111 | |
{ |
112 | 0 | throw new MojoExecutionException( "Failed to open JIRA XML file " + xmlPath, e ); |
113 | |
} |
114 | |
finally |
115 | |
{ |
116 | 0 | IOUtil.close( xmlStream ); |
117 | 0 | } |
118 | 0 | } |
119 | |
|
120 | |
void parse( InputSource xmlSource ) |
121 | |
throws MojoExecutionException |
122 | |
{ |
123 | |
try |
124 | |
{ |
125 | 1 | SAXParserFactory factory = SAXParserFactory.newInstance(); |
126 | 1 | SAXParser saxParser = factory.newSAXParser(); |
127 | |
|
128 | 1 | saxParser.parse( xmlSource, this ); |
129 | |
} |
130 | 0 | catch ( Throwable t ) |
131 | |
{ |
132 | 0 | throw new MojoExecutionException( "Failed to parse JIRA XML.", t ); |
133 | 1 | } |
134 | 1 | } |
135 | |
|
136 | |
public void startElement( String namespaceURI, String sName, String qName, Attributes attrs ) |
137 | |
throws SAXException |
138 | |
{ |
139 | 67 | if ( qName.equals( "item" ) ) |
140 | |
{ |
141 | 2 | issue = new Issue(); |
142 | |
|
143 | 2 | currentParent = "item"; |
144 | |
} |
145 | 65 | else if ( qName.equals( "key" ) ) |
146 | |
{ |
147 | 2 | String id = attrs.getValue( "id" ); |
148 | 2 | if ( id != null ) |
149 | |
{ |
150 | 2 | issue.setId( id.trim() ); |
151 | |
} |
152 | 2 | } |
153 | 63 | else if ( qName.equals( "build-info" ) ) |
154 | |
{ |
155 | 1 | currentParent = "build-info"; |
156 | |
} |
157 | 67 | } |
158 | |
|
159 | |
public void endElement( String namespaceURI, String sName, String qName ) |
160 | |
throws SAXException |
161 | |
{ |
162 | 67 | if ( qName.equals( "item" ) ) |
163 | |
{ |
164 | 2 | issueList.add( issue ); |
165 | |
|
166 | 2 | currentParent = ""; |
167 | |
} |
168 | 65 | else if ( qName.equals( "key" ) ) |
169 | |
{ |
170 | 2 | issue.setKey( currentElement.toString().trim() ); |
171 | |
} |
172 | 63 | else if ( qName.equals( "summary" ) ) |
173 | |
{ |
174 | 2 | issue.setSummary( currentElement.toString().trim() ); |
175 | |
} |
176 | 61 | else if ( qName.equals( "type" ) ) |
177 | |
{ |
178 | 2 | issue.setType( currentElement.toString().trim() ); |
179 | |
} |
180 | 59 | else if ( qName.equals( "link" ) && currentParent.equals( "item" ) ) |
181 | |
{ |
182 | 2 | issue.setLink( currentElement.toString().trim() ); |
183 | |
} |
184 | 57 | else if ( qName.equals( "priority" ) ) |
185 | |
{ |
186 | 2 | issue.setPriority( currentElement.toString().trim() ); |
187 | |
} |
188 | 55 | else if ( qName.equals( "status" ) ) |
189 | |
{ |
190 | 2 | issue.setStatus( currentElement.toString().trim() ); |
191 | |
} |
192 | 53 | else if ( qName.equals( "resolution" ) ) |
193 | |
{ |
194 | 2 | issue.setResolution( currentElement.toString().trim() ); |
195 | |
} |
196 | 51 | else if ( qName.equals( "assignee" ) ) |
197 | |
{ |
198 | 2 | issue.setAssignee( currentElement.toString().trim() ); |
199 | |
} |
200 | 49 | else if ( qName.equals( "reporter" ) ) |
201 | |
{ |
202 | 2 | issue.setReporter( currentElement.toString().trim() ); |
203 | |
} |
204 | 47 | else if ( qName.equals( "version" ) && currentParent.equals( "item" ) ) |
205 | |
{ |
206 | 0 | issue.setVersion( currentElement.toString().trim() ); |
207 | |
} |
208 | 47 | else if ( qName.equals( "version" ) && currentParent.equals( "build-info" ) ) |
209 | |
{ |
210 | 1 | jiraVersion = currentElement.toString().trim(); |
211 | |
} |
212 | 46 | else if ( qName.equals( "fixVersion" ) ) |
213 | |
{ |
214 | 0 | issue.addFixVersion( currentElement.toString().trim() ); |
215 | |
} |
216 | 46 | else if ( qName.equals( "component" ) ) |
217 | |
{ |
218 | 0 | issue.addComponent( currentElement.toString().trim() ); |
219 | |
} |
220 | 46 | else if ( qName.equals( "comment" ) ) |
221 | |
{ |
222 | 3 | issue.addComment( currentElement.toString().trim() ); |
223 | |
} |
224 | 43 | else if ( qName.equals( "title" ) && currentParent.equals( "item" ) ) |
225 | |
{ |
226 | 2 | issue.setTitle( currentElement.toString().trim() ); |
227 | |
} |
228 | 41 | else if ( qName.equals( "created" ) && currentParent.equals( "item" ) && sdf != null ) |
229 | |
{ |
230 | |
try |
231 | |
{ |
232 | 0 | issue.setCreated( sdf.parse( currentElement.toString().trim() ) ); |
233 | |
} |
234 | 0 | catch ( ParseException e ) |
235 | |
{ |
236 | 0 | log.warn( "Element \"Created\". " + e.getMessage() + ". Using the pattern \"" + datePattern + "\"" ); |
237 | 0 | } |
238 | |
} |
239 | 41 | else if ( qName.equals( "updated" ) && currentParent.equals( "item" ) && sdf != null ) |
240 | |
{ |
241 | |
try |
242 | |
{ |
243 | 0 | issue.setUpdated( sdf.parse( currentElement.toString().trim() ) ); |
244 | |
} |
245 | 0 | catch ( ParseException e ) |
246 | |
{ |
247 | 0 | log.warn( "Element \"Updated\". " + e.getMessage() + ". Using the pattern \"" + datePattern + "\"" ); |
248 | 0 | } |
249 | |
} |
250 | |
|
251 | 67 | currentElement.setLength( 0 ); |
252 | 67 | } |
253 | |
|
254 | |
public void characters( char[] buf, int offset, int len ) |
255 | |
throws SAXException |
256 | |
{ |
257 | 2736 | currentElement.append( buf, offset, len ); |
258 | 2736 | } |
259 | |
|
260 | |
public List<Issue> getIssueList() |
261 | |
{ |
262 | 1 | return Collections.unmodifiableList( this.issueList ); |
263 | |
} |
264 | |
|
265 | |
public String getJiraVersion() |
266 | |
{ |
267 | 1 | return jiraVersion; |
268 | |
} |
269 | |
} |