|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| package org.qedeq.kernel.bo.save; |
|
19 |
| |
|
20 |
| import java.io.File; |
|
21 |
| import java.io.FileOutputStream; |
|
22 |
| import java.io.IOException; |
|
23 |
| import java.io.OutputStream; |
|
24 |
| import java.net.URL; |
|
25 |
| |
|
26 |
| import org.qedeq.kernel.bo.module.ModuleDataException; |
|
27 |
| import org.qedeq.kernel.bo.module.QedeqBo; |
|
28 |
| import org.qedeq.kernel.common.SourceFileExceptionList; |
|
29 |
| import org.qedeq.kernel.context.KernelContext; |
|
30 |
| import org.qedeq.kernel.latex.Qedeq2Xml; |
|
31 |
| import org.qedeq.kernel.trace.Trace; |
|
32 |
| import org.qedeq.kernel.utility.IoUtility; |
|
33 |
| import org.qedeq.kernel.utility.TextOutput; |
|
34 |
| import org.qedeq.kernel.xml.mapper.ModuleDataException2XmlFileException; |
|
35 |
| import org.qedeq.kernel.xml.parser.DefaultSourceFileExceptionList; |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| public final class Xml2Xml { |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
0
| private Xml2Xml() {
|
|
50 |
| |
|
51 |
| } |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
3
| public static String generate(final File from, final File to)
|
|
62 |
| throws SourceFileExceptionList { |
|
63 |
3
| final String method = "generate(File, File)";
|
|
64 |
3
| File destination;
|
|
65 |
3
| try {
|
|
66 |
3
| if (to != null) {
|
|
67 |
3
| destination = to.getCanonicalFile();
|
|
68 |
| } else { |
|
69 |
0
| String xml = from.getName();
|
|
70 |
0
| if (xml.toLowerCase().endsWith(".xml")) {
|
|
71 |
0
| xml = xml.substring(0, xml.length() - 4);
|
|
72 |
| } |
|
73 |
0
| destination = new File(from.getParentFile(), xml + "_.xml").getCanonicalFile();
|
|
74 |
| } |
|
75 |
| } catch (IOException e) { |
|
76 |
0
| Trace.trace(Xml2Xml.class, method, e);
|
|
77 |
0
| throw new DefaultSourceFileExceptionList(e);
|
|
78 |
| } |
|
79 |
3
| return generate(IoUtility.toUrl(from), destination);
|
|
80 |
| } |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
3
| public static String generate(final URL from, final File to)
|
|
91 |
| throws SourceFileExceptionList { |
|
92 |
3
| final String method = "generate(URL, File)";
|
|
93 |
3
| Trace.begin(Xml2Xml.class, method);
|
|
94 |
3
| Trace.param(Xml2Xml.class, method, "from", from);
|
|
95 |
3
| Trace.param(Xml2Xml.class, method, "to", to);
|
|
96 |
3
| QedeqBo qedeqBo = null;
|
|
97 |
3
| TextOutput printer = null;
|
|
98 |
3
| try {
|
|
99 |
3
| qedeqBo = KernelContext.getInstance().loadModule(from);
|
|
100 |
3
| IoUtility.createNecessaryDirectories(to);
|
|
101 |
3
| final OutputStream outputStream = new FileOutputStream(to);
|
|
102 |
3
| printer = new TextOutput(to.getName(), outputStream);
|
|
103 |
3
| Qedeq2Xml.print(from, qedeqBo, printer);
|
|
104 |
3
| return to.getCanonicalPath();
|
|
105 |
| } catch (IOException e) { |
|
106 |
0
| Trace.trace(Xml2Xml.class, method, e);
|
|
107 |
0
| throw new DefaultSourceFileExceptionList(e);
|
|
108 |
| } catch (RuntimeException e) { |
|
109 |
0
| Trace.trace(Xml2Xml.class, method, e);
|
|
110 |
0
| throw new DefaultSourceFileExceptionList(e);
|
|
111 |
| } catch (ModuleDataException e) { |
|
112 |
0
| Trace.trace(Xml2Xml.class, method, e);
|
|
113 |
0
| Trace.param(Xml2Xml.class, method, "context", e.getContext());
|
|
114 |
0
| throw ModuleDataException2XmlFileException.createXmlFileExceptionList(e,
|
|
115 |
| qedeqBo.getQedeq()); |
|
116 |
| } finally { |
|
117 |
3
| if (printer != null) {
|
|
118 |
3
| printer.close();
|
|
119 |
| } |
|
120 |
3
| Trace.end(Xml2Xml.class, method);
|
|
121 |
| } |
|
122 |
| } |
|
123 |
| |
|
124 |
| } |