~ubuntu-branches/ubuntu/utopic/libjmathtex-java/utopic

« back to all changes in this revision

Viewing changes to be/ugent/caagt/jmathtex/TeXFormulaSettingsParser.java

  • Committer: Bazaar Package Importer
  • Author(s): Adriaan Peeters
  • Date: 2007-09-25 14:07:55 UTC
  • Revision ID: james.westby@ubuntu.com-20070925140755-mro37pqu4wkhqa9t
Tags: upstream-0.7~pre
ImportĀ upstreamĀ versionĀ 0.7~pre

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* TeXFormulaSettingsParser.java
 
2
 * =========================================================================
 
3
 * This file is part of the JMathTeX Library - http://jmathtex.sourceforge.net
 
4
 *
 
5
 * Copyright (C) 2004-2007 Universiteit Gent
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or (at
 
10
 * your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful, but
 
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * General Public License for more details.
 
16
 *
 
17
 * A copy of the GNU General Public License can be found in the file
 
18
 * LICENSE.txt provided with the source distribution of this program (see
 
19
 * the META-INF directory in the source jar). This license can also be
 
20
 * found on the GNU website at http://www.gnu.org/licenses/gpl.html.
 
21
 *
 
22
 * If you did not receive a copy of the GNU General Public License along
 
23
 * with this program, contact the lead developer, or write to the Free
 
24
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
25
 * 02110-1301, USA.
 
26
 *
 
27
 */
 
28
 
 
29
package be.ugent.caagt.jmathtex;
 
30
 
 
31
import java.util.HashSet;
 
32
import java.util.List;
 
33
import java.util.Set;
 
34
 
 
35
import org.jdom.Element;
 
36
import org.jdom.input.SAXBuilder;
 
37
 
 
38
/**
 
39
 * Parses predefined TeXFormula's from an XML-file.
 
40
 */
 
41
class TeXFormulaSettingsParser {
 
42
    
 
43
    public static final String RESOURCE_NAME = "TeXFormulaSettings.xml";
 
44
    public static final String CHARTODEL_MAPPING_EL = "Map";
 
45
    
 
46
    private Element root;
 
47
    
 
48
    public TeXFormulaSettingsParser() throws ResourceParseException {
 
49
        try {
 
50
            root = new SAXBuilder().build(
 
51
                    TeXFormulaSettingsParser.class.getResourceAsStream(RESOURCE_NAME))
 
52
                    .getRootElement();
 
53
            
 
54
        } catch (Exception e) { // JDOMException or IOException
 
55
            throw new XMLResourceParseException(RESOURCE_NAME, e);
 
56
        }
 
57
    }
 
58
    
 
59
    public String[] parseSymbolMappings() throws ResourceParseException {
 
60
        String[] mappings = new String[FontInfo.NUMBER_OF_CHAR_CODES];
 
61
        Element charToSymbol = root.getChild("CharacterToSymbolMappings");
 
62
        if (charToSymbol != null) // element present
 
63
            addToMap(charToSymbol.getChildren("Map"), mappings);
 
64
        return mappings;
 
65
    }
 
66
    
 
67
    public String[] parseDelimiterMappings() throws ResourceParseException {
 
68
        String[] mappings = new String[FontInfo.NUMBER_OF_CHAR_CODES];
 
69
        Element charToDelimiter = root.getChild("CharacterToDelimiterMappings");
 
70
        if (charToDelimiter != null) // element present
 
71
            addToMap(charToDelimiter.getChildren(CHARTODEL_MAPPING_EL),
 
72
                    mappings);
 
73
        return mappings;
 
74
    }
 
75
    
 
76
    private static void addToMap(List mapList, String[] table) throws ResourceParseException {
 
77
        for (Object obj : mapList) {
 
78
            Element map = (Element) obj;
 
79
            String ch = map.getAttributeValue("char");
 
80
            String symbol = map.getAttributeValue("symbol");
 
81
            // both attributes are required!
 
82
            if (ch == null)
 
83
                throw new XMLResourceParseException(RESOURCE_NAME, map.getName(),
 
84
                        "char", null);
 
85
            else if (symbol == null)
 
86
                throw new XMLResourceParseException(RESOURCE_NAME, map.getName(),
 
87
                        "symbol", null);
 
88
            if (ch.length() == 1) // valid element found
 
89
                table[ch.charAt(0)] =  symbol;
 
90
            else
 
91
                // only single-character mappings allowed, ignore others
 
92
                throw new XMLResourceParseException(RESOURCE_NAME, map.getName(),
 
93
                        "char",
 
94
                        "must have a value that contains exactly 1 character!");
 
95
        }
 
96
    }
 
97
    
 
98
    public Set<String> parseTextStyles() throws ResourceParseException {
 
99
        Set<String> res = new HashSet<String>();
 
100
        Element textStyles = root.getChild("TextStyles");
 
101
        if (textStyles != null) { // element present
 
102
            for (Object obj : textStyles.getChildren("TextStyle")) {
 
103
                Element style = (Element) obj;
 
104
                String name = style.getAttributeValue("name");
 
105
                if (name == null)
 
106
                    throw new XMLResourceParseException(RESOURCE_NAME, style
 
107
                            .getName(), "name", null);
 
108
                else
 
109
                    res.add(name);
 
110
            }
 
111
        }
 
112
        return res;
 
113
    }
 
114
}