587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
package com.nokia.helium.sysdef.ant.taskdefs;
|
|
18 |
|
|
19 |
import java.io.File;
|
628
|
20 |
import java.io.FileNotFoundException;
|
587
|
21 |
import java.io.FileOutputStream;
|
|
22 |
import java.io.IOException;
|
|
23 |
import java.util.Map;
|
|
24 |
|
|
25 |
import javax.xml.transform.ErrorListener;
|
|
26 |
import javax.xml.transform.Transformer;
|
628
|
27 |
import javax.xml.transform.TransformerConfigurationException;
|
587
|
28 |
import javax.xml.transform.TransformerException;
|
|
29 |
import javax.xml.transform.TransformerFactory;
|
|
30 |
import javax.xml.transform.stream.StreamResult;
|
|
31 |
import javax.xml.transform.stream.StreamSource;
|
|
32 |
|
|
33 |
import org.apache.tools.ant.BuildException;
|
|
34 |
import org.apache.tools.ant.Project;
|
|
35 |
import org.apache.tools.ant.Task;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* This is the base class for manipulating Sysdef v3 files using XML Stylesheet.
|
|
39 |
* The implementation/interface is not frozen yet. It is morelikely going to
|
|
40 |
* change in the future, depending of the underlaying implementation.
|
|
41 |
*
|
|
42 |
*/
|
|
43 |
public abstract class AbstractSydefTask extends Task implements ErrorListener {
|
|
44 |
|
|
45 |
private File srcFile;
|
|
46 |
private File destFile;
|
|
47 |
private File epocroot;
|
628
|
48 |
private boolean failOnError = true; // Error should not fail by default.
|
|
49 |
private boolean failOnWarning; // Warning should not fail by default.
|
587
|
50 |
|
|
51 |
/**
|
|
52 |
* Get the source file.
|
|
53 |
* @return
|
|
54 |
*/
|
|
55 |
public File getSrcFile() {
|
|
56 |
return srcFile;
|
|
57 |
}
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Defines the location of the source system definition file.
|
|
61 |
* @param srcfile
|
|
62 |
* @ant.required
|
|
63 |
*/
|
|
64 |
public void setSrcFile(File srcfile) {
|
|
65 |
this.srcFile = srcfile;
|
|
66 |
}
|
|
67 |
|
|
68 |
/**
|
|
69 |
* Get the location of the output file.
|
|
70 |
* @return
|
|
71 |
*/
|
|
72 |
public File getDestFile() {
|
|
73 |
return destFile;
|
|
74 |
}
|
|
75 |
|
|
76 |
/**
|
|
77 |
* The the name of the destination file.
|
|
78 |
* @param destfile
|
|
79 |
* @ant.required
|
|
80 |
*/
|
|
81 |
public void setDestFile(File destfile) {
|
|
82 |
this.destFile = destfile;
|
|
83 |
}
|
|
84 |
|
|
85 |
/**
|
|
86 |
* Get the epocroot.
|
|
87 |
* If epocroot is not set by the user it return the value from the EPOCROOT environment variable.
|
|
88 |
* If the EPOCROOT environment variable is not defined then a BuildException is thrown.
|
|
89 |
* @return the epocroot location as a File object.
|
|
90 |
*/
|
|
91 |
public File getEpocroot() {
|
|
92 |
if (epocroot == null) {
|
|
93 |
if (System.getenv("EPOCROOT") != null) {
|
|
94 |
return new File(System.getenv("EPOCROOT"));
|
|
95 |
}
|
|
96 |
else {
|
|
97 |
throw new BuildException("'epocroot' attribute has not been defined.");
|
|
98 |
}
|
|
99 |
}
|
|
100 |
return epocroot;
|
|
101 |
}
|
|
102 |
|
|
103 |
/**
|
|
104 |
* Location of the EPOCROOT.
|
|
105 |
* @ant.not-required By default the EPOCROOT environment variable is used.
|
|
106 |
* @param epocroot path to the epocroot.
|
|
107 |
*/
|
|
108 |
public void setEpocroot(File epocroot) {
|
|
109 |
this.epocroot = epocroot;
|
|
110 |
}
|
|
111 |
|
|
112 |
/**
|
|
113 |
* Shall we fail the build on error.
|
|
114 |
* @return is the task should failonerror.
|
|
115 |
*/
|
|
116 |
public boolean isFailOnError() {
|
|
117 |
return failOnError;
|
|
118 |
}
|
|
119 |
|
|
120 |
/**
|
628
|
121 |
* Shall we fail the build on warning.
|
|
122 |
* @return is the task should fail on warning.
|
|
123 |
*/
|
|
124 |
public boolean isFailOnWarning() {
|
|
125 |
return failOnWarning;
|
|
126 |
}
|
|
127 |
|
|
128 |
/**
|
|
129 |
* Defines if the task should fail on error.
|
587
|
130 |
* @param failonerror
|
|
131 |
* @ant.not-required Default is true.
|
|
132 |
*/
|
|
133 |
public void setFailOnError(boolean failonerror) {
|
|
134 |
this.failOnError = failonerror;
|
|
135 |
}
|
|
136 |
|
|
137 |
/**
|
628
|
138 |
* Defines if the task should fail on warning.
|
|
139 |
* @param failonerror
|
|
140 |
* @ant.not-required Default is true.
|
|
141 |
*/
|
|
142 |
public void setFailOnWarning(boolean failOnWarning) {
|
|
143 |
this.failOnWarning = failOnWarning;
|
|
144 |
}
|
|
145 |
|
|
146 |
/**
|
587
|
147 |
* This method should be defined by the implementing class
|
|
148 |
* to define the location of the XSLT file.
|
|
149 |
* @return the XSLT file location.
|
|
150 |
*/
|
628
|
151 |
protected abstract File getXsl();
|
587
|
152 |
|
|
153 |
/**
|
|
154 |
* Check if required attribute have been configured correctly.
|
|
155 |
* If not the method will raise a BuildException.
|
|
156 |
*/
|
|
157 |
protected void check() {
|
|
158 |
if (getSrcFile() == null) {
|
|
159 |
throw new BuildException("'srcfile' attribute is not defined");
|
|
160 |
}
|
|
161 |
if (getDestFile() == null) {
|
|
162 |
throw new BuildException("'destfile' attribute is not defined");
|
|
163 |
}
|
|
164 |
File xslt = getXsl();
|
|
165 |
if (!xslt.exists()) {
|
|
166 |
throw new BuildException("Could not find " + xslt);
|
|
167 |
}
|
|
168 |
if (!getSrcFile().exists()) {
|
|
169 |
throw new BuildException("Could not find source file " + srcFile);
|
|
170 |
}
|
|
171 |
}
|
|
172 |
|
|
173 |
/**
|
|
174 |
* Transform the srcfile using the stylesheet provided by getXsl. The data parameters are
|
|
175 |
* passed to the template engine. The result is save to the destfile.
|
|
176 |
*
|
|
177 |
* @param data a set of key/value to pass to the XSLT engine.
|
|
178 |
*/
|
|
179 |
public void transform(Map<String, String> data) {
|
|
180 |
check();
|
|
181 |
if (getDestFile().exists()) {
|
|
182 |
log("Deleting previous output file: " + destFile, Project.MSG_DEBUG);
|
|
183 |
getDestFile().delete();
|
|
184 |
}
|
|
185 |
|
|
186 |
FileOutputStream output = null;
|
|
187 |
try {
|
|
188 |
output = new FileOutputStream(getDestFile());
|
|
189 |
TransformerFactory factory = TransformerFactory.newInstance();
|
|
190 |
Transformer transformer = factory.newTransformer(new StreamSource(getXsl()));
|
|
191 |
|
|
192 |
transformer.setParameter("path", getSrcFile());
|
|
193 |
for (Map.Entry<String, String> entry : data.entrySet()) {
|
|
194 |
transformer.setParameter(entry.getKey(), entry.getValue());
|
|
195 |
}
|
|
196 |
transformer.setErrorListener(this);
|
|
197 |
transformer.transform(new StreamSource(getSrcFile()), new StreamResult(
|
|
198 |
output));
|
|
199 |
output.close();
|
|
200 |
} catch (FileNotFoundException exc) {
|
|
201 |
// deleting the intermediate file in case of error.
|
|
202 |
if (getDestFile().exists()) {
|
|
203 |
// closing current output stream, so we can delete the file
|
|
204 |
try {
|
|
205 |
if (output != null) {
|
|
206 |
output.close();
|
|
207 |
}
|
|
208 |
} catch (IOException ioe) {
|
|
209 |
// we should just ignore that error.
|
|
210 |
log(ioe, Project.MSG_DEBUG);
|
|
211 |
}
|
|
212 |
log("Deleting " + destFile + " because an error occured.", Project.MSG_INFO);
|
|
213 |
getDestFile().delete();
|
|
214 |
}
|
|
215 |
// Raising the error to Ant.
|
|
216 |
throw new BuildException(exc.toString());
|
|
217 |
} catch (TransformerConfigurationException exc) {
|
|
218 |
// deleting the intermediate file in case of error.
|
|
219 |
if (destFile.exists()) {
|
|
220 |
// closing current output stream, so we can delete the file
|
|
221 |
try {
|
|
222 |
if (output != null) {
|
|
223 |
output.close();
|
|
224 |
}
|
|
225 |
} catch (IOException ioe) {
|
|
226 |
// we should just ignore that error.
|
|
227 |
log(ioe, Project.MSG_DEBUG);
|
|
228 |
}
|
|
229 |
log("Deleting " + destFile + " because an error occured.", Project.MSG_INFO);
|
|
230 |
getDestFile().delete();
|
|
231 |
}
|
|
232 |
// Raising the error to Ant.
|
|
233 |
throw new BuildException(exc.toString());
|
|
234 |
} catch (TransformerException exc) {
|
|
235 |
// deleting the intermediate file in case of error.
|
|
236 |
if (getDestFile().exists()) {
|
|
237 |
// closing current output stream, so we can delete the file
|
|
238 |
try {
|
|
239 |
if (output != null) {
|
|
240 |
output.close();
|
|
241 |
}
|
|
242 |
} catch (IOException ioe) {
|
|
243 |
// we should just ignore that error.
|
|
244 |
log(ioe, Project.MSG_DEBUG);
|
|
245 |
}
|
|
246 |
log("Deleting " + getDestFile() + " because an error occured.", Project.MSG_INFO);
|
|
247 |
getDestFile().delete();
|
|
248 |
}
|
|
249 |
// Raising the error to Ant.
|
|
250 |
throw new BuildException(exc.toString());
|
|
251 |
} catch (IOException e) {
|
|
252 |
// we should just ignore that error, error while closing the file.
|
|
253 |
log(e.getMessage(), Project.MSG_DEBUG);
|
|
254 |
}
|
|
255 |
}
|
|
256 |
|
|
257 |
/**
|
|
258 |
* {@inheritDoc}
|
|
259 |
* Reports errors to the Ant logging system of throw the exception if the task
|
|
260 |
* is set to failonerror.
|
|
261 |
*/
|
|
262 |
@Override
|
|
263 |
public void error(TransformerException message) throws TransformerException {
|
|
264 |
if (this.isFailOnError()) {
|
|
265 |
throw message;
|
|
266 |
} else {
|
|
267 |
log("ERROR: " + message.getMessageAndLocation(), Project.MSG_ERR);
|
|
268 |
}
|
|
269 |
}
|
|
270 |
|
|
271 |
/**
|
|
272 |
* {@inheritDoc}
|
|
273 |
* Fails the task in case of fatal error. The is nothing we can do about that.
|
|
274 |
*/
|
|
275 |
@Override
|
|
276 |
public void fatalError(TransformerException message) throws TransformerException {
|
|
277 |
log("ERROR: " + message.getMessageAndLocation(), Project.MSG_ERR);
|
|
278 |
throw message;
|
|
279 |
}
|
|
280 |
|
|
281 |
/**
|
|
282 |
* {@inheritDoc}
|
|
283 |
* Reports errors to the Ant logging system of throw the exception if the task
|
|
284 |
* is set to failonerror.
|
|
285 |
*/
|
|
286 |
@Override
|
|
287 |
public void warning(TransformerException message) throws TransformerException {
|
628
|
288 |
if (this.isFailOnWarning()) {
|
587
|
289 |
throw message;
|
|
290 |
} else {
|
|
291 |
log("WARNING: " + message.getMessageAndLocation(), Project.MSG_WARN);
|
|
292 |
}
|
|
293 |
}
|
|
294 |
}
|