Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../../../img/srcFileCovDistChart4.png 93% of files have more coverage
56   203   23   9.33
22   126   0.41   6
6     3.83  
1    
 
  Qedeq2Utf8Executor       Line # 44 56 23 38.1% 0.3809524
 
  (9)
 
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.bo.service.unicode;
17   
18    import java.io.File;
19    import java.io.FileOutputStream;
20    import java.io.IOException;
21    import java.util.Locale;
22   
23    import org.qedeq.base.io.Parameters;
24    import org.qedeq.base.io.TextOutput;
25    import org.qedeq.base.trace.Trace;
26    import org.qedeq.kernel.bo.KernelContext;
27    import org.qedeq.kernel.bo.log.QedeqLog;
28    import org.qedeq.kernel.bo.module.InternalServiceCall;
29    import org.qedeq.kernel.bo.module.InternalServiceProcess;
30    import org.qedeq.kernel.bo.module.KernelQedeqBo;
31    import org.qedeq.kernel.bo.module.PluginExecutor;
32    import org.qedeq.kernel.se.common.Plugin;
33    import org.qedeq.kernel.se.common.SourceFileExceptionList;
34   
35   
36    /**
37    * Transfer a QEDEQ module into a UTF-8 text file.
38    * <p>
39    * <b>This is just a quick written generator. This class just generates some text output to be able
40    * to get a visual impression of a QEDEQ module.</b>
41    *
42    * @author Michael Meyling
43    */
 
44    public class Qedeq2Utf8Executor implements PluginExecutor {
45   
46    /** This class. */
47    private static final Class CLASS = Qedeq2Utf8Executor.class;
48   
49    /** Output goes here. */
50    private TextOutput printer;
51   
52    /** Current destination file. */
53    private File destination;
54   
55    /** Visit all nodes with this visitor. */
56    private final Qedeq2UnicodeVisitor visitor;
57   
58    /** Break automatically before this column number. */
59    private int maxColumns;
60   
61    /** Generate text for these languages. */
62    private String[] languages;
63   
64    /** Current language selection. See {@link #languages}. */
65    private int run = 0;
66   
67    /**
68    * Constructor.
69    *
70    * @param plugin This plugin we work for.
71    * @param prop QEDEQ BO object.
72    * @param parameters Plugin parameter.
73    */
 
74  17 toggle public Qedeq2Utf8Executor(final Plugin plugin, final KernelQedeqBo prop,
75    final Parameters parameters) {
76  17 final boolean info = parameters.getBoolean("info");
77    // automatically line break after this column. 0 means no automatic line breaking
78  17 maxColumns = parameters.getInt("maximumColumn");
79  17 if (parameters.getInt("maximumColumn") != 0) {
80  17 maxColumns = Math.max(10, maxColumns);
81    }
82  17 final boolean brief = parameters.getBoolean("brief");
83  17 visitor = new Qedeq2UnicodeVisitor(plugin, prop, info , maxColumns, true, brief);
84    }
85   
 
86  0 toggle public Object executePlugin(final InternalServiceCall call, final Object data) {
87  0 final String method = "executePlugin()";
88  0 try {
89  0 QedeqLog.getInstance().logRequest("Generate UTF-8", visitor.getQedeqBo().getUrl());
90  0 languages = visitor.getQedeqBo().getSupportedLanguages();
91  0 for (run = 0; run < languages.length; run++) {
92  0 final String result = generateUtf8(call.getInternalServiceProcess(), languages[run], "1");
93  0 if (languages[run] != null) {
94  0 QedeqLog.getInstance().logSuccessfulReply(
95    "UTF-8 for language \"" + languages[run]
96    + "\" was generated into \"" + result + "\"", visitor.getQedeqBo().getUrl());
97    } else {
98  0 QedeqLog.getInstance().logSuccessfulReply(
99    "UTF-8 for default language "
100    + "was generated into \"" + result + "\"", visitor.getQedeqBo().getUrl());
101    }
102    }
103  0 if (languages.length == 0) {
104  0 QedeqLog.getInstance().logMessage("no supported language found, assuming 'en'");
105  0 final String result = generateUtf8(call.getInternalServiceProcess(), "en", "1");
106  0 QedeqLog.getInstance().logSuccessfulReply(
107    "UTF-8 for language \"en"
108    + "\" was generated into \"" + result + "\"", visitor.getQedeqBo().getUrl());
109    }
110    } catch (final SourceFileExceptionList e) {
111  0 final String msg = "Generation failed";
112  0 Trace.fatal(CLASS, this, method, msg, e);
113  0 QedeqLog.getInstance().logFailureReply(msg, visitor.getQedeqBo().getUrl(), e.getMessage());
114    } catch (IOException e) {
115  0 final String msg = "Generation failed";
116  0 Trace.fatal(CLASS, this, method, msg, e);
117  0 QedeqLog.getInstance().logFailureReply(msg, visitor.getQedeqBo().getUrl(), e.getMessage());
118    } catch (final RuntimeException e) {
119  0 Trace.fatal(CLASS, this, method, "unexpected problem", e);
120  0 QedeqLog.getInstance().logFailureReply(
121    "Generation failed", visitor.getQedeqBo().getUrl(), "unexpected problem: "
122  0 + (e.getMessage() != null ? e.getMessage() : e.toString()));
123    }
124  0 return null;
125    }
126   
127    /**
128    * Gives a UTF-8 representation of given QEDEQ module as InputStream.
129    *
130    * @param process This process executes us.
131    * @param language Filter text to get and produce text in this language only. Might
132    * be <code>null</code>
133    * @param level Filter for this detail level. LATER mime 20050205: not supported
134    * yet.
135    * @return Name of generated file.
136    * @throws SourceFileExceptionList Major problem occurred.
137    * @throws IOException File IO failed.
138    */
 
139  17 toggle public String generateUtf8(final InternalServiceProcess process, final String language, final String level)
140    throws SourceFileExceptionList, IOException {
141   
142    // first we try to get more information about required modules and their predicates..
143  17 try {
144  17 visitor.getQedeqBo().getKernelServices().loadRequiredModules(
145    process, visitor.getQedeqBo());
146  17 visitor.getQedeqBo().getKernelServices().checkWellFormedness(
147    process, visitor.getQedeqBo());
148    } catch (Exception e) {
149    // we continue and ignore external predicates
150  0 Trace.trace(CLASS, "generateUtf8(KernelQedeqBo, String, String)", e);
151    }
152  17 String lan = "en";
153  17 if (language != null) {
154  17 lan = language;
155    }
156    // if (level == null) {
157    // this.level = "1";
158    // } else {
159    // this.level = level;
160    // }
161  17 String txt = visitor.getQedeqBo().getModuleAddress().getFileName();
162  17 if (txt.toLowerCase(Locale.US).endsWith(".xml")) {
163  17 txt = txt.substring(0, txt.length() - 4);
164    }
165  17 if (lan.length() > 0) {
166  17 txt = txt + "_" + lan;
167    }
168  17 destination = new File(KernelContext.getInstance().getConfig()
169    .getGenerationDirectory(), txt + ".txt").getCanonicalFile();
170  17 printer = new TextOutput(visitor.getQedeqBo().getName(), new FileOutputStream(destination),
171    "UTF-8");
172   
173  17 try {
174  17 visitor.generateUtf8(process, printer, lan, level);
175    } finally {
176  17 printer.flush();
177  17 printer.close();
178    }
179  17 if (printer.checkError()) {
180  0 throw printer.getError();
181    }
182  17 return destination.toString();
183    }
184   
 
185  0 toggle public String getLocationDescription() {
186  0 if (languages != null && run < languages.length) {
187  0 return languages[run] + " " + visitor.getLocationDescription();
188    }
189  0 if (languages != null && languages.length > 0) {
190  0 return languages[languages.length] + " " + visitor.getLocationDescription();
191    }
192  0 return "unknown";
193    }
194   
 
195  0 toggle public double getVisitPercentage() {
196  0 return visitor.getVisitPercentage() / languages.length * (run + 1);
197    }
198   
 
199  0 toggle public boolean getInterrupted() {
200  0 return visitor.getInterrupted();
201    }
202   
203    }