source: josm/trunk/src/com/kitfox/svg/xml/StyleAttribute.java@ 17333

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

see #20129 - Fix typos and misspellings in the code (patch by gaben)

  • Property svn:eol-style set to native
File size: 8.9 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 January 27, 2004, 2:53 PM
35 */
36
37package com.kitfox.svg.xml;
38
39import com.kitfox.svg.SVGConst;
40import java.awt.*;
41import java.io.*;
42import java.net.*;
43import java.util.logging.Level;
44import java.util.logging.Logger;
45import java.util.regex.*;
46
47/**
48 * @author Mark McKay
49 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
50 */
51public class StyleAttribute implements Serializable
52{
53 public static final long serialVersionUID = 0;
54
55 static final Pattern patternUrl = Pattern.compile("\\s*url\\((.*)\\)\\s*");
56 static final Matcher matchFpNumUnits = Pattern.compile("\\s*([-+]?((\\d*\\.\\d+)|(\\d+))([-+]?[eE]\\d+)?)\\s*(px|cm|mm|in|pc|pt|em|ex)\\s*").matcher("");
57
58 String name;
59 String stringValue;
60
61 boolean colorCompatible = false;
62 boolean urlCompatible = false;
63
64 /** Creates a new instance of StyleAttribute */
65 public StyleAttribute()
66 {
67 this(null, null);
68 }
69
70 public StyleAttribute(String name)
71 {
72 this(name, null);
73 }
74
75 public StyleAttribute(String name, String stringValue)
76 {
77 setName(name);
78 setStringValue(stringValue);
79 }
80
81 public String getName() {
82 return name;
83 }
84
85 public StyleAttribute setName(String name)
86 {
87 this.name = name == null ? null : name.intern();
88 return this;
89 }
90
91 public String getStringValue()
92 {
93 return stringValue;
94 }
95
96 public String[] getStringList()
97 {
98 return XMLParseUtil.parseStringList(stringValue);
99 }
100
101 public void setStringValue(String value)
102 {
103 stringValue = value == null ? null : value.intern();
104 }
105
106 public boolean getBooleanValue() {
107 return stringValue.toLowerCase().equals("true");
108 }
109
110 public int getIntValue() {
111 return XMLParseUtil.findInt(stringValue);
112 }
113
114 public int[] getIntList() {
115 return XMLParseUtil.parseIntList(stringValue);
116 }
117
118 public double getDoubleValue() {
119 return XMLParseUtil.findDouble(stringValue);
120 }
121
122 public double[] getDoubleList() {
123 return XMLParseUtil.parseDoubleList(stringValue);
124 }
125
126 public float getFloatValue() {
127 return XMLParseUtil.findFloat(stringValue);
128 }
129
130 public float[] getFloatList() {
131 return XMLParseUtil.parseFloatList(stringValue);
132 }
133
134 public float getRatioValue() {
135 return (float)XMLParseUtil.parseRatio(stringValue);
136// try { return Float.parseFloat(stringValue); }
137// catch (Exception e) {}
138// return 0f;
139 }
140
141 public String getUnits() {
142 matchFpNumUnits.reset(stringValue);
143 if (!matchFpNumUnits.matches()) return null;
144 return matchFpNumUnits.group(6);
145 }
146
147 public NumberWithUnits getNumberWithUnits() {
148 return XMLParseUtil.parseNumberWithUnits(stringValue);
149 }
150
151 public float getFloatValueWithUnits()
152 {
153 NumberWithUnits number = getNumberWithUnits();
154 return convertUnitsToPixels(number.getUnits(), number.getValue());
155 }
156
157 static public float convertUnitsToPixels(int unitType, float value)
158 {
159 if (unitType == NumberWithUnits.UT_UNITLESS || unitType == NumberWithUnits.UT_PERCENT)
160 {
161 return value;
162 }
163
164 float pixPerInch;
165 try
166 {
167 pixPerInch = (float)Toolkit.getDefaultToolkit().getScreenResolution();
168 }
169 catch (HeadlessException ex)
170 {
171 //default to 72 dpi
172 pixPerInch = 72;
173 }
174 final float inchesPerCm = .3936f;
175
176 switch (unitType)
177 {
178 case NumberWithUnits.UT_IN:
179 return value * pixPerInch;
180 case NumberWithUnits.UT_CM:
181 return value * inchesPerCm * pixPerInch;
182 case NumberWithUnits.UT_MM:
183 return value * .1f * inchesPerCm * pixPerInch;
184 case NumberWithUnits.UT_PT:
185 return value * (1f / 72f) * pixPerInch;
186 case NumberWithUnits.UT_PC:
187 return value * (1f / 6f) * pixPerInch;
188 }
189
190 return value;
191 }
192
193 public Color getColorValue()
194 {
195 return ColorTable.parseColor(stringValue);
196 }
197
198 public String parseURLFn()
199 {
200 Matcher matchUrl = patternUrl.matcher(stringValue);
201 if (!matchUrl.matches())
202 {
203 return null;
204 }
205 return matchUrl.group(1);
206 }
207
208 public URL getURLValue(URL docRoot)
209 {
210 String fragment = parseURLFn();
211 if (fragment == null) return null;
212 try {
213 return new URL(docRoot, fragment);
214 }
215 catch (Exception e)
216 {
217 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, null, e);
218 return null;
219 }
220 }
221
222 public URL getURLValue(URI docRoot)
223 {
224 String fragment = parseURLFn();
225 if (fragment == null) return null;
226 try {
227 URI ref = docRoot.resolve(fragment);
228 return ref.toURL();
229 }
230 catch (Exception e)
231 {
232 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, null, e);
233 return null;
234 }
235 }
236
237 public URI getURIValue()
238 {
239 return getURIValue(null);
240 }
241
242 /**
243 * Parse this style attribute as a URL and return it in URI form resolved
244 * against the passed base.
245 *
246 * @param base - URI to resolve against. If null, will return value without
247 * attempting to resolve it.
248 */
249 public URI getURIValue(URI base)
250 {
251 try {
252 String fragment = parseURLFn();
253 if (fragment == null) fragment = stringValue.replaceAll("\\s+", "");
254 if (fragment == null) return null;
255
256 //======================
257 //This gets around a bug in the 1.5.0 JDK
258 if (Pattern.matches("[a-zA-Z]:!\\\\.*", fragment))
259 {
260 File file = new File(fragment);
261 return file.toURI();
262 }
263 //======================
264
265 //[scheme:]scheme-specific-part[#fragment]
266
267 URI uriFrag = new URI(fragment);
268 if (uriFrag.isAbsolute())
269 {
270 //Has scheme
271 return uriFrag;
272 }
273
274 if (base == null) return uriFrag;
275
276 URI relBase = new URI(null, base.getSchemeSpecificPart(), null);
277 URI relUri;
278 if (relBase.isOpaque())
279 {
280 relUri = new URI(null, base.getSchemeSpecificPart(), uriFrag.getFragment());
281 }
282 else
283 {
284 relUri = relBase.resolve(uriFrag);
285 }
286 return new URI(base.getScheme() + ":" + relUri);
287 }
288 catch (Exception e)
289 {
290 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, null, e);
291 return null;
292 }
293 }
294
295 public static void main(String[] args)
296 {
297 try
298 {
299 URI uri = new URI("jar:http://www.kitfox.com/jackal/jackal.jar!/res/doc/about.svg");
300 uri = uri.resolve("#myFragment");
301
302 System.err.println(uri.toString());
303
304 uri = new URI("http://www.kitfox.com/jackal/jackal.html");
305 uri = uri.resolve("#myFragment");
306
307 System.err.println(uri.toString());
308 }
309 catch (Exception e)
310 {
311 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, null, e);
312 }
313 }
314}
Note: See TracBrowser for help on using the repository browser.