1 18 package org.apache.batik.dom.svg; 19 20 import org.apache.batik.parser.LengthListHandler; 21 import org.apache.batik.parser.LengthListParser; 22 import org.apache.batik.parser.ParseException; 23 import org.w3c.dom.DOMException ; 24 import org.w3c.dom.Element ; 25 import org.w3c.dom.svg.SVGException; 26 import org.w3c.dom.svg.SVGLength; 27 import org.w3c.dom.svg.SVGLengthList; 28 29 30 37 public abstract class AbstractSVGLengthList 38 extends AbstractSVGList 39 implements SVGLengthList { 40 41 42 45 protected short direction; 46 47 50 public final static String SVG_LENGTH_LIST_SEPARATOR 51 =" "; 52 53 56 protected String getItemSeparator(){ 57 return SVG_LENGTH_LIST_SEPARATOR; 58 } 59 60 65 protected abstract SVGException createSVGException(short type, 66 String key, 67 Object [] args); 68 69 72 protected abstract Element getElement(); 73 74 77 protected AbstractSVGLengthList(short direction) { 78 super(); 79 this.direction = direction; 80 } 81 82 84 public SVGLength initialize ( SVGLength newItem ) 85 throws DOMException , SVGException { 86 87 return (SVGLength)initializeImpl(newItem); 88 } 89 90 92 public SVGLength getItem ( int index ) 93 throws DOMException { 94 95 return (SVGLength)getItemImpl(index); 96 } 97 98 100 public SVGLength insertItemBefore ( SVGLength newItem, int index ) 101 throws DOMException , SVGException { 102 103 return (SVGLength)insertItemBeforeImpl(newItem,index); 104 } 105 106 108 public SVGLength replaceItem ( SVGLength newItem, int index ) 109 throws DOMException , SVGException { 110 111 return (SVGLength)replaceItemImpl(newItem,index); 112 } 113 114 116 public SVGLength removeItem ( int index ) 117 throws DOMException { 118 119 return (SVGLength)removeItemImpl(index); 120 } 121 122 124 public SVGLength appendItem ( SVGLength newItem ) 125 throws DOMException , SVGException { 126 127 return (SVGLength) appendItemImpl(newItem); 128 } 129 130 132 protected SVGItem createSVGItem(Object newItem){ 133 134 SVGLength l = (SVGLength)newItem; 135 136 return new SVGLengthItem(l.getUnitType(), l.getValueInSpecifiedUnits(),direction); 137 } 138 139 145 protected void doParse(String value, ListHandler handler) 146 throws ParseException{ 147 148 LengthListParser lengthListParser = new LengthListParser(); 149 150 LengthListBuilder builder = new LengthListBuilder(handler); 151 152 lengthListParser.setLengthListHandler(builder); 153 lengthListParser.parse(value); 154 155 } 156 157 160 protected void checkItemType(Object newItem) 161 throws SVGException { 162 if ( !( newItem instanceof SVGLength ) ){ 163 createSVGException(SVGException.SVG_WRONG_TYPE_ERR, 164 "expected SVGLength", 165 null); 166 } 167 } 168 169 172 protected class SVGLengthItem 173 extends AbstractSVGLength 174 implements SVGItem { 175 176 179 public SVGLengthItem(short type, float value,short direction){ 180 super(direction); 181 this.unitType = type; 182 this.value = value; 183 } 184 185 187 protected SVGOMElement getAssociatedElement(){ 188 return (SVGOMElement)AbstractSVGLengthList.this.getElement(); 189 } 190 191 194 protected AbstractSVGList parentList; 195 196 201 public void setParent(AbstractSVGList list){ 202 parentList = list; 203 } 204 205 211 public AbstractSVGList getParent(){ 212 return parentList; 213 } 214 215 219 protected void reset(){ 220 if ( parentList != null ){ 221 parentList.itemChanged(); 222 } 223 } 224 225 } 226 227 231 protected class LengthListBuilder 232 implements LengthListHandler { 233 234 237 protected ListHandler listHandler; 238 239 protected float currentValue; 241 protected short currentType; 243 244 246 public LengthListBuilder(ListHandler listHandler){ 247 this.listHandler = listHandler; 248 } 249 250 252 public void startLengthList() 253 throws ParseException{ 254 255 listHandler.startList(); 256 } 257 260 public void startLength() throws ParseException { 261 currentType = SVGLength.SVG_LENGTHTYPE_NUMBER; 262 currentValue = 0.0f; 263 } 264 265 268 public void lengthValue(float v) throws ParseException { 269 currentValue = v; 270 } 271 272 275 public void em() throws ParseException { 276 currentType = SVGLength.SVG_LENGTHTYPE_EMS; 277 } 278 279 282 public void ex() throws ParseException { 283 currentType = SVGLength.SVG_LENGTHTYPE_EXS; 284 } 285 286 289 public void in() throws ParseException { 290 currentType = SVGLength.SVG_LENGTHTYPE_IN; 291 } 292 293 296 public void cm() throws ParseException { 297 currentType = SVGLength.SVG_LENGTHTYPE_CM; 298 } 299 300 303 public void mm() throws ParseException { 304 currentType = SVGLength.SVG_LENGTHTYPE_MM; 305 } 306 307 310 public void pc() throws ParseException { 311 currentType = SVGLength.SVG_LENGTHTYPE_PC; 312 } 313 314 317 public void pt() throws ParseException { 318 currentType = SVGLength.SVG_LENGTHTYPE_EMS; 319 } 320 321 324 public void px() throws ParseException { 325 currentType = SVGLength.SVG_LENGTHTYPE_PX; 326 } 327 328 331 public void percentage() throws ParseException { 332 currentType = SVGLength.SVG_LENGTHTYPE_PERCENTAGE; 333 } 334 335 338 public void endLength() throws ParseException { 339 listHandler.item(new SVGLengthItem(currentType,currentValue,direction)); 340 } 341 342 344 public void endLengthList() 345 throws ParseException { 346 listHandler.endList(); 347 } 348 } 349 350 } 351 | Popular Tags |