1
/* TeXFormulaSettingsParser.java
2
* =========================================================================
3
* This file is part of the JMathTeX Library - http://jmathtex.sourceforge.net
5
* Copyright (C) 2004-2007 Universiteit Gent
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.
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.
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.
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
29
package be.ugent.caagt.jmathtex;
31
import java.util.HashSet;
32
import java.util.List;
35
import org.jdom.Element;
36
import org.jdom.input.SAXBuilder;
39
* Parses predefined TeXFormula's from an XML-file.
41
class TeXFormulaSettingsParser {
43
public static final String RESOURCE_NAME = "TeXFormulaSettings.xml";
44
public static final String CHARTODEL_MAPPING_EL = "Map";
48
public TeXFormulaSettingsParser() throws ResourceParseException {
50
root = new SAXBuilder().build(
51
TeXFormulaSettingsParser.class.getResourceAsStream(RESOURCE_NAME))
54
} catch (Exception e) { // JDOMException or IOException
55
throw new XMLResourceParseException(RESOURCE_NAME, e);
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);
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),
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!
83
throw new XMLResourceParseException(RESOURCE_NAME, map.getName(),
85
else if (symbol == null)
86
throw new XMLResourceParseException(RESOURCE_NAME, map.getName(),
88
if (ch.length() == 1) // valid element found
89
table[ch.charAt(0)] = symbol;
91
// only single-character mappings allowed, ignore others
92
throw new XMLResourceParseException(RESOURCE_NAME, map.getName(),
94
"must have a value that contains exactly 1 character!");
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");
106
throw new XMLResourceParseException(RESOURCE_NAME, style
107
.getName(), "name", null);