1 45 package org.jfree.xml; 46 47 import java.awt.BasicStroke ; 48 import java.awt.Color ; 49 import java.awt.Stroke ; 50 import java.awt.geom.Rectangle2D ; 51 import java.lang.reflect.Field ; 52 53 import org.xml.sax.Attributes ; 54 import org.xml.sax.SAXException ; 55 56 61 public class ParserUtil { 62 63 75 public static int parseInt(final String text, final String message) throws SAXException { 76 if (text == null) { 77 throw new SAXException (message); 78 } 79 80 try { 81 return Integer.parseInt(text); 82 } 83 catch (NumberFormatException nfe) { 84 throw new SAXException ("NumberFormatError: " + message); 85 } 86 } 87 88 96 public static int parseInt(final String text, final int defaultVal) { 97 if (text == null) { 98 return defaultVal; 99 } 100 101 try { 102 return Integer.parseInt(text); 103 } 104 catch (NumberFormatException nfe) { 105 return defaultVal; 106 } 107 } 108 109 121 public static float parseFloat(final String text, final String message) throws SAXException { 122 if (text == null) { 123 throw new SAXException (message); 124 } 125 try { 126 return Float.parseFloat(text); 127 } 128 catch (NumberFormatException nfe) { 129 throw new SAXException ("NumberFormatError: " + message); 130 } 131 } 132 133 143 public static float parseFloat(final String text, final float defaultVal) { 144 if (text == null) { 145 return defaultVal; 146 } 147 try { 148 return Float.parseFloat(text); 149 } 150 catch (NumberFormatException nfe) { 151 return defaultVal; 152 } 153 } 154 155 164 public static boolean parseBoolean(final String text, final boolean defaultVal) { 165 if (text == null) { 166 return defaultVal; 167 } 168 return text.equalsIgnoreCase("true"); 169 } 170 171 179 public static String parseString(final String text, final String defaultVal) { 180 if (text == null) { 181 return defaultVal; 182 } 183 return text; 184 } 185 186 195 public static Stroke parseStroke(final String weight) { 196 try { 197 if (weight != null) { 198 final Float w = new Float (weight); 199 return new BasicStroke (w.floatValue()); 200 } 201 } 202 catch (NumberFormatException nfe) { 203 } 205 return new BasicStroke (1); 206 } 207 208 219 public static Color parseColor(final String color) { 220 return parseColor(color, Color.black); 221 } 222 223 235 public static Color parseColor(final String color, final Color defaultValue) { 236 if (color == null) { 237 return defaultValue; 238 } 239 try { 240 return Color.decode(color); 242 } 243 catch (NumberFormatException nfe) { 244 try { 246 final Field f = Color .class.getField(color); 249 250 return (Color ) f.get(null); 251 } 252 catch (Exception ce) { 253 return defaultValue; 256 } 257 } 258 } 259 260 261 272 public static float parseRelativeFloat(final String value, final String exceptionMessage) 273 throws SAXException { 274 if (value == null) { 275 throw new SAXException (exceptionMessage); 276 } 277 final String tvalue = value.trim(); 278 if (tvalue.endsWith("%")) { 279 final String number = tvalue.substring(0, tvalue.indexOf("%")); 280 final float f = parseFloat(number, exceptionMessage) * -1.0f; 281 return f; 282 } 283 else { 284 return parseFloat(tvalue, exceptionMessage); 285 } 286 } 287 288 298 public static Rectangle2D getElementPosition(final Attributes atts) throws SAXException { 299 final float x = ParserUtil.parseRelativeFloat(atts.getValue("x"), 300 "Element x not specified"); 301 final float y = ParserUtil.parseRelativeFloat(atts.getValue("y"), 302 "Element y not specified"); 303 final float w = ParserUtil.parseRelativeFloat(atts.getValue("width"), 304 "Element width not specified"); 305 final float h = ParserUtil.parseRelativeFloat(atts.getValue("height"), 306 "Element height not specified"); 307 final Rectangle2D.Float retval = new Rectangle2D.Float (x, y, w, h); 308 return retval; 309 } 310 311 } 312
| Popular Tags
|