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