Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../../img/srcFileCovDistChart8.png 62% of files have more coverage
41   159   13   6.83
10   99   0.32   6
6     2.17  
1    
 
  Xml2Xml       Line # 48 41 13 71.9% 0.71929824
 
No Tests
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.kernel.xml.dao;
17   
18    import java.io.File;
19    import java.io.FileOutputStream;
20    import java.io.IOException;
21    import java.io.OutputStream;
22    import java.net.URL;
23    import java.util.Locale;
24   
25    import org.qedeq.base.io.IoUtility;
26    import org.qedeq.base.io.Parameters;
27    import org.qedeq.base.io.TextOutput;
28    import org.qedeq.base.io.UrlUtility;
29    import org.qedeq.base.trace.Trace;
30    import org.qedeq.base.utility.YodaUtility;
31    import org.qedeq.kernel.bo.common.KernelServices;
32    import org.qedeq.kernel.bo.module.InternalKernelServices;
33    import org.qedeq.kernel.bo.module.InternalServiceCall;
34    import org.qedeq.kernel.bo.module.InternalServiceProcess;
35    import org.qedeq.kernel.bo.module.KernelQedeqBo;
36    import org.qedeq.kernel.bo.service.control.ServiceProcessManager;
37    import org.qedeq.kernel.se.common.ModuleAddress;
38    import org.qedeq.kernel.se.common.Plugin;
39    import org.qedeq.kernel.se.common.SourceFileExceptionList;
40    import org.qedeq.kernel.se.visitor.InterruptException;
41   
42   
43    /**
44    * Test application.
45    *
46    * @author Michael Meyling
47    */
 
48    public final class Xml2Xml implements Plugin {
49   
50    /** This class. */
51    private static final Class CLASS = Xml2Xml.class;
52   
53    /**
54    * Constructor.
55    */
 
56  143 toggle private Xml2Xml() {
57    // nothing to do
58    }
59   
60    /**
61    * Generate XML file out of XML file.
62    *
63    * @param services Use this kernel services.
64    * @param internal Use this internal kernel services.
65    * @param from Read this XML file.
66    * @param to Write to this file. Could be <code>null</code>.
67    * @throws SourceFileExceptionList Module could not be successfully loaded.
68    * @throws InterruptException User canceled process.
69    * @return File name of generated LaTeX file.
70    */
 
71  143 toggle public static String generate(final KernelServices services, final InternalKernelServices internal,
72    final File from, final File to) throws SourceFileExceptionList, InterruptException {
73  143 final String method = "generate(File, File)";
74  143 File destination = null;
75  143 try {
76  143 if (to != null) {
77  143 destination = to.getCanonicalFile();
78    } else {
79  0 String xml = from.getName();
80  0 if (xml.toLowerCase(Locale.US).endsWith(".xml")) {
81  0 xml = xml.substring(0, xml.length() - 4);
82    }
83  0 destination = new File(from.getParentFile(), xml + "_.xml").getCanonicalFile();
84    }
85  143 return generate(services, internal, UrlUtility.toUrl(from), destination);
86    } catch (IOException e) {
87  0 Trace.fatal(CLASS, "Writing failed destionation", method, e);
88  0 throw internal.createSourceFileExceptionList(
89    DaoErrors.WRITING_MODULE_FILE_FAILED_CODE,
90    DaoErrors.WRITING_MODULE_FILE_FAILED_TEXT + destination,
91    to + "", e);
92    }
93    }
94   
95    /**
96    * Generate XML file out of XML file.
97    *
98    * @param services Here we get our kernel services.
99    * @param internal Our internal kernel services.
100    * @param from Read this XML file.
101    * @param to Write to this file. Could not be <code>null</code>.
102    * @throws SourceFileExceptionList Module could not be successfully loaded.
103    * @throws IOException Writing (or reading) failed.
104    * @throws InterruptException Generation canceled by user.
105    * @return File name of generated LaTeX file.
106    */
 
107  143 toggle private static String generate(final KernelServices services, final InternalKernelServices internal,
108    final URL from, final File to)
109    throws SourceFileExceptionList, IOException, InterruptException {
110  143 final String method = "generate(URL, File)";
111  143 Trace.begin(CLASS, method);
112  143 Trace.param(CLASS, method, "from", from);
113  143 Trace.param(CLASS, method, "to", to);
114  143 TextOutput printer = null;
115  143 InternalServiceCall call = null;
116  143 try {
117  143 final ModuleAddress address = services.getModuleAddress(from);
118    // TODO mime 20080303: find a solution without casting!
119  143 final InternalServiceProcess process = internal.createServiceProcess("generate XML");
120  143 final Plugin plugin = new Xml2Xml();
121  143 final KernelQedeqBo prop = internal.loadKernelModule(process, address);
122  143 if (!prop.isLoaded()) {
123  11 throw prop.getErrors();
124    }
125  132 IoUtility.createNecessaryDirectories(to);
126  132 final OutputStream outputStream = new FileOutputStream(to);
127  132 printer = new TextOutput(to.getName(), outputStream, "UTF-8");
128  132 call = internal.createServiceCall(plugin, prop, Parameters.EMPTY,
129    Parameters.EMPTY, process, null);
130  132 Qedeq2Xml.print(call.getInternalServiceProcess(), plugin, prop, printer);
131  132 return to.getCanonicalPath();
132    } finally {
133    // FIXME 20130521 m31: use executePlugin or something that automatically ends the service call
134  143 if (call != null) {
135  132 try {
136  132 ((ServiceProcessManager) YodaUtility.getFieldValue(internal, "processManager"))
137    .endServiceCall(call);
138    } catch (NoSuchFieldException e) {
139  0 throw new RuntimeException(e);
140    }
141    }
142  143 if (printer != null) {
143  132 printer.close();
144    }
145  143 Trace.end(CLASS, method);
146    }
147    }
148   
 
149  0 toggle public String getServiceId() {
150  0 return CLASS.getName();
151    }
 
152  0 toggle public String getServiceAction() {
153  0 return "Xml2Xml";
154    }
 
155  0 toggle public String getServiceDescription() {
156  0 return "transform XML QEDEQ module in a new XML file";
157    }
158   
159    }