1 28 29 package org.jibx.binding.model; 30 31 import org.jibx.binding.util.StringArray; 32 import org.jibx.runtime.EnumSet; 33 import org.jibx.runtime.IUnmarshallingContext; 34 import org.jibx.runtime.JiBXException; 35 36 44 45 public class ValueElement extends ElementBase implements IComponent 46 { 47 48 public static final StringArray s_allowedAttributes = 49 new StringArray(new String [] { "constant", "format", "ident", "style" }, 50 new StringArray(new StringArray 51 (NameAttributes.s_allowedAttributes, 52 PropertyAttributes.s_allowedAttributes), 53 StringAttributes.s_allowedAttributes)); 54 55 58 public static final int CDATA_STYLE = 2; 59 public static final int TEXT_STYLE = 3; 60 61 private static final EnumSet s_styleEnum = 62 new EnumSet(NestingAttributes.s_styleEnum, CDATA_STYLE, 63 new String [] { "cdata", "text" }); 64 65 public static final int NONE_IDENT = 0; 66 public static final int DEF_IDENT = 1; 67 public static final int REF_IDENT = 2; 68 69 70 static final EnumSet s_identEnum = new EnumSet(NONE_IDENT, 71 new String [] { "none", "def", "ref" }); 72 73 76 77 private String m_constantValue; 78 79 80 private String m_styleName; 81 82 83 private int m_styleIndex; 84 85 86 private String m_identName; 87 88 89 private int m_identIndex; 90 91 92 private NameAttributes m_nameAttrs; 93 94 95 private PropertyAttributes m_propertyAttrs; 96 97 98 private StringAttributes m_stringAttrs; 99 100 103 public ValueElement() { 104 super(VALUE_ELEMENT); 105 m_nameAttrs = new NameAttributes(); 106 m_propertyAttrs = new PropertyAttributes(); 107 m_stringAttrs = new StringAttributes(); 108 } 109 110 115 public String getConstantValue() { 116 return m_constantValue; 117 } 118 119 124 public void setConstantValue(String value) { 125 m_constantValue = value; 126 } 127 128 133 public String getStyleName() { 134 return m_styleName; 135 } 136 137 142 public int getStyle() { 143 return m_styleIndex; 144 } 145 146 151 public void setStyleName(String name) { 152 m_styleName = name; 153 } 154 155 161 public String getEffectiveStyleName() { 162 return s_styleEnum.getName(m_styleIndex); 163 } 164 165 170 public String getIdentName() { 171 return m_identName; 172 } 173 174 179 public int getIdent() { 180 return m_identIndex; 181 } 182 183 188 public void setIdentName(String name) { 189 m_identName = name; 190 } 191 192 195 200 public String getName() { 201 return m_nameAttrs.getName(); 202 } 203 204 209 public void setName(String name) { 210 m_nameAttrs.setName(name); 211 } 212 213 218 public String getUri() { 219 return m_nameAttrs.getUri(); 220 } 221 222 227 public void setUri(String uri) { 228 m_nameAttrs.setUri(uri); 229 } 230 231 236 public String getPrefix() { 237 return m_nameAttrs.getPrefix(); 238 } 239 240 245 public void setPrefix(String prefix) { 246 m_nameAttrs.setPrefix(prefix); 247 } 248 249 255 public NamespaceElement getNamespace() { 256 return m_nameAttrs.getNamespace(); 257 } 258 259 262 267 public String getUsageName() { 268 return m_propertyAttrs.getUsageName(); 269 } 270 271 276 public int getUsage() { 277 return m_propertyAttrs.getUsage(); 278 } 279 280 285 public void setUsageName(String name) { 286 m_propertyAttrs.setUsageName(name); 287 } 288 289 294 public String getDeclaredType() { 295 return m_propertyAttrs.getDeclaredType(); 296 } 297 298 303 public void setDeclaredType(String type) { 304 m_propertyAttrs.setDeclaredType(type); 305 } 306 307 312 public String getFieldName() { 313 return m_propertyAttrs.getFieldName(); 314 } 315 316 321 public IClassItem getField() { 322 return m_propertyAttrs.getField(); 323 } 324 325 330 public void setFieldName(String field) { 331 m_propertyAttrs.setFieldName(field); 332 } 333 334 339 public String getTestName() { 340 return m_propertyAttrs.getTestName(); 341 } 342 343 349 public IClassItem getTest() { 350 return m_propertyAttrs.getTest(); 351 } 352 353 358 public void setTestName(String test) { 359 m_propertyAttrs.setTestName(test); 360 } 361 362 367 public String getGetName() { 368 return m_propertyAttrs.getGetName(); 369 } 370 371 377 public IClassItem getGet() { 378 return m_propertyAttrs.getGet(); 379 } 380 381 387 public IClass getGetType() { 388 return m_propertyAttrs.getGetType(); 389 } 390 391 396 public void setGetName(String get) { 397 m_propertyAttrs.setGetName(get); 398 } 399 400 405 public String getSetName() { 406 return m_propertyAttrs.getSetName(); 407 } 408 409 415 public IClassItem getSet() { 416 return m_propertyAttrs.getSet(); 417 } 418 419 425 public IClass getSetType() { 426 return m_propertyAttrs.getSetType(); 427 } 428 429 434 public void setSetName(String set) { 435 m_propertyAttrs.setSetName(set); 436 } 437 438 445 public boolean isImplicit() { 446 return m_propertyAttrs.isImplicit(); 447 } 448 449 452 457 public String getDefaultText() { 458 return m_stringAttrs.getDefaultText(); 459 } 460 461 466 public Object getDefault() { 467 return m_stringAttrs.getDefault(); 468 } 469 470 475 public void setDefaultText(String value) { 476 m_stringAttrs.setDefaultText(value); 477 } 478 479 484 public String getFormatName() { 485 return m_stringAttrs.getFormatName(); 486 } 487 488 493 public void setFormatName(String name) { 494 m_stringAttrs.setFormatName(name); 495 } 496 497 503 public String getSerializerName() { 504 return m_stringAttrs.getSerializerName(); 505 } 506 507 513 public IClassItem getSerializer() { 514 return m_stringAttrs.getSerializer(); 515 } 516 517 522 public void setSerializerName(String name) { 523 m_stringAttrs.setSerializerName(name); 524 } 525 526 532 public String getDeserializerName() { 533 return m_stringAttrs.getDeserializerName(); 534 } 535 536 542 public IClassItem getDeserializer() { 543 return m_stringAttrs.getDeserializer(); 544 } 545 546 551 public void setDeserializerName(String name) { 552 m_stringAttrs.setDeserializerName(name); 553 } 554 555 558 564 private void preSet(IUnmarshallingContext uctx) throws JiBXException { 565 validateAttributes(uctx, s_allowedAttributes); 566 } 567 568 571 public void prevalidate(ValidationContext vctx) { 572 573 if (m_styleName != null) { 575 m_styleIndex = s_styleEnum.getValue(m_styleName); 576 if (m_styleIndex < 0) { 577 vctx.addError("Value \"" + m_styleName + 578 "\" is not a valid choice for style"); 579 } 580 } else { 581 m_styleIndex = vctx.getParentElement().getDefaultStyle(); 582 } 583 584 m_identIndex = s_identEnum.getValue(m_identName); 586 if (m_identIndex < 0) { 587 vctx.addError("Value \"" + m_identName + 588 " is not a valid choice for ident"); 589 } 590 591 m_nameAttrs.setIsAttribute 593 (m_styleIndex == NestingAttributes.ATTRIBUTE_STYLE); 594 m_nameAttrs.prevalidate(vctx); 595 m_propertyAttrs.prevalidate(vctx); 596 if (m_styleIndex == CDATA_STYLE || m_styleIndex == TEXT_STYLE) { 597 if (m_nameAttrs.getName() != null) { 598 vctx.addFatal("Values with \"text\" or \"cdata\" style " + 599 "cannot have names"); 600 } 601 } else { 602 if (m_nameAttrs.getName() == null) { 603 vctx.addFatal 604 ("Missing required name for element or attribute value"); 605 } 606 } 607 608 if (m_constantValue == null) { 610 611 if (m_identIndex == DEF_IDENT) { 613 vctx.getContextObject().setIdChild(this, vctx); 614 } 615 616 if (!vctx.isSkipped(this)) { 618 619 if (m_identIndex == REF_IDENT) { 621 if (m_propertyAttrs.isImplicit()) { 622 vctx.addFatal 623 ("No property value - ID reference can only " + 624 "be used with a property of the appropriate type"); 625 } 626 if (m_stringAttrs.getDeserializerName() != null || 627 m_stringAttrs.getSerializerName() != null || 628 m_stringAttrs.getFormatName() != null || 629 m_stringAttrs.getDefaultText() != null) { 630 vctx.addWarning("String attributes serializer, " + 631 "deserializer, format, and default are " + 632 "prohibited with ID references"); 633 } 634 } else { 635 m_stringAttrs.setType(m_propertyAttrs.getType()); 636 m_stringAttrs.prevalidate(vctx); 637 } 638 super.prevalidate(vctx); 639 } 640 641 } else { 642 643 if (m_identIndex != NONE_IDENT) { 645 vctx.addFatal("ident value must be \"none\" for constant"); 646 } else if (m_propertyAttrs.hasProperty() || 647 m_propertyAttrs.getDeclaredType() != null) { 648 vctx.addFatal 649 ("Property attributes cannot be used with constant"); 650 } else if (m_stringAttrs.getDefaultText() != null || 651 m_stringAttrs.getDeserializerName() != null || 652 m_stringAttrs.getSerializerName() != null || 653 m_stringAttrs.getFormatName() != null) { 654 vctx.addFatal("String attributes cannot be used with constant"); 655 } 656 } 657 } 658 659 662 public void validate(ValidationContext vctx) { 663 664 m_nameAttrs.validate(vctx); 666 m_propertyAttrs.validate(vctx); 667 if (!vctx.isSkipped(this)) { 668 669 if (m_identIndex == REF_IDENT) { 671 String type = m_propertyAttrs.getType().getName(); 672 if (!vctx.getBindingRoot().isIdClass(type)) { 673 vctx.addError("No ID definitions for compatible type"); 674 } 675 } 676 super.validate(vctx); 677 } 678 } 679 680 683 686 public boolean hasAttribute() { 687 return m_styleIndex == NestingAttributes.ATTRIBUTE_STYLE; 688 } 689 690 693 public boolean hasContent() { 694 return m_styleIndex != NestingAttributes.ATTRIBUTE_STYLE; 695 } 696 697 700 public boolean isOptional() { 701 return m_propertyAttrs.getUsage() == PropertyAttributes.OPTIONAL_USAGE; 702 } 703 704 707 public boolean hasName() { 708 return m_nameAttrs.getName() != null; 709 } 710 711 714 public boolean hasType() { 715 return m_propertyAttrs.getType() != null; 716 } 717 718 721 public IClass getType() { 722 return m_propertyAttrs.getType(); 723 } 724 } | Popular Tags |