1 56 57 package org.jdom; 58 59 import java.io.*; 60 61 71 public class Attribute implements Serializable, Cloneable { 72 73 private static final String CVS_ID = 74 "@(#) $RCSfile: Attribute.java,v $ $Revision: 1.52 $ $Date: 2004/03/01 23:58:28 $ $Name: $"; 75 76 82 public final static int UNDECLARED_TYPE = 0; 83 84 89 public final static int CDATA_TYPE = 1; 90 91 96 public final static int ID_TYPE = 2; 97 98 104 public final static int IDREF_TYPE = 3; 105 106 112 public final static int IDREFS_TYPE = 4; 113 114 119 public final static int ENTITY_TYPE = 5; 120 121 128 public final static int ENTITIES_TYPE = 6; 129 130 140 public final static int NMTOKEN_TYPE = 7; 141 142 147 public final static int NMTOKENS_TYPE = 8; 148 149 154 public final static int NOTATION_TYPE = 9; 155 156 162 public final static int ENUMERATED_TYPE = 10; 163 164 166 167 168 169 protected String name; 170 171 172 protected transient Namespace namespace; 173 174 175 protected String value; 176 177 178 protected int type = UNDECLARED_TYPE; 179 180 181 protected Object parent; 182 183 186 protected Attribute() {} 187 188 203 public Attribute(String name, String value, Namespace namespace) { 204 setName(name); 205 setValue(value); 206 setNamespace(namespace); 207 } 208 209 227 public Attribute(String name, String value, int type, Namespace namespace) { 228 setName(name); 229 setValue(value); 230 setAttributeType(type); 231 setNamespace(namespace); 232 } 233 234 251 public Attribute(String name, String value) { 252 this(name, value, UNDECLARED_TYPE, Namespace.NO_NAMESPACE); 253 } 254 255 275 public Attribute(String name, String value, int type) { 276 this(name, value, type, Namespace.NO_NAMESPACE); 277 } 278 279 285 public Element getParent() { 286 return (Element) parent; 287 } 288 289 296 public Document getDocument() { 297 if (parent != null) { 298 return ((Element)parent).getDocument(); 299 } 300 return null; 301 } 302 303 309 protected Attribute setParent(Element parent) { 310 this.parent = parent; 311 return this; 312 } 313 314 320 public Attribute detach() { 321 Element p = getParent(); 322 if (p != null) { 323 p.removeAttribute(this.getName(), this.getNamespace()); 324 } 325 return this; 326 } 327 328 346 public String getName() { 347 return name; 348 } 349 350 358 public Attribute setName(String name) { 359 String reason; 360 if ((reason = Verifier.checkAttributeName(name)) != null) { 361 throw new IllegalNameException(name, "attribute", reason); 362 } 363 this.name = name; 364 return this; 365 } 366 367 386 public String getQualifiedName() { 387 String prefix = namespace.getPrefix(); 390 if ((prefix != null) && (!prefix.equals(""))) { 391 return new StringBuffer (prefix) 392 .append(':') 393 .append(getName()) 394 .toString(); 395 } else { 396 return getName(); 397 } 398 } 399 400 412 public String getNamespacePrefix() { 413 return namespace.getPrefix(); 414 } 415 416 423 public String getNamespaceURI() { 424 return namespace.getURI(); 425 } 426 427 433 public Namespace getNamespace() { 434 return namespace; 435 } 436 437 447 public Attribute setNamespace(Namespace namespace) { 448 if (namespace == null) { 449 namespace = Namespace.NO_NAMESPACE; 450 } 451 452 if (namespace != Namespace.NO_NAMESPACE && 455 namespace.getPrefix().equals("")) { 456 throw new IllegalNameException("", "attribute namespace", 457 "An attribute namespace without a prefix can only be the " + 458 "NO_NAMESPACE namespace"); 459 } 460 this.namespace = namespace; 461 return this; 462 } 463 470 public String getValue() { 471 return value; 472 } 473 474 483 public Attribute setValue(String value) { 484 String reason = null; 485 if ((reason = Verifier.checkCharacterData(value)) != null) { 486 throw new IllegalDataException(value, "attribute", reason); 487 } 488 this.value = value; 489 return this; 490 } 491 492 498 public int getAttributeType() { 499 return type; 500 } 501 502 510 public Attribute setAttributeType(int type) { 511 if ((type < UNDECLARED_TYPE) || (type > ENUMERATED_TYPE)) { 512 throw new IllegalDataException(String.valueOf(type), 513 "attribute", "Illegal attribute type"); 514 } 515 this.type = type; 516 return this; 517 } 518 519 526 public String toString() { 527 return new StringBuffer () 528 .append("[Attribute: ") 529 .append(getQualifiedName()) 530 .append("=\"") 531 .append(value) 532 .append("\"") 533 .append("]") 534 .toString(); 535 } 536 537 545 public final boolean equals(Object ob) { 546 return (ob == this); 547 } 548 549 554 public final int hashCode() { 555 return super.hashCode(); 556 } 557 558 563 public Object clone() { 564 Attribute attribute = null; 565 566 try { 567 attribute = (Attribute) super.clone(); 568 } catch(CloneNotSupportedException ce) { 569 } 571 572 575 attribute.parent = null; 577 return attribute; 578 } 579 580 584 593 public int getIntValue() throws DataConversionException { 594 try { 595 return Integer.parseInt(value.trim()); 596 } catch (NumberFormatException e) { 597 throw new DataConversionException(name, "int"); 598 } 599 } 600 601 610 public long getLongValue() throws DataConversionException { 611 try { 612 return Long.parseLong(value.trim()); 613 } catch (NumberFormatException e) { 614 throw new DataConversionException(name, "long"); 615 } 616 } 617 618 627 public float getFloatValue() throws DataConversionException { 628 try { 629 return Float.valueOf(value.trim()).floatValue(); 631 } catch (NumberFormatException e) { 632 throw new DataConversionException(name, "float"); 633 } 634 } 635 636 645 public double getDoubleValue() throws DataConversionException { 646 try { 647 return Double.valueOf(value.trim()).doubleValue(); 649 } catch (NumberFormatException e) { 650 throw new DataConversionException(name, "double"); 651 } 652 } 653 654 664 public boolean getBooleanValue() throws DataConversionException { 665 String valueTrim = value.trim(); 666 if ((valueTrim.equalsIgnoreCase("true")) || 667 (valueTrim.equalsIgnoreCase("on")) || 668 (valueTrim.equalsIgnoreCase("1")) || 669 (valueTrim.equalsIgnoreCase("yes"))) { 670 return true; 671 } else if ((valueTrim.equalsIgnoreCase("false")) || 672 (valueTrim.equalsIgnoreCase("off")) || 673 (valueTrim.equalsIgnoreCase("0")) || 674 (valueTrim.equalsIgnoreCase("no"))) { 675 return false; 676 } else { 677 throw new DataConversionException(name, "boolean"); 678 } 679 } 680 681 private void writeObject(ObjectOutputStream out) throws IOException { 684 685 out.defaultWriteObject(); 686 687 out.writeObject(namespace.getPrefix()); 690 out.writeObject(namespace.getURI()); 691 } 692 693 private void readObject(ObjectInputStream in) 694 throws IOException, ClassNotFoundException { 695 696 in.defaultReadObject(); 697 698 namespace = Namespace.getNamespace( 699 (String )in.readObject(), (String )in.readObject()); 700 } 701 } 702 | Popular Tags |