1 27 package org.htmlparser.lexer; 28 29 import org.htmlparser.Attribute; 30 import org.htmlparser.lexer.Page; 31 32 45 public class PageAttribute 46 extends 47 Attribute 48 { 49 52 protected Page mPage; 53 54 58 protected int mNameStart; 59 60 63 protected int mNameEnd; 64 65 69 protected int mValueStart; 70 71 74 protected int mValueEnd; 75 76 88 public PageAttribute (Page page, int name_start, int name_end, int value_start, int value_end, char quote) 89 { 90 mPage = page; 91 mNameStart = name_start; 92 mNameEnd = name_end; 93 mValueStart = value_start; 94 mValueEnd = value_end; 95 setName (null); 96 setAssignment (null); 97 setValue (null); 98 setQuote (quote); 99 } 100 101 105 private void init () 106 { 107 mPage = null; 108 mNameStart = -1; 109 mNameEnd = -1; 110 mValueStart = -1; 111 mValueEnd = -1; 112 } 113 114 123 public PageAttribute (String name, String assignment, String value, char quote) 124 { 125 super (name, assignment, value, quote); 126 init (); 127 } 128 129 138 public PageAttribute (String name, String value, char quote) 139 { 140 super (name, value, quote); 141 init (); 142 } 143 144 150 public PageAttribute (String value) 151 { 152 super (value); 153 init (); 154 } 155 156 164 public PageAttribute (String name, String value) 165 { 166 super (name, value); 167 init (); 168 } 169 170 177 public PageAttribute (String name, String assignment, String value) 178 { 179 super (name, assignment, value); 180 init (); 181 } 182 183 188 public PageAttribute () 189 { 190 super (); 191 init (); 192 } 193 194 201 public String getName () 202 { 203 String ret; 204 205 ret = super.getName (); 206 if (null == ret) 207 { 208 if ((null != mPage) && (0 <= mNameStart)) 209 { 210 ret = mPage.getText (mNameStart, mNameEnd); 211 setName (ret); } 213 } 214 215 return (ret); 216 } 217 218 223 public void getName (StringBuffer buffer) 224 { 225 String name; 226 227 name = super.getName (); 228 if (null == name) 229 { 230 if ((null != mPage) && (0 <= mNameStart)) 231 mPage.getText (buffer, mNameStart, mNameEnd); 232 } 233 else 234 buffer.append (name); 235 } 236 237 243 public String getAssignment () 244 { 245 String ret; 246 247 ret = super.getAssignment (); 248 if (null == ret) 249 { 250 if ((null != mPage) && (0 <= mNameEnd) && (0 <= mValueStart)) 251 { 252 ret = mPage.getText (mNameEnd, mValueStart); 253 if (ret.endsWith ("\"") || ret.endsWith ("'")) 256 ret = ret.substring (0, ret.length () - 1); 257 setAssignment (ret); } 259 } 260 261 return (ret); 262 } 263 264 269 public void getAssignment (StringBuffer buffer) 270 { 271 int length; 272 char ch; 273 String assignment; 274 275 assignment = super.getAssignment (); 276 if (null == assignment) 277 { 278 if ((null != mPage) && (0 <= mNameEnd) && (0 <= mValueStart)) 279 { 280 mPage.getText (buffer, mNameEnd, mValueStart); 281 length = buffer.length () - 1; 284 ch = buffer.charAt (length); 285 if (('\'' == ch) || ('"' == ch)) 286 buffer.setLength (length); 287 } 288 } 289 else 290 buffer.append (assignment); 291 } 292 293 303 public String getValue () 304 { 305 String ret; 306 307 ret = super.getValue (); 308 if (null == ret) 309 { 310 if ((null != mPage) && (0 <= mValueEnd)) 311 { 312 ret = mPage.getText (mValueStart, mValueEnd); 313 setValue (ret); } 315 } 316 317 return (ret); 318 } 319 320 325 public void getValue (StringBuffer buffer) 326 { 327 String value; 328 329 value = super.getValue (); 330 if (null == value) 331 { 332 if ((null != mPage) && (0 <= mValueEnd)) 333 mPage.getText (buffer, mNameStart, mNameEnd); 334 } 335 else 336 buffer.append (value); 337 } 338 339 346 public String getRawValue () 347 { 348 char quote; 349 StringBuffer buffer; 350 String ret; 351 352 ret = getValue (); 353 if (null != ret && (0 != (quote = getQuote ()))) 354 { 355 buffer = new StringBuffer (ret.length() + 2); 356 buffer.append (quote); 357 buffer.append (ret); 358 buffer.append (quote); 359 ret = buffer.toString (); 360 } 361 362 return (ret); 363 } 364 365 372 public void getRawValue (StringBuffer buffer) 373 { 374 char quote; 375 376 if (null == mValue) 377 { 378 if (0 <= mValueEnd) 379 { 380 if (0 != (quote = getQuote ())) 381 buffer.append (quote); 382 if (mValueStart != mValueEnd) 383 mPage.getText (buffer, mValueStart, mValueEnd); 384 if (0 != quote) 385 buffer.append (quote); 386 } 387 } 388 else 389 { 390 if (0 != (quote = getQuote ())) 391 buffer.append (quote); 392 buffer.append (mValue); 393 if (0 != quote) 394 buffer.append (quote); 395 } 396 } 397 398 403 public Page getPage () 404 { 405 return (mPage); 406 } 407 408 414 public void setPage (Page page) 415 { 416 mPage = page; 417 } 418 419 423 public int getNameStartPosition () 424 { 425 return (mNameStart); 426 } 427 428 432 public void setNameStartPosition (int start) 433 { 434 mNameStart = start; 435 setName (null); } 437 438 442 public int getNameEndPosition () 443 { 444 return (mNameEnd); 445 } 446 447 451 public void setNameEndPosition (int end) 452 { 453 mNameEnd = end; 454 setName (null); setAssignment (null); } 457 458 462 public int getValueStartPosition () 463 { 464 return (mValueStart); 465 } 466 467 471 public void setValueStartPosition (int start) 472 { 473 mValueStart = start; 474 setAssignment (null); setValue (null); } 477 478 482 public int getValueEndPosition () 483 { 484 return (mValueEnd); 485 } 486 487 491 public void setValueEndPosition (int end) 492 { 493 mValueEnd = end; 494 setValue (null); } 496 497 502 public boolean isWhitespace () 503 { 504 return (((null == super.getName ()) && (null == mPage)) 505 || ((null != mPage) && (0 > mNameStart))); 506 } 507 508 513 public boolean isStandAlone () 514 { 515 return (!isWhitespace () && (null == super.getAssignment ()) && !isValued () && ((null == mPage) || ((null != mPage) && (0 <= mNameEnd) && (0 > mValueStart)))); 521 } 522 523 528 public boolean isEmpty () 529 { 530 return (!isWhitespace () && !isStandAlone () && (null == super.getValue ()) && ((null == mPage) || ((null != mPage) && (0 > mValueEnd)))); 536 } 537 538 543 public boolean isValued () 544 { 545 return ((null != super.getValue ()) || ((null != mPage) && ((0 <= mValueStart) && (0 <= mValueEnd)) && (mValueStart != mValueEnd))); 548 } 549 550 554 public int getLength () 555 { 556 String name; 557 String assignment; 558 String value; 559 char quote; 560 int ret; 561 562 ret = 0; 563 name = super.getName (); 564 if (null != name) 565 ret += name.length (); 566 else if ((null != mPage) && (0 <= mNameStart) && (0 <= mNameEnd)) 567 ret += mNameEnd - mNameStart; 568 assignment = super.getAssignment (); 569 if (null != assignment) 570 ret += assignment.length (); 571 else if ((null != mPage) && (0 <= mNameEnd) && (0 <= mValueStart)) 572 ret += mValueStart - mNameEnd; 573 value = super.getValue (); 574 if (null != value) 575 ret += value.length (); 576 else if ((null != mPage) && (0 <= mValueStart) && (0 <= mValueEnd)) 577 ret += mValueEnd - mValueStart; 578 quote = getQuote (); 579 if (0 != quote) 580 ret += 2; 581 582 return (ret); 583 } 584 } 585 | Popular Tags |