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 1685894 2015-06-16 19:29:09Z khmarbaise $
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 
104             if ( changesDocument == null )
105             {
106                 log.error( "Cannot build Changes Report from file: " + xmlPath.getPath() );
107                 return;
108             }
109 
110             Properties properties = changesDocument.getProperties();
111 
112             if ( properties != null )
113             {
114                 if ( properties.getAuthor() != null )
115                 {
116                     this.author = properties.getAuthor().getName();
117                     this.authorEmail = properties.getAuthor().getName();
118                 }
119                 this.title = properties.getTitle();
120             }
121 
122             Body body = changesDocument.getBody();
123 
124             if ( body != null )
125             {
126                 this.releaseList = body.getReleases();
127             }
128 
129         }
130         catch ( Throwable e )
131         {
132             log.error( "An error occurred when parsing the changes.xml file: ", e );
133             throw new ChangesXMLRuntimeException( "An error occurred when parsing the changes.xml file", e );
134         }
135         finally
136         {
137             IOUtil.close( fileInputStream );
138         }
139     }
140 
141     /**
142      * Sets the {@link ChangesXML#author} attribute.
143      * 
144      * @param author the new value of the {@link ChangesXML#author} attribute
145      */
146     public void setAuthor( String author )
147     {
148         this.author = author;
149     }
150 
151     /**
152      * Returns the current value of the author attribute.
153      * 
154      * @return the current value of the author attribute
155      */
156     public String getAuthor()
157     {
158         return author;
159     }
160 
161     /**
162      * Sets the {@link ChangesXML#releaseList} attribute.
163      * 
164      * @param releaseList the new value of the {@link ChangesXML#releaseList} attribute
165      */
166     public void setReleaseList( List<Release> releaseList )
167     {
168         this.releaseList = releaseList;
169     }
170 
171     /**
172      * Returns the current value of the {@link ChangesXML#releaseList} attribute.
173      * 
174      * @return the current value of the {@link ChangesXML#releaseList} attribute
175      */
176     public List<Release> getReleaseList()
177     {
178         return releaseList == null ? Collections.<Release>emptyList() : releaseList;
179     }
180 
181     /**
182      * Sets the {@link ChangesXML#title} attribute.
183      * 
184      * @param title the new value of the {@link ChangesXML#title} attribute
185      */
186     public void setTitle( String title )
187     {
188         this.title = title;
189     }
190 
191     /**
192      * Returns the current value of the {@link ChangesXML#title} attribute.
193      * 
194      * @return the current value of the {@link ChangesXML#title} attribute
195      */
196     public String getTitle()
197     {
198         return title;
199     }
200 
201     /**
202      * Returns the current value of the {@link ChangesXML#changesDocument} attribute.
203      * 
204      * @return the current value of the {@link ChangesXML#changesDocument} attribute
205      */
206     public ChangesDocument getChangesDocument()
207     {
208         return changesDocument;
209     }
210 
211     /**
212      * Returns the current value of the {@link ChangesXML#authorEmail} attribute.
213      * 
214      * @return the current value of the {@link ChangesXML#authorEmail} attribute
215      */
216     public String getAuthorEmail()
217     {
218         return authorEmail;
219     }
220 
221     /**
222      * Sets the {@link ChangesXML#authorEmail} attribute.
223      * 
224      * @param authorEmail the new value of the {@link ChangesXML#authorEmail} attribute
225      */
226     public void setAuthorEmail( String authorEmail )
227     {
228         this.authorEmail = authorEmail;
229     }
230 
231 }