~ubuntu-branches/debian/sid/latexdraw/sid

« back to all changes in this revision

Viewing changes to latexDraw/parsers/svg/parsers/SVGLengthParser.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
import latexDraw.parsers.svg.SVGLength;
 
6
import latexDraw.parsers.svg.SVGLength.LengthType;
 
7
 
 
8
/**
 
9
 * Defines a parser that parses SVG length code.<br>
 
10
 *<br>
 
11
 * This file is part of LaTeXDraw.<br>
 
12
 * Copyright (c) 2005-2008 Arnaud BLOUIN<br>
 
13
 *<br>
 
14
 *  LaTeXDraw is free software; you can redistribute it and/or modify
 
15
 *  it under the terms of the GNU General Public License as published by
 
16
 *  the Free Software Foundation; either version 2 of the License, or
 
17
 *  (at your option) any later version.<br>
 
18
 *<br>
 
19
 *  LaTeXDraw is distributed without any warranty; without even the 
 
20
 *  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
 
21
 *  PURPOSE. See the GNU General Public License for more details.<br>
 
22
 *<br>
 
23
 * 10/24/07<br>
 
24
 * @author Arnaud BLOUIN<br>
 
25
 * @version 0.1<br>
 
26
 * @since 0.1
 
27
 */
 
28
public class SVGLengthParser extends SVGNumberParser
 
29
{
 
30
 
 
31
        /**
 
32
         * The constructor.
 
33
         * @param code The code to parse.
 
34
         * @since 0.1
 
35
         */
 
36
        public SVGLengthParser(String code)
 
37
        {
 
38
                super(code);
 
39
        }
 
40
 
 
41
        
 
42
        
 
43
        /**
 
44
         * Parses an SVG length.
 
45
         * @return An SVGLength.
 
46
         * @throws ParseException If a problem occurs or if not managed unit are parsed (%, em and ex).
 
47
         * @since 0.1
 
48
         */
 
49
        public SVGLength parseLength() throws ParseException
 
50
        {
 
51
                double value = parseNumber(false);
 
52
                LengthType lgthType;
 
53
                String errorUnit = "Invalid unit", valueAsStr;//$NON-NLS-1$
 
54
                
 
55
                setPosition(0);
 
56
                valueAsStr = parseNumberAsString(false);
 
57
                skipWSP();
 
58
                
 
59
                switch(getChar())
 
60
                {
 
61
                        case EOP:
 
62
                                lgthType = LengthType.NUMBER;
 
63
                                break;
 
64
                                
 
65
                        case 'p':
 
66
                                switch(nextChar())
 
67
                                {
 
68
                                        case EOP:
 
69
                                        default:
 
70
                                                throw new ParseException(errorUnit, getPosition());
 
71
                                                
 
72
                                        case 't':
 
73
                                                lgthType = LengthType.PT;
 
74
                                                break;
 
75
                                                        
 
76
                                        case 'x':
 
77
                                                lgthType = LengthType.PX;
 
78
                                                break;
 
79
                                                
 
80
                                        case 'c':
 
81
                                                lgthType = LengthType.PC;
 
82
                                                break;
 
83
                                }
 
84
                                
 
85
                                break;
 
86
                                
 
87
                        case '%':
 
88
                                throw new ParseException("Not yet managed: %", getPosition());//$NON-NLS-1$
 
89
                                
 
90
                        case 'c':
 
91
                                if(nextChar()=='m')
 
92
                                        lgthType = LengthType.CM;
 
93
                                else
 
94
                                        throw new ParseException(errorUnit, getPosition());
 
95
                                
 
96
                                break;
 
97
                                
 
98
                        case 'm':
 
99
                                if(nextChar()=='m')
 
100
                                        lgthType = LengthType.MM;
 
101
                                else
 
102
                                        throw new ParseException(errorUnit, getPosition());
 
103
                                break;
 
104
                                
 
105
                        case 'i':
 
106
                                if(nextChar()=='n')
 
107
                                        lgthType = LengthType.IN;
 
108
                                else
 
109
                                        throw new ParseException(errorUnit, getPosition());
 
110
                                break;
 
111
                                
 
112
                        case 'e':
 
113
                                switch(nextChar())
 
114
                                {
 
115
                                        case EOP:
 
116
                                        default:
 
117
                                                throw new ParseException(errorUnit, getPosition());
 
118
                                                
 
119
                                        case 'm':
 
120
                                                throw new ParseException("Not yet managed: em", getPosition());//$NON-NLS-1$
 
121
                                                
 
122
                                        case 'x':
 
123
                                                throw new ParseException("Not yet managed: ex", getPosition());//$NON-NLS-1$
 
124
                                }
 
125
                                
 
126
                        default:
 
127
                                throw new ParseException(errorUnit, getPosition());
 
128
                }
 
129
                
 
130
                return new SVGLength(UnitProcessor.toUserUnit(value, lgthType), lgthType, valueAsStr);
 
131
        }
 
132
        
 
133
        
 
134
        
 
135
        /**
 
136
         * Parses an SVG coordinate.
 
137
         * @return An SVGLength.
 
138
         * @throws ParseException If a problem occurs or if not managed unit are parsed (%, em and ex).
 
139
         * @since 0.1
 
140
         */
 
141
        public SVGLength parseCoordinate() throws ParseException
 
142
        {
 
143
                return parseLength();
 
144
        }
 
145
        
 
146
        
 
147
        
 
148
        /**
 
149
         * Parses a number or a percentage (not yet managed).
 
150
         * @return An SVGLength.
 
151
         * @throws ParseException If a problem occurs or if a percentage is parsed.
 
152
         * @since 0.1
 
153
         */
 
154
        public SVGLength parseNumberOrPercent() throws ParseException
 
155
        {
 
156
                double value = parseNumber(false);
 
157
                LengthType type;
 
158
                String valueAsStr;
 
159
                
 
160
                setPosition(0);
 
161
                valueAsStr = parseNumberAsString(false);
 
162
                skipWSP();
 
163
                
 
164
                switch(getChar())
 
165
                {
 
166
                        default:
 
167
                        case EOP:
 
168
                                type = LengthType.NUMBER;
 
169
                                break;
 
170
                                
 
171
                        case '%':
 
172
                                throw new ParseException("Not yet managed: %", getPosition());//$NON-NLS-1$
 
173
                }
 
174
                
 
175
                return new SVGLength(UnitProcessor.toUserUnit(value, type), type, valueAsStr);
 
176
        }
 
177
}