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  import org.apache.maven.plugin.logging.Log;
28  import org.apache.maven.plugins.changes.model.Body;
29  import org.apache.maven.plugins.changes.model.ChangesDocument;
30  import org.apache.maven.plugins.changes.model.Properties;
31  import org.apache.maven.plugins.changes.model.io.xpp3.ChangesXpp3Reader;
32  import org.codehaus.plexus.util.IOUtil;
33  
34  /**
35   * A facade for a changes.xml file.
36   *
37   * @version $Id: ChangesXML.java 1579079 2014-03-18 22:57:22Z dennisl $
38   */
39  public class ChangesXML
40  {
41  
42      private List releaseList;
43  
44      private String author;
45  
46      private String title;
47  
48      private String authorEmail;
49  
50      private ChangesDocument changesDocument;
51  
52      public ChangesXML( File xmlPath, Log log )
53      {
54  
55          if ( xmlPath == null || !xmlPath.exists() )
56          {
57              log.error( "changes xml file is null or not exists " );
58              return;
59          }
60  
61          FileInputStream fileInputStream = null;
62  
63          try
64          {
65  
66              ChangesXpp3Reader reader = new ChangesXpp3Reader();
67  
68              fileInputStream = new FileInputStream( xmlPath );
69              changesDocument = reader.read( fileInputStream, false );
70  
71              if ( changesDocument == null )
72              {
73                  log.error( "Cannot build Changes Report from file: " + xmlPath.getPath() );
74                  return;
75              }
76  
77              Properties properties = changesDocument.getProperties();
78  
79              if ( properties != null )
80              {
81                  if ( properties.getAuthor() != null )
82                  {
83                      this.author = properties.getAuthor().getName();
84                      this.authorEmail = properties.getAuthor().getName();
85                  }
86                  this.title = properties.getTitle();
87              }
88  
89  
90              Body body = changesDocument.getBody();
91  
92  
93              if ( body != null )
94              {
95                  this.releaseList = body.getReleases();
96              }
97  
98          }
99          catch ( Throwable e )
100         {
101             // FIXME throw an Exception ?
102             log.error( "An error occurred when parsing the changes.xml file: ", e );
103         }
104         finally
105         {
106             IOUtil.close( fileInputStream );
107         }
108     }
109 
110     public void setAuthor( String author )
111     {
112         this.author = author;
113     }
114 
115     public String getAuthor()
116     {
117         return author;
118     }
119 
120     public void setReleaseList( List releaseList )
121     {
122         this.releaseList = releaseList;
123     }
124 
125     public List getReleaseList()
126     {
127         return releaseList == null ? Collections.EMPTY_LIST : releaseList;
128     }
129 
130     public void setTitle( String title )
131     {
132         this.title = title;
133     }
134 
135     public String getTitle()
136     {
137         return title;
138     }
139 
140     public ChangesDocument getChangesDocument()
141     {
142         return changesDocument;
143     }
144 
145     public String getAuthorEmail()
146     {
147         return authorEmail;
148     }
149 
150     public void setAuthorEmail( String authorEmail )
151     {
152         this.authorEmail = authorEmail;
153     }
154 
155 }