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

« back to all changes in this revision

Viewing changes to be/ugent/caagt/jmathtex/mathml/NumberUnit.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
/* NumberUnit.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.mathml;
 
30
 
 
31
import java.util.Map;
 
32
 
 
33
import be.ugent.caagt.jmathtex.TeXConstants;
 
34
import java.util.HashMap;
 
35
 
 
36
/**
 
37
 * Responsible for parsing attribute values that can have units.
 
38
 */
 
39
class NumberUnit {
 
40
    // no unit found
 
41
    public static final int NO_UNIT = -1;
 
42
    
 
43
    // possible units
 
44
    private static Map<String,Integer> mathUnits = new HashMap<String,Integer> ();
 
45
    
 
46
    // parsed value
 
47
    private float number;
 
48
    
 
49
    // parsed unit
 
50
    private final int unit;
 
51
    
 
52
    static {
 
53
      /*
 
54
       * supported MathML unit types
 
55
       */
 
56
        mathUnits.put("em", TeXConstants.UNIT_EM);
 
57
        mathUnits.put("ex", TeXConstants.UNIT_EX);
 
58
        mathUnits.put("px", TeXConstants.UNIT_PIXEL);
 
59
        mathUnits.put("pt", TeXConstants.UNIT_POINT);
 
60
        mathUnits.put("pc", TeXConstants.UNIT_PICA); // autoboxing
 
61
    }
 
62
    
 
63
    public NumberUnit(String value, boolean unitAllowed) throws MathMLException {
 
64
        // remove surrounding whitespace
 
65
        value = value.trim();
 
66
        // find unit
 
67
        boolean noUnit = false;
 
68
        if (value.length() >= 2) { // unit = 2 characters
 
69
            Object u = mathUnits.get(value.substring(value.length() - 2)
 
70
            .toLowerCase());
 
71
            if (u == null) {
 
72
                noUnit = true;
 
73
                unit = 0;
 
74
            } else {// unit found
 
75
                // set unit
 
76
                unit = ((Integer) u).intValue();
 
77
                // remove unit from the string
 
78
                value = value.substring(0, value.length() - 2);
 
79
            }
 
80
            
 
81
        } else {
 
82
            noUnit = true;
 
83
            unit = 0;
 
84
        }
 
85
        
 
86
        // parse value
 
87
        try {
 
88
            double val = Double.parseDouble(value);
 
89
            // if the value is 0 then the unit doesn't have to be specified, even
 
90
            // if unitAllowed=true
 
91
            if (unitAllowed && noUnit && val != 0)
 
92
                throw new MathMLException("Invalid unit in argument!");
 
93
            else
 
94
                // set value
 
95
                number = (float) val;
 
96
        } catch (NumberFormatException e) {
 
97
            throw new MathMLException("Invalid number in argument : '" + value
 
98
                    + "'!", e);
 
99
        }
 
100
    }
 
101
    
 
102
    public float getNumber() {
 
103
        return number;
 
104
    }
 
105
    
 
106
    public int getUnit() {
 
107
        return unit;
 
108
    }
 
109
}