1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. 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,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.maven.plugins.changes;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.util.Collections;
24 import java.util.List;
25
26 import org.apache.maven.plugin.logging.Log;
27 import org.apache.maven.plugins.changes.model.Body;
28 import org.apache.maven.plugins.changes.model.ChangesDocument;
29 import org.apache.maven.plugins.changes.model.Properties;
30 import org.apache.maven.plugins.changes.model.Release;
31 import org.apache.maven.plugins.changes.model.io.xpp3.ChangesXpp3Reader;
32
33 /**
34 * A facade for a changes.xml file.
35 *
36 * @version $Id$
37 */
38 public class ChangesXML {
39
40 /** The list of releases in the changes.xml file. */
41 private List<Release> releaseList;
42
43 /** The author in the changes.xml file. */
44 private String author;
45
46 /** The title of the changes.xml file. */
47 private String title;
48
49 /** The e-mail address of the author in the changes.xml file. */
50 private String authorEmail;
51
52 /** The changes.xml document. */
53 private ChangesDocument changesDocument;
54
55 /**
56 * Constructor that sets the changes.xml file and the logger.
57 *
58 * @param xmlPath the changes.xml file
59 * @param log the logger
60 * @throws ChangesXMLRuntimeException if there was a fatal error while parsing the changes.xml file
61 */
62 public ChangesXML(File xmlPath, Log log) throws ChangesXMLRuntimeException {
63
64 if (xmlPath == null || !xmlPath.exists()) {
65 log.error("changes xml file is null or not exists ");
66 return;
67 }
68
69 try {
70
71 ChangesXpp3Reader reader = new ChangesXpp3Reader();
72
73 try (FileInputStream fileInputStream = new FileInputStream(xmlPath)) {
74 changesDocument = reader.read(fileInputStream, false);
75 }
76
77 if (changesDocument == null) {
78 log.error("Cannot build Changes Report from file: " + xmlPath.getPath());
79 return;
80 }
81
82 Properties properties = changesDocument.getProperties();
83
84 if (properties != null) {
85 if (properties.getAuthor() != null) {
86 this.author = properties.getAuthor().getName();
87 this.authorEmail = properties.getAuthor().getName();
88 }
89 this.title = properties.getTitle();
90 }
91
92 Body body = changesDocument.getBody();
93
94 if (body != null) {
95 this.releaseList = body.getReleases();
96 }
97
98 } catch (Throwable e) {
99 log.error("An error occurred when parsing the changes.xml file: ", e);
100 throw new ChangesXMLRuntimeException("An error occurred when parsing the changes.xml file", e);
101 }
102 }
103
104 /**
105 * Sets the {@link ChangesXML#author} attribute.
106 *
107 * @param author the new value of the {@link ChangesXML#author} attribute
108 */
109 public void setAuthor(String author) {
110 this.author = author;
111 }
112
113 /**
114 * Returns the current value of the author attribute.
115 *
116 * @return the current value of the author attribute
117 */
118 public String getAuthor() {
119 return author;
120 }
121
122 /**
123 * Sets the {@link ChangesXML#releaseList} attribute.
124 *
125 * @param releaseList the new value of the {@link ChangesXML#releaseList} attribute
126 */
127 public void setReleaseList(List<Release> releaseList) {
128 this.releaseList = releaseList;
129 }
130
131 /**
132 * Returns the current value of the {@link ChangesXML#releaseList} attribute.
133 *
134 * @return the current value of the {@link ChangesXML#releaseList} attribute
135 */
136 public List<Release> getReleaseList() {
137 return releaseList == null ? Collections.emptyList() : releaseList;
138 }
139
140 /**
141 * Sets the {@link ChangesXML#title} attribute.
142 *
143 * @param title the new value of the {@link ChangesXML#title} attribute
144 */
145 public void setTitle(String title) {
146 this.title = title;
147 }
148
149 /**
150 * Returns the current value of the {@link ChangesXML#title} attribute.
151 *
152 * @return the current value of the {@link ChangesXML#title} attribute
153 */
154 public String getTitle() {
155 return title;
156 }
157
158 /**
159 * Returns the current value of the {@link ChangesXML#changesDocument} attribute.
160 *
161 * @return the current value of the {@link ChangesXML#changesDocument} attribute
162 */
163 public ChangesDocument getChangesDocument() {
164 return changesDocument;
165 }
166
167 /**
168 * Returns the current value of the {@link ChangesXML#authorEmail} attribute.
169 *
170 * @return the current value of the {@link ChangesXML#authorEmail} attribute
171 */
172 public String getAuthorEmail() {
173 return authorEmail;
174 }
175
176 /**
177 * Sets the {@link ChangesXML#authorEmail} attribute.
178 *
179 * @param authorEmail the new value of the {@link ChangesXML#authorEmail} attribute
180 */
181 public void setAuthorEmail(String authorEmail) {
182 this.authorEmail = authorEmail;
183 }
184 }