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.IOException;
019
020 import org.qedeq.base.io.Parameters;
021 import org.qedeq.base.io.StringOutput;
022 import org.qedeq.base.trace.Trace;
023 import org.qedeq.kernel.bo.common.ServiceProcess;
024 import org.qedeq.kernel.bo.log.QedeqLog;
025 import org.qedeq.kernel.bo.module.InternalServiceProcess;
026 import org.qedeq.kernel.bo.module.KernelQedeqBo;
027 import org.qedeq.kernel.bo.module.PluginExecutor;
028 import org.qedeq.kernel.se.common.Plugin;
029 import org.qedeq.kernel.se.common.SourceFileExceptionList;
030
031
032 /**
033 * Transfer a QEDEQ module into a UTF-8 text file.
034 * <p>
035 * <b>This is just a quick written generator. This class just generates some text output to be able
036 * to get a visual impression of a QEDEQ module.</b>
037 *
038 * @author Michael Meyling
039 */
040 public final class Qedeq2UnicodeTextExecutor implements PluginExecutor {
041
042 /** This class. */
043 private static final Class CLASS = Qedeq2UnicodeTextExecutor.class;
044
045 /** Output goes here. */
046 private StringOutput printer;
047
048 /** Filter text to get and produce text in this language. */
049 private String language;
050
051 /** Visitor for producing the text output. */
052 private final Qedeq2UnicodeVisitor visitor;
053
054
055 /**
056 * Constructor.
057 *
058 * @param plugin This plugin we work for.
059 * @param prop QEDEQ BO object.
060 * @param parameters Plugin parameter.
061 */
062 Qedeq2UnicodeTextExecutor(final Plugin plugin, final KernelQedeqBo prop,
063 final Parameters parameters) {
064 language = parameters.getString("language");
065 final boolean info = parameters.getBoolean("info");
066 // automatically line break after this column. 0 means no automatic line breaking
067 int maxColumns = parameters.getInt("maximumColumn");
068 maxColumns = Math.max(10, maxColumns);
069 visitor = new Qedeq2UnicodeVisitor(plugin, prop, info , maxColumns, false, false);
070 }
071
072 public Object executePlugin(final InternalServiceProcess process, final Object data) {
073 final String method = "executePlugin()";
074 String result = "";
075 try {
076 QedeqLog.getInstance().logRequest("Show UTF-8 text", visitor.getQedeqBo().getUrl());
077 result = generateUtf8(process, language, "1");
078 QedeqLog.getInstance().logSuccessfulReply(
079 "UTF-8 text was shown", visitor.getQedeqBo().getUrl());
080 } catch (final SourceFileExceptionList e) {
081 final String msg = "Generation failed";
082 Trace.fatal(CLASS, this, method, msg, e);
083 QedeqLog.getInstance().logFailureReply(msg, visitor.getQedeqBo().getUrl(), e.getMessage());
084 } catch (IOException e) {
085 final String msg = "Generation failed";
086 Trace.fatal(CLASS, this, method, msg, e);
087 QedeqLog.getInstance().logFailureReply(msg, visitor.getQedeqBo().getUrl(), e.getMessage());
088 } catch (final RuntimeException e) {
089 Trace.fatal(CLASS, this, method, "unexpected problem", e);
090 QedeqLog.getInstance().logFailureReply(
091 "Generation failed", visitor.getQedeqBo().getUrl(), "unexpected problem: "
092 + (e.getMessage() != null ? e.getMessage() : e.toString()));
093 }
094 return result;
095 }
096
097 /**
098 * Gives a UTF-8 representation of given QEDEQ module as InputStream.
099 *
100 * @param process This process executes us.
101 * @param language Filter text to get and produce text in this language only.
102 * @param level Filter for this detail level. LATER mime 20050205: not supported
103 * yet.
104 * @return Name of generated file.
105 * @throws SourceFileExceptionList Major problem occurred.
106 * @throws IOException File IO failed.
107 */
108 public String generateUtf8(final ServiceProcess process, final String language, final String level)
109 throws SourceFileExceptionList, IOException {
110
111 String lan = "en";
112 if (language != null) {
113 lan = language;
114 }
115 printer = new StringOutput();
116
117 visitor.generateUtf8(process, printer, lan, level);
118 return printer.toString();
119 }
120
121 public String getLocationDescription() {
122 return visitor.getLocationDescription();
123 }
124
125 public double getExecutionPercentage() {
126 return visitor.getExecutionPercentage();
127 }
128
129 public boolean getInterrupted() {
130 return visitor.getInterrupted();
131 }
132
133 }
|