1 19 20 package org.netbeans.modules.xml.axi.impl; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import java.util.SortedMap ; 30 import java.util.TreeMap ; 31 import java.util.Map.Entry; 32 import org.netbeans.modules.xml.axi.datatype.CustomDatatype; 33 import org.netbeans.modules.xml.schema.model.*; 34 import org.netbeans.modules.xml.xam.dom.NamedComponentReference; 35 import org.netbeans.modules.xml.axi.*; 36 import org.netbeans.modules.xml.axi.AXIDocument; 37 import org.netbeans.modules.xml.axi.AXIModel; 38 import org.netbeans.modules.xml.axi.AXIModelFactory; 39 import org.netbeans.modules.xml.axi.AXIType; 40 import org.netbeans.modules.xml.axi.AbstractAttribute; 41 import org.netbeans.modules.xml.axi.AbstractElement; 42 import org.netbeans.modules.xml.axi.Attribute; 43 import org.netbeans.modules.xml.axi.Compositor; 44 import org.netbeans.modules.xml.axi.ContentModel; 45 import org.netbeans.modules.xml.axi.Element; 46 import org.netbeans.modules.xml.axi.SchemaGenerator; 47 import org.netbeans.modules.xml.axi.SchemaGenerator.PrimitiveCart; 48 import org.netbeans.modules.xml.axi.SchemaGenerator.UniqueId; 49 import org.netbeans.modules.xml.axi.datatype.Datatype; 50 import org.netbeans.modules.xml.axi.visitor.AXINonCyclicVisitor; 51 52 56 public abstract class DefaultSchemaGenerator extends SchemaGenerator { 57 58 protected AXIModel am; 59 60 protected SchemaModel sm; 61 62 protected SchemaGenerator.UniqueId id; 63 64 protected SchemaGenerator.PrimitiveCart pc; 65 66 java.util.List <AXIComponent> path = new ArrayList <AXIComponent>(); 67 68 protected SchemaComponent sc; 69 70 protected SchemaComponent scParent; 71 72 protected SchemaComponent datatypeParent; 73 74 protected SortedMap <Integer , java.util.List <Object >> fixNamesMap = null; 75 76 protected java.util.List <SchemaComponent> createGlobals = null; 77 78 protected HashMap <SchemaComponent, SchemaComponent> refMap = null; 79 80 protected HashMap <Class , HashMap <String , SchemaComponent>> namesMap = null; 81 82 protected java.util.List <Element> elementReuseList = null; 83 84 protected SchemaGeneratorHelper sgh; 85 86 private int fgeCount; 87 88 public final static int commitRange = Integer.getInteger("schematools.axi.adp", 0); 89 90 93 public DefaultSchemaGenerator(SchemaGenerator.Mode mode) { 94 super(mode); 95 id = createUniqueId(); 96 pc = createPrimitiveCart(); 97 fixNamesMap = new TreeMap <Integer , java.util.List <Object >>(); 98 createGlobals = new ArrayList <SchemaComponent>(); 99 refMap = new HashMap <SchemaComponent, SchemaComponent>(); 100 namesMap = new HashMap <Class , HashMap <String , SchemaComponent>>(); 101 elementReuseList = new ArrayList <Element>(); 102 fgeCount = 0; 103 } 104 105 public void updateSchema(SchemaModel sm) throws IOException { 106 assert getMode() == SchemaGenerator.Mode.UPDATE; 107 sgh = new UpdateHelper(); 108 sgh.execute(sm); 109 } 110 111 public void transformSchema(SchemaModel sm) throws IOException { 112 assert getMode() == SchemaGenerator.Mode.TRANSFORM; 113 sgh = new TransformHelper(); 114 sgh.execute(sm); 115 } 116 117 public void visit(Element element) { 118 if(getMode() != SchemaGenerator.Mode.TRANSFORM && element instanceof ElementRef) 119 prepareElementRef((ElementRef)element); 120 else 121 prepareLocalElement(element); 122 } 123 124 public void visit(Attribute attribute) { 125 if(getMode() != SchemaGenerator.Mode.TRANSFORM && attribute instanceof AttributeRef) 126 prepareAttributeRef((AttributeRef)attribute); 127 else 128 prepareLocalAttribute(attribute); 129 } 130 131 public void visit(AXIType type) { 132 if(type instanceof Datatype) 133 ((Datatype)type).accept(this); 134 } 135 136 public void visit(Datatype d) { 137 SchemaGeneratorUtil.createInlineSimpleType(d, sm, this.datatypeParent, pc); 138 } 139 140 public void visit(ContentModel cm) { 141 if(scParent instanceof Schema) { 142 GlobalComplexType gct = SchemaGeneratorUtil.createGlobalComplexType(sm); 143 assert gct != null; 144 gct.setName(cm.getName()); 145 SchemaGeneratorUtil.populateContentModel(gct, cm); 146 if(getMode() != SchemaGenerator.Mode.TRANSFORM) { 147 cm.setPeer(gct); 148 int index = cm.getIndex(false); 149 SchemaGeneratorUtil.addChildComponent(sm, sm.getSchema(), gct, index); 150 } else 151 scParent = gct; 152 scParent = gct; 153 for(AXIComponent child: cm.getChildren()) { 154 child.accept(this); 155 } 156 } 157 } 158 159 public void visit(Compositor compositor) { 160 int index = -1; 161 if(getMode() != SchemaGenerator.Mode.TRANSFORM) 162 index = compositor.getIndex(false); 163 switch(compositor.getType()) { 164 case SEQUENCE: { 165 Sequence seq = null; 166 if(scParent instanceof ComplexType) { 167 if(scParent instanceof LocalComplexType) { 168 LocalComplexType lct = (LocalComplexType) scParent; 169 if(lct.getDefinition() != null) { 170 ComplexTypeDefinition ctd = lct.getDefinition(); 171 if(ctd instanceof SimpleContent) { 172 transformToComplexContent(ctd); 173 seq = SchemaGeneratorUtil.createSequence(sm, lct); 174 } else 175 seq = SchemaGeneratorUtil.createSequence(sm, ctd, index); 176 } else 177 seq = SchemaGeneratorUtil.createSequence(sm, 178 (LocalComplexType) scParent); 179 } else if(scParent instanceof GlobalComplexType) { 180 GlobalComplexType gct = (GlobalComplexType) scParent; 181 if(gct.getDefinition() != null) { 182 ComplexTypeDefinition ctd = gct.getDefinition(); 183 if(ctd instanceof SimpleContent) { 184 transformToComplexContent(ctd); 185 seq = SchemaGeneratorUtil.createSequence(sm, gct); 186 } else 187 seq = SchemaGeneratorUtil.createSequence(sm, ctd, index); 188 } else 189 seq = SchemaGeneratorUtil.createSequence(sm, 190 (GlobalComplexType) scParent); 191 } 192 } else if(scParent instanceof ComplexContentDefinition) { 193 ComplexContentDefinition ccd = (ComplexContentDefinition) scParent; 194 if(ccd instanceof ComplexContentRestriction && 195 ((ComplexContentRestriction)ccd).getDefinition() != null) 196 seq = SchemaGeneratorUtil.createSequence(sm, 197 ((ComplexContentRestriction)ccd).getDefinition(), index); 198 else 203 seq = SchemaGeneratorUtil.createSequence(sm, 204 (ComplexContentDefinition) scParent); 205 } else if(scParent instanceof Sequence) { 206 seq = SchemaGeneratorUtil.createSequence(sm, (Sequence)scParent, index); 207 } else if(scParent instanceof Choice) { 208 seq = SchemaGeneratorUtil.createSequence(sm, (Choice)scParent, index); 209 } 210 SchemaGeneratorUtil.populateCompositor(seq, compositor); 211 if(getMode() != SchemaGenerator.Mode.TRANSFORM) 212 compositor.setPeer(seq); 213 else 214 scParent = seq; 215 } 216 break; 217 218 case CHOICE: { 219 Choice c = null; 220 if(scParent instanceof LocalComplexType) { 221 LocalComplexType lct = (LocalComplexType) scParent; 222 if(lct.getDefinition() != null) 223 c = SchemaGeneratorUtil.createChoice(sm, lct.getDefinition(), index); 224 else 225 c = SchemaGeneratorUtil.createChoice(sm, 226 (LocalComplexType) scParent); 227 } 228 if(scParent instanceof GlobalComplexType) { 229 GlobalComplexType gct = (GlobalComplexType) scParent; 230 if(gct.getDefinition() != null) { 231 ComplexTypeDefinition ctd = gct.getDefinition(); 232 if(ctd instanceof SimpleContent) { 233 transformToComplexContent(ctd); 234 c = SchemaGeneratorUtil.createChoice(sm, gct); 235 } else 236 c = SchemaGeneratorUtil.createChoice(sm, ctd, index); 237 } 238 else 239 c = SchemaGeneratorUtil.createChoice(sm, 240 (GlobalComplexType) scParent); 241 } else if(scParent instanceof ComplexContentDefinition) { 242 ComplexContentDefinition ccd = (ComplexContentDefinition) scParent; 243 if(ccd instanceof ComplexContentRestriction && 244 ((ComplexContentRestriction)ccd).getDefinition() != null) 245 c = SchemaGeneratorUtil.createChoice(sm, 246 ((ComplexContentRestriction)ccd).getDefinition(), index); 247 else 252 c = SchemaGeneratorUtil.createChoice(sm, 253 (ComplexContentDefinition) scParent); 254 } else if(scParent instanceof Choice) { 255 c = SchemaGeneratorUtil.createChoice(sm, (Choice)scParent, index); 256 } else if(scParent instanceof Sequence) { 257 c = SchemaGeneratorUtil.createChoice(sm, (Sequence)scParent, index); 258 } 259 SchemaGeneratorUtil.populateCompositor(c, compositor); 260 if(getMode() != SchemaGenerator.Mode.TRANSFORM) 261 compositor.setPeer(c); 262 else 263 scParent = c; 264 } 265 break; 266 267 case ALL: { 268 All a = null; 269 if(scParent instanceof ComplexType) { 270 a = SchemaGeneratorUtil.createAll(sm, (ComplexType) scParent); 271 } else if(scParent instanceof ComplexContentDefinition) { 272 ComplexContentDefinition ccd = (ComplexContentDefinition) scParent; 273 if(ccd instanceof ComplexContentRestriction && 274 ((ComplexContentRestriction)ccd).getDefinition() != null) 275 a = (All) 276 ((ComplexContentRestriction)ccd).getDefinition(); 277 else 282 a = SchemaGeneratorUtil.createAll(sm, 283 (ComplexContentDefinition) scParent); 284 } 285 SchemaGeneratorUtil.populateCompositor(a, compositor); 286 if(getMode() != SchemaGenerator.Mode.TRANSFORM) 287 compositor.setPeer(a); 288 else 289 scParent = a; 290 } 291 break; 292 default: assert false; 293 } 294 } 295 296 private ComplexType transformToComplexContent(final ComplexTypeDefinition ctd) { 297 ComplexType lct = null; 298 if(ctd instanceof SimpleContent) { 299 lct = (ComplexType) ctd.getParent(); 300 if(ctd.getChildren().get(0) instanceof SimpleExtension) { 301 NamedComponentReference base = ((SimpleExtension)ctd.getChildren().get(0)).getBase(); 302 if(base.get() instanceof GlobalSimpleType) { 303 SimpleContent sc1 = (SimpleContent) ctd; 304 copyAttribute(lct, sc1); 305 copyAttribute(lct, sc1.getLocalDefinition()); 306 } 307 } 308 } 309 return lct; 310 } 311 312 private void copyAttribute(final LocalAttributeContainer lac, 313 final SchemaComponent scd) { 314 for(SchemaComponent sc: scd.getChildren()) { 315 if(sc instanceof org.netbeans.modules.xml.schema.model.Attribute) 316 lac.addLocalAttribute( 317 (LocalAttribute) ((org.netbeans.modules.xml.schema.model.Attribute)sc). 318 copy(lac)); 319 } 320 } 321 322 public void visit(AXIComponent c) { 323 throw new IllegalArgumentException ("No action taken on this component: "+ 324 c.toString()); 325 } 326 327 protected abstract SchemaGenerator.Pattern getSchemaDesignPattern(); 328 329 333 protected void prepareGlobalElement(Element element) { 334 GlobalElement e = null; 335 ElementReference eref = null; 336 int index = -1; 337 if(getMode() != SchemaGenerator.Mode.TRANSFORM) 338 index = element.getIndex(false); 339 if(scParent instanceof Schema) { 340 e = createGlobalElement(element); 341 sgh.addElement(e, index); 342 prepareFixGlobalElementNames(element, (GlobalElement) e, null); 343 } else if(scParent instanceof ComplexTypeDefinition) { 344 String seed = element.getName(); 345 346 boolean found = false; 347 if(SchemaGeneratorUtil.isSimpleElement(element) || 348 SchemaGeneratorUtil.hasProxyChild(element)) { 349 HashMap <String , SchemaComponent> map = 350 namesMap.get(GlobalElement.class); 351 if(map != null && map.get(seed) != null) { 352 GlobalElement ge1 = (GlobalElement) map.get(seed); 353 354 GlobalElement ge2 = SchemaGeneratorUtil.createGlobalElement(sm); 355 ge2.setName(element.getName()); 356 SchemaGeneratorUtil.populateElement(ge2, element); 357 this.datatypeParent = ge2; 358 if(element.getType() instanceof Datatype) 359 element.getType().accept(this); 360 else if(element.getType() instanceof ContentModel) 361 SchemaGeneratorUtil.setType(ge2, 362 (GlobalComplexType) ((ContentModel)element. 363 getType()).getPeer()); 364 365 if(SchemaGeneratorUtil.isIdentical(ge1, ge2)) { 366 found = true; 367 e = ge1; 368 if(!elementReuseList.contains(element)) 369 elementReuseList.add(element); 370 } 371 } 372 } 373 if(!found) { 374 e = createGlobalElement(element); 375 sgh.addElement(e, -1); 376 } 377 eref = SchemaGeneratorUtil.createElementReference(sm, scParent, e, index); 378 addRef(eref, e); 379 prepareFixGlobalElementNames(element, e, eref); 380 } 381 assert e != null; 382 sc = e; 383 384 SchemaGeneratorUtil.populateElement(e, element); 386 387 if(eref != null) 388 SchemaGeneratorUtil.populateElement(eref, element); 389 390 this.datatypeParent = e; 391 if(element.getType() instanceof Datatype) 392 element.getType().accept(this); 393 394 setPeer(element, e, eref); 395 } 396 397 protected void prepareElementRef(ElementRef element) { 398 int index = element.getIndex(); 399 org.netbeans.modules.xml.schema.model.ElementReference eRef = 400 SchemaGeneratorUtil.createElementReference(sm, (ComplexTypeDefinition) scParent, 401 (GlobalElement)element.getReferent().getPeer(), index); 402 setPeer(element, null, eRef); 403 } 404 405 protected void prepareLocalElement(Element element) { 406 org.netbeans.modules.xml.schema.model.Element e = null; 407 int index = -1; 408 if(getMode() != SchemaGenerator.Mode.TRANSFORM) 409 index = element.getIndex(false); 410 if(scParent instanceof Schema) { 411 e = createGlobalElement(element); 412 sgh.addElement((GlobalElement) e, index); 413 prepareFixGlobalElementNames(element, (GlobalElement) e, null); 414 } else if(scParent instanceof ComplexTypeDefinition) { 415 e = SchemaGeneratorUtil.createLocalElement(sm, 416 (ComplexTypeDefinition) scParent, element.getName(), index); 417 } 418 assert e != null; 419 sc = e; 420 421 SchemaGeneratorUtil.populateElement(e, element); 423 424 this.datatypeParent = e; 425 if(element.getType() instanceof Datatype) 426 element.getType().accept(this); 427 setPeer(element, e, null); 428 } 429 430 protected void prepareAttributeRef(AttributeRef attribute) { 431 int index = attribute.getIndex(); 432 org.netbeans.modules.xml.schema.model.AttributeReference aRef = 433 SchemaGeneratorUtil.createAttributeReference(sm, scParent, 434 (GlobalAttribute)attribute.getReferent().getPeer(), index); 435 setPeer(attribute, null, aRef); 436 } 437 438 protected void prepareLocalAttribute(Attribute attribute) { 439 assert scParent != null; 440 int index = -1; 441 if(getMode() != SchemaGenerator.Mode.TRANSFORM) 442 index = attribute.getIndex(); 443 LocalAttribute attr = null; 444 if(scParent instanceof ComplexType && 445 ((ComplexType)scParent).getDefinition() instanceof ComplexTypeDefinition) { 446 if(((ComplexType)scParent).getDefinition() instanceof SimpleContent) { 447 SimpleContentDefinition def = ((SimpleContent)((ComplexType)scParent). 448 getDefinition()).getLocalDefinition(); 449 if(def instanceof SimpleContentRestriction) 450 attr = SchemaGeneratorUtil.createLocalAttribute(sm, attribute.getName(), 451 (SimpleContentRestriction)def, index); 452 else if(def instanceof Extension) 453 attr = SchemaGeneratorUtil.createLocalAttribute(sm, attribute.getName(), 454 (Extension)def, index); 455 } 456 else if(((ComplexType)scParent).getDefinition() instanceof ComplexContent) { 457 ComplexContentDefinition def = ((ComplexContent)((ComplexType)scParent). 458 getDefinition()).getLocalDefinition(); 459 if(def instanceof ComplexContentRestriction) 460 attr = SchemaGeneratorUtil.createLocalAttribute(sm, attribute.getName(), 461 (ComplexContentRestriction)def, index); 462 else if(def instanceof Extension) 463 attr = SchemaGeneratorUtil.createLocalAttribute(sm, attribute.getName(), 464 (Extension)def, index); 465 } 466 else { 467 attr = SchemaGeneratorUtil.createLocalAttribute(sm, attribute.getName(), 468 scParent, index); 469 } 470 } else 471 attr = SchemaGeneratorUtil.createLocalAttribute(sm, attribute.getName(), 472 scParent, index); 473 assert attr != null; 474 475 SchemaGeneratorUtil.populateAttribute(attr, attribute); 477 478 this.datatypeParent = attr; 479 if(attribute.getType() instanceof Datatype) 480 attribute.getType().accept(this); 481 482 if(getMode() != SchemaGenerator.Mode.TRANSFORM) 483 attribute.setPeer(attr); 484 } 485 486 protected GlobalElement createGlobalElement(final Element element) { 487 GlobalElement ge = SchemaGeneratorUtil.createGlobalElement(sm); 488 String eName = findUniqueGlobalName( 489 GlobalElement.class, ge, element.getName()); 490 ge.setName(eName); 491 return ge; 492 } 493 494 protected GlobalComplexType createGlobalComplexType(final String seed) { 495 GlobalComplexType gct; 496 gct = (GlobalComplexType) 497 SchemaGeneratorUtil.createGlobalComplexType(sm); 498 String typeName = findUniqueGlobalName( 499 GlobalComplexType.class, gct, seed); 500 gct.setName(typeName); 501 return gct; 502 } 503 504 public void createGlobalSimpleType( 505 final Datatype d, final SchemaModel sm, final SchemaComponent sc, 506 final SchemaGenerator.UniqueId id, SchemaGenerator.PrimitiveCart pc) { 507 if(d != null) { 508 NamedComponentReference<GlobalSimpleType> ref =null; 509 if(SchemaGeneratorUtil.isPrimitiveType(d)) { 510 ref = SchemaGeneratorUtil.createPrimitiveType(d, sc, pc); 511 } else { 512 GlobalSimpleType gst; 513 gst = SchemaGeneratorUtil.createGlobalSimpleType(sm); 514 String typeName = d.getName(); 515 typeName = findUniqueGlobalName( 516 GlobalSimpleType.class, gst, 517 "New"+typeName.substring(0, 1).toUpperCase()+ 518 typeName.substring(1)+"Type"+String.valueOf(id.nextId())); 519 gst.setName(typeName); 520 sgh.addSimpleType(gst, -1); 521 if(d instanceof CustomDatatype) 522 SchemaGeneratorUtil.populateSimpleType( 523 ((CustomDatatype)d).getBase(), sm, gst, pc); 524 else 525 SchemaGeneratorUtil.populateSimpleType(d, sm, gst, pc); 526 ref = sc.createReferenceTo(gst, GlobalSimpleType.class); 527 } 528 SchemaGeneratorUtil.setSimpleType(sc, ref); 529 } 530 } 531 532 protected GlobalType createPeerGlobalComplexType(Element element) { 533 org.netbeans.modules.xml.schema.model.Element e = 534 (org.netbeans.modules.xml.schema.model.Element) element.getPeer(); 535 if(e instanceof ElementReference) 536 e = (GlobalElement) getRef(e); 537 GlobalComplexType gct = createGlobalComplexType(element.getName()+"Type"); 538 assert gct != null; 539 sgh.addComplexType(gct, -1); 540 SchemaGeneratorUtil.setType(e, gct); 541 return gct; 542 } 543 544 protected void prepareFixGlobalElementNames(final Element element, final GlobalElement e, 545 final ElementReference eref) { 546 java.util.List <Object > scs = new ArrayList <Object >(); 547 scs.add(element); 548 scs.add(e); 549 scs.add(eref); 550 fixNamesMap.put(new Integer (fgeCount++), scs); 551 } 552 553 protected void fixGlobalElementNames() { 554 namesMap.clear(); 556 557 HashMap <GlobalElement, java.util.List <ElementReference>> erefMap = new 559 HashMap <GlobalElement, java.util.List <ElementReference>>(); 560 for (Entry<Integer ,java.util.List <Object >> e : fixNamesMap.entrySet()) { 561 java.util.List <Object > scs = e.getValue(); 562 if(scs != null && scs.size() > 1) { 563 GlobalElement ge = (GlobalElement) scs.get(1); 564 ElementReference eref = (ElementReference) scs.get(2); 565 java.util.List <ElementReference> erefs = erefMap.get(ge); 566 if(erefs == null) { 567 erefs = new ArrayList <ElementReference>(); 568 erefMap.put(ge, erefs); 569 } 570 if(eref != null && !erefs.contains(eref)) 571 erefs.add(eref); 572 } 573 } 574 575 int count = 0; 576 Iterator it = erefMap.keySet().iterator(); 577 while(it.hasNext()) { 578 if(commitRange > 0 && (count++)%commitRange==0) { 579 sm.endTransaction(); 580 sm.startTransaction(); 581 } 582 GlobalElement ge = (GlobalElement) it.next(); 583 java.util.List <ElementReference> erefs = erefMap.get(ge); 584 String name = findUniqueGlobalName( 585 GlobalElement.class, ge, ge.getName()); 586 ge.setName(name); 587 588 for(ElementReference eref:erefs) 589 eref.setRef(eref.createReferenceTo(ge, GlobalElement.class)); 590 } 591 592 erefMap.clear(); 593 erefMap = null; 594 fixNamesMap.clear(); 595 } 596 597 <T extends NameableSchemaComponent>String 598 findUniqueGlobalName(Class <T> type, NameableSchemaComponent c, 599 final String seed) { 600 return sgh.findUniqueGlobalName(type, c, seed); 601 } 602 603 protected SchemaComponent getParent(final AXIComponent axiparent) 604 throws IllegalArgumentException { 605 return sgh.getParent(axiparent); 606 } 607 608 protected void setPeer(final Element element, 609 final org.netbeans.modules.xml.schema.model.Element e, 610 final ElementReference eref) { 611 sgh.setPeer(element, e, eref); 612 } 613 614 protected void setPeer(final Attribute attribute, 615 final org.netbeans.modules.xml.schema.model.Attribute a, 616 final AttributeReference aRef) { 617 sgh.setPeer(attribute, a, aRef); 618 } 619 620 private UniqueId createUniqueId() { 621 return new UniqueId() { 622 private int lastId = -1; 623 public int nextId() { 624 return ++lastId; 625 } 626 }; 627 } 628 629 private PrimitiveCart createPrimitiveCart() { 630 return new PrimitiveCart() { 631 private Map <SchemaComponent, Datatype> pc = new HashMap <SchemaComponent, Datatype>(); 632 private Map <String , GlobalSimpleType> ptypes = new HashMap <String , GlobalSimpleType>(); 633 private GlobalSimpleType def = null; 634 public void add(Datatype d, SchemaComponent referer) { 635 pc.put(referer, d); 636 } 637 public Set <Map.Entry <SchemaComponent, Datatype>> getEntries() { 638 return pc.entrySet(); 639 } 640 public GlobalSimpleType getDefaultPrimitive() { 641 if(def==null) { 642 def = getPrimitiveType("string"); } 644 return def; 645 } 646 public GlobalSimpleType getPrimitiveType(String typeName) { 647 if(ptypes.isEmpty()) { 648 SchemaModel primitiveModel = SchemaModelFactory.getDefault().getPrimitiveTypesModel(); 649 Collection <GlobalSimpleType> primitives = primitiveModel.getSchema().getSimpleTypes(); 650 for(GlobalSimpleType ptype: primitives){ 651 ptypes.put(ptype.getName(), ptype); 652 } 653 } 654 return ptypes.get(typeName); 655 } 656 }; 657 } 658 659 protected void addRef(SchemaComponent referer, SchemaComponent ref) { 660 sgh.addRef(referer, ref); 661 } 662 663 protected SchemaComponent getRef(SchemaComponent referer) { 664 return sgh.getRef(referer); 665 } 666 667 protected void addToGlobal(SchemaComponent sc) { 668 createGlobals.add(sc); 669 } 670 671 private void fixPrimitiveTypes() { 672 for(Map.Entry <SchemaComponent, Datatype> e: pc.getEntries()) { 673 Datatype d = (Datatype) e.getValue(); 674 SchemaComponent referer = (SchemaComponent) e.getKey(); 675 String typeName = d.getName(); 676 if(d instanceof CustomDatatype) 677 typeName = ((CustomDatatype)d).getBase().getName(); 678 GlobalSimpleType gst = pc.getPrimitiveType(typeName); 679 NamedComponentReference<GlobalSimpleType> ref = 680 referer.createReferenceTo(gst, GlobalSimpleType.class); 681 SchemaGeneratorUtil.setSimpleType(referer, ref); 682 } 683 } 684 685 protected void clear() { 686 path.clear(); 687 path = null; 688 689 createGlobals.clear(); 690 createGlobals = null; 691 692 fixNamesMap.clear(); 693 fixNamesMap = null; 694 695 refMap.clear(); 696 refMap = null; 697 698 namesMap.clear(); 699 namesMap = null; 700 } 701 702 interface SchemaGeneratorHelper { 703 704 public void execute(SchemaModel sm) throws IOException ; 705 706 public SchemaComponent getParent(final AXIComponent axiparent) 707 throws IllegalArgumentException ; 708 709 712 public <T extends NameableSchemaComponent>String 713 findUniqueGlobalName(Class <T> type, NameableSchemaComponent c, 714 final String seed); 715 716 public void setPeer(final Element element, 717 final org.netbeans.modules.xml.schema.model.Element e, 718 final ElementReference eref); 719 720 public void setPeer(final Attribute attribute, 721 final org.netbeans.modules.xml.schema.model.Attribute a, 722 final AttributeReference aRef); 723 724 public void addRef(SchemaComponent referer, SchemaComponent ref); 725 726 public SchemaComponent getRef(SchemaComponent referer); 727 728 public void addElement(GlobalElement ge, int index); 729 730 public void addComplexType(GlobalComplexType gct, int index); 731 732 public void addSimpleType(GlobalSimpleType gst, int index); 733 } 734 735 class UpdateHelper implements SchemaGeneratorHelper { 736 737 UpdateHelper() { 738 } 739 740 public void execute(SchemaModel sm) throws IOException { 741 DefaultSchemaGenerator.this.sm = sm; 742 DefaultSchemaGenerator.this.am = 743 AXIModelFactory.getDefault().getModel(sm); 744 SchemaUpdate su = SchemaGeneratorUtil.getSchemaUpdate(am); 745 Collection <SchemaUpdate.UpdateUnit> us = su.getUpdateUnits(); 746 try { 747 ((AXIModelImpl)am).disableAutoSync(); 748 sm.startTransaction(); 749 for(SchemaUpdate.UpdateUnit u:us) { 750 AXIComponent source = u.getSource(); 751 if(source.getModel() != am) continue; 753 SchemaUpdate.UpdateUnit.Type type = u.getType(); 754 if(type == SchemaUpdate.UpdateUnit.Type.CHILD_ADDED) 755 addSchemaComponent(source, u); 756 else if(type == SchemaUpdate.UpdateUnit.Type.CHILD_DELETED) 757 SchemaGeneratorUtil.removeSchemaComponent(source, u, sm); 758 else if(type == SchemaUpdate.UpdateUnit.Type.CHILD_MODIFIED) 759 SchemaGeneratorUtil.modifySchemaComponent(source, u, sm, pc); 760 } 761 } finally { 763 clear(); 764 sm.endTransaction(); 765 ((AXIModelImpl)am).enableAutoSync(); 766 } 767 } 768 769 protected void addSchemaComponent(AXIComponent source, 770 SchemaUpdate.UpdateUnit u) { 771 assert u.getNewValue() != null; 772 scParent = DefaultSchemaGenerator.this.getParent( 773 ((AXIComponent)u.getNewValue()).getParent()); 774 assert scParent != null; 775 ((AXIComponent)u.getNewValue()).accept(DefaultSchemaGenerator.this); 776 } 777 778 public <T extends NameableSchemaComponent>String 779 findUniqueGlobalName(Class <T> type, NameableSchemaComponent c, 780 final String seed) { 781 return SchemaGeneratorUtil.findUniqueGlobalName(type, seed, sm); 782 } 783 784 public SchemaComponent getParent(final AXIComponent axiparent) 785 throws IllegalArgumentException { 786 SchemaComponent scParent = null; 787 if(axiparent instanceof AXIDocument) 788 scParent = sm.getSchema(); 789 else if(axiparent instanceof Element){ 790 SchemaComponent e = axiparent.getPeer(); 791 if(e instanceof ElementReference) 792 e = getRef(axiparent.getPeer()); 793 assert e != null; 794 SchemaComponent lct = SchemaGeneratorUtil.getLocalComplexType(e); 795 if(lct == null) { 796 lct = SchemaGeneratorUtil.getGlobalComplexType(e); 797 if(lct == null) { 798 if((e instanceof GlobalElement && ((GlobalElement)e).getType() != null && 799 ((GlobalElement)e).getType().get() instanceof GlobalSimpleType) || 800 (e instanceof LocalElement && ((LocalElement)e).getType() != null && 801 ((LocalElement)e).getType().get() instanceof GlobalSimpleType)) { 802 SimpleContent newsc = sm.getFactory().createSimpleContent(); 803 SimpleExtension se = sm.getFactory().createSimpleExtension(); 804 newsc.setLocalDefinition(se); 805 GlobalType gst = null; 806 if(e instanceof GlobalElement) 807 gst = ((GlobalElement)e).getType().get(); 808 else if(e instanceof LocalElement) 809 gst = ((LocalElement)e).getType().get(); 810 if(gst != null) 811 se.setBase(se.createReferenceTo(gst, GlobalType.class)); 812 lct = SchemaGeneratorUtil.createLocalComplexType(sm, e); 813 ((LocalComplexType)lct).setDefinition(newsc); 814 } else 815 lct = SchemaGeneratorUtil.createLocalComplexType(sm, e); 816 } 817 } 818 assert lct != null; 819 scParent = lct; 820 } else if(axiparent instanceof ContentModel){ 821 scParent = axiparent.getPeer(); 822 } else if(axiparent instanceof Compositor){ 823 scParent = axiparent.getPeer(); 824 } 825 return scParent; 826 } 827 828 public void setPeer(final Element element, 829 final org.netbeans.modules.xml.schema.model.Element e, 830 final ElementReference eref) { 831 if(eref != null) 832 element.setPeer(eref); 833 else 834 element.setPeer(e); 835 } 836 837 public void setPeer(final Attribute attribute, 838 final org.netbeans.modules.xml.schema.model.Attribute a, 839 final AttributeReference aRef) { 840 if(aRef != null) 841 attribute.setPeer(aRef); 842 else 843 attribute.setPeer(a); 844 } 845 846 public void addElement(GlobalElement ge, int index) { 847 if(index != -1) 848 SchemaGeneratorUtil.addChildComponent(sm, sm.getSchema(), ge, index); 849 else 850 sm.getSchema().addElement((GlobalElement) ge); 851 } 852 853 public void addComplexType(GlobalComplexType gct, int index) { 854 if(index != -1) 855 SchemaGeneratorUtil.addChildComponent(sm, sm.getSchema(), gct, index); 856 else 857 sm.getSchema().addComplexType(gct); 858 } 859 860 public void addSimpleType(GlobalSimpleType gst, int index) { 861 if(index != -1) 862 SchemaGeneratorUtil.addChildComponent(sm, sm.getSchema(), gst, index); 863 else 864 sm.getSchema().addSimpleType(gst); 865 } 866 867 public void addRef(SchemaComponent referer, SchemaComponent ref) { 868 } 869 870 public SchemaComponent getRef(SchemaComponent referer) { 871 if(referer instanceof ElementReference) 872 return ((ElementReference)referer).getRef().get(); 873 else if(referer instanceof org.netbeans.modules.xml.schema.model.Element) 874 if(referer instanceof GlobalElement && 875 ((GlobalElement)referer).getType() != null && 876 ((GlobalElement)referer).getType().get() instanceof GlobalComplexType) 877 return ((GlobalElement)referer).getType().get(); 878 else if(referer instanceof LocalElement && 879 ((LocalElement)referer).getType() != null && 880 ((LocalElement)referer).getType().get() instanceof GlobalComplexType) 881 return ((LocalElement)referer).getType().get(); 882 return null; 883 } 884 885 protected void clear() { 886 DefaultSchemaGenerator.this.clear(); 887 } 888 } 889 890 class TransformHelper implements SchemaGeneratorHelper { 891 892 TransformHelper() { 893 } 894 895 public void execute(SchemaModel sm) throws IOException { 896 DefaultSchemaGenerator.this.sm = sm; 897 DefaultSchemaGenerator.this.am = AXIModelFactory.getDefault().getModel(sm); 898 Schema schema = sm.getSchema(); 899 assert schema != null; 900 try { 901 HashMap <Class , Map <String , SchemaComponent>> allGlobals = 902 new HashMap <Class , Map <String , SchemaComponent>>(); 903 java.util.List <Element> lrges = preTransform(schema, allGlobals); 904 905 ((AXIModelImpl)am).disableAutoSync(); 906 sm.startTransaction(); 907 for(Element element : lrges) 908 transformChildren(element, schema); 909 910 postTransform(schema, allGlobals); 911 } finally { 912 clear(); 913 try { 914 sm.endTransaction(); 915 } 916 finally { 917 ((AXIModelImpl)am).enableAutoSync(); 918 am.sync(); 919 } 920 } 921 } 922 923 public SchemaComponent getParent(final AXIComponent axiparent) 924 throws IllegalArgumentException { 925 throw new IllegalArgumentException ("should not call this api during transform"); 926 } 927 928 protected void transformChildren(AXIComponent component, 929 SchemaComponent parent) { 930 if(!SchemaGeneratorUtil.fromSameSchemaModel(component.getPeer(), sm)) 932 return; 933 assert parent != null; 934 DefaultSchemaGenerator.this.scParent = parent; 935 936 component.accept(DefaultSchemaGenerator.this); 937 if(elementReuseList.contains(component)) 938 return; 939 SchemaComponent cc = DefaultSchemaGenerator.this.sc; 940 941 if(component instanceof Element) { 943 Element orig = (Element)component; 944 if(orig.isReference()) { 945 orig = SchemaGeneratorUtil.findOriginalElement(orig); 946 } 947 if(path.size() > 0 && path.contains(orig)) 948 return; 949 path.add(orig); 950 } 951 try { 952 if(component.getChildren().size() > 0) { 953 parent = scParent; 954 } 955 assert parent != null; 956 if(component instanceof AbstractElement) { 957 for(AbstractAttribute attr : 958 ((AbstractElement)component).getAttributes()) { 959 if(!SchemaGeneratorUtil.fromSameSchemaModel(attr.getPeer(), sm)) 961 continue; 962 DefaultSchemaGenerator.this.scParent = parent; 963 attr.accept(DefaultSchemaGenerator.this); 964 } 965 } 966 for(AXIComponent child: component.getChildren()) { 967 if(!(child instanceof AbstractAttribute)) { 968 transformChildren(child, parent); 969 } 970 } 971 } finally { 972 if(component instanceof Element) 973 path.remove(path.size()-1); 974 } 975 } 976 977 protected java.util.List <Element> preTransform(Schema schema, 978 Map <Class , Map <String , SchemaComponent>> allGlobals) { 979 java.util.List <Element> lrges = 980 SchemaGeneratorUtil.findMasterGlobalElements( 981 DefaultSchemaGenerator.this.am); 982 983 AXINonCyclicVisitor visitor = new AXINonCyclicVisitor(am); 985 visitor.expand(lrges); 986 987 989 SortedMap <String , SchemaComponent> ggmap = 990 new TreeMap <String , SchemaComponent>(); 991 allGlobals.put(GlobalGroup.class, ggmap); 992 for(GlobalGroup ag:schema.getGroups()) 993 ggmap.put(ag.getName(), ag); 994 995 SortedMap <String , SchemaComponent> gctmap = 996 new TreeMap <String , SchemaComponent>(); 997 allGlobals.put(GlobalComplexType.class, gctmap); 998 for(GlobalComplexType gct:schema.getComplexTypes()) 999 gctmap.put(gct.getName(), gct); 1000 1001 SortedMap <String , SchemaComponent> gagmap = 1002 new TreeMap <String , SchemaComponent>(); 1003 allGlobals.put(GlobalAttributeGroup.class, gagmap); 1004 for(GlobalAttributeGroup ag:schema.getAttributeGroups()) 1005 gagmap.put(ag.getName(), ag); 1006 1007 SortedMap <String , SchemaComponent> gstmap = 1008 new TreeMap <String , SchemaComponent>(); 1009 allGlobals.put(GlobalSimpleType.class, gstmap); 1010 for(GlobalType gst:schema.getSimpleTypes()) 1011 gstmap.put(gst.getName(), gst); 1012 1013 SortedMap <String , SchemaComponent> gemap = 1014 new TreeMap <String , SchemaComponent>(); 1015 allGlobals.put(GlobalElement.class, gemap); 1016 for(GlobalElement ge:schema.getElements()) 1017 gemap.put(ge.getName(), ge); 1018 1019 SortedMap <String , SchemaComponent> gamap = 1020 new TreeMap <String , SchemaComponent>(); 1021 allGlobals.put(GlobalAttribute.class, gamap); 1022 for(GlobalAttribute ga:schema.getAttributes()) 1023 gamap.put(ga.getName(), ga); 1024 1025 return lrges; 1026 } 1027 1028 protected void postTransform(Schema schema, 1029 HashMap <Class , Map <String , SchemaComponent>> allGlobals) { 1030 removeAllGlobals(schema, allGlobals); 1032 1033 addAllGlobals(schema, createGlobals); 1035 1036 fixGlobalElementNames(); 1038 1039 fixPrimitiveTypes(); 1041 } 1042 1043 private void removeAllGlobals(final Schema schema, 1045 final HashMap <Class , Map <String , SchemaComponent>> allGlobals) { 1046 removeGlobalSchemaComponent(GlobalSimpleType.class, allGlobals, schema); 1048 1049 removeGlobalSchemaComponent(GlobalAttribute.class, allGlobals, schema); 1051 1052 removeGlobalSchemaComponent(GlobalAttributeGroup.class, allGlobals, schema); 1054 1055 removeGlobalSchemaComponent(GlobalComplexType.class, allGlobals, schema); 1057 1058 removeGlobalSchemaComponent(GlobalGroup.class, allGlobals, schema); 1060 1061 removeGlobalSchemaComponent(GlobalElement.class, allGlobals, schema); 1063 } 1064 1065 private void removeGlobalSchemaComponent(final Class type, 1066 final HashMap <Class , Map <String , SchemaComponent>> allGlobals, 1067 final Schema schema) { 1068 Map <String , SchemaComponent> gmap = allGlobals.get(type); 1069 if(gmap == null) return; 1070 int count = 0; 1071 for (Map.Entry entry : gmap.entrySet()) { 1072 SchemaComponent sc = (SchemaComponent) entry.getValue(); 1073 commitTransaction(count); 1074 if(sc instanceof GlobalSimpleType) 1075 schema.removeSimpleType((GlobalSimpleType) sc); 1076 else if(sc instanceof GlobalAttribute) 1077 schema.removeAttribute((GlobalAttribute) sc); 1078 else if(sc instanceof GlobalAttributeGroup) 1079 schema.removeAttributeGroup((GlobalAttributeGroup) sc); 1080 else if(sc instanceof GlobalComplexType) 1081 schema.removeComplexType((GlobalComplexType) sc); 1082 else if(sc instanceof GlobalGroup) 1083 schema.removeGroup((GlobalGroup) sc); 1084 else if(sc instanceof GlobalElement) 1085 schema.removeElement((GlobalElement) sc); 1086 } 1087 } 1088 1089 private void commitTransaction(int count) { 1090 if(commitRange > 0 && (count++)%commitRange==0) { 1091 sm.endTransaction(); 1092 sm.startTransaction(); 1093 } 1094 } 1095 1096 1099 public <T extends NameableSchemaComponent>String 1100 findUniqueGlobalName(Class <T> type, NameableSchemaComponent c, 1101 final String seed) { 1102 HashMap <String , SchemaComponent> map = namesMap.get(type); 1103 if(map == null) { 1104 map = new HashMap <String , SchemaComponent>(); 1105 namesMap.put(type, map); 1106 } 1107 int count = 0; 1108 boolean found = true; 1109 while(found) { 1110 found = false; 1111 SchemaComponent sc = 1112 map.get(count>0?(seed + String.valueOf(count)):seed); 1113 if(sc != null) { 1114 count++; 1115 found = true; 1116 } 1117 } 1118 String uniqueName = count>0?(seed + String.valueOf(count)):seed; 1119 map.put(uniqueName, c); 1120 return uniqueName; 1121 } 1122 1123 public void setPeer(final Element element, 1124 final org.netbeans.modules.xml.schema.model.Element e, 1125 final ElementReference eref) { 1126 if(element.getChildren().size() > 0) { 1127 SchemaComponent lct = SchemaGeneratorUtil.getLocalComplexType(e); 1128 if(lct == null) { 1129 lct = SchemaGeneratorUtil.findTypeFromOtherModel(e, element, sm); 1131 if(lct == null) 1132 lct = SchemaGeneratorUtil.createLocalComplexType(sm, e); 1133 assert lct != null; 1134 scParent = lct; 1135 } 1136 } else 1137 scParent = e; 1138 } 1139 1140 public void setPeer(final Attribute attribute, 1141 final org.netbeans.modules.xml.schema.model.Attribute a, 1142 final AttributeReference aref) { 1143 } 1145 1146 public void addElement(GlobalElement ge, int index) { 1147 addToGlobal(ge); 1149 } 1150 1151 public void addComplexType(GlobalComplexType gct, int index) { 1152 addToGlobal(gct); 1154 } 1155 1156 public void addSimpleType(GlobalSimpleType gst, int index) { 1157 addToGlobal(gst); 1159 } 1160 1161 protected void addAllGlobals(final Schema schema, 1162 java.util.List <SchemaComponent> createGlobals) { 1163 for(int i=0;i<createGlobals.size();i++) { 1164 SchemaComponent sc = createGlobals.get(i); 1165 if(commitRange > 0 && i%commitRange==0) { 1166 sm.endTransaction(); 1167 sm.startTransaction(); 1168 } 1169 if(sc instanceof GlobalElement) 1170 sm.getSchema().addElement((GlobalElement) sc); 1171 else if(sc instanceof GlobalComplexType) 1172 sm.getSchema().addComplexType((GlobalComplexType) sc); 1173 if(sc instanceof GlobalSimpleType) 1174 sm.getSchema().addSimpleType((GlobalSimpleType) sc); 1175 else if(sc instanceof GlobalGroup) 1176 sm.getSchema().addGroup((GlobalGroup) sc); 1177 else if(sc instanceof GlobalAttributeGroup) 1178 sm.getSchema().addAttributeGroup( 1179 (GlobalAttributeGroup) sc); 1180 } 1181 } 1182 1183 public void addRef(SchemaComponent referer, SchemaComponent ref) { 1184 refMap.put(referer, ref); 1185 } 1186 1187 public SchemaComponent getRef(SchemaComponent referer) { 1188 return refMap.get(referer); 1189 } 1190 1191 protected void clear() { 1192 DefaultSchemaGenerator.this.clear(); 1193 } 1194 } 1195} 1196 | Popular Tags |