1 28 29 package org.jibx.binding.def; 30 31 import java.util.ArrayList ; 32 import java.util.HashMap ; 33 34 import org.jibx.binding.classes.ClassFile; 35 import org.jibx.binding.classes.MethodBuilder; 36 import org.jibx.binding.util.ArrayMap; 37 38 import org.jibx.runtime.JiBXException; 39 40 47 48 public class DefinitionContext 49 { 50 51 private final IContainer m_container; 52 53 54 private final DefinitionContext m_context; 55 56 57 private NamespaceDefinition m_attributeDefault; 58 59 60 private NamespaceDefinition m_elementDefault; 61 62 63 private ArrayList m_namespaces; 64 65 66 private HashMap m_prefixMap; 67 68 69 private HashMap m_uriMap; 70 71 73 private ArrayMap m_classMap; 74 75 76 private ArrayList m_mappings; 77 78 79 private HashMap m_convertMap; 80 81 82 private HashMap m_formatMap; 83 84 85 private HashMap m_namedStructureMap; 86 87 93 94 public DefinitionContext(IContainer contain) { 95 m_container = contain; 96 m_context = contain.getDefinitionContext(); 97 m_convertMap = new HashMap (); 99 m_formatMap = new HashMap (); 100 if (m_context == null) { 101 m_namedStructureMap = new HashMap (); 102 } 103 } 104 105 113 114 public void addNamespace(NamespaceDefinition def) throws JiBXException { 115 116 if (m_namespaces == null) { 118 m_namespaces = new ArrayList (); 119 m_prefixMap = new HashMap (); 120 m_uriMap = new HashMap (); 121 } 122 123 if (def.isAttributeDefault()) { 125 if (m_attributeDefault == null) { 126 m_attributeDefault = def; 127 } else { 128 throw new JiBXException 129 ("Multiple default attribute namespaces at level"); 130 } 131 } 132 133 if (def.isElementDefault()) { 135 if (m_elementDefault == null) { 136 m_elementDefault = def; 137 } else { 138 throw new JiBXException 139 ("Multiple default element namespaces at level"); 140 } 141 } 142 143 String prefix = def.getPrefix(); 145 if (m_prefixMap.get(prefix) != null) { 146 throw new JiBXException("Namespace prefix conflict"); 147 } 148 149 String uri = def.getUri(); 151 Object prior = m_uriMap.get(uri); 152 if (prior != null && ((NamespaceDefinition)prior).getPrefix() != null) { 153 return; 154 } 155 156 def.setIndex(m_container.getBindingRoot(). 158 getNamespaceUriIndex(def.getUri())); 159 m_namespaces.add(def); 160 m_prefixMap.put(prefix, def); 161 m_uriMap.put(uri, def); 162 } 163 164 171 172 public void addMapping(IMapping def) throws JiBXException { 173 174 if (m_mappings == null) { 176 m_classMap = new ArrayMap(); 177 m_mappings = new ArrayList (); 178 } 179 180 String cname = def.getBoundType(); 182 int index = m_classMap.findOrAdd(cname); 183 if (index < m_mappings.size()) { 184 throw new JiBXException 185 ("Conflicting mappings for class " + cname); 186 } else { 187 m_mappings.add(def); 188 } 189 } 190 191 199 200 public void addNamedStructure(String name, IComponent comp) 201 throws JiBXException { 202 if (m_namedStructureMap == null) { 203 m_context.addNamedStructure(name, comp); 204 } else { 205 m_namedStructureMap.put(name, comp); 206 } 207 } 208 209 216 217 private NamespaceDefinition getDefaultNamespace(boolean attr) { 218 NamespaceDefinition ns; 219 if (attr) { 220 ns = m_attributeDefault; 221 } else { 222 ns = m_elementDefault; 223 } 224 if (ns == null && m_context != null) { 225 ns = m_context.getDefaultNamespace(attr); 226 } 227 return ns; 228 } 229 230 238 239 public String getDefaultURI(boolean attr) { 240 NamespaceDefinition ns = getDefaultNamespace(attr); 241 if (ns == null) { 242 return null; 243 } else { 244 return ns.getUri(); 245 } 246 } 247 248 256 257 public int getDefaultIndex(boolean attr) { 258 NamespaceDefinition ns = getDefaultNamespace(attr); 259 if (ns == null) { 260 return 0; 261 } else { 262 return ns.getIndex(); 263 } 264 } 265 266 276 277 public int getNamespaceIndex(String uri, boolean attr) 278 throws JiBXException { 279 280 if (m_uriMap != null) { 282 Object value = m_uriMap.get(uri); 283 if (value != null) { 284 if (!attr || ((NamespaceDefinition)value).getPrefix() != null) { 285 return ((NamespaceDefinition)value).getIndex(); 286 } 287 } 288 } 289 290 if (m_context == null) { 292 throw new JiBXException("Namespace URI \"" + uri + 293 "\" not defined or not usable"); 294 } else { 295 return m_context.getNamespaceIndex(uri, attr); 296 } 297 } 298 299 305 306 public IMapping getMappingAtLevel(String name) { 307 308 if (m_classMap != null) { 310 311 int index = m_classMap.find(name); 313 if (index >= 0) { 314 return (IMapping)m_mappings.get(index); 315 } 316 } 317 return null; 318 } 319 320 330 331 public IMapping getClassMapping(String name) { 332 333 IMapping def = getMappingAtLevel(name); 335 if (def == null && m_context != null) { 336 337 def = m_context.getClassMapping(name); 339 340 } 341 return def; 342 } 343 344 352 353 public IComponent getNamedStructure(String name) throws JiBXException { 354 355 IComponent comp = null; 357 if (m_namedStructureMap != null) { 358 comp = (IComponent)m_namedStructureMap.get(name); 359 } 360 if (comp == null) { 361 if (m_context == null) { 362 throw new JiBXException("Referenced label \"" + name + 363 "\" not defined"); 364 } else { 365 comp = m_context.getNamedStructure(name); 366 } 367 } 368 return comp; 369 } 370 371 376 377 public ArrayList getMappings() { 378 return m_mappings; 379 } 380 381 390 391 public StringConversion getSpecificConversion(String name) { 392 StringConversion conv = (StringConversion)m_convertMap.get(name); 393 if (conv == null && m_context != null) { 394 conv = m_context.getSpecificConversion(name); 395 } 396 return conv; 397 } 398 399 408 409 public StringConversion getConversion(ClassFile clas) { 410 411 StringConversion conv = getSpecificConversion(clas.getName()); 413 if (conv == null) { 414 return BindingDefinition.s_objectConversion; 415 } else { 416 return conv; 417 } 418 } 419 420 428 429 public StringConversion getNamedConversion(String name) { 430 StringConversion conv = (StringConversion)m_formatMap.get(name); 431 if (conv == null && m_context != null) { 432 conv = m_context.getNamedConversion(name); 433 } 434 return conv; 435 } 436 437 445 446 public void addConversion(String name, StringConversion conv) 447 throws JiBXException { 448 if (m_formatMap.put(name, conv) != null) { 449 throw new JiBXException("Duplicate conversion defined with name " + 450 name); 451 } 452 } 453 454 462 463 public void setConversion(StringConversion conv) 464 throws JiBXException { 465 if (m_convertMap.put(conv.getTypeName(), conv) != null) { 466 throw new JiBXException("Duplicate conversion defined for type " + 467 conv.getTypeName()); 468 } 469 } 470 471 478 479 public void setNamedConversion(String name, StringConversion conv) 480 throws JiBXException { 481 addConversion(name, conv); 482 } 483 484 492 493 public void setDefaultConversion(String name, StringConversion conv) 494 throws JiBXException { 495 addConversion(name, conv); 496 setConversion(conv); 497 } 498 499 505 506 public boolean hasNamespace() { 507 return m_namespaces != null && m_namespaces.size() > 0; 508 } 509 510 520 521 private void genFillNamespaceIndexes(ArrayList nss, MethodBuilder mb) 522 throws JiBXException { 523 if (nss != null) { 524 for (int i = 0; i < nss.size(); i++) { 525 mb.appendDUP(); 526 mb.appendLoadConstant(i); 527 mb.appendLoadConstant 528 (((NamespaceDefinition)nss.get(i)).getIndex()); 529 mb.appendIASTORE(); 530 } 531 } 532 } 533 534 544 545 private void genFillNamespacePrefixes(ArrayList nss, MethodBuilder mb) 546 throws JiBXException { 547 if (nss != null) { 548 for (int i = 0; i < nss.size(); i++) { 549 mb.appendDUP(); 550 mb.appendLoadConstant(i); 551 String prefix = ((NamespaceDefinition)nss.get(i)).getPrefix(); 552 if (prefix == null) { 553 prefix = ""; 554 } 555 mb.appendLoadConstant(prefix); 556 mb.appendAASTORE(); 557 } 558 } 559 } 560 561 568 569 public void genLoadNamespaces(MethodBuilder mb) throws JiBXException { 570 571 int count = m_namespaces == null ? 0 : m_namespaces.size(); 573 mb.appendLoadConstant(count); 574 mb.appendCreateArray("int"); 575 genFillNamespaceIndexes(m_namespaces, mb); 576 577 mb.appendLoadConstant(count); 579 mb.appendCreateArray("java.lang.String"); 580 genFillNamespacePrefixes(m_namespaces, mb); 581 } 582 583 591 592 public void generateCode(boolean verbose) throws JiBXException { 593 if (m_mappings != null) { 594 for (int i = 0; i < m_mappings.size(); i++) { 595 IMapping mapping = (IMapping)m_mappings.get(i); 596 if (verbose) { 597 System.out.println("Generating code for mapping " + 598 mapping.getBoundType()); 599 } 600 ((IMapping)m_mappings.get(i)).generateCode(); 601 } 602 } 603 } 604 605 612 public void linkMappings() throws JiBXException { 613 614 if (m_mappings != null) { 616 for (int i = 0; i < m_mappings.size(); i++) { 617 Object obj = m_mappings.get(i); 618 if (obj instanceof MappingDefinition) { 619 ((MappingDefinition)obj).linkMappings(); 620 } 621 } 622 } 623 } 624 625 635 636 public void setLinkages() throws JiBXException { 637 638 if (m_mappings != null) { 640 for (int i = 0; i < m_mappings.size(); i++) { 641 Object obj = m_mappings.get(i); 642 if (obj instanceof MappingDefinition) { 643 ((MappingDefinition)obj).setLinkages(); 644 } 645 } 646 } 647 } 648 649 public void print(int depth) { 651 BindingDefinition.indent(depth); 652 System.out.print("context"); 653 if (m_namespaces != null) { 654 System.out.print(" (ns#=" + m_namespaces.size() + ')'); 655 } 656 if (m_mappings != null) { 657 System.out.print(" (mp#=" + m_mappings.size() + ')'); 658 } 659 if (m_namedStructureMap != null) { 660 System.out.print(" (nm#=" + m_namedStructureMap.size() + ')'); 661 } 662 if (m_convertMap != null) { 663 System.out.print(" (cv#=" + m_convertMap.size() + ')'); 664 } 665 if (m_formatMap != null) { 666 System.out.print(" (fm#=" + m_formatMap.size() + ')'); 667 } 668 System.out.println(); 669 if (m_namespaces != null) { 670 for (int i = 0; i < m_namespaces.size(); i++) { 671 NamespaceDefinition ndef = 672 (NamespaceDefinition)m_namespaces.get(i); 673 ndef.print(depth+1); 674 } 675 } 676 if (m_mappings != null) { 677 for (int i = 0; i < m_mappings.size(); i++) { 678 Object obj = m_mappings.get(i); 679 if (obj instanceof MappingDefinition) { 680 MappingDefinition mdef = (MappingDefinition)obj; 681 mdef.print(depth+1); 682 } else if (obj instanceof MappingDirect) { 683 MappingDirect mdir = (MappingDirect)obj; 684 mdir.print(depth+1); 685 } else { 686 BindingDefinition.indent(depth+1); 687 System.out.println("unexpected type " + 688 obj.getClass().getName()); 689 } 690 } 691 } 692 } 693 } 694 | Popular Tags |