source: josm/trunk/src/com/kitfox/svg/xml/XMLParseUtil.java@ 10866

Last change on this file since 10866 was 10866, checked in by Don-vip, 8 years ago

see #13232 - remove unused classes from JCS and SvgSalamander

  • Property svn:eol-style set to native
File size: 9.4 KB
Line 
1/*
2 * SVG Salamander
3 * Copyright (c) 2004, Mark McKay
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
9 *
10 * - Redistributions of source code must retain the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials
16 * provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
32 * projects can be found at http://www.kitfox.com
33 *
34 * Created on February 18, 2004, 1:49 PM
35 */
36
37package com.kitfox.svg.xml;
38
39import com.kitfox.svg.SVGConst;
40import java.awt.*;
41import java.util.*;
42import java.util.regex.*;
43import java.util.logging.Level;
44import java.util.logging.Logger;
45
46/**
47 * @author Mark McKay
48 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
49 */
50public class XMLParseUtil
51{
52 static final Matcher fpMatch = Pattern.compile("([-+]?((\\d*\\.\\d+)|(\\d+))([eE][+-]?\\d+)?)(\\%|in|cm|mm|pt|pc|px|em|ex)?").matcher("");
53 static final Matcher intMatch = Pattern.compile("[-+]?\\d+").matcher("");
54
55 /** Creates a new instance of XMLParseUtil */
56 private XMLParseUtil()
57 {
58 }
59
60 public static String[] parseStringList(String list)
61 {
62 final Matcher matchWs = Pattern.compile("[^\\s]+").matcher("");
63 matchWs.reset(list);
64
65 LinkedList matchList = new LinkedList();
66 while (matchWs.find())
67 {
68 matchList.add(matchWs.group());
69 }
70
71 String[] retArr = new String[matchList.size()];
72 return (String[])matchList.toArray(retArr);
73 }
74
75 public static double parseDouble(String val)
76 {
77 /*
78 if (val == null) return 0.0;
79
80 double retVal = 0.0;
81 try
82 { retVal = Double.parseDouble(val); }
83 catch (Exception e)
84 {}
85 return retVal;
86 */
87 return findDouble(val);
88 }
89
90 /**
91 * Searches the given string for the first floating point number it contains,
92 * parses and returns it.
93 */
94 public synchronized static double findDouble(String val)
95 {
96 if (val == null) return 0;
97
98 fpMatch.reset(val);
99 try
100 {
101 if (!fpMatch.find()) return 0;
102 }
103 catch (StringIndexOutOfBoundsException e)
104 {
105 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
106 "XMLParseUtil: regex parse problem: '" + val + "'", e);
107 }
108
109 val = fpMatch.group(1);
110 //System.err.println("Parsing " + val);
111
112 double retVal = 0;
113 try
114 {
115 retVal = Double.parseDouble(val);
116
117 float pixPerInch;
118 try {
119 pixPerInch = (float)Toolkit.getDefaultToolkit().getScreenResolution();
120 }
121 catch (NoClassDefFoundError err)
122 {
123 //Default value for headless X servers
124 pixPerInch = 72;
125 }
126 final float inchesPerCm = .3936f;
127 final String units = fpMatch.group(6);
128
129 if ("%".equals(units)) retVal /= 100;
130 else if ("in".equals(units))
131 {
132 retVal *= pixPerInch;
133 }
134 else if ("cm".equals(units))
135 {
136 retVal *= inchesPerCm * pixPerInch;
137 }
138 else if ("mm".equals(units))
139 {
140 retVal *= inchesPerCm * pixPerInch * .1f;
141 }
142 else if ("pt".equals(units))
143 {
144 retVal *= (1f / 72f) * pixPerInch;
145 }
146 else if ("pc".equals(units))
147 {
148 retVal *= (1f / 6f) * pixPerInch;
149 }
150 }
151 catch (Exception e)
152 {}
153 return retVal;
154 }
155
156 /**
157 * Scans an input string for double values. For each value found, places
158 * in a list. This method regards any characters not part of a floating
159 * point value to be seperators. Thus this will parse whitespace seperated,
160 * comma seperated, and many other separation schemes correctly.
161 */
162 public synchronized static double[] parseDoubleList(String list)
163 {
164 if (list == null) return null;
165
166 fpMatch.reset(list);
167
168 LinkedList doubList = new LinkedList();
169 while (fpMatch.find())
170 {
171 String val = fpMatch.group(1);
172 doubList.add(Double.valueOf(val));
173 }
174
175 double[] retArr = new double[doubList.size()];
176 Iterator it = doubList.iterator();
177 int idx = 0;
178 while (it.hasNext())
179 {
180 retArr[idx++] = ((Double)it.next()).doubleValue();
181 }
182
183 return retArr;
184 }
185
186 /**
187 * Searches the given string for the first floating point number it contains,
188 * parses and returns it.
189 */
190 public synchronized static float findFloat(String val)
191 {
192 if (val == null) return 0f;
193
194 fpMatch.reset(val);
195 if (!fpMatch.find()) return 0f;
196
197 val = fpMatch.group(1);
198 //System.err.println("Parsing " + val);
199
200 float retVal = 0f;
201 try
202 {
203 retVal = Float.parseFloat(val);
204 String units = fpMatch.group(6);
205 if ("%".equals(units)) retVal /= 100;
206 }
207 catch (Exception e)
208 {}
209 return retVal;
210 }
211
212 public synchronized static float[] parseFloatList(String list)
213 {
214 if (list == null) return null;
215
216 fpMatch.reset(list);
217
218 LinkedList floatList = new LinkedList();
219 while (fpMatch.find())
220 {
221 String val = fpMatch.group(1);
222 floatList.add(Float.valueOf(val));
223 }
224
225 float[] retArr = new float[floatList.size()];
226 Iterator it = floatList.iterator();
227 int idx = 0;
228 while (it.hasNext())
229 {
230 retArr[idx++] = ((Float)it.next()).floatValue();
231 }
232
233 return retArr;
234 }
235
236 /**
237 * Searches the given string for the first integer point number it contains,
238 * parses and returns it.
239 */
240 public static int findInt(String val)
241 {
242 if (val == null) return 0;
243
244 intMatch.reset(val);
245 if (!intMatch.find()) return 0;
246
247 val = intMatch.group();
248
249 int retVal = 0;
250 try
251 { retVal = Integer.parseInt(val); }
252 catch (Exception e)
253 {}
254 return retVal;
255 }
256
257 public static int[] parseIntList(String list)
258 {
259 if (list == null) return null;
260
261 intMatch.reset(list);
262
263 LinkedList intList = new LinkedList();
264 while (intMatch.find())
265 {
266 String val = intMatch.group();
267 intList.add(Integer.valueOf(val));
268 }
269
270 int[] retArr = new int[intList.size()];
271 Iterator it = intList.iterator();
272 int idx = 0;
273 while (it.hasNext())
274 {
275 retArr[idx++] = ((Integer)it.next()).intValue();
276 }
277
278 return retArr;
279 }
280
281 /**
282 * The input string represents a ratio. Can either be specified as a
283 * double number on the range of [0.0 1.0] or as a percentage [0% 100%]
284 */
285 public static double parseRatio(String val)
286 {
287 if (val == null || val.equals("")) return 0.0;
288
289 if (val.charAt(val.length() - 1) == '%')
290 {
291 parseDouble(val.substring(0, val.length() - 1));
292 }
293 return parseDouble(val);
294 }
295
296 public static NumberWithUnits parseNumberWithUnits(String val)
297 {
298 if (val == null) return null;
299
300 return new NumberWithUnits(val);
301 }
302
303 /**
304 * Takes a CSS style string and returns a hash of them.
305 * @param styleString - A CSS formatted string of styles. Eg,
306 * "font-size:12;fill:#d32c27;fill-rule:evenodd;stroke-width:1pt;"
307 * @param map - A map to which these styles will be added
308 */
309 public static HashMap parseStyle(String styleString, HashMap map) {
310 final Pattern patSemi = Pattern.compile(";");
311
312 String[] styles = patSemi.split(styleString);
313
314 for (int i = 0; i < styles.length; i++)
315 {
316 if (styles[i].length() == 0)
317 {
318 continue;
319 }
320
321 int colon = styles[i].indexOf(':');
322 if (colon == -1)
323 {
324 continue;
325 }
326
327 String key = styles[i].substring(0, colon).trim();
328 String value = styles[i].substring(colon + 1).trim();
329
330 map.put(key, new StyleAttribute(key, value));
331 }
332
333 return map;
334 }
335}
Note: See TracBrowser for help on using the repository browser.