View Javadoc

1   package org.apache.maven.changes;
2   
3   /* ====================================================================
4    *   Copyright 2001-2004 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  
20  import java.io.File;
21  import java.io.FileNotFoundException;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.OutputStream;
25  import java.io.UnsupportedEncodingException;
26  
27  import java.text.DateFormat;
28  import java.text.ParseException;
29  import java.text.SimpleDateFormat;
30  
31  import java.util.Date;
32  import java.util.Iterator;
33  
34  import org.apache.commons.io.FileUtils;
35  
36  import org.dom4j.Attribute;
37  import org.dom4j.Document;
38  import org.dom4j.DocumentException;
39  import org.dom4j.Element;
40  
41  import org.dom4j.io.OutputFormat;
42  import org.dom4j.io.SAXReader;
43  import org.dom4j.io.XMLWriter;
44  
45  /**
46   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
47   *
48   * @version $Id: ReleaseVersion.java 441044 2006-09-07 10:10:40Z ltheussl $
49   */
50  public class ReleaseVersion
51  {
52      public static final void releaseVersion( File changesFile, String currentVersion, String version, String encoding)
53          throws DocumentException, FileNotFoundException, UnsupportedEncodingException, IOException
54      {
55          DateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
56          releaseVersion( changesFile, currentVersion, version, encoding, dateFormat.format( new Date() ) );
57      }
58  
59      static final Document transformVersion( File changesFile, String currentVersion, String version, String date )
60          throws DocumentException, FileNotFoundException, UnsupportedEncodingException, IOException
61      {
62          SAXReader r = new SAXReader();
63  
64          Document doc = r.read( changesFile );
65  
66          Element root = doc.getRootElement();
67  
68          Element releases = root.element( "body" );
69  
70          for ( Iterator i = releases.elementIterator( "release" ); i.hasNext(); )
71          {
72              Element e = ( Element ) i.next();
73  
74              Attribute v = e.attribute( "version" );
75  
76              if ( v != null )
77              {
78                  String text = v.getText();
79                  Attribute d = e.attribute( "date" );
80                  boolean updateRelease = false;
81                  if ( text.equals( currentVersion ) || text.equals( version ) )
82                  {
83                      updateRelease = true;
84                  }
85                  if ( updateRelease && d != null )
86                  {
87                      DateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
88                      try
89                      {
90                          dateFormat.parse( d.getText() );
91                          // date is set correctly
92                          updateRelease = false;
93                      }
94                      catch ( ParseException ex )
95                      {
96                          // intentional
97                      }
98                  }
99  
100                 if ( updateRelease )
101                 {
102                     v.setText( version );
103 
104                     if ( d == null )
105                     {
106                         e.addAttribute( "date", date );
107                     }
108                     else
109                     {
110                         d.setText( date );
111                     }
112                     return doc;
113                 }
114             }
115         }
116 
117         // indicates no transform necessary
118         return null;
119     }
120 
121     public static final void releaseVersion( File changesFile, String currentVersion, String version, String encoding, String date )
122         throws DocumentException, FileNotFoundException, UnsupportedEncodingException, IOException
123     {
124         Document doc = transformVersion( changesFile, currentVersion, version, date );
125         if ( doc == null )
126         {
127             // no transform necessary
128             return;
129         }
130 
131         // Backup the original first.
132         FileUtils.copyFile( changesFile, new File( changesFile + ".backup" ) );
133 
134         OutputStream os = new FileOutputStream( changesFile );
135 
136         OutputFormat format = new OutputFormat();
137         format.setIndentSize( 2 );
138         format.setNewlines( true );
139         format.setTrimText( true );
140         format.setPadText( true );
141         if ( encoding == null || "".equals( encoding ) )
142         {
143             String docsEncoding = doc.getXMLEncoding();
144             if ( docsEncoding != null )
145             {
146                 format.setEncoding( docsEncoding );
147             }
148         }
149         else
150         {
151             format.setEncoding( encoding );
152         }
153 
154         XMLWriter writer = new XMLWriter( format );
155         writer.setOutputStream( os );
156         writer.write( doc );
157         writer.flush();
158         writer.close();
159     }
160 }
161