001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
004 *
005 * "Hilbert II" is free software; you can redistribute
006 * it and/or modify it under the terms of the GNU General Public
007 * License as published by the Free Software Foundation; either
008 * version 2 of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 */
015
016 package org.qedeq.kernel.bo.service.unicode;
017
018 import java.io.File;
019 import java.io.FileOutputStream;
020 import java.io.IOException;
021 import java.util.Locale;
022
023 import org.qedeq.base.io.Parameters;
024 import org.qedeq.base.io.TextOutput;
025 import org.qedeq.base.trace.Trace;
026 import org.qedeq.kernel.bo.KernelContext;
027 import org.qedeq.kernel.bo.common.PluginExecutor;
028 import org.qedeq.kernel.bo.log.QedeqLog;
029 import org.qedeq.kernel.bo.module.KernelQedeqBo;
030 import org.qedeq.kernel.se.common.Plugin;
031 import org.qedeq.kernel.se.common.SourceFileExceptionList;
032
033
034 /**
035 * Transfer a QEDEQ module into a UTF-8 text file.
036 * <p>
037 * <b>This is just a quick written generator. This class just generates some text output to be able
038 * to get a visual impression of a QEDEQ module.</b>
039 *
040 * @author Michael Meyling
041 */
042 public class Qedeq2Utf8Executor implements PluginExecutor {
043
044 /** This class. */
045 private static final Class CLASS = Qedeq2Utf8Executor.class;
046
047 /** Output goes here. */
048 private TextOutput printer;
049
050 /** Current destination file. */
051 private File destination;
052
053 /** Visit all nodes with this visitor. */
054 private final Qedeq2UnicodeVisitor visitor;
055
056 /** Break automatically before this column number. */
057 private int maxColumns;
058
059 /** Generate text for these languages. */
060 private String[] languages;
061
062 /** Current language selection. See {@link #languages}. */
063 private int run = 0;
064
065 /**
066 * Constructor.
067 *
068 * @param plugin This plugin we work for.
069 * @param prop QEDEQ BO object.
070 * @param parameters Plugin parameter.
071 */
072 public Qedeq2Utf8Executor(final Plugin plugin, final KernelQedeqBo prop, final Parameters parameters) {
073 final boolean info = parameters.getBoolean("info");
074 // automatically line break after this column. 0 means no automatic line breaking
075 maxColumns = parameters.getInt("maximumColumn");
076 if (parameters.getInt("maximumColumn") != 0) {
077 maxColumns = Math.max(10, maxColumns);
078 }
079 final boolean brief = parameters.getBoolean("brief");
080 visitor = new Qedeq2UnicodeVisitor(plugin, prop, info , maxColumns, true, brief);
081 }
082
083 public Object executePlugin() {
084 final String method = "executePlugin()";
085 try {
086 QedeqLog.getInstance().logRequest("Generate UTF-8", visitor.getQedeqBo().getUrl());
087 languages = visitor.getQedeqBo().getSupportedLanguages();
088 for (run = 0; run < languages.length; run++) {
089 final String result = generateUtf8(languages[run], "1");
090 if (languages[run] != null) {
091 QedeqLog.getInstance().logSuccessfulReply(
092 "UTF-8 for language \"" + languages[run]
093 + "\" was generated into \"" + result + "\"", visitor.getQedeqBo().getUrl());
094 } else {
095 QedeqLog.getInstance().logSuccessfulReply(
096 "UTF-8 for default language "
097 + "was generated into \"" + result + "\"", visitor.getQedeqBo().getUrl());
098 }
099 }
100 } catch (final SourceFileExceptionList e) {
101 final String msg = "Generation failed";
102 Trace.fatal(CLASS, this, method, msg, e);
103 QedeqLog.getInstance().logFailureReply(msg, visitor.getQedeqBo().getUrl(), e.getMessage());
104 } catch (IOException e) {
105 final String msg = "Generation failed";
106 Trace.fatal(CLASS, this, method, msg, e);
107 QedeqLog.getInstance().logFailureReply(msg, visitor.getQedeqBo().getUrl(), e.getMessage());
108 } catch (final RuntimeException e) {
109 Trace.fatal(CLASS, this, method, "unexpected problem", e);
110 QedeqLog.getInstance().logFailureReply(
111 "Generation failed", visitor.getQedeqBo().getUrl(), "unexpected problem: "
112 + (e.getMessage() != null ? e.getMessage() : e.toString()));
113 }
114 return null;
115 }
116
117 /**
118 * Gives a UTF-8 representation of given QEDEQ module as InputStream.
119 *
120 * @param language Filter text to get and produce text in this language only.
121 * @param level Filter for this detail level. LATER mime 20050205: not supported
122 * yet.
123 * @return Name of generated file.
124 * @throws SourceFileExceptionList Major problem occurred.
125 * @throws IOException File IO failed.
126 */
127 public String generateUtf8(final String language, final String level)
128 throws SourceFileExceptionList, IOException {
129
130 // first we try to get more information about required modules and their predicates..
131 try {
132 visitor.getQedeqBo().getKernelServices().loadRequiredModules(
133 visitor.getQedeqBo().getModuleAddress());
134 visitor.getQedeqBo().getKernelServices().checkModule(
135 visitor.getQedeqBo().getModuleAddress());
136 } catch (Exception e) {
137 // we continue and ignore external predicates
138 Trace.trace(CLASS, "generateUtf8(KernelQedeqBo, String, String)", e);
139 }
140 String lan = "en";
141 if (language != null) {
142 lan = language;
143 }
144 // if (level == null) {
145 // this.level = "1";
146 // } else {
147 // this.level = level;
148 // }
149 String txt = visitor.getQedeqBo().getModuleAddress().getFileName();
150 if (txt.toLowerCase(Locale.US).endsWith(".xml")) {
151 txt = txt.substring(0, txt.length() - 4);
152 }
153 if (lan != null && lan.length() > 0) {
154 txt = txt + "_" + lan;
155 }
156 destination = new File(KernelContext.getInstance().getConfig()
157 .getGenerationDirectory(), txt + ".txt").getCanonicalFile();
158 printer = new TextOutput(visitor.getQedeqBo().getName(), new FileOutputStream(destination),
159 "UTF-8");
160
161 try {
162 visitor.generateUtf8(printer, lan, level);
163 } finally {
164 if (printer != null) {
165 printer.flush();
166 printer.close();
167 }
168 }
169 if (printer != null && printer.checkError()) {
170 throw printer.getError();
171 }
172 return destination.toString();
173 }
174
175 public String getLocationDescription() {
176 if (languages != null && run < languages.length) {
177 return languages[run] + " " + visitor.getLocationDescription();
178 }
179 if (languages != null && languages.length > 0) {
180 return languages[languages.length] + " " + visitor.getLocationDescription();
181 }
182 return "unknown";
183 }
184
185 public double getExecutionPercentage() {
186 return visitor.getExecutionPercentage() / languages.length * (run + 1);
187 }
188
189 }
|