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.util.ArrayList; |
24 | |
import java.util.HashMap; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.List; |
27 | |
import java.util.Map; |
28 | |
|
29 | |
import javax.xml.parsers.SAXParser; |
30 | |
import javax.xml.parsers.SAXParserFactory; |
31 | |
|
32 | |
import org.apache.maven.plugins.changes.model.Action; |
33 | |
import org.apache.maven.plugins.changes.model.Release; |
34 | |
import org.xml.sax.Attributes; |
35 | |
import org.xml.sax.SAXException; |
36 | |
import org.xml.sax.helpers.DefaultHandler; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public class JiraXML |
46 | |
extends DefaultHandler |
47 | |
{ |
48 | |
private List issueList; |
49 | |
|
50 | 0 | private StringBuffer currentElement = new StringBuffer( 1024 ); |
51 | |
|
52 | 0 | private String currentParent = ""; |
53 | |
|
54 | |
private JiraIssue issue; |
55 | |
|
56 | |
public JiraXML( File xmlPath ) |
57 | 0 | { |
58 | 0 | SAXParserFactory factory = SAXParserFactory.newInstance(); |
59 | |
|
60 | 0 | issueList = new ArrayList(); |
61 | |
|
62 | |
try |
63 | |
{ |
64 | 0 | SAXParser saxParser = factory.newSAXParser(); |
65 | |
|
66 | 0 | saxParser.parse( xmlPath, this ); |
67 | |
} |
68 | 0 | catch ( Throwable t ) |
69 | |
{ |
70 | 0 | t.printStackTrace(); |
71 | 0 | } |
72 | 0 | } |
73 | |
|
74 | |
public void startElement( String namespaceURI, String sName, String qName, Attributes attrs ) |
75 | |
throws SAXException |
76 | |
{ |
77 | 0 | if ( qName.equals( "item" ) ) |
78 | |
{ |
79 | 0 | issue = new JiraIssue(); |
80 | |
|
81 | 0 | currentParent = "item"; |
82 | |
} |
83 | 0 | } |
84 | |
|
85 | |
public void endElement( String namespaceURI, String sName, String qName ) |
86 | |
throws SAXException |
87 | |
{ |
88 | 0 | if ( qName.equals( "item" ) ) |
89 | |
{ |
90 | 0 | issueList.add( issue ); |
91 | |
|
92 | 0 | currentParent = ""; |
93 | |
} |
94 | 0 | else if ( qName.equals( "key" ) ) |
95 | |
{ |
96 | 0 | issue.setKey( currentElement.toString().trim() ); |
97 | |
} |
98 | 0 | else if ( qName.equals( "summary" ) ) |
99 | |
{ |
100 | 0 | issue.setSummary( currentElement.toString().trim() ); |
101 | |
} |
102 | 0 | else if ( qName.equals( "type" ) ) |
103 | |
{ |
104 | 0 | issue.setType( currentElement.toString().trim() ); |
105 | |
} |
106 | 0 | else if ( qName.equals( "link" ) && currentParent.equals( "item" ) ) |
107 | |
{ |
108 | 0 | issue.setLink( currentElement.toString().trim() ); |
109 | |
} |
110 | 0 | else if ( qName.equals( "priority" ) ) |
111 | |
{ |
112 | 0 | issue.setPriority( currentElement.toString().trim() ); |
113 | |
} |
114 | 0 | else if ( qName.equals( "status" ) ) |
115 | |
{ |
116 | 0 | issue.setStatus( currentElement.toString().trim() ); |
117 | |
} |
118 | 0 | else if ( qName.equals( "resolution" ) ) |
119 | |
{ |
120 | 0 | issue.setResolution( currentElement.toString().trim() ); |
121 | |
} |
122 | 0 | else if ( qName.equals( "assignee" ) ) |
123 | |
{ |
124 | 0 | issue.setAssignee( currentElement.toString().trim() ); |
125 | |
} |
126 | 0 | else if ( qName.equals( "reporter" ) ) |
127 | |
{ |
128 | 0 | issue.setReporter( currentElement.toString().trim() ); |
129 | |
} |
130 | 0 | else if ( qName.equals( "version" ) && currentParent.equals( "item" ) ) |
131 | |
{ |
132 | 0 | issue.setVersion( currentElement.toString().trim() ); |
133 | |
} |
134 | 0 | else if ( qName.equals( "fixVersion" ) ) |
135 | |
{ |
136 | 0 | issue.setFixVersion( currentElement.toString().trim() ); |
137 | |
} |
138 | 0 | else if ( qName.equals( "component" ) ) |
139 | |
{ |
140 | 0 | issue.setComponent( currentElement.toString().trim() ); |
141 | |
} |
142 | 0 | else if ( qName.equals( "comment" ) ) |
143 | |
{ |
144 | 0 | issue.addComment( currentElement.toString().trim() ); |
145 | |
} |
146 | 0 | else if ( qName.equals( "title" ) && currentParent.equals( "item" ) ) |
147 | |
{ |
148 | 0 | issue.setTitle( currentElement.toString().trim() ); |
149 | |
} |
150 | |
|
151 | 0 | currentElement.setLength( 0 ); |
152 | 0 | } |
153 | |
|
154 | |
public void characters( char[] buf, int offset, int len ) |
155 | |
throws SAXException |
156 | |
{ |
157 | 0 | currentElement.append( buf, offset, len ); |
158 | 0 | } |
159 | |
|
160 | |
public List getIssueList() |
161 | |
{ |
162 | 0 | return this.issueList; |
163 | |
} |
164 | |
|
165 | |
public static List getReleases( List issues ) |
166 | |
{ |
167 | |
|
168 | 0 | Map releasesMap = new HashMap(); |
169 | |
|
170 | |
|
171 | 0 | for ( int i = 0; i < issues.size(); i++ ) |
172 | |
{ |
173 | 0 | JiraIssue issue = (JiraIssue) issues.get( i ); |
174 | |
|
175 | 0 | if ( issue.getFixVersion() != null ) |
176 | |
{ |
177 | |
|
178 | 0 | Release release = (Release) releasesMap.get( issue.getFixVersion() ); |
179 | 0 | if ( release == null ) |
180 | |
{ |
181 | |
|
182 | 0 | release = new Release(); |
183 | 0 | release.setVersion( issue.getFixVersion() ); |
184 | 0 | releasesMap.put( issue.getFixVersion(), release ); |
185 | |
} |
186 | |
|
187 | |
|
188 | 0 | Action action = createAction( issue ); |
189 | 0 | release.addAction( action ); |
190 | |
} |
191 | |
} |
192 | |
|
193 | |
|
194 | 0 | List releasesList = new ArrayList(); |
195 | 0 | for ( Iterator iterator = releasesMap.entrySet().iterator(); iterator.hasNext(); ) |
196 | |
{ |
197 | 0 | Release o = (Release) ( (Map.Entry) iterator.next() ).getValue(); |
198 | 0 | releasesList.add( o ); |
199 | |
} |
200 | 0 | return releasesList; |
201 | |
} |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
private static Action createAction( JiraIssue issue ) |
210 | |
{ |
211 | 0 | Action action = new Action(); |
212 | |
|
213 | 0 | action.setIssue( issue.getKey() ); |
214 | |
|
215 | 0 | String type = ""; |
216 | 0 | if ( issue.getType().equals( "Bug" ) ) |
217 | |
{ |
218 | 0 | type = "fix"; |
219 | |
} |
220 | 0 | else if ( issue.getType().equals( "New Feature" ) ) |
221 | |
{ |
222 | 0 | type = "add"; |
223 | |
} |
224 | 0 | else if ( issue.getType().equals( "Improvement" ) ) |
225 | |
{ |
226 | 0 | type = "update"; |
227 | |
} |
228 | 0 | action.setType( type ); |
229 | |
|
230 | 0 | action.setDev( issue.getAssignee() ); |
231 | |
|
232 | |
|
233 | 0 | action.setDueTo( "" ); |
234 | |
|
235 | |
|
236 | 0 | action.setAction( issue.getSummary() ); |
237 | 0 | return action; |
238 | |
} |
239 | |
} |