1 | |
package org.jtheque.core.utils.file; |
2 | |
|
3 | |
import org.jdom.Document; |
4 | |
import org.jdom.output.Format; |
5 | |
import org.jdom.output.XMLOutputter; |
6 | |
import org.jtheque.core.managers.Managers; |
7 | |
import org.jtheque.core.managers.log.ILoggingManager; |
8 | |
import org.jtheque.utils.io.FileUtils; |
9 | |
|
10 | |
import java.io.File; |
11 | |
import java.io.FileNotFoundException; |
12 | |
import java.io.FileOutputStream; |
13 | |
import java.io.IOException; |
14 | |
import java.io.OutputStream; |
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public final class XMLUtils { |
38 | |
|
39 | |
|
40 | |
|
41 | |
private XMLUtils() { |
42 | 0 | super(); |
43 | 0 | } |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public static void writeXml(Document doc, String path) { |
52 | 0 | XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat()); |
53 | |
|
54 | 0 | boolean fileOk = true; |
55 | |
|
56 | 0 | File f = new File(path); |
57 | |
|
58 | 0 | if (!f.exists()) { |
59 | |
try { |
60 | 0 | fileOk = f.createNewFile(); |
61 | 0 | } catch (IOException e) { |
62 | 0 | fileOk = false; |
63 | 0 | Managers.getManager(ILoggingManager.class).getLogger(XMLUtils.class).error(e); |
64 | 0 | } |
65 | |
} |
66 | |
|
67 | 0 | if (fileOk) { |
68 | 0 | OutputStream stream = null; |
69 | |
try { |
70 | 0 | stream = new FileOutputStream(path); |
71 | |
|
72 | 0 | sortie.output(doc, stream); |
73 | 0 | } catch (FileNotFoundException e) { |
74 | 0 | Managers.getManager(ILoggingManager.class).getLogger(XMLUtils.class).error(e); |
75 | 0 | } catch (IOException e) { |
76 | 0 | Managers.getManager(ILoggingManager.class).getLogger(XMLUtils.class).error(e); |
77 | |
} finally { |
78 | 0 | FileUtils.close(stream); |
79 | 0 | } |
80 | |
} |
81 | 0 | } |
82 | |
} |