Element2Utf8Impl.java
01 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
02  *
03  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
04  *
05  * "Hilbert II" is free software; you can redistribute
06  * it and/or modify it under the terms of the GNU General Public
07  * License as published by the Free Software Foundation; either
08  * version 2 of the License, or (at your option) any later version.
09  *
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;
17 
18 import java.util.ArrayList;
19 import java.util.List;
20 
21 import org.apache.commons.lang.ArrayUtils;
22 import org.qedeq.base.utility.StringUtility;
23 import org.qedeq.kernel.bo.common.Element2Utf8;
24 import org.qedeq.kernel.bo.service.unicode.Latex2UnicodeParser;
25 import org.qedeq.kernel.se.base.list.Element;
26 
27 
28 /**
29  * Transfer a QEDEQ formulas into UTF-8 text.
30  *
31  @author  Michael Meyling
32  */
33 public final class Element2Utf8Impl implements Element2Utf8 {
34 
35     /** We use this converter. */
36     private Element2LatexImpl converter;
37 
38     /**
39      * Constructor.
40      *
41      @param   converter   This converter can produce at least LaTeX.
42      */
43     public Element2Utf8Impl(final Element2LatexImpl converter) {
44         this.converter = converter;
45     }
46 
47     public String getUtf8(final Element element) {
48         return getUtf8(element, 0)[0];
49     }
50 
51     public String[] getUtf8(final Element element, final int maxCols) {
52         final String result = Latex2UnicodeParser.transform(null, converter.getLatex(element)0);
53         if (maxCols <= || result.length()  < maxCols) {
54             return new String[] {result};
55         }
56         final List list = new ArrayList();
57         int index = 0;
58         while (index < result.length()) {
59             list.add(StringUtility.substring(result, index, maxCols));
60             index += maxCols;
61         }
62         return (String[]) list.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
63     }
64 
65 }