1 15 package org.apache.hivemind.ant; 16 17 import java.util.Collection ; 18 import java.util.HashSet ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Set ; 22 23 import javax.xml.parsers.DocumentBuilder ; 24 import javax.xml.parsers.DocumentBuilderFactory ; 25 import javax.xml.parsers.ParserConfigurationException ; 26 27 import org.apache.hivemind.ApplicationRuntimeException; 28 import org.apache.hivemind.Attribute; 29 import org.apache.hivemind.ErrorHandler; 30 import org.apache.hivemind.definition.ConfigurationPointDefinition; 31 import org.apache.hivemind.definition.ContributionDefinition; 32 import org.apache.hivemind.definition.ImplementationConstructor; 33 import org.apache.hivemind.definition.ModuleDefinition; 34 import org.apache.hivemind.definition.Occurances; 35 import org.apache.hivemind.definition.RegistryDefinition; 36 import org.apache.hivemind.definition.ImplementationDefinition; 37 import org.apache.hivemind.definition.InterceptorDefinition; 38 import org.apache.hivemind.definition.ServicePointDefinition; 39 import org.apache.hivemind.definition.Visibility; 40 import org.apache.hivemind.impl.CreateClassServiceConstructor; 41 import org.apache.hivemind.impl.DefaultErrorHandler; 42 import org.apache.hivemind.impl.ExtensionResolver; 43 import org.apache.hivemind.impl.InvokeFactoryServiceConstructor; 44 import org.apache.hivemind.impl.RegistryBuilder; 45 import org.apache.hivemind.parse.AttributeMappingDescriptor; 46 import org.apache.hivemind.parse.ConversionDescriptor; 47 import org.apache.hivemind.schema.AttributeModel; 48 import org.apache.hivemind.schema.ElementModel; 49 import org.apache.hivemind.schema.Rule; 50 import org.apache.hivemind.schema.impl.SchemaImpl; 51 import org.apache.hivemind.schema.rules.CreateObjectRule; 52 import org.apache.hivemind.schema.rules.InvokeParentRule; 53 import org.apache.hivemind.schema.rules.PushAttributeRule; 54 import org.apache.hivemind.schema.rules.PushContentRule; 55 import org.apache.hivemind.schema.rules.ReadAttributeRule; 56 import org.apache.hivemind.schema.rules.ReadContentRule; 57 import org.apache.hivemind.schema.rules.SetModuleRule; 58 import org.apache.hivemind.schema.rules.SetParentRule; 59 import org.apache.hivemind.schema.rules.SetPropertyRule; 60 import org.apache.hivemind.util.IdUtils; 61 import org.apache.hivemind.xml.definition.impl.XmlServicePointDefinitionImpl; 62 import org.w3c.dom.Document ; 63 import org.w3c.dom.Element ; 64 65 102 public class RegistrySerializer 103 { 104 private Set _processedSchemas = new HashSet (); 105 106 private RegistryDefinition _registryDefinition; 107 108 private ErrorHandler _handler; 109 110 private Document _document; 111 112 private ModuleDefinition _md; 113 114 public RegistrySerializer() 115 { 116 _handler = new DefaultErrorHandler(); 117 } 118 119 public Document createRegistryDocument(RegistryDefinition registryDefinition) 120 { 121 _registryDefinition = registryDefinition; 122 ExtensionResolver resolver = new ExtensionResolver(_registryDefinition, _handler); 123 resolver.resolveExtensions(); 124 125 DocumentBuilder builder = getBuilder(); 126 127 _document = builder.newDocument(); 128 129 Element registry = _document.createElement("registry"); 130 131 133 _document.appendChild(registry); 134 135 for (Iterator i = _registryDefinition.getModules().iterator(); i.hasNext();) 136 { 137 ModuleDefinition module = (ModuleDefinition) i.next(); 138 139 Element moduleElement = getModuleElement(module); 140 registry.appendChild(moduleElement); 141 142 } 143 144 return _document; 145 } 146 147 private Element getModuleElement(ModuleDefinition md) 148 { 149 _md = md; 150 Element module = _document.createElement("module"); 151 152 module.setAttribute("id", md.getId()); 153 module.setAttribute("package", md.getPackageName()); 154 155 157 addDependencies(module); 158 159 addServicePoints(module); 160 161 addConfigurationPoints(module); 162 163 168 170 return module; 171 } 172 173 private void addDependencies(Element module) 174 { 175 Collection dependencies = _md.getDependencies(); 176 177 if (dependencies != null) 178 { 179 for (Iterator i = dependencies.iterator(); i.hasNext();) 180 { 181 String dd = (String ) i.next(); 182 183 Element dependency = getDependencyElement(dd); 184 185 module.appendChild(dependency); 186 } 187 } 188 } 189 190 private void addServicePoints(Element module) 191 { 192 Collection servicePoints = _md.getServicePoints(); 193 194 if (servicePoints != null) 195 { 196 for (Iterator i = servicePoints.iterator(); i.hasNext();) 197 { 198 ServicePointDefinition spd = (ServicePointDefinition) i.next(); 199 200 Element servicePoint = getServicePointElement(spd); 201 202 module.appendChild(servicePoint); 203 204 } 205 } 206 } 207 208 private void addConfigurationPoints(Element module) 209 { 210 Collection configurationPoints = _md.getConfigurationPoints(); 211 212 if (configurationPoints != null) 213 { 214 for (Iterator i = configurationPoints.iterator(); i.hasNext();) 215 { 216 ConfigurationPointDefinition cpd = (ConfigurationPointDefinition) i.next(); 217 218 Element configurationPoint = getConfigurationPointElement(cpd); 219 220 module.appendChild(configurationPoint); 221 } 222 } 223 } 224 225 242 259 279 296 private Element getDependencyElement(String dependsOn) 297 { 298 Element dependency = _document.createElement("dependency"); 299 300 dependency.setAttribute("module-id", dependsOn); 301 302 return dependency; 303 } 304 305 private Element getServicePointElement(ServicePointDefinition spd) 306 { 307 Element servicePoint = _document.createElement("service-point"); 308 309 servicePoint.setAttribute("id", qualify(spd.getId())); 310 servicePoint.setAttribute("interface", spd.getInterfaceClassName()); 311 313 if (spd.getVisibility() == Visibility.PRIVATE) 314 servicePoint.setAttribute("visibility", "private"); 315 316 if (spd instanceof XmlServicePointDefinitionImpl) 317 processXmlServicePoint(servicePoint, (XmlServicePointDefinitionImpl) spd); 318 319 ImplementationDefinition ib = spd.getDefaultImplementation(); 320 321 if (ib != null) 322 { 323 Element instanceBuilder = getInstanceBuilderElement(ib, ib.getServiceConstructor()); 324 325 servicePoint.appendChild(instanceBuilder); 326 } 327 328 Collection interceptors = spd.getInterceptors(); 329 330 if (interceptors != null) 331 { 332 for (Iterator i = interceptors.iterator(); i.hasNext();) 333 { 334 InterceptorDefinition icd = (InterceptorDefinition) i.next(); 335 336 Element interceptor = getInterceptorElement(icd); 337 338 servicePoint.appendChild(interceptor); 339 } 340 } 341 342 return servicePoint; 343 } 344 345 private void processXmlServicePoint(Element servicePointElement, XmlServicePointDefinitionImpl xmlServicePoint) 346 { 347 if (xmlServicePoint.getParametersCount() != Occurances.REQUIRED) 348 servicePointElement.setAttribute("parameters-occurs", xmlServicePoint.getParametersCount().getName() 349 .toLowerCase()); 350 if (xmlServicePoint.getParametersSchema() != null) 351 addSchema(servicePointElement, (SchemaImpl) xmlServicePoint.getParametersSchema(), "parameters-schema"); 352 355 } 356 357 private Element getConfigurationPointElement(ConfigurationPointDefinition cpd) 358 { 359 Element configurationPoint = _document.createElement("configuration-point"); 360 361 configurationPoint.setAttribute("id", qualify(cpd.getId())); 362 if (cpd.getVisibility() == Visibility.PRIVATE) 363 configurationPoint.setAttribute("visibility", "private"); 364 365 367 369 Collection contributions = cpd.getContributions(); 370 for (Iterator iter = contributions.iterator(); iter.hasNext();) 371 { 372 ContributionDefinition contribution = (ContributionDefinition) iter.next(); 373 Element contributionElement = getContributionElement(contribution, cpd.getId()); 374 375 configurationPoint.appendChild(contributionElement); 376 377 } 378 379 return configurationPoint; 380 } 381 382 390 private Element getContributionElement(ContributionDefinition cd, String configurationPointId) 391 { 392 Element contribution = _document.createElement("contribution"); 393 394 contribution.setAttribute("configuration-id", qualify(configurationPointId)); 395 396 399 413 415 return contribution; 416 } 417 418 441 450 private Element getInstanceBuilderElement(ImplementationDefinition id, ImplementationConstructor ib) 451 { 452 Element instanceBuilder; 453 454 if (ib instanceof CreateClassServiceConstructor) 455 { 456 CreateClassServiceConstructor cid = (CreateClassServiceConstructor) ib; 457 instanceBuilder = _document.createElement("create-instance"); 458 459 instanceBuilder.setAttribute("class", cid.getInstanceClassName()); 460 if (!id.getServiceModel().equals("singleton")) 461 instanceBuilder.setAttribute("model", id.getServiceModel()); 462 } 463 else 464 { 465 InvokeFactoryServiceConstructor ifd = (InvokeFactoryServiceConstructor) ib; 466 instanceBuilder = _document.createElement("invoke-factory"); 467 468 if (!ifd.getFactoryServiceId().equals("hivemind.BuilderFactory")) 469 instanceBuilder.setAttribute("service-id", qualify(ifd.getFactoryServiceId())); 470 if (id.getServiceModel() != null) 471 instanceBuilder.setAttribute("model", id.getServiceModel()); 472 473 List parameters = ifd.getParameters(); 474 475 if (parameters != null) 476 { 477 for (Iterator i = parameters.iterator(); i.hasNext();) 478 { 479 org.apache.hivemind.Element parameter = (org.apache.hivemind.Element) i.next(); 480 481 Element element = getParamterElement(parameter); 482 483 instanceBuilder.appendChild(element); 484 } 485 } 486 } 487 488 return instanceBuilder; 489 } 490 491 private Element getInterceptorElement(InterceptorDefinition icd) 492 { 493 Element interceptor = _document.createElement("interceptor"); 494 495 return interceptor; 501 } 502 503 private Element getParamterElement(org.apache.hivemind.Element parameter) 504 { 505 Element element = _document.createElement(parameter.getElementName()); 506 507 List attributes = parameter.getAttributes(); 508 509 for (Iterator i = attributes.iterator(); i.hasNext();) 510 { 511 Attribute attribute = (Attribute) i.next(); 512 513 element.setAttribute(attribute.getName(), attribute.getValue()); 514 } 515 516 List elements = parameter.getElements(); 517 518 for (Iterator i = elements.iterator(); i.hasNext();) 519 { 520 org.apache.hivemind.Element nestedParameter = (org.apache.hivemind.Element) i.next(); 521 522 element.appendChild(getParamterElement(nestedParameter)); 523 } 524 525 return element; 526 } 527 528 private void addSchema(Element container, SchemaImpl s, String elementName) 529 { 530 if (_processedSchemas.contains(s)) 531 return; 532 533 Element schema = _document.createElement(elementName); 534 535 if (s.getId() != null) 536 schema.setAttribute("id", qualify(s.getId())); 537 538 if (s.getVisibility() == Visibility.PRIVATE) 539 schema.setAttribute("visibility", "private"); 540 541 schema.appendChild(_document.createTextNode(s.getAnnotation())); 542 543 for (Iterator j = s.getElementModel().iterator(); j.hasNext();) 544 { 545 ElementModel em = (ElementModel) j.next(); 546 547 Element element = getElementElement(em); 548 549 schema.appendChild(element); 550 } 551 552 container.appendChild(schema); 553 554 _processedSchemas.add(s); 555 } 556 557 private Element getRulesElement(ElementModel em) 558 { 559 Element rules = _document.createElement("rules"); 560 561 for (Iterator i = em.getRules().iterator(); i.hasNext();) 562 { 563 Rule r = (Rule) i.next(); 564 565 Element rule = null; 566 567 if (r instanceof CreateObjectRule) 568 { 569 CreateObjectRule cor = (CreateObjectRule) r; 570 rule = _document.createElement("create-object"); 571 572 rule.setAttribute("class", cor.getClassName()); 573 } 574 else if (r instanceof InvokeParentRule) 575 { 576 InvokeParentRule ipr = (InvokeParentRule) r; 577 rule = _document.createElement("invoke-parent"); 578 579 rule.setAttribute("method", ipr.getMethodName()); 580 if (ipr.getDepth() != 1) 581 rule.setAttribute("depth", Integer.toString(ipr.getDepth())); 582 } 583 else if (r instanceof PushAttributeRule) 584 { 585 PushAttributeRule par = (PushAttributeRule) r; 586 rule = _document.createElement("push-attribute"); 587 588 rule.setAttribute("attribute", par.getAttributeName()); 589 } 590 else if (r instanceof PushContentRule) 591 { 592 rule = _document.createElement("push-content"); 593 } 594 else if (r instanceof ReadAttributeRule) 595 { 596 ReadAttributeRule rar = (ReadAttributeRule) r; 597 rule = _document.createElement("read-attribute"); 598 599 rule.setAttribute("property", rar.getPropertyName()); 600 rule.setAttribute("attribute", rar.getAttributeName()); 601 if (!rar.getSkipIfNull()) 602 rule.setAttribute("skip-if-null", "false"); 603 if (rar.getTranslator() != null) 604 rule.setAttribute("translator", rar.getTranslator()); 605 } 606 else if (r instanceof ReadContentRule) 607 { 608 ReadContentRule rcr = (ReadContentRule) r; 609 rule = _document.createElement("read-content"); 610 611 rule.setAttribute("property", rcr.getPropertyName()); 612 } 613 else if (r instanceof SetModuleRule) 614 { 615 SetModuleRule smr = (SetModuleRule) r; 616 rule = _document.createElement("set-module"); 617 618 rule.setAttribute("property", smr.getPropertyName()); 619 } 620 else if (r instanceof SetParentRule) 621 { 622 SetParentRule spr = (SetParentRule) r; 623 rule = _document.createElement("set-parent"); 624 625 rule.setAttribute("property", spr.getPropertyName()); 626 } 627 else if (r instanceof SetPropertyRule) 628 { 629 SetPropertyRule spr = (SetPropertyRule) r; 630 rule = _document.createElement("set-property"); 631 632 rule.setAttribute("property", spr.getPropertyName()); 633 rule.setAttribute("value", spr.getValue()); 634 } 635 else if (r instanceof ConversionDescriptor) 636 { 637 ConversionDescriptor cd = (ConversionDescriptor) r; 638 rule = _document.createElement("conversion"); 639 640 rule.setAttribute("class", cd.getClassName()); 641 if (!cd.getParentMethodName().equals("addElement")) 642 rule.setAttribute("parent-method", cd.getParentMethodName()); 643 644 for (Iterator j = cd.getAttributeMappings().iterator(); j.hasNext();) 645 { 646 AttributeMappingDescriptor amd = (AttributeMappingDescriptor) j.next(); 647 648 Element map = _document.createElement("map"); 649 650 map.setAttribute("attribute", amd.getAttributeName()); 651 map.setAttribute("property", amd.getPropertyName()); 652 653 rule.appendChild(map); 654 } 655 } 656 else 657 { 658 rule = _document.createElement("custom"); 659 660 rule.setAttribute("class", r.getClass().getName()); 661 } 662 663 if (rule != null) 664 rules.appendChild(rule); 665 } 666 return rules; 667 } 668 669 private Element getElementElement(ElementModel em) 670 { 671 Element element = _document.createElement("element"); 672 element.setAttribute("name", em.getElementName()); 673 674 element.appendChild(_document.createTextNode(em.getAnnotation())); 675 676 for (Iterator i = em.getAttributeModels().iterator(); i.hasNext();) 677 { 678 AttributeModel am = (AttributeModel) i.next(); 679 680 Element attribute = getAttributeElement(am); 681 682 element.appendChild(attribute); 683 } 684 685 for (Iterator i = em.getElementModel().iterator(); i.hasNext();) 686 { 687 ElementModel nestedEm = (ElementModel) i.next(); 688 689 Element nestedElement = getElementElement(nestedEm); 690 691 element.appendChild(nestedElement); 692 } 693 694 if (!em.getRules().isEmpty()) 695 { 696 Element rules = getRulesElement(em); 697 698 element.appendChild(rules); 699 } 700 701 return element; 702 } 703 704 private Element getAttributeElement(AttributeModel am) 705 { 706 Element attribute = _document.createElement("attribute"); 707 708 attribute.setAttribute("name", am.getName()); 709 if (am.isRequired()) 710 attribute.setAttribute("required", "true"); 711 if (am.isUnique()) 712 attribute.setAttribute("unique", "true"); 713 if (am.getDefault() != null) 714 attribute.setAttribute("default", am.getDefault()); 715 if (!am.getTranslator().equals("smart")) 716 attribute.setAttribute("translator", am.getTranslator()); 717 718 attribute.appendChild(_document.createTextNode(am.getAnnotation())); 719 720 return attribute; 721 } 722 723 private String qualify(String id) 724 { 725 return IdUtils.qualify(_md.getId(), id); 726 } 727 728 private DocumentBuilder getBuilder() 729 { 730 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 731 732 factory.setIgnoringComments(true); 733 734 try 735 { 736 return factory.newDocumentBuilder(); 737 } 738 catch (ParserConfigurationException e) 739 { 740 throw new ApplicationRuntimeException(e); 741 } 742 } 743 744 public static Document createDefaultRegistryDocument() 745 { 746 RegistryBuilder builder = new RegistryBuilder(); 747 builder.autoDetectModules(); 748 749 RegistrySerializer serializer = new RegistrySerializer(); 750 751 return serializer.createRegistryDocument(builder.getRegistryDefinition()); 752 } 753 } | Popular Tags |