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