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