1 27 package org.htmlparser; 28 29 import java.io.Serializable ; 30 31 192 public class Attribute 193 implements 194 Serializable 195 { 196 201 protected String mName; 202 203 209 protected String mAssignment; 210 211 217 protected String mValue; 218 219 223 protected char mQuote; 224 225 234 public Attribute (String name, String assignment, String value, char quote) 235 { 236 setName (name); 237 setAssignment (assignment); 238 if (0 == quote) 239 setRawValue (value); 240 else 241 { 242 setValue (value); 243 setQuote (quote); 244 } 245 } 246 247 256 public Attribute (String name, String value, char quote) 257 { 258 this (name, (null == value ? "" : "="), value, quote); 259 } 260 261 267 public Attribute (String value) 268 { 269 if (0 != value.trim ().length ()) 270 throw new IllegalArgumentException ("non whitespace value"); 271 else 272 { 273 setName (null); 274 setAssignment (null); 275 setValue (value); 276 setQuote ((char)0); 277 } 278 } 279 280 288 public Attribute (String name, String value) 289 { 290 this (name, (null == value ? "" : "="), value, (char)0); 291 } 292 293 300 public Attribute (String name, String assignment, String value) 301 { 302 this (name, assignment, value, (char)0); 303 } 304 305 310 public Attribute () 311 { 312 this (null, null, null, (char)0); 313 } 314 315 322 public String getName () 323 { 324 return (mName); 325 } 326 327 332 public void getName (StringBuffer buffer) 333 { 334 if (null != mName) 335 buffer.append (mName); 336 } 337 338 346 public void setName (String name) 347 { 348 mName = name; 349 } 350 351 357 public String getAssignment () 358 { 359 return (mAssignment); 360 } 361 362 367 public void getAssignment (StringBuffer buffer) 368 { 369 if (null != mAssignment) 370 buffer.append (mAssignment); 371 } 372 373 381 public void setAssignment (String assignment) 382 { 383 mAssignment = assignment; 384 } 385 386 396 public String getValue () 397 { 398 return (mValue); 399 } 400 401 406 public void getValue (StringBuffer buffer) 407 { 408 if (null != mValue) 409 buffer.append (mValue); 410 } 411 412 421 public void setValue (String value) 422 { 423 mValue = value; 424 } 425 426 431 public char getQuote () 432 { 433 return (mQuote); 434 } 435 436 441 public void getQuote (StringBuffer buffer) 442 { 443 if (0 != mQuote) 444 buffer.append (mQuote); 445 } 446 447 454 public void setQuote (char quote) 455 { 456 mQuote = quote; 457 } 458 459 466 public String getRawValue () 467 { 468 char quote; 469 StringBuffer buffer; 470 String ret; 471 472 if (isValued ()) 473 { 474 quote = getQuote (); 475 if (0 != quote) 476 { 477 buffer = new StringBuffer (); buffer.append (quote); 479 getValue (buffer); 480 buffer.append (quote); 481 ret = buffer.toString (); 482 } 483 else 484 ret = getValue (); 485 } 486 else 487 ret = null; 488 489 return (ret); 490 } 491 492 499 public void getRawValue (StringBuffer buffer) 500 { 501 getQuote (buffer); 502 getValue (buffer); 503 getQuote (buffer); 504 } 505 506 517 public void setRawValue (String value) 518 { 519 char ch; 520 boolean needed; 521 boolean singleq; 522 boolean doubleq; 523 String ref; 524 StringBuffer buffer; 525 char quote; 526 527 quote = 0; 528 if ((null != value) && (0 != value.trim ().length ())) 529 { 530 if (value.startsWith ("'") && value.endsWith ("'") && (2 <= value.length ())) 531 { 532 quote = '\''; 533 value = value.substring (1, value.length () - 1); 534 } 535 else if (value.startsWith ("\"") && value.endsWith ("\"") && (2 <= value.length ())) 536 { 537 quote = '"'; 538 value = value.substring (1, value.length () - 1); 539 } 540 else 541 { 542 needed = false; 545 singleq = true; 546 doubleq = true; 547 for (int i = 0; i < value.length (); i++) 548 { 549 ch = value.charAt (i); 550 if ('\'' == ch) 551 { 552 singleq = false; 553 needed = true; 554 } 555 else if ('"' == ch) 556 { 557 doubleq = false; 558 needed = true; 559 } 560 else if (!('-' == ch) && !('.' == ch) && !('_' == ch) 561 && !(':' == ch) && !Character.isLetterOrDigit (ch)) 562 { 563 needed = true; 564 } 565 } 566 567 if (needed) 569 { 570 if (doubleq) 571 quote = '"'; 572 else if (singleq) 573 quote = '\''; 574 else 575 { 576 quote = '"'; 579 ref = """; buffer = new StringBuffer (value.length() * 5); 582 for (int i = 0; i < value.length (); i++) 583 { 584 ch = value.charAt (i); 585 if (quote == ch) 586 buffer.append (ref); 587 else 588 buffer.append (ch); 589 } 590 value = buffer.toString (); 591 } 592 } 593 } 594 } 595 setValue (value); 596 setQuote (quote); 597 } 598 599 604 public boolean isWhitespace () 605 { 606 return (null == getName ()); 607 } 608 609 614 public boolean isStandAlone () 615 { 616 return ((null != getName ()) && (null == getAssignment ())); 617 } 618 619 624 public boolean isEmpty () 625 { 626 return ((null != getAssignment ()) && (null == getValue ())); 627 } 628 629 634 public boolean isValued () 635 { 636 return (null != getValue ()); 637 } 638 639 643 public int getLength () 644 { 645 String name; 646 String assignment; 647 String value; 648 char quote; 649 int ret; 650 651 ret = 0; 652 name = getName (); 653 if (null != name) 654 ret += name.length (); 655 assignment = getAssignment (); 656 if (null != assignment) 657 ret += assignment.length (); 658 value = getValue (); 659 if (null != value) 660 ret += value.length (); 661 quote = getQuote (); 662 if (0 != quote) 663 ret += 2; 664 665 return (ret); 666 } 667 668 684 public String toString () 685 { 686 int length; 687 StringBuffer ret; 688 689 length = getLength (); 691 ret = new StringBuffer (length); 692 toString (ret); 693 694 return (ret.toString ()); 695 } 696 697 702 public void toString (StringBuffer buffer) 703 { 704 getName (buffer); 705 getAssignment (buffer); 706 getRawValue (buffer); 707 } 708 709 } 710 | Popular Tags |