| 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.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.InternalModuleServiceCall; |
| 29 | import org.qedeq.kernel.bo.module.InternalServiceJob; |
| 30 | import org.qedeq.kernel.bo.module.KernelQedeqBo; |
| 31 | import org.qedeq.kernel.bo.service.basis.ModuleServicePluginExecutor; |
| 32 | import org.qedeq.kernel.se.common.ModuleService; |
| 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 ModuleServicePluginExecutor { |
| 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 | public Qedeq2Utf8Executor(final ModuleService plugin, final KernelQedeqBo prop, |
| 75 | final Parameters parameters) { |
| 76 | final boolean info = parameters.getBoolean("info"); |
| 77 | // automatically line break after this column. 0 means no automatic line breaking |
| 78 | maxColumns = parameters.getInt("maximumColumn"); |
| 79 | if (parameters.getInt("maximumColumn") != 0) { |
| 80 | maxColumns = Math.max(10, maxColumns); |
| 81 | } |
| 82 | final boolean brief = parameters.getBoolean("brief"); |
| 83 | visitor = new Qedeq2UnicodeVisitor(plugin, prop, info , maxColumns, true, brief); |
| 84 | } |
| 85 | |
| 86 | public Object executePlugin(final InternalModuleServiceCall call, final Object data) { |
| 87 | final String method = "executePlugin()"; |
| 88 | try { |
| 89 | QedeqLog.getInstance().logRequest("Generate UTF-8", visitor.getKernelQedeqBo().getUrl()); |
| 90 | languages = visitor.getKernelQedeqBo().getSupportedLanguages(); |
| 91 | for (run = 0; run < languages.length; run++) { |
| 92 | final String result = generateUtf8(call.getInternalServiceProcess(), languages[run], "1"); |
| 93 | if (languages[run] != null) { |
| 94 | QedeqLog.getInstance().logSuccessfulReply( |
| 95 | "UTF-8 for language \"" + languages[run] |
| 96 | + "\" was generated into \"" + result + "\"", visitor.getKernelQedeqBo().getUrl()); |
| 97 | } else { |
| 98 | QedeqLog.getInstance().logSuccessfulReply( |
| 99 | "UTF-8 for default language " |
| 100 | + "was generated into \"" + result + "\"", visitor.getKernelQedeqBo().getUrl()); |
| 101 | } |
| 102 | } |
| 103 | if (languages.length == 0) { |
| 104 | QedeqLog.getInstance().logMessage("no supported language found, assuming 'en'"); |
| 105 | final String result = generateUtf8(call.getInternalServiceProcess(), "en", "1"); |
| 106 | QedeqLog.getInstance().logSuccessfulReply( |
| 107 | "UTF-8 for language \"en" |
| 108 | + "\" was generated into \"" + result + "\"", visitor.getKernelQedeqBo().getUrl()); |
| 109 | } |
| 110 | } catch (final SourceFileExceptionList e) { |
| 111 | final String msg = "Generation failed"; |
| 112 | Trace.fatal(CLASS, this, method, msg, e); |
| 113 | QedeqLog.getInstance().logFailureReply(msg, visitor.getKernelQedeqBo().getUrl(), e.getMessage()); |
| 114 | } catch (IOException e) { |
| 115 | final String msg = "Generation failed"; |
| 116 | Trace.fatal(CLASS, this, method, msg, e); |
| 117 | QedeqLog.getInstance().logFailureReply(msg, visitor.getKernelQedeqBo().getUrl(), e.getMessage()); |
| 118 | } catch (final RuntimeException e) { |
| 119 | Trace.fatal(CLASS, this, method, "unexpected problem", e); |
| 120 | QedeqLog.getInstance().logFailureReply( |
| 121 | "Generation failed", visitor.getKernelQedeqBo().getUrl(), "unexpected problem: " |
| 122 | + (e.getMessage() != null ? e.getMessage() : e.toString())); |
| 123 | } |
| 124 | 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 | public String generateUtf8(final InternalServiceJob 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 | try { |
| 144 | visitor.getKernelQedeqBo().getKernelServices().loadRequiredModules( |
| 145 | process, visitor.getKernelQedeqBo()); |
| 146 | visitor.getKernelQedeqBo().getKernelServices().checkWellFormedness( |
| 147 | process, visitor.getKernelQedeqBo()); |
| 148 | } catch (Exception e) { |
| 149 | // we continue and ignore external predicates |
| 150 | Trace.trace(CLASS, "generateUtf8(KernelQedeqBo, String, String)", e); |
| 151 | } |
| 152 | String lan = "en"; |
| 153 | if (language != null) { |
| 154 | lan = language; |
| 155 | } |
| 156 | // if (level == null) { |
| 157 | // this.level = "1"; |
| 158 | // } else { |
| 159 | // this.level = level; |
| 160 | // } |
| 161 | String txt = visitor.getKernelQedeqBo().getModuleAddress().getFileName(); |
| 162 | if (txt.toLowerCase(Locale.US).endsWith(".xml")) { |
| 163 | txt = txt.substring(0, txt.length() - 4); |
| 164 | } |
| 165 | if (lan.length() > 0) { |
| 166 | txt = txt + "_" + lan; |
| 167 | } |
| 168 | destination = new File(KernelContext.getInstance().getConfig() |
| 169 | .getGenerationDirectory(), txt + ".txt").getCanonicalFile(); |
| 170 | printer = new TextOutput(visitor.getKernelQedeqBo().getName(), new FileOutputStream(destination), |
| 171 | "UTF-8"); |
| 172 | |
| 173 | try { |
| 174 | visitor.generateUtf8(process, printer, lan, level); |
| 175 | } finally { |
| 176 | printer.flush(); |
| 177 | printer.close(); |
| 178 | } |
| 179 | if (printer.checkError()) { |
| 180 | throw printer.getError(); |
| 181 | } |
| 182 | return destination.toString(); |
| 183 | } |
| 184 | |
| 185 | public String getLocationDescription() { |
| 186 | if (languages != null && run < languages.length) { |
| 187 | return languages[run] + " " + visitor.getLocationDescription(); |
| 188 | } |
| 189 | if (languages != null && languages.length > 0) { |
| 190 | return languages[languages.length] + " " + visitor.getLocationDescription(); |
| 191 | } |
| 192 | return "unknown"; |
| 193 | } |
| 194 | |
| 195 | public double getVisitPercentage() { |
| 196 | return visitor.getVisitPercentage() / languages.length * (run + 1); |
| 197 | } |
| 198 | |
| 199 | public boolean getInterrupted() { |
| 200 | return visitor.getInterrupted(); |
| 201 | } |
| 202 | |
| 203 | } |