|
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.ModuleAddress; |
|
27 |
| import org.qedeq.kernel.bo.module.ModuleDataException; |
|
28 |
| import org.qedeq.kernel.bo.module.ModuleProperties; |
|
29 |
| import org.qedeq.kernel.common.SourceFileExceptionList; |
|
30 |
| import org.qedeq.kernel.context.KernelContext; |
|
31 |
| import org.qedeq.kernel.latex.Qedeq2Xml; |
|
32 |
| import org.qedeq.kernel.trace.Trace; |
|
33 |
| import org.qedeq.kernel.utility.IoUtility; |
|
34 |
| import org.qedeq.kernel.utility.TextOutput; |
|
35 |
| import org.qedeq.kernel.xml.mapper.ModuleDataException2XmlFileException; |
|
36 |
| import org.qedeq.kernel.xml.parser.DefaultSourceFileExceptionList; |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| public final class Xml2Xml { |
|
46 |
| |
|
47 |
| |
|
48 |
| private static final Class CLASS = Xml2Xml.class; |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
0
| private Xml2Xml() {
|
|
54 |
| |
|
55 |
| } |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
3
| public static String generate(final File from, final File to)
|
|
66 |
| throws SourceFileExceptionList { |
|
67 |
3
| final String method = "generate(File, File)";
|
|
68 |
3
| File destination;
|
|
69 |
3
| try {
|
|
70 |
3
| if (to != null) {
|
|
71 |
3
| destination = to.getCanonicalFile();
|
|
72 |
| } else { |
|
73 |
0
| String xml = from.getName();
|
|
74 |
0
| if (xml.toLowerCase().endsWith(".xml")) {
|
|
75 |
0
| xml = xml.substring(0, xml.length() - 4);
|
|
76 |
| } |
|
77 |
0
| destination = new File(from.getParentFile(), xml + "_.xml").getCanonicalFile();
|
|
78 |
| } |
|
79 |
| } catch (IOException e) { |
|
80 |
0
| Trace.trace(CLASS, method, e);
|
|
81 |
0
| throw new DefaultSourceFileExceptionList(e);
|
|
82 |
| } |
|
83 |
3
| return generate(IoUtility.toUrl(from), destination);
|
|
84 |
| } |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
3
| public static String generate(final URL from, final File to)
|
|
95 |
| throws SourceFileExceptionList { |
|
96 |
3
| final String method = "generate(URL, File)";
|
|
97 |
3
| Trace.begin(CLASS, method);
|
|
98 |
3
| Trace.param(CLASS, method, "from", from);
|
|
99 |
3
| Trace.param(CLASS, method, "to", to);
|
|
100 |
3
| TextOutput printer = null;
|
|
101 |
3
| try {
|
|
102 |
3
| final ModuleAddress address = KernelContext.getInstance().getModuleAddress(from);
|
|
103 |
3
| final ModuleProperties prop = KernelContext.getInstance().loadModule(address);
|
|
104 |
3
| IoUtility.createNecessaryDirectories(to);
|
|
105 |
3
| final OutputStream outputStream = new FileOutputStream(to);
|
|
106 |
3
| printer = new TextOutput(to.getName(), outputStream);
|
|
107 |
3
| try {
|
|
108 |
3
| Qedeq2Xml.print(address, prop.getModule(), printer);
|
|
109 |
| } catch (ModuleDataException e) { |
|
110 |
0
| Trace.trace(CLASS, method, e);
|
|
111 |
0
| Trace.param(CLASS, method, "context", e.getContext());
|
|
112 |
0
| throw ModuleDataException2XmlFileException.createXmlFileExceptionList(e,
|
|
113 |
| prop.getModule().getQedeq()); |
|
114 |
| } |
|
115 |
3
| return to.getCanonicalPath();
|
|
116 |
| } catch (IOException e) { |
|
117 |
0
| Trace.trace(CLASS, method, e);
|
|
118 |
0
| throw new DefaultSourceFileExceptionList(e);
|
|
119 |
| } catch (RuntimeException e) { |
|
120 |
0
| Trace.trace(CLASS, method, e);
|
|
121 |
0
| throw new DefaultSourceFileExceptionList(e);
|
|
122 |
| } finally { |
|
123 |
3
| if (printer != null) {
|
|
124 |
3
| printer.close();
|
|
125 |
| } |
|
126 |
3
| Trace.end(CLASS, method);
|
|
127 |
| } |
|
128 |
| } |
|
129 |
| |
|
130 |
| } |