Coverage Report - org.apache.maven.plugin.jira.JiraXML
 
Classes in this File Line Coverage Branch Coverage Complexity
JiraXML
0%
0/81
0%
0/50
4,429
 
 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: org.apache.maven.plugin.jira.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  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  
         // A Map of releases keyed by fixVersion
 167  0
         Map releasesMap = new HashMap();
 168  
 
 169  
         // Loop through all issues looking for fixVersions
 170  0
         for ( int i = 0; i < issues.size(); i++ )
 171  
         {
 172  0
             JiraIssue issue = (JiraIssue) issues.get( i );
 173  
             // Do NOT create a release for issues that lack a fixVersion
 174  0
             if ( issue.getFixVersion() != null )
 175  
             {
 176  
                 // Try to get a matching Release from the map
 177  0
                 Release release = (Release) releasesMap.get( issue.getFixVersion() );
 178  0
                 if ( release == null )
 179  
                 {
 180  
                     // Add a new Release to the Map if it wasn't there
 181  0
                     release = new Release();
 182  0
                     release.setVersion( issue.getFixVersion() );
 183  0
                     releasesMap.put( issue.getFixVersion(), release );
 184  
                 }
 185  
 
 186  
                 // Add this issue as an Action to this release
 187  0
                 Action action = createAction( issue );
 188  0
                 release.addAction( action );
 189  
             }
 190  
         }
 191  
 
 192  
         // Extract the releases from the Map to a List
 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  
      * 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  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  
         // Set dueTo to the empty String instead of null to make Velocity happy
 232  0
         action.setDueTo( "" );
 233  
         //action.setDueTo( issue.getReporter() );
 234  
 
 235  0
         action.setAction( issue.getSummary() );
 236  0
         return action;
 237  
     }
 238  
 }