~ubuntu-branches/debian/squeeze/latexdraw/squeeze

« back to all changes in this revision

Viewing changes to latexDraw/parsers/svg/parsers/SVGNumberParser.java

  • Committer: Bazaar Package Importer
  • Author(s): Stuart Prescott
  • Date: 2009-05-03 23:49:35 UTC
  • Revision ID: james.westby@ubuntu.com-20090503234935-cls7n48x018g0vk2
Tags: upstream-2.0.2+1
ImportĀ upstreamĀ versionĀ 2.0.2+1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package latexDraw.parsers.svg.parsers;
 
2
 
 
3
import java.text.ParseException;
 
4
 
 
5
/**
 
6
 * Defines a SVG number parser.<br>
 
7
 *<br>
 
8
 * This file is part of LaTeXDraw.<br>
 
9
 * Copyright (c) 2005-2008 Arnaud BLOUIN<br>
 
10
 *<br>
 
11
 *  LaTeXDraw is free software; you can redistribute it and/or modify
 
12
 *  it under the terms of the GNU General Public License as published by
 
13
 *  the Free Software Foundation; either version 2 of the License, or
 
14
 *  (at your option) any later version.<br>
 
15
 *<br>
 
16
 *  LaTeXDraw is distributed without any warranty; without even the 
 
17
 *  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
 
18
 *  PURPOSE. See the GNU General Public License for more details.<br>
 
19
 *<br>
 
20
 * 10/20/07<br>
 
21
 * @author Arnaud BLOUIN<br>
 
22
 * @version 0.1<br>
 
23
 * @since 0.1
 
24
 */
 
25
public class SVGNumberParser extends AbstractSVGParser
 
26
{
 
27
        /** 
 
28
         * The constructor.
 
29
         * @param code The code to parse.
 
30
         */
 
31
        public SVGNumberParser(String code)
 
32
        {
 
33
                super(code);
 
34
        }
 
35
        
 
36
        
 
37
        
 
38
        /**
 
39
         * Parses a flag (a boolean).
 
40
         * @return True or false.
 
41
         * @throws ParseException If an error occurs.
 
42
         * @since 0.1
 
43
         */
 
44
        public boolean parseFlag() throws ParseException
 
45
        {
 
46
                skipWSP();
 
47
                int c = getChar();
 
48
                
 
49
                if(c=='0' || c=='1')
 
50
                {
 
51
                                boolean flag = c=='1' ? true : false;
 
52
                                
 
53
                                nextChar();
 
54
                                return flag;
 
55
                }
 
56
                
 
57
                throw new ParseException("Flag expected.", getPosition());              //$NON-NLS-1$
 
58
        }
 
59
        
 
60
        
 
61
        
 
62
        /**
 
63
         * Parses a number (a double).
 
64
         * @param unsigned True: the parsed number must be unsigned.
 
65
         * @return The parsed number as a string.
 
66
         * @throws ParseException If an error occurs or if the parsed number is signed and <code>unsigned</code> is true.
 
67
         * @since 0.1
 
68
         */
 
69
        public String parseNumberAsString(boolean unsigned) throws ParseException
 
70
        {
 
71
                boolean again = true;
 
72
                int c, start;
 
73
                
 
74
                skipWSP();
 
75
                start = getPosition();
 
76
                c = getChar();
 
77
                
 
78
                if(c=='-' || c=='+')// Reading the sign
 
79
                {
 
80
                        if(unsigned)
 
81
                                throw new ParseException("Unsigned number expected.", getPosition());//$NON-NLS-1$
 
82
                        
 
83
                        c = nextChar();
 
84
                }
 
85
                
 
86
                while(again && c!=EOP) // Reading the first part of the number.
 
87
                        if(c<48 || c>58)
 
88
                                again = false;
 
89
                        else
 
90
                                c = nextChar();
 
91
                
 
92
                if(c=='.')
 
93
                {
 
94
                        c = nextChar();
 
95
                        again = true;
 
96
                        
 
97
                        while(again && c!=EOP) // Reading the second part of the number.
 
98
                                if(c<48 || c>58)
 
99
                                        again = false;
 
100
                                else
 
101
                                        c = nextChar();
 
102
                }
 
103
                
 
104
                if(c=='E' || c=='e')// Reading the exponent.
 
105
                {
 
106
                        c = nextChar();
 
107
                        again = true;
 
108
                        
 
109
                        if(c=='-' || c=='+')// Reading the sign
 
110
                                c = nextChar();
 
111
                        
 
112
                        while(again && c!=EOP) // Reading the exponent.
 
113
                                if(c<48 || c>58)
 
114
                                        again = false;
 
115
                                else
 
116
                                        c = nextChar();
 
117
                }
 
118
                
 
119
                try {  Double.valueOf(code.substring(start, getPosition())); }
 
120
                catch(NumberFormatException e) { throw new ParseException("Invalid number.", getPosition()); }//$NON-NLS-1$
 
121
                
 
122
                return code.substring(start, getPosition());
 
123
        }
 
124
 
 
125
        
 
126
        
 
127
        /**
 
128
         * Parses a number (a double).
 
129
         * @param unsigned True: the parsed number must be unsigned.
 
130
         * @return The parsed number.
 
131
         * @throws ParseException If an error occurs or if the parsed number is signed and <code>unsigned</code> is true.
 
132
         * @since 0.1
 
133
         */
 
134
        public double parseNumber(boolean unsigned) throws ParseException
 
135
        {
 
136
                String number = parseNumberAsString(unsigned);
 
137
                
 
138
                try {  return Double.valueOf(number); }
 
139
                catch(NumberFormatException e) { throw new ParseException("Invalid number.", getPosition()); }//$NON-NLS-1$
 
140
        }
 
141
        
 
142
        
 
143
        
 
144
        /**
 
145
         * @return True if the current character is the beginning of a number.
 
146
         * @param unsigned True: the next number must not have a sign.
 
147
         * @since 0.1
 
148
         */
 
149
        protected boolean isNumber(boolean unsigned)
 
150
        {
 
151
                int c = getChar();
 
152
                
 
153
                if(unsigned)
 
154
                        return c=='.' || (c>=48 && c<=57);
 
155
                
 
156
                return c=='-' || c=='+' || c=='.' || (c>=48 && c<=57);
 
157
        }
 
158
}