204
|
|
void saveAppFile(File file) throws ParserConfigurationException,
|
205
|
|
TransformerException, IOException {
|
206
|
|
Document xmlDoc = null;
|
207
|
|
DocumentBuilderFactory docBFac;
|
208
|
|
DocumentBuilder docBuild;
|
209
|
|
|
210
|
|
docBFac = DocumentBuilderFactory.newInstance();
|
211
|
|
docBFac.setNamespaceAware(true);
|
212
|
|
docBuild = docBFac.newDocumentBuilder();
|
213
|
|
xmlDoc = docBuild.newDocument();
|
214
|
|
|
215
|
|
if (xmlDoc != null) {
|
216
|
|
Element documentElement = xmlDoc.createElement("SimpleJavaApp");
|
217
|
|
xmlDoc.appendChild(documentElement);
|
218
|
|
|
219
|
|
Element bookListElement = xmlDoc.createElement("BookList");
|
220
|
|
|
221
|
|
for (Book book : getBooks()) {
|
222
|
|
bookListElement.appendChild(book.getXMLRepresentation(xmlDoc));
|
223
|
|
}
|
224
|
|
|
225
|
|
documentElement.appendChild(bookListElement);
|
226
|
|
}
|
227
|
|
|
228
|
|
FileOutputStream out = new FileOutputStream(file);
|
229
|
|
try {
|
230
|
|
DOMSource domSource = new DOMSource(xmlDoc);
|
231
|
|
StreamResult streamResult = new StreamResult(out);
|
232
|
|
TransformerFactory tf = TransformerFactory.newInstance();
|
233
|
|
Transformer serializer = tf.newTransformer();
|
234
|
|
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
|
235
|
|
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
|
236
|
|
serializer.transform(domSource, streamResult);
|
237
|
|
out.flush();
|
238
|
|
|
239
|
|
/*
|
240
|
|
* All saving operations were successful, so the AppFile can be set
|
241
|
|
* to 'not modified'.
|
242
|
|
*/
|
243
|
|
this.setModified(false);
|
244
|
|
this.setPath(file.getAbsolutePath());
|
245
|
|
} finally {
|
246
|
|
out.close();
|
247
|
|
}
|
248
|
|
}
|