001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2011, 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 import java.util.Map;
023
024 import org.qedeq.base.io.IoUtility;
025 import org.qedeq.base.io.TextOutput;
026 import org.qedeq.base.trace.Trace;
027 import org.qedeq.kernel.bo.KernelContext;
028 import org.qedeq.kernel.bo.common.PluginExecutor;
029 import org.qedeq.kernel.bo.log.QedeqLog;
030 import org.qedeq.kernel.bo.module.KernelQedeqBo;
031 import org.qedeq.kernel.se.common.Plugin;
032 import org.qedeq.kernel.se.common.SourceFileExceptionList;
033
034
035 /**
036 * Transfer a QEDEQ module into a UTF-8 text file.
037 * <p>
038 * <b>This is just a quick written generator. This class just generates some text output to be able
039 * to get a visual impression of a QEDEQ module.</b>
040 *
041 * @author Michael Meyling
042 */
043 public class Qedeq2Utf8Executor implements PluginExecutor {
044
045 /** This class. */
046 private static final Class CLASS = Qedeq2Utf8Executor.class;
047
048 /** Output goes here. */
049 private TextOutput printer;
050
051 /** Current destination file. */
052 private File destination;
053
054 /** Visit all nodes with this visitor. */
055 private final Qedeq2UnicodeVisitor visitor;
056
057 /** Break automatically before this column number. */
058 private int maxColumns;
059
060 /** Generate text for these languages. */
061 private String[] languages;
062
063 /** Current language selection. See {@link #languages}. */
064 private int run = 0;
065
066 /**
067 * Constructor.
068 *
069 * @param plugin This plugin we work for.
070 * @param prop QEDEQ BO object.
071 * @param parameters Plugin parameter.
072 */
073 public Qedeq2Utf8Executor(final Plugin plugin, final KernelQedeqBo prop, final Map parameters) {
074 String infoString = null;
075 String maxColumnsString = "0";
076 if (parameters != null) {
077 infoString = (String) parameters.get("info");
078 if (infoString == null) {
079 infoString = "false";
080 }
081 maxColumnsString = (String) parameters.get("maximumColumn");
082 if (maxColumnsString == null || maxColumnsString.length() == 0) {
083 maxColumnsString = "80";
084 }
085 }
086 boolean info = "true".equalsIgnoreCase(infoString);
087 maxColumns = 0;
088 try {
089 maxColumns = Integer.parseInt(maxColumnsString.trim());
090 } catch (RuntimeException e) {
091 // ignore
092 }
093 visitor = new Qedeq2UnicodeVisitor(plugin, prop, info , maxColumns, true);
094 }
095
096 public Object executePlugin() {
097 final String method = "executePlugin()";
098 final String ref = "\"" + IoUtility.easyUrl(visitor.getQedeqBo().getUrl()) + "\"";
099 try {
100 QedeqLog.getInstance().logRequest("Generate UTF-8 from " + ref);
101 languages = visitor.getQedeqBo().getSupportedLanguages();
102 for (run = 0; run < languages.length; run++) {
103 final String result = generateUtf8(languages[run], "1");
104 if (languages[run] != null) {
105 QedeqLog.getInstance().logSuccessfulReply(
106 "UTF-8 for language \"" + languages[run]
107 + "\" was generated from " + ref + " into \"" + result + "\"");
108 } else {
109 QedeqLog.getInstance().logSuccessfulReply(
110 "UTF-8 for default language "
111 + "was generated from " + ref + " into \"" + result + "\"");
112 }
113 }
114 } catch (final SourceFileExceptionList e) {
115 final String msg = "Generation failed for " + ref;
116 Trace.fatal(CLASS, this, method, msg, e);
117 QedeqLog.getInstance().logFailureReply(msg, e.getMessage());
118 } catch (IOException e) {
119 final String msg = "Generation failed for " + ref;
120 Trace.fatal(CLASS, this, method, msg, e);
121 QedeqLog.getInstance().logFailureReply(msg, e.getMessage());
122 } catch (final RuntimeException e) {
123 Trace.fatal(CLASS, this, method, "unexpected problem", e);
124 QedeqLog.getInstance().logFailureReply(
125 "Generation failed", "unexpected problem: "
126 + (e.getMessage() != null ? e.getMessage() : e.toString()));
127 }
128 return null;
129 }
130
131 /**
132 * Gives a UTF-8 representation of given QEDEQ module as InputStream.
133 *
134 * @param language Filter text to get and produce text in this language only.
135 * @param level Filter for this detail level. LATER mime 20050205: not supported
136 * yet.
137 * @return Name of generated file.
138 * @throws SourceFileExceptionList Major problem occurred.
139 * @throws IOException File IO failed.
140 */
141 public String generateUtf8(final String language, final String level)
142 throws SourceFileExceptionList, IOException {
143
144 // first we try to get more information about required modules and their predicates..
145 try {
146 visitor.getQedeqBo().getKernelServices().loadRequiredModules(
147 visitor.getQedeqBo().getModuleAddress());
148 visitor.getQedeqBo().getKernelServices().checkModule(
149 visitor.getQedeqBo().getModuleAddress());
150 } catch (Exception e) {
151 // we continue and ignore external predicates
152 Trace.trace(CLASS, "generateUtf8(KernelQedeqBo, String, String)", e);
153 }
154 String lan = "en";
155 if (language != null) {
156 lan = language;
157 }
158 // if (level == null) {
159 // this.level = "1";
160 // } else {
161 // this.level = level;
162 // }
163 String txt = visitor.getQedeqBo().getModuleAddress().getFileName();
164 if (txt.toLowerCase(Locale.US).endsWith(".xml")) {
165 txt = txt.substring(0, txt.length() - 4);
166 }
167 if (lan != null && lan.length() > 0) {
168 txt = txt + "_" + lan;
169 }
170 destination = new File(KernelContext.getInstance().getConfig()
171 .getGenerationDirectory(), txt + ".txt").getCanonicalFile();
172 printer = new TextOutput(visitor.getQedeqBo().getName(), new FileOutputStream(destination),
173 "UTF-8");
174
175 try {
176 visitor.generateUtf8(printer, lan, level);
177 } finally {
178 if (printer != null) {
179 printer.flush();
180 printer.close();
181 }
182 }
183 if (printer != null && printer.checkError()) {
184 throw printer.getError();
185 }
186 return destination.toString();
187 }
188
189 public String getExecutionActionDescription() {
190 if (languages != null && run < languages.length) {
191 return languages[run] + " " + visitor.getExecutionActionDescription();
192 }
193 if (languages != null && languages.length > 0) {
194 return languages[languages.length] + " " + visitor.getExecutionActionDescription();
195 }
196 return "unknown";
197 }
198
199 public double getExecutionPercentage() {
200 return visitor.getExecutionPercentage() / languages.length * (run + 1);
201 }
202
203 }
|