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