Coverage Report - org.apache.maven.changelog.ChangeLogHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
ChangeLogHandler
89% 
100% 
8,25
 
 1  
 package org.apache.maven.changelog;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2006 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *      http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import org.apache.maven.scm.ChangeFile;
 20  
 import org.apache.maven.scm.ChangeSet;
 21  
 import org.apache.maven.scm.command.changelog.ChangeLogSet;
 22  
 import org.xml.sax.Attributes;
 23  
 import org.xml.sax.SAXException;
 24  
 import org.xml.sax.helpers.DefaultHandler;
 25  
 
 26  
 import java.text.ParseException;
 27  
 import java.text.SimpleDateFormat;
 28  
 import java.util.Collection;
 29  
 import java.util.Date;
 30  
 import java.util.LinkedList;
 31  
 import java.util.TimeZone;
 32  
 
 33  
 /**
 34  
  * Change log generated xml parser.  SAXParser listener for processing a previously generated xml into several
 35  
  * change log sets.
 36  
  */
 37  
 public class ChangeLogHandler
 38  
     extends DefaultHandler
 39  
 {
 40  
     private Collection changeSets;
 41  
 
 42  55
     private String bufData = "";
 43  
 
 44  
     private ChangeFile bufFile;
 45  
 
 46  
     private ChangeSet bufEntry;
 47  
 
 48  
     private LinkedList bufEntries;
 49  
 
 50  
     private ChangeLogSet bufSet;
 51  
 
 52  
     private String currentPattern;
 53  
 
 54  
     /**
 55  
      * contructor
 56  
      *
 57  
      * @param changeSets collection object to store all change sets found within the xml document
 58  
      */
 59  
     public ChangeLogHandler( Collection changeSets )
 60  55
     {
 61  55
         this.changeSets = changeSets;
 62  55
     }
 63  
 
 64  
     /**
 65  
      * @see org.xml.sax.helpers.DefaultHandler#characters(char[],int,int)
 66  
      */
 67  
     public void characters( char[] ch, int start, int length )
 68  
         throws SAXException
 69  
     {
 70  3825
         bufData += new String( ch, start, length );
 71  3825
     }
 72  
 
 73  
     /**
 74  
      * @see org.xml.sax.helpers.DefaultHandler#endElement(String,String,String)
 75  
      */
 76  
     public void endElement( String uri, String localName, String qName )
 77  
         throws SAXException
 78  
     {
 79  890
         if ( "changeset".equals( qName ) )
 80  
         {
 81  75
             changeSets.add( bufSet );
 82  
         }
 83  
 
 84  890
         if ( "changelog-entry".equals( qName ) )
 85  
         {
 86  80
             bufEntries.add( bufEntry );
 87  
         }
 88  
 
 89  890
         if ( "file".equals( qName ) )
 90  
         {
 91  120
             bufEntry.addFile( bufFile );
 92  
         }
 93  770
         else if ( "date".equals( qName ) )
 94  
         {
 95  
             try
 96  
             {
 97  80
                 long ms = 0;
 98  80
                 if ( bufEntry.getDate() != null )
 99  
                 {
 100  40
                     ms = bufEntry.getDate().getTime();
 101  
                 }
 102  80
                 bufEntry.setDate( new Date( ms + new SimpleDateFormat( currentPattern ).parse( bufData ).getTime() ) );
 103  
             }
 104  0
             catch ( ParseException e )
 105  
             {
 106  0
                 throw new SAXException( e );
 107  80
             }
 108  
         }
 109  690
         else if ( "time".equals( qName ) )
 110  
         {
 111  
             try
 112  
             {
 113  80
                 long ms = 0;
 114  80
                 if ( bufEntry.getDate() != null )
 115  
                 {
 116  40
                     ms = bufEntry.getDate().getTime();
 117  
                 }
 118  80
                 bufEntry.setDate( new Date( ms + new SimpleDateFormat( currentPattern ).parse( bufData ).getTime() + TimeZone.getDefault().getRawOffset()) );
 119  
             }
 120  0
             catch ( ParseException e )
 121  
             {
 122  0
                 throw new SAXException( e );
 123  80
             }
 124  
         }
 125  610
         else if ( "author".equals( qName ) )
 126  
         {
 127  80
             bufEntry.setAuthor( bufData );
 128  
         }
 129  530
         else if ( "msg".equals( qName ) )
 130  
         {
 131  80
             bufEntry.setComment( bufData );
 132  
         }
 133  
 
 134  890
         if ( "revision".equals( qName ) )
 135  
         {
 136  120
             bufFile.setRevision( bufData );
 137  
         }
 138  770
         else if ( "name".equals( qName ) )
 139  
         {
 140  120
             bufFile.setName( bufData.replaceFirst( " \\(from [^:]+:\\d+\\)", "" ) );
 141  
         }
 142  890
     }
 143  
 
 144  
     /**
 145  
      * @see org.xml.sax.helpers.DefaultHandler#startElement(String,String,String,Attributes)
 146  
      */
 147  
     public void startElement( String uri, String localName, String qName, Attributes attributes )
 148  
         throws SAXException
 149  
     {
 150  890
         bufData = "";
 151  
 
 152  890
         if ( "file".equals( qName ) )
 153  
         {
 154  120
             bufFile = new ChangeFile( "" );
 155  
         }
 156  770
         else if ( "changelog-entry".equals( qName ) )
 157  
         {
 158  80
             bufEntry = new ChangeSet();
 159  
         }
 160  690
         else if ( "date".equals( qName ) )
 161  
         {
 162  80
             currentPattern = attributes.getValue( "pattern" );
 163  80
             if ( currentPattern == null )
 164  
             {
 165  80
                 currentPattern = "yyyy-MM-dd";
 166  
             }
 167  
         }
 168  610
         else if ( "time".equals( qName ) )
 169  
         {
 170  80
             currentPattern = attributes.getValue( "pattern" );
 171  80
             if ( currentPattern == null )
 172  
             {
 173  80
                 currentPattern = "HH:mm:ss";
 174  
             }
 175  
         }
 176  530
         else if ( "changeset".equals( qName ) )
 177  
         {
 178  75
             bufEntries = new LinkedList();
 179  
 
 180  75
             currentPattern = attributes.getValue( "datePattern" );
 181  75
             if ( currentPattern == null )
 182  
             {
 183  40
                 currentPattern = "yyyy-MM-dd";
 184  
             }
 185  
 
 186  75
             SimpleDateFormat formatter = new SimpleDateFormat( currentPattern );
 187  
 
 188  75
             String start = attributes.getValue( "start" );
 189  
 
 190  75
             String end = attributes.getValue( "end" );
 191  
 
 192  75
             Date startDate = null;
 193  
 
 194  75
             Date endDate = null;
 195  
 
 196  75
             if ( start != null )
 197  
             {
 198  
                 try
 199  
                 {
 200  75
                     startDate = formatter.parse( start );
 201  
                 }
 202  0
                 catch ( ParseException e )
 203  
                 {
 204  0
                     throw new SAXException( "Can't parse start date '" + start + "'.", e );
 205  75
                 }
 206  
             }
 207  
 
 208  75
             if ( end != null )
 209  
             {
 210  
                 try
 211  
                 {
 212  75
                     endDate = formatter.parse( end );
 213  
                 }
 214  0
                 catch ( ParseException e )
 215  
                 {
 216  0
                     throw new SAXException( "Can't parse end date '" + end + "'.", e );
 217  75
                 }
 218  
             }
 219  
 
 220  75
             bufSet = new ChangeLogSet( bufEntries, startDate, endDate );
 221  
         }
 222  890
     }
 223  
 }