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