View Javadoc

1   /* This file is part of the project "Hilbert II" - http://www.qedeq.org" target="alexandria_uri">http://www.qedeq.org
2    *
3    * Copyright 2000-2014,  Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU General Public License for more details.
14   */
15  
16  package org.qedeq.kernel.bo.service.unicode;
17  
18  import java.io.IOException;
19  
20  import org.qedeq.base.io.Parameters;
21  import org.qedeq.base.io.StringOutput;
22  import org.qedeq.base.trace.Trace;
23  import org.qedeq.kernel.bo.log.QedeqLog;
24  import org.qedeq.kernel.bo.module.InternalModuleServiceCall;
25  import org.qedeq.kernel.bo.module.InternalServiceJob;
26  import org.qedeq.kernel.bo.module.KernelQedeqBo;
27  import org.qedeq.kernel.bo.service.basis.ModuleServicePluginExecutor;
28  import org.qedeq.kernel.se.common.ModuleService;
29  import org.qedeq.kernel.se.common.SourceFileExceptionList;
30  
31  
32  /**
33   * Transfer a QEDEQ module into a UTF-8 text file.
34   * <p>
35   * <b>This is just a quick written generator. This class just generates some text output to be able
36   * to get a visual impression of a QEDEQ module.</b>
37   *
38   * @author  Michael Meyling
39   */
40  public final class Qedeq2UnicodeTextExecutor implements ModuleServicePluginExecutor {
41  
42      /** This class. */
43      private static final Class CLASS = Qedeq2UnicodeTextExecutor.class;
44  
45      /** Output goes here. */
46      private StringOutput printer;
47  
48      /** Filter text to get and produce text in this language. */
49      private String language;
50  
51      /** Visitor for producing the text output. */
52      private final Qedeq2UnicodeVisitor visitor;
53  
54  
55      /**
56       * Constructor.
57       *
58       * @param   plugin      This plugin we work for.
59       * @param   prop        QEDEQ BO object.
60       * @param   parameters  Plugin parameter.
61       */
62      Qedeq2UnicodeTextExecutor(final ModuleService plugin, final KernelQedeqBo prop,
63              final Parameters parameters) {
64          language = parameters.getString("language");
65          final boolean info = parameters.getBoolean("info");
66          // automatically line break after this column. 0 means no automatic line breaking
67          int maxColumns = parameters.getInt("maximumColumn");
68          maxColumns = Math.max(10, maxColumns);
69          visitor = new Qedeq2UnicodeVisitor(plugin, prop, info , maxColumns, false, false);
70      }
71  
72      public Object executePlugin(final InternalModuleServiceCall call, final Object data) {
73          final String method = "executePlugin()";
74          String result = "";
75          try {
76              QedeqLog.getInstance().logRequest("Show UTF-8 text", visitor.getKernelQedeqBo().getUrl());
77              result = generateUtf8(call.getInternalServiceProcess(), language, "1");
78              QedeqLog.getInstance().logSuccessfulReply(
79                  "UTF-8 text was shown", visitor.getKernelQedeqBo().getUrl());
80          } catch (final SourceFileExceptionList e) {
81              final String msg = "Generation failed";
82              Trace.fatal(CLASS, this, method, msg, e);
83              QedeqLog.getInstance().logFailureReply(msg, visitor.getKernelQedeqBo().getUrl(), e.getMessage());
84          } catch (IOException e) {
85              final String msg = "Generation failed";
86              Trace.fatal(CLASS, this, method, msg, e);
87              QedeqLog.getInstance().logFailureReply(msg, visitor.getKernelQedeqBo().getUrl(), e.getMessage());
88          } catch (final RuntimeException e) {
89              Trace.fatal(CLASS, this, method, "unexpected problem", e);
90              QedeqLog.getInstance().logFailureReply(
91                  "Generation failed", visitor.getKernelQedeqBo().getUrl(), "unexpected problem: "
92                  + (e.getMessage() != null ? e.getMessage() : e.toString()));
93          }
94          return result;
95      }
96  
97      /**
98       * Gives a UTF-8 representation of given QEDEQ module as InputStream.
99       *
100      * @param   process     We run in this service process.
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 InternalServiceJob process, final String language,
109             final String level) 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 getVisitPercentage() {
126         return visitor.getVisitPercentage();
127     }
128 
129     public boolean getInterrupted() {
130         return visitor.getInterrupted();
131     }
132 
133 }