View Javadoc

1   package org.apache.maven.plugin.jira;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * XML parser for <code>JiraIssue</code>s. This works on an XML file downloaded
40   * from JIRA and creates a List of issues that is exposed to the user of the
41   * class.
42   *
43   * @version $Id: JiraXML.html 816588 2012-05-08 12:37:27Z hboutemy $
44   */
45  public class JiraXML
46      extends DefaultHandler
47  {
48      private List issueList;
49  
50      private StringBuffer currentElement = new StringBuffer( 1024 );
51  
52      private String currentParent = "";
53  
54      private JiraIssue issue;
55  
56      public JiraXML( File xmlPath )
57      {
58          SAXParserFactory factory = SAXParserFactory.newInstance();
59  
60          issueList = new ArrayList();
61  
62          try
63          {
64              SAXParser saxParser = factory.newSAXParser();
65  
66              saxParser.parse( xmlPath, this );
67          }
68          catch ( Throwable t )
69          {
70              t.printStackTrace();
71          }
72      }
73  
74      public void startElement( String namespaceURI, String sName, String qName, Attributes attrs )
75          throws SAXException
76      {
77          if ( qName.equals( "item" ) )
78          {
79              issue = new JiraIssue();
80  
81              currentParent = "item";
82          }
83      }
84  
85      public void endElement( String namespaceURI, String sName, String qName )
86          throws SAXException
87      {
88          if ( qName.equals( "item" ) )
89          {
90              issueList.add( issue );
91  
92              currentParent = "";
93          }
94          else if ( qName.equals( "key" ) )
95          {
96              issue.setKey( currentElement.toString().trim() );
97          }
98          else if ( qName.equals( "summary" ) )
99          {
100             issue.setSummary( currentElement.toString().trim() );
101         }
102         else if ( qName.equals( "type" ) )
103         {
104             issue.setType( currentElement.toString().trim() );
105         }
106         else if ( qName.equals( "link" ) && currentParent.equals( "item" ) )
107         {
108             issue.setLink( currentElement.toString().trim() );
109         }
110         else if ( qName.equals( "priority" ) )
111         {
112             issue.setPriority( currentElement.toString().trim() );
113         }
114         else if ( qName.equals( "status" ) )
115         {
116             issue.setStatus( currentElement.toString().trim() );
117         }
118         else if ( qName.equals( "resolution" ) )
119         {
120             issue.setResolution( currentElement.toString().trim() );
121         }
122         else if ( qName.equals( "assignee" ) )
123         {
124             issue.setAssignee( currentElement.toString().trim() );
125         }
126         else if ( qName.equals( "reporter" ) )
127         {
128             issue.setReporter( currentElement.toString().trim() );
129         }
130         else if ( qName.equals( "version" ) && currentParent.equals( "item" ) )
131         {
132             issue.setVersion( currentElement.toString().trim() );
133         }
134         else if ( qName.equals( "fixVersion" ) )
135         {
136             issue.setFixVersion( currentElement.toString().trim() );
137         }
138         else if ( qName.equals( "component" ) )
139         {
140             issue.setComponent( currentElement.toString().trim() );
141         }
142         else if ( qName.equals( "comment" ) )
143         {
144             issue.addComment( currentElement.toString().trim() );
145         }
146         else if ( qName.equals( "title" ) && currentParent.equals( "item" ) )
147         {
148             issue.setTitle( currentElement.toString().trim() );
149         }
150 
151         currentElement.setLength( 0 );
152     }
153 
154     public void characters( char[] buf, int offset, int len )
155         throws SAXException
156     {
157         currentElement.append( buf, offset, len );
158     }
159 
160     public List getIssueList()
161     {
162         return this.issueList;
163     }
164 
165     public static List getReleases( List issues )
166     {
167         // A Map of releases keyed by fixVersion
168         Map releasesMap = new HashMap();
169 
170         // Loop through all issues looking for fixVersions
171         for ( int i = 0; i < issues.size(); i++ )
172         {
173             JiraIssue issue = (JiraIssue) issues.get( i );
174             // Do NOT create a release for issues that lack a fixVersion
175             if ( issue.getFixVersion() != null )
176             {
177                 // Try to get a matching Release from the map
178                 Release release = (Release) releasesMap.get( issue.getFixVersion() );
179                 if ( release == null )
180                 {
181                     // Add a new Release to the Map if it wasn't there
182                     release = new Release();
183                     release.setVersion( issue.getFixVersion() );
184                     releasesMap.put( issue.getFixVersion(), release );
185                 }
186 
187                 // Add this issue as an Action to this release
188                 Action action = createAction( issue );
189                 release.addAction( action );
190             }
191         }
192 
193         // Extract the releases from the Map to a List
194         List releasesList = new ArrayList();
195         for ( Iterator iterator = releasesMap.entrySet().iterator(); iterator.hasNext(); )
196         {
197             Release o = (Release) ( (Map.Entry) iterator.next() ).getValue();
198             releasesList.add( o );
199         }
200         return releasesList;
201     }
202 
203     /**
204      * Create an <code>Action</code> from a JIRA issue.
205      *
206      * @param issue The issue to extract the information from
207      * @return An <code>Action</code>
208      */
209     private static Action createAction( JiraIssue issue )
210     {
211         Action action = new Action();
212 
213         action.setIssue( issue.getKey() );
214 
215         String type = "";
216         if ( issue.getType().equals( "Bug" ) )
217         {
218             type = "fix";
219         }
220         else if ( issue.getType().equals( "New Feature" ) )
221         {
222             type = "add";
223         }
224         else if ( issue.getType().equals( "Improvement" ) )
225         {
226             type = "update";
227         }
228         action.setType( type );
229 
230         action.setDev( issue.getAssignee() );
231 
232         // Set dueTo to the empty String instead of null to make Velocity happy
233         action.setDueTo( "" );
234         //action.setDueTo( issue.getReporter() );
235 
236         action.setAction( issue.getSummary() );
237         return action;
238     }
239 }