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