View Javadoc
1   package org.apache.maven.plugin.changes;
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 java.io.File;
23  import java.io.FileInputStream;
24  import java.util.Collections;
25  import java.util.List;
26  
27  /*
28   * Licensed to the Apache Software Foundation (ASF) under one
29   * or more contributor license agreements.  See the NOTICE file
30   * distributed with this work for additional information
31   * regarding copyright ownership.  The ASF licenses this file
32   * to you under the Apache License, Version 2.0 (the
33   * "License"); you may not use this file except in compliance
34   * with the License.  You may obtain a copy of the License at
35   *
36   *   http://www.apache.org/licenses/LICENSE-2.0
37   *
38   * Unless required by applicable law or agreed to in writing,
39   * software distributed under the License is distributed on an
40   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
41   * KIND, either express or implied.  See the License for the
42   * specific language governing permissions and limitations
43   * under the License.
44   */
45  
46  import org.apache.maven.plugin.logging.Log;
47  import org.apache.maven.plugins.changes.model.Body;
48  import org.apache.maven.plugins.changes.model.ChangesDocument;
49  import org.apache.maven.plugins.changes.model.Properties;
50  import org.apache.maven.plugins.changes.model.Release;
51  import org.apache.maven.plugins.changes.model.io.xpp3.ChangesXpp3Reader;
52  import org.codehaus.plexus.util.IOUtil;
53  
54  /**
55   * A facade for a changes.xml file.
56   *
57   * @version $Id: ChangesXML.java 1742353 2016-05-05 03:22:53Z schulte $
58   */
59  public class ChangesXML
60  {
61  
62      /** The list of releases in the changes.xml file. */
63      private List<Release> releaseList;
64  
65      /** The author in the changes.xml file. */
66      private String author;
67  
68      /** The title of the changes.xml file. */
69      private String title;
70  
71      /** The e-mail address of the author in the changes.xml file. */
72      private String authorEmail;
73  
74      /** The changes.xml document. */
75      private ChangesDocument changesDocument;
76  
77      /**
78       * Constructor that sets the changes.xml file and the logger.
79       * 
80       * @param xmlPath the changes.xml file
81       * @param log the logger
82       * @throws ChangesXMLRuntimeException if there was a fatal error while parsing the changes.xml file
83       */
84      public ChangesXML( File xmlPath, Log log )
85          throws ChangesXMLRuntimeException
86      {
87  
88          if ( xmlPath == null || !xmlPath.exists() )
89          {
90              log.error( "changes xml file is null or not exists " );
91              return;
92          }
93  
94          FileInputStream fileInputStream = null;
95  
96          try
97          {
98  
99              ChangesXpp3Reader reader = new ChangesXpp3Reader();
100 
101             fileInputStream = new FileInputStream( xmlPath );
102             changesDocument = reader.read( fileInputStream, false );
103             fileInputStream.close();
104             fileInputStream = null;
105 
106             if ( changesDocument == null )
107             {
108                 log.error( "Cannot build Changes Report from file: " + xmlPath.getPath() );
109                 return;
110             }
111 
112             Properties properties = changesDocument.getProperties();
113 
114             if ( properties != null )
115             {
116                 if ( properties.getAuthor() != null )
117                 {
118                     this.author = properties.getAuthor().getName();
119                     this.authorEmail = properties.getAuthor().getName();
120                 }
121                 this.title = properties.getTitle();
122             }
123 
124             Body body = changesDocument.getBody();
125 
126             if ( body != null )
127             {
128                 this.releaseList = body.getReleases();
129             }
130 
131         }
132         catch ( Throwable e )
133         {
134             log.error( "An error occurred when parsing the changes.xml file: ", e );
135             throw new ChangesXMLRuntimeException( "An error occurred when parsing the changes.xml file", e );
136         }
137         finally
138         {
139             IOUtil.close( fileInputStream );
140         }
141     }
142 
143     /**
144      * Sets the {@link ChangesXML#author} attribute.
145      * 
146      * @param author the new value of the {@link ChangesXML#author} attribute
147      */
148     public void setAuthor( String author )
149     {
150         this.author = author;
151     }
152 
153     /**
154      * Returns the current value of the author attribute.
155      * 
156      * @return the current value of the author attribute
157      */
158     public String getAuthor()
159     {
160         return author;
161     }
162 
163     /**
164      * Sets the {@link ChangesXML#releaseList} attribute.
165      * 
166      * @param releaseList the new value of the {@link ChangesXML#releaseList} attribute
167      */
168     public void setReleaseList( List<Release> releaseList )
169     {
170         this.releaseList = releaseList;
171     }
172 
173     /**
174      * Returns the current value of the {@link ChangesXML#releaseList} attribute.
175      * 
176      * @return the current value of the {@link ChangesXML#releaseList} attribute
177      */
178     public List<Release> getReleaseList()
179     {
180         return releaseList == null ? Collections.<Release>emptyList() : releaseList;
181     }
182 
183     /**
184      * Sets the {@link ChangesXML#title} attribute.
185      * 
186      * @param title the new value of the {@link ChangesXML#title} attribute
187      */
188     public void setTitle( String title )
189     {
190         this.title = title;
191     }
192 
193     /**
194      * Returns the current value of the {@link ChangesXML#title} attribute.
195      * 
196      * @return the current value of the {@link ChangesXML#title} attribute
197      */
198     public String getTitle()
199     {
200         return title;
201     }
202 
203     /**
204      * Returns the current value of the {@link ChangesXML#changesDocument} attribute.
205      * 
206      * @return the current value of the {@link ChangesXML#changesDocument} attribute
207      */
208     public ChangesDocument getChangesDocument()
209     {
210         return changesDocument;
211     }
212 
213     /**
214      * Returns the current value of the {@link ChangesXML#authorEmail} attribute.
215      * 
216      * @return the current value of the {@link ChangesXML#authorEmail} attribute
217      */
218     public String getAuthorEmail()
219     {
220         return authorEmail;
221     }
222 
223     /**
224      * Sets the {@link ChangesXML#authorEmail} attribute.
225      * 
226      * @param authorEmail the new value of the {@link ChangesXML#authorEmail} attribute
227      */
228     public void setAuthorEmail( String authorEmail )
229     {
230         this.authorEmail = authorEmail;
231     }
232 
233 }