Coverage Report - org.apache.maven.plugin.changelog.ChangeLogHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
ChangeLogHandler
89%
66/74
90%
38/42
8,25
 
 1  
 package org.apache.maven.plugin.changelog;
 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.scm.ChangeFile;
 23  
 import org.apache.maven.scm.ChangeSet;
 24  
 import org.apache.maven.scm.ScmTag;
 25  
 import org.apache.maven.scm.command.changelog.ChangeLogSet;
 26  
 import org.xml.sax.Attributes;
 27  
 import org.xml.sax.SAXException;
 28  
 import org.xml.sax.helpers.DefaultHandler;
 29  
 
 30  
 import java.text.ParseException;
 31  
 import java.text.SimpleDateFormat;
 32  
 import java.util.Collection;
 33  
 import java.util.Date;
 34  
 import java.util.LinkedList;
 35  
 import java.util.TimeZone;
 36  
 
 37  
 /**
 38  
  * Change log generated xml parser.  SAXParser listener for processing a previously generated xml into several
 39  
  * change log sets.
 40  
  *
 41  
  * @version $Id: ChangeLogHandler.java 803814 2009-08-13 09:24:45Z vsiveton $
 42  
  */
 43  
 public class ChangeLogHandler
 44  
     extends DefaultHandler
 45  
 {
 46  
     private Collection changeSets;
 47  
 
 48  11
     private String bufData = "";
 49  
 
 50  
     private ChangeFile bufFile;
 51  
 
 52  
     private ChangeSet bufEntry;
 53  
 
 54  
     private LinkedList bufEntries;
 55  
 
 56  
     private ChangeLogSet bufSet;
 57  
 
 58  
     private String currentPattern;
 59  
 
 60  
     /**
 61  
      * contructor
 62  
      *
 63  
      * @param changeSets collection object to store all change sets found within the xml document
 64  
      */
 65  
     public ChangeLogHandler( Collection changeSets )
 66  11
     {
 67  11
         this.changeSets = changeSets;
 68  11
     }
 69  
 
 70  
     /** {@inheritDoc} */
 71  
     public void characters( char[] ch, int start, int length )
 72  
         throws SAXException
 73  
     {
 74  356
         bufData += new String( ch, start, length );
 75  356
     }
 76  
 
 77  
     /** {@inheritDoc} */
 78  
     public void endElement( String uri, String localName, String qName )
 79  
         throws SAXException
 80  
     {
 81  178
         if ( "changeset".equals( qName ) )
 82  
         {
 83  15
             changeSets.add( bufSet );
 84  
         }
 85  
 
 86  178
         if ( "changelog-entry".equals( qName ) )
 87  
         {
 88  16
             bufEntries.add( bufEntry );
 89  
         }
 90  
 
 91  178
         if ( "file".equals( qName ) )
 92  
         {
 93  24
             bufEntry.addFile( bufFile );
 94  
         }
 95  154
         else if ( "date".equals( qName ) )
 96  
         {
 97  
             try
 98  
             {
 99  16
                 long ms = 0;
 100  16
                 if ( bufEntry.getDate() != null )
 101  
                 {
 102  8
                     ms = bufEntry.getDate().getTime();
 103  
                 }
 104  16
                 bufEntry.setDate( new Date( ms + new SimpleDateFormat( currentPattern ).parse( bufData ).getTime() ) );
 105  
             }
 106  0
             catch ( ParseException e )
 107  
             {
 108  0
                 throw new SAXException( e );
 109  16
             }
 110  
         }
 111  138
         else if ( "time".equals( qName ) )
 112  
         {
 113  
             try
 114  
             {
 115  16
                 long ms = 0;
 116  16
                 if ( bufEntry.getDate() != null )
 117  
                 {
 118  8
                     ms = bufEntry.getDate().getTime();
 119  
                 }
 120  16
                 bufEntry.setDate( new Date( ms + new SimpleDateFormat( currentPattern ).parse( bufData ).getTime()
 121  
                     + TimeZone.getDefault().getRawOffset() ) );
 122  
             }
 123  0
             catch ( ParseException e )
 124  
             {
 125  0
                 throw new SAXException( e );
 126  16
             }
 127  
         }
 128  122
         else if ( "author".equals( qName ) )
 129  
         {
 130  16
             bufEntry.setAuthor( bufData );
 131  
         }
 132  106
         else if ( "msg".equals( qName ) )
 133  
         {
 134  16
             bufEntry.setComment( bufData );
 135  
         }
 136  
 
 137  178
         if ( "revision".equals( qName ) )
 138  
         {
 139  24
             bufFile.setRevision( bufData );
 140  
         }
 141  154
         else if ( "name".equals( qName ) )
 142  
         {
 143  24
             bufFile.setName( bufData.replaceFirst( " \\(from [^:]+:\\d+\\)", "" ) );
 144  
         }
 145  178
     }
 146  
 
 147  
     /** {@inheritDoc} */
 148  
     public void startElement( String uri, String localName, String qName, Attributes attributes )
 149  
         throws SAXException
 150  
     {
 151  178
         bufData = "";
 152  
 
 153  178
         if ( "file".equals( qName ) )
 154  
         {
 155  24
             bufFile = new ChangeFile( "" );
 156  
         }
 157  154
         else if ( "changelog-entry".equals( qName ) )
 158  
         {
 159  16
             bufEntry = new ChangeSet();
 160  
         }
 161  138
         else if ( "date".equals( qName ) )
 162  
         {
 163  16
             currentPattern = attributes.getValue( "pattern" );
 164  16
             if ( currentPattern == null )
 165  
             {
 166  16
                 currentPattern = "yyyy-MM-dd";
 167  
             }
 168  
         }
 169  122
         else if ( "time".equals( qName ) )
 170  
         {
 171  16
             currentPattern = attributes.getValue( "pattern" );
 172  16
             if ( currentPattern == null )
 173  
             {
 174  16
                 currentPattern = "HH:mm:ss";
 175  
             }
 176  
         }
 177  106
         else if ( "changeset".equals( qName ) )
 178  
         {
 179  15
             bufEntries = new LinkedList();
 180  
 
 181  15
             currentPattern = attributes.getValue( "datePattern" );
 182  15
             if ( currentPattern == null )
 183  
             {
 184  8
                 currentPattern = "yyyy-MM-dd";
 185  
             }
 186  
 
 187  15
             SimpleDateFormat formatter = new SimpleDateFormat( currentPattern );
 188  
 
 189  15
             String start = attributes.getValue( "start" );
 190  
 
 191  15
             String end = attributes.getValue( "end" );
 192  
 
 193  15
             Date startDate = null;
 194  
 
 195  15
             Date endDate = null;
 196  
 
 197  15
             if ( start != null )
 198  
             {
 199  
                 try
 200  
                 {
 201  15
                     startDate = formatter.parse( start );
 202  
                 }
 203  0
                 catch ( ParseException e )
 204  
                 {
 205  0
                     throw new SAXException( "Can't parse start date '" + start + "'.", e );
 206  15
                 }
 207  
             }
 208  
 
 209  15
             if ( end != null )
 210  
             {
 211  
                 try
 212  
                 {
 213  15
                     endDate = formatter.parse( end );
 214  
                 }
 215  0
                 catch ( ParseException e )
 216  
                 {
 217  0
                     throw new SAXException( "Can't parse end date '" + end + "'.", e );
 218  15
                 }
 219  
             }
 220  
 
 221  15
             bufSet = new ChangeLogSet( bufEntries, startDate, endDate );
 222  15
             bufSet.setStartVersion( new ScmTag( attributes.getValue( "startTag" ) ) );
 223  15
             bufSet.setEndVersion( new ScmTag( attributes.getValue( "endTag" ) ) );
 224  
         }
 225  178
     }
 226  
 }