1
package latexDraw.parsers.svg.parsers;
3
import java.text.ParseException;
6
* Defines a SVG number parser.<br>
8
* This file is part of LaTeXDraw.<br>
9
* Copyright (c) 2005-2008 Arnaud BLOUIN<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>
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>
21
* @author Arnaud BLOUIN<br>
25
public class SVGNumberParser extends AbstractSVGParser
29
* @param code The code to parse.
31
public SVGNumberParser(String code)
39
* Parses a flag (a boolean).
40
* @return True or false.
41
* @throws ParseException If an error occurs.
44
public boolean parseFlag() throws ParseException
51
boolean flag = c=='1' ? true : false;
57
throw new ParseException("Flag expected.", getPosition()); //$NON-NLS-1$
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.
69
public String parseNumberAsString(boolean unsigned) throws ParseException
75
start = getPosition();
78
if(c=='-' || c=='+')// Reading the sign
81
throw new ParseException("Unsigned number expected.", getPosition());//$NON-NLS-1$
86
while(again && c!=EOP) // Reading the first part of the number.
97
while(again && c!=EOP) // Reading the second part of the number.
104
if(c=='E' || c=='e')// Reading the exponent.
109
if(c=='-' || c=='+')// Reading the sign
112
while(again && c!=EOP) // Reading the exponent.
119
try { Double.valueOf(code.substring(start, getPosition())); }
120
catch(NumberFormatException e) { throw new ParseException("Invalid number.", getPosition()); }//$NON-NLS-1$
122
return code.substring(start, getPosition());
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.
134
public double parseNumber(boolean unsigned) throws ParseException
136
String number = parseNumberAsString(unsigned);
138
try { return Double.valueOf(number); }
139
catch(NumberFormatException e) { throw new ParseException("Invalid number.", getPosition()); }//$NON-NLS-1$
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.
149
protected boolean isNumber(boolean unsigned)
154
return c=='.' || (c>=48 && c<=57);
156
return c=='-' || c=='+' || c=='.' || (c>=48 && c<=57);