View Javadoc

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