Qedeq2Utf8Executor.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.File;
019 import java.io.FileOutputStream;
020 import java.io.IOException;
021 import java.util.Locale;
022 
023 import org.qedeq.base.io.Parameters;
024 import org.qedeq.base.io.TextOutput;
025 import org.qedeq.base.trace.Trace;
026 import org.qedeq.kernel.bo.KernelContext;
027 import org.qedeq.kernel.bo.log.QedeqLog;
028 import org.qedeq.kernel.bo.module.InternalServiceProcess;
029 import org.qedeq.kernel.bo.module.KernelQedeqBo;
030 import org.qedeq.kernel.bo.module.PluginExecutor;
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,
074             final Parameters parameters) {
075         final boolean info = parameters.getBoolean("info");
076         // automatically line break after this column. 0 means no automatic line breaking
077         maxColumns = parameters.getInt("maximumColumn");
078         if (parameters.getInt("maximumColumn"!= 0) {
079             maxColumns = Math.max(10, maxColumns);
080         }
081         final boolean brief = parameters.getBoolean("brief");
082         visitor = new Qedeq2UnicodeVisitor(plugin, prop, info , maxColumns, true, brief);
083     }
084 
085     public Object executePlugin(final InternalServiceProcess process, final Object data) {
086         final String method = "executePlugin()";
087         try {
088             QedeqLog.getInstance().logRequest("Generate UTF-8", visitor.getQedeqBo().getUrl());
089             languages = visitor.getQedeqBo().getSupportedLanguages();
090             for (run = 0; run < languages.length; run++) {
091                 final String result = generateUtf8(process, languages[run]"1");
092                 if (languages[run!= null) {
093                     QedeqLog.getInstance().logSuccessfulReply(
094                         "UTF-8 for language \"" + languages[run]
095                         "\" was generated into \"" + result + "\"", visitor.getQedeqBo().getUrl());
096                 else {
097                     QedeqLog.getInstance().logSuccessfulReply(
098                         "UTF-8 for default language "
099                         "was generated into \"" + result + "\"", visitor.getQedeqBo().getUrl());
100                 }
101             }
102             if (languages.length == 0) {
103                 QedeqLog.getInstance().logMessage("no supported language found, assuming 'en'");
104                 final String result = generateUtf8(process, "en""1");
105                 QedeqLog.getInstance().logSuccessfulReply(
106                     "UTF-8 for language \"en"
107                     "\" was generated into \"" + result + "\"", visitor.getQedeqBo().getUrl());
108             }
109         catch (final SourceFileExceptionList e) {
110             final String msg = "Generation failed";
111             Trace.fatal(CLASS, this, method, msg, e);
112             QedeqLog.getInstance().logFailureReply(msg, visitor.getQedeqBo().getUrl(), e.getMessage());
113         catch (IOException e) {
114             final String msg = "Generation failed";
115             Trace.fatal(CLASS, this, method, msg, e);
116             QedeqLog.getInstance().logFailureReply(msg, visitor.getQedeqBo().getUrl(), e.getMessage());
117         catch (final RuntimeException e) {
118             Trace.fatal(CLASS, this, method, "unexpected problem", e);
119             QedeqLog.getInstance().logFailureReply(
120                 "Generation failed", visitor.getQedeqBo().getUrl()"unexpected problem: "
121                 (e.getMessage() != null ? e.getMessage() : e.toString()));
122         }
123         return null;
124     }
125 
126     /**
127      * Gives a UTF-8 representation of given QEDEQ module as InputStream.
128      *
129      @param   process     This process executes us.
130      @param   language    Filter text to get and produce text in this language only. Might
131      *                      be <code>null</code>
132      @param   level       Filter for this detail level. LATER mime 20050205: not supported
133      *                      yet.
134      @return  Name of generated file.
135      @throws  SourceFileExceptionList Major problem occurred.
136      @throws  IOException     File IO failed.
137      */
138     public String generateUtf8(final InternalServiceProcess process, final String language, final String level)
139             throws SourceFileExceptionList, IOException {
140 
141         // first we try to get more information about required modules and their predicates..
142         try {
143             visitor.getQedeqBo().getKernelServices().loadRequiredModules(
144                 visitor.getQedeqBo(), process);
145             visitor.getQedeqBo().getKernelServices().checkWellFormedness(
146                 visitor.getQedeqBo(), process);
147         catch (Exception e) {
148             // we continue and ignore external predicates
149             Trace.trace(CLASS, "generateUtf8(KernelQedeqBo, String, String)", e);
150         }
151         String lan = "en";
152         if (language != null) {
153             lan = language;
154         }
155 //        if (level == null) {
156 //            this.level = "1";
157 //        } else {
158 //            this.level = level;
159 //        }
160         String txt = visitor.getQedeqBo().getModuleAddress().getFileName();
161         if (txt.toLowerCase(Locale.US).endsWith(".xml")) {
162             txt = txt.substring(0, txt.length() 4);
163         }
164         if (lan.length() 0) {
165             txt = txt + "_" + lan;
166         }
167         destination = new File(KernelContext.getInstance().getConfig()
168             .getGenerationDirectory(), txt + ".txt").getCanonicalFile();
169         printer = new TextOutput(visitor.getQedeqBo().getName()new FileOutputStream(destination),
170             "UTF-8");
171 
172         try {
173             visitor.generateUtf8(process, printer, lan, level);
174         finally {
175             printer.flush();
176             printer.close();
177         }
178         if (printer.checkError()) {
179             throw printer.getError();
180         }
181         return destination.toString();
182     }
183 
184     public String getLocationDescription() {
185         if (languages != null && run < languages.length) {
186             return languages[run" " + visitor.getLocationDescription();
187         }
188         if (languages != null && languages.length > 0) {
189             return languages[languages.length" " + visitor.getLocationDescription();
190         }
191         return "unknown";
192     }
193 
194     public double getExecutionPercentage() {
195         return visitor.getExecutionPercentage() / languages.length * (run + 1);
196     }
197 
198     public boolean getInterrupted() {
199         return visitor.getInterrupted();
200     }
201 
202 }