Coverage Report - org.apache.maven.plugin.jira.JiraXML
 
Classes in this File Line Coverage Branch Coverage Complexity
JiraXML
0%
0/81
0%
0/62
0
 
 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.text.ParseException;
 24  
 import java.text.SimpleDateFormat;
 25  
 import java.util.ArrayList;
 26  
 import java.util.Collections;
 27  
 import java.util.List;
 28  
 import java.util.Locale;
 29  
 
 30  
 import javax.xml.parsers.SAXParser;
 31  
 import javax.xml.parsers.SAXParserFactory;
 32  
 
 33  
 import org.apache.maven.plugin.issues.Issue;
 34  
 import org.apache.maven.plugin.logging.Log;
 35  
 import org.xml.sax.Attributes;
 36  
 import org.xml.sax.SAXException;
 37  
 import org.xml.sax.helpers.DefaultHandler;
 38  
 
 39  
 /**
 40  
  * XML parser that extracts <code>Issue</code>s from JIRA. This works on an XML
 41  
  * file downloaded from JIRA and creates a <code>List</code> of issues that is
 42  
  * exposed to the user of the class.
 43  
  *
 44  
  * @version $Id: org.apache.maven.plugin.jira.JiraXML.html 816601 2012-05-08 12:50:18Z hboutemy $
 45  
  */
 46  
 public class JiraXML
 47  
     extends DefaultHandler
 48  
 {
 49  
     private final List<Issue> issueList;
 50  
 
 51  0
     private final StringBuffer currentElement = new StringBuffer( 1024 );
 52  
 
 53  0
     private String currentParent = "";
 54  
 
 55  
     private final String datePattern;
 56  
 
 57  
     private Issue issue;
 58  
 
 59  0
     private String jiraVersion = null;
 60  
 
 61  
     private final Log log;
 62  
 
 63  0
     private SimpleDateFormat sdf = null;
 64  
 
 65  
     /**
 66  
      *
 67  
      * @param log not null.
 68  
      * @param datePattern may be null.
 69  
      * @since 2.4
 70  
      */
 71  
     public JiraXML( Log log, String datePattern )
 72  0
     {
 73  0
         this.log = log;
 74  0
         this.datePattern = datePattern;
 75  
 
 76  0
         if ( datePattern == null )
 77  
         {
 78  0
             sdf = null;
 79  
         }
 80  
         else
 81  
         {
 82  
             // @todo Do we need to be able to configure the locale of the JIRA server as well?
 83  0
             sdf = new SimpleDateFormat( datePattern, Locale.ENGLISH );
 84  
         }
 85  
 
 86  0
         this.issueList = new ArrayList<Issue>( 16 );
 87  0
     }
 88  
 
 89  
     /**
 90  
      * Parse the given xml file. The list of issues can then be retrieved with {@link #getIssueList()}.
 91  
      *
 92  
      * @param xmlPath the file to pares.
 93  
      *
 94  
      * @since 2.4
 95  
      */
 96  
     public void parseXML( File xmlPath )
 97  
     {
 98  0
         parse( xmlPath );
 99  0
     }
 100  
 
 101  
     private void parse( File xmlPath )
 102  
     {
 103  
         try
 104  
         {
 105  0
             SAXParserFactory factory = SAXParserFactory.newInstance();
 106  0
             SAXParser saxParser = factory.newSAXParser();
 107  
 
 108  0
             saxParser.parse( xmlPath, this );
 109  
         }
 110  0
         catch ( Throwable t )
 111  
         {
 112  0
             log.warn( t );
 113  0
         }
 114  0
     }
 115  
 
 116  
     public void startElement( String namespaceURI, String sName, String qName, Attributes attrs )
 117  
         throws SAXException
 118  
     {
 119  0
         if ( qName.equals( "item" ) )
 120  
         {
 121  0
             issue = new Issue();
 122  
 
 123  0
             currentParent = "item";
 124  
         }
 125  0
         else if ( qName.equals( "key" ) )
 126  
         {
 127  0
             String id = attrs.getValue( "id" );
 128  0
             if ( id != null )
 129  
             {
 130  0
                 issue.setId( id.trim() );
 131  
             }
 132  0
         }
 133  0
         else if ( qName.equals( "build-info" ) )
 134  
         {
 135  0
             currentParent = "build-info";
 136  
         }
 137  0
     }
 138  
 
 139  
     public void endElement( String namespaceURI, String sName, String qName )
 140  
         throws SAXException
 141  
     {
 142  0
         if ( qName.equals( "item" ) )
 143  
         {
 144  0
             issueList.add( issue );
 145  
 
 146  0
             currentParent = "";
 147  
         }
 148  0
         else if ( qName.equals( "key" ) )
 149  
         {
 150  0
             issue.setKey( currentElement.toString().trim() );
 151  
         }
 152  0
         else if ( qName.equals( "summary" ) )
 153  
         {
 154  0
             issue.setSummary( currentElement.toString().trim() );
 155  
         }
 156  0
         else if ( qName.equals( "type" ) )
 157  
         {
 158  0
             issue.setType( currentElement.toString().trim() );
 159  
         }
 160  0
         else if ( qName.equals( "link" ) && currentParent.equals( "item" ) )
 161  
         {
 162  0
             issue.setLink( currentElement.toString().trim() );
 163  
         }
 164  0
         else if ( qName.equals( "priority" ) )
 165  
         {
 166  0
             issue.setPriority( currentElement.toString().trim() );
 167  
         }
 168  0
         else if ( qName.equals( "status" ) )
 169  
         {
 170  0
             issue.setStatus( currentElement.toString().trim() );
 171  
         }
 172  0
         else if ( qName.equals( "resolution" ) )
 173  
         {
 174  0
             issue.setResolution( currentElement.toString().trim() );
 175  
         }
 176  0
         else if ( qName.equals( "assignee" ) )
 177  
         {
 178  0
             issue.setAssignee( currentElement.toString().trim() );
 179  
         }
 180  0
         else if ( qName.equals( "reporter" ) )
 181  
         {
 182  0
             issue.setReporter( currentElement.toString().trim() );
 183  
         }
 184  0
         else if ( qName.equals( "version" ) && currentParent.equals( "item" ) )
 185  
         {
 186  0
             issue.setVersion( currentElement.toString().trim() );
 187  
         }
 188  0
         else if ( qName.equals( "version" ) && currentParent.equals( "build-info" ) )
 189  
         {
 190  0
             jiraVersion = currentElement.toString().trim();
 191  
         }
 192  0
         else if ( qName.equals( "fixVersion" ) )
 193  
         {
 194  0
             issue.addFixVersion( currentElement.toString().trim() );
 195  
         }
 196  0
         else if ( qName.equals( "component" ) )
 197  
         {
 198  0
             issue.addComponent( currentElement.toString().trim() );
 199  
         }
 200  0
         else if ( qName.equals( "comment" ) )
 201  
         {
 202  0
             issue.addComment( currentElement.toString().trim() );
 203  
         }
 204  0
         else if ( qName.equals( "title" ) && currentParent.equals( "item" ) )
 205  
         {
 206  0
             issue.setTitle( currentElement.toString().trim() );
 207  
         }
 208  0
         else if ( qName.equals( "created" ) && currentParent.equals( "item" ) && sdf != null )
 209  
         {
 210  
             try
 211  
             {
 212  0
                 issue.setCreated( sdf.parse( currentElement.toString().trim() ) );
 213  
             }
 214  0
             catch ( ParseException e )
 215  
             {
 216  0
                 log.warn( "Element \"Created\". " + e.getMessage() + ". Using the pattern \"" + datePattern + "\"" );
 217  0
             }
 218  
         }
 219  0
         else if ( qName.equals( "updated" ) && currentParent.equals( "item" ) && sdf != null )
 220  
         {
 221  
             try
 222  
             {
 223  0
                 issue.setUpdated( sdf.parse( currentElement.toString().trim() ) );
 224  
             }
 225  0
             catch ( ParseException e )
 226  
             {
 227  0
                 log.warn( "Element \"Updated\". " + e.getMessage() + ". Using the pattern \"" + datePattern + "\"" );
 228  0
             }
 229  
         }
 230  
 
 231  0
         currentElement.setLength( 0 );
 232  0
     }
 233  
 
 234  
     public void characters( char[] buf, int offset, int len )
 235  
         throws SAXException
 236  
     {
 237  0
         currentElement.append( buf, offset, len );
 238  0
     }
 239  
 
 240  
     public List<Issue> getIssueList()
 241  
     {
 242  0
         return Collections.unmodifiableList( this.issueList );
 243  
     }
 244  
 
 245  
     public String getJiraVersion()
 246  
     {
 247  0
         return jiraVersion;
 248  
     }
 249  
 }