Clover Coverage Report
Coverage timestamp: Fri Feb 14 2014 01:49:02 UTC
../../../../../img/srcFileCovDistChart7.png 78% of files have more coverage
40   155   12   6.67
8   96   0.3   6
6     2  
1    
 
  Xml2Xml       Line # 48 40 12 68.5% 0.6851852
 
  (1)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2014, 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.job.InternalModuleServiceCallImpl;
33    import org.qedeq.kernel.bo.module.InternalKernelServices;
34    import org.qedeq.kernel.bo.module.InternalServiceJob;
35    import org.qedeq.kernel.bo.module.KernelQedeqBo;
36    import org.qedeq.kernel.bo.service.internal.ServiceProcessManager;
37    import org.qedeq.kernel.se.common.ModuleAddress;
38    import org.qedeq.kernel.se.common.ModuleService;
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 ModuleService {
49   
50    /** This class. */
51    private static final Class CLASS = Xml2Xml.class;
52   
53    /**
54    * Constructor.
55    */
 
56  146 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  146 toggle public static String generate(final KernelServices services, final InternalKernelServices internal,
72    final File from, final File to) throws SourceFileExceptionList, InterruptException {
73  146 final String method = "generate(File, File)";
74  146 File destination = null;
75  146 try {
76  146 if (to != null) {
77  146 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  146 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  146 toggle private static String generate(final KernelServices services, final InternalKernelServices internal,
108    final URL from, final File to)
109    throws SourceFileExceptionList, IOException, InterruptException {
110  146 final String method = "generate(URL, File)";
111  146 Trace.begin(CLASS, method);
112  146 Trace.param(CLASS, method, "from", from);
113  146 Trace.param(CLASS, method, "to", to);
114  146 TextOutput printer = null;
115  146 try {
116  146 final ModuleAddress address = services.getModuleAddress(from);
117    // if this class returns into production code you must get rid of the following:
118  146 final InternalServiceJob process;
119  146 try {
120  146 process = ((ServiceProcessManager) YodaUtility.getFieldValue(internal, "processManager"))
121    .createServiceProcess("generate XML");
122    } catch (NoSuchFieldException e) {
123  0 e.printStackTrace();
124  0 throw new RuntimeException(e);
125    }
126  146 final ModuleService plugin = new Xml2Xml();
127  146 final KernelQedeqBo prop = internal.loadKernelModule(process, address);
128  146 if (!prop.isLoaded()) {
129  11 throw prop.getErrors();
130    }
131  135 IoUtility.createNecessaryDirectories(to);
132  135 final OutputStream outputStream = new FileOutputStream(to);
133  135 printer = new TextOutput(to.getName(), outputStream, "UTF-8");
134  135 final InternalModuleServiceCallImpl call = new InternalModuleServiceCallImpl(plugin, prop, Parameters.EMPTY, Parameters.EMPTY, process, null);
135  135 Qedeq2Xml.print(call.getInternalServiceProcess(), plugin, prop, printer);
136  135 return to.getCanonicalPath();
137    } finally {
138  146 if (printer != null) {
139  135 printer.close();
140    }
141  146 Trace.end(CLASS, method);
142    }
143    }
144   
 
145  0 toggle public String getServiceId() {
146  0 return CLASS.getName();
147    }
 
148  0 toggle public String getServiceAction() {
149  0 return "Xml2Xml";
150    }
 
151  0 toggle public String getServiceDescription() {
152  0 return "transform XML QEDEQ module in a new XML file";
153    }
154   
155    }