1 18 package org.apache.batik.dom.svg; 19 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 23 import org.apache.batik.css.dom.CSSOMSVGColor; 24 import org.apache.batik.css.dom.CSSOMSVGPaint; 25 import org.apache.batik.css.dom.CSSOMSVGStyleDeclaration; 26 import org.apache.batik.css.dom.CSSOMValue; 27 import org.apache.batik.css.engine.CSSEngine; 28 import org.apache.batik.css.engine.CSSStylableElement; 29 import org.apache.batik.css.engine.SVGCSSEngine; 30 import org.apache.batik.css.engine.StyleMap; 31 import org.apache.batik.css.engine.value.Value; 32 import org.apache.batik.css.engine.value.svg.SVGColorManager; 33 import org.apache.batik.css.engine.value.svg.SVGPaintManager; 34 import org.apache.batik.dom.AbstractDocument; 35 import org.w3c.dom.Attr ; 36 import org.w3c.dom.DOMException ; 37 import org.w3c.dom.Node ; 38 import org.w3c.dom.css.CSSStyleDeclaration; 39 import org.w3c.dom.css.CSSValue; 40 import org.w3c.dom.svg.SVGAnimatedString; 41 42 49 public abstract class SVGStylableElement 50 extends SVGOMElement 51 implements CSSStylableElement { 52 53 56 protected StyleMap computedStyleMap; 57 58 61 protected SVGStylableElement() { 62 } 63 64 69 protected SVGStylableElement(String prefix, AbstractDocument owner) { 70 super(prefix, owner); 71 } 72 73 75 78 public StyleMap getComputedStyleMap(String pseudoElement) { 79 return computedStyleMap; 80 } 81 82 85 public void setComputedStyleMap(String pseudoElement, StyleMap sm) { 86 computedStyleMap = sm; 87 } 88 89 92 public String getXMLId() { 93 return getAttributeNS(null, "id"); 94 } 95 96 99 public String getCSSClass() { 100 return getAttributeNS(null, "class"); 101 } 102 103 106 public URL getCSSBase() { 107 try { 108 String bu = XMLBaseSupport.getCascadedXMLBase(this); 109 if (bu == null) { 110 return null; 111 } 112 return new URL (bu); 113 } catch (MalformedURLException e) { 114 e.printStackTrace(); 116 throw new InternalError (); 117 } 118 } 119 120 124 public boolean isPseudoInstanceOf(String pseudoClass) { 125 if (pseudoClass.equals("first-child")) { 126 Node n = getPreviousSibling(); 127 while (n != null && n.getNodeType() != ELEMENT_NODE) { 128 n = n.getPreviousSibling(); 129 } 130 return n == null; 131 } 132 return false; 133 } 134 135 137 140 public CSSStyleDeclaration getStyle() { 141 CSSStyleDeclaration result = 142 (CSSStyleDeclaration)getLiveAttributeValue(null, 143 SVG_STYLE_ATTRIBUTE); 144 if (result == null) { 145 CSSEngine eng = ((SVGOMDocument)getOwnerDocument()).getCSSEngine(); 146 result = new StyleDeclaration(eng); 147 } 148 return result; 149 } 150 151 155 public CSSValue getPresentationAttribute(String name) { 156 CSSValue result = (CSSValue)getLiveAttributeValue(null, name); 157 if (result != null) 158 return result; 159 160 CSSEngine eng = ((SVGOMDocument)getOwnerDocument()).getCSSEngine(); 161 int idx = eng.getPropertyIndex(name); 162 if (idx == -1) 163 return null; 164 165 if (idx > SVGCSSEngine.FINAL_INDEX) { 166 if (eng.getValueManagers()[idx] instanceof SVGPaintManager) { 167 result = new PresentationAttributePaintValue(eng, name); 168 } 169 if (eng.getValueManagers()[idx] instanceof SVGColorManager) { 170 result = new PresentationAttributeColorValue(eng, name); 171 } 172 } else { 173 switch (idx) { 174 case SVGCSSEngine.FILL_INDEX: 175 case SVGCSSEngine.STROKE_INDEX: 176 result = new PresentationAttributePaintValue(eng, name); 177 break; 178 179 case SVGCSSEngine.FLOOD_COLOR_INDEX: 180 case SVGCSSEngine.LIGHTING_COLOR_INDEX: 181 case SVGCSSEngine.STOP_COLOR_INDEX: 182 result = new PresentationAttributeColorValue(eng, name); 183 break; 184 185 default: 186 result = new PresentationAttributeValue(eng, name); 187 } 188 } 189 putLiveAttributeValue(null, name, (LiveAttributeValue)result); 190 return result; 191 } 192 193 197 public SVGAnimatedString getClassName() { 198 return getAnimatedStringAttribute(null, SVG_CLASS_ATTRIBUTE); 199 } 200 201 204 public class PresentationAttributeValue 205 extends CSSOMValue 206 implements LiveAttributeValue, 207 CSSOMValue.ValueProvider { 208 209 212 protected CSSEngine cssEngine; 213 214 217 protected String property; 218 219 222 protected Value value; 223 224 227 protected boolean mutate; 228 229 232 public PresentationAttributeValue(CSSEngine eng, String prop) { 233 super(null); 234 valueProvider = this; 235 setModificationHandler(new AbstractModificationHandler() { 236 protected Value getValue() { 237 return PresentationAttributeValue.this.getValue(); 238 } 239 public void textChanged(String text) throws DOMException { 240 value = cssEngine.parsePropertyValue 241 (SVGStylableElement.this, property, text); 242 mutate = true; 243 setAttributeNS(null, property, text); 244 mutate = false; 245 } 246 }); 247 248 cssEngine = eng; 249 property = prop; 250 251 Attr attr = getAttributeNodeNS(null, prop); 252 if (attr != null) { 253 value = cssEngine.parsePropertyValue 254 (SVGStylableElement.this, prop, attr.getValue()); 255 } 256 } 257 258 260 263 public Value getValue() { 264 if (value == null) { 265 throw new DOMException (DOMException.INVALID_STATE_ERR, ""); 266 } 267 return value; 268 } 269 270 272 275 public void attrAdded(Attr node, String newv) { 276 if (!mutate) { 277 value = cssEngine.parsePropertyValue 278 (SVGStylableElement.this, property, newv); 279 } 280 } 281 282 285 public void attrModified(Attr node, String oldv, String newv) { 286 if (!mutate) { 287 value = cssEngine.parsePropertyValue 288 (SVGStylableElement.this, property, newv); 289 } 290 } 291 292 295 public void attrRemoved(Attr node, String oldv) { 296 if (!mutate) { 297 value = null; 298 } 299 } 300 } 301 302 305 public class PresentationAttributeColorValue 306 extends CSSOMSVGColor 307 implements LiveAttributeValue, 308 CSSOMSVGColor.ValueProvider { 309 310 313 protected CSSEngine cssEngine; 314 315 318 protected String property; 319 320 323 protected Value value; 324 325 328 protected boolean mutate; 329 330 333 public PresentationAttributeColorValue(CSSEngine eng, String prop) { 334 super(null); 335 valueProvider = this; 336 setModificationHandler(new AbstractModificationHandler() { 337 protected Value getValue() { 338 return PresentationAttributeColorValue.this.getValue(); 339 } 340 public void textChanged(String text) throws DOMException { 341 value = cssEngine.parsePropertyValue 342 (SVGStylableElement.this, property, text); 343 mutate = true; 344 setAttributeNS(null, property, text); 345 mutate = false; 346 } 347 }); 348 349 cssEngine = eng; 350 property = prop; 351 352 Attr attr = getAttributeNodeNS(null, prop); 353 if (attr != null) { 354 value = cssEngine.parsePropertyValue 355 (SVGStylableElement.this, prop, attr.getValue()); 356 } 357 } 358 359 361 364 public Value getValue() { 365 if (value == null) { 366 throw new DOMException (DOMException.INVALID_STATE_ERR, ""); 367 } 368 return value; 369 } 370 371 373 376 public void attrAdded(Attr node, String newv) { 377 if (!mutate) { 378 value = cssEngine.parsePropertyValue 379 (SVGStylableElement.this, property, newv); 380 } 381 } 382 383 386 public void attrModified(Attr node, String oldv, String newv) { 387 if (!mutate) { 388 value = cssEngine.parsePropertyValue 389 (SVGStylableElement.this, property, newv); 390 } 391 } 392 393 396 public void attrRemoved(Attr node, String oldv) { 397 if (!mutate) { 398 value = null; 399 } 400 } 401 } 402 403 406 public class PresentationAttributePaintValue 407 extends CSSOMSVGPaint 408 implements LiveAttributeValue, 409 CSSOMSVGPaint.ValueProvider { 410 411 414 protected CSSEngine cssEngine; 415 416 419 protected String property; 420 421 424 protected Value value; 425 426 429 protected boolean mutate; 430 431 434 public PresentationAttributePaintValue(CSSEngine eng, String prop) { 435 super(null); 436 valueProvider = this; 437 setModificationHandler(new AbstractModificationHandler() { 438 protected Value getValue() { 439 return PresentationAttributePaintValue.this.getValue(); 440 } 441 public void textChanged(String text) throws DOMException { 442 value = cssEngine.parsePropertyValue 443 (SVGStylableElement.this, property, text); 444 mutate = true; 445 setAttributeNS(null, property, text); 446 mutate = false; 447 } 448 }); 449 450 451 cssEngine = eng; 452 property = prop; 453 454 Attr attr = getAttributeNodeNS(null, prop); 455 if (attr != null) { 456 value = cssEngine.parsePropertyValue 457 (SVGStylableElement.this, prop, attr.getValue()); 458 } 459 } 460 461 463 466 public Value getValue() { 467 if (value == null) { 468 throw new DOMException (DOMException.INVALID_STATE_ERR, ""); 469 } 470 return value; 471 } 472 473 475 478 public void attrAdded(Attr node, String newv) { 479 if (!mutate) { 480 value = cssEngine.parsePropertyValue 481 (SVGStylableElement.this, property, newv); 482 } 483 } 484 485 488 public void attrModified(Attr node, String oldv, String newv) { 489 if (!mutate) { 490 value = cssEngine.parsePropertyValue 491 (SVGStylableElement.this, property, newv); 492 } 493 } 494 495 498 public void attrRemoved(Attr node, String oldv) { 499 if (!mutate) { 500 value = null; 501 } 502 } 503 } 504 505 508 public class StyleDeclaration 509 extends CSSOMSVGStyleDeclaration 510 implements LiveAttributeValue, 511 CSSOMSVGStyleDeclaration.ValueProvider, 512 CSSOMSVGStyleDeclaration.ModificationHandler, 513 CSSEngine.MainPropertyReceiver { 514 515 518 protected org.apache.batik.css.engine.StyleDeclaration declaration; 519 520 523 protected boolean mutate; 524 525 528 public StyleDeclaration(CSSEngine eng) { 529 super(null, null, eng); 530 valueProvider = this; 531 setModificationHandler(this); 532 533 declaration = cssEngine.parseStyleDeclaration 534 (SVGStylableElement.this, 535 getAttributeNS(null, SVG_STYLE_ATTRIBUTE)); 536 } 537 538 540 543 public Value getValue(String name) { 544 int idx = cssEngine.getPropertyIndex(name); 545 for (int i = 0; i < declaration.size(); i++) { 546 if (idx == declaration.getIndex(i)) { 547 return declaration.getValue(i); 548 } 549 } 550 return null; 551 } 552 553 556 public boolean isImportant(String name) { 557 int idx = cssEngine.getPropertyIndex(name); 558 for (int i = 0; i < declaration.size(); i++) { 559 if (idx == declaration.getIndex(i)) { 560 return declaration.getPriority(i); 561 } 562 } 563 return false; 564 } 565 566 569 public String getText() { 570 return declaration.toString(cssEngine); 571 } 572 573 576 public int getLength() { 577 return declaration.size(); 578 } 579 580 583 public String item(int idx) { 584 return cssEngine.getPropertyName(declaration.getIndex(idx)); 585 } 586 587 589 592 public void attrAdded(Attr node, String newv) { 593 if (!mutate) { 594 declaration = cssEngine.parseStyleDeclaration 595 (SVGStylableElement.this, newv); 596 } 597 } 598 599 602 public void attrModified(Attr node, String oldv, String newv) { 603 if (!mutate) { 604 declaration = cssEngine.parseStyleDeclaration 605 (SVGStylableElement.this, newv); 606 } 607 } 608 609 612 public void attrRemoved(Attr node, String oldv) { 613 if (!mutate) { 614 declaration = 615 new org.apache.batik.css.engine.StyleDeclaration(); 616 } 617 } 618 619 621 624 public void textChanged(String text) throws DOMException { 625 declaration = cssEngine.parseStyleDeclaration 626 (SVGStylableElement.this, text); 627 mutate = true; 628 setAttributeNS(null, SVG_STYLE_ATTRIBUTE, text); 629 mutate = false; 630 } 631 632 635 public void propertyRemoved(String name) throws DOMException { 636 int idx = cssEngine.getPropertyIndex(name); 637 for (int i = 0; i < declaration.size(); i++) { 638 if (idx == declaration.getIndex(i)) { 639 declaration.remove(i); 640 mutate = true; 641 setAttributeNS(null, SVG_STYLE_ATTRIBUTE, 642 declaration.toString(cssEngine)); 643 mutate = false; 644 return; 645 } 646 } 647 } 648 649 public void setMainProperty(String name, Value v, boolean important) { 650 int idx = cssEngine.getPropertyIndex(name); 651 if (idx == -1) 652 return; 654 int i=0; 655 for (; i < declaration.size(); i++) { 656 if (idx == declaration.getIndex(i)) 657 break; 658 } 659 if (i < declaration.size()) 660 declaration.put(i, v, idx, important); 661 else 662 declaration.append(v, idx, important); 663 } 664 665 668 public void propertyChanged(String name, String value, String prio) 669 throws DOMException { 670 boolean important = ((prio != null) && (prio.length() >0)); 671 cssEngine.setMainProperties(SVGStylableElement.this, 672 this, name, value, important); 673 mutate = true; 674 setAttributeNS(null, SVG_STYLE_ATTRIBUTE, 675 declaration.toString(cssEngine)); 676 mutate = false; 677 } 678 } 679 } 680 | Popular Tags |