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