1 19 package org.sample.registry.model.impl; 20 21 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent; 22 import org.sample.registry.model.Entries; 23 import org.sample.registry.model.KnownTypes; 24 import org.sample.registry.model.Registry; 25 import org.sample.registry.model.RegistryComponent; 26 import org.sample.registry.model.RegistryComponentFactory; 27 import org.sample.registry.model.RegistryVisitor; 28 import org.sample.registry.model.Service; 29 import org.sample.registry.model.ServiceProvider; 30 import org.sample.registry.model.ServiceType; 31 import org.sample.registry.model.v09.Registry09; 32 import org.sample.registry.model.v09.Service09; 33 import org.w3c.dom.Element ; 34 35 public class RegistryComponentFactoryImpl implements RegistryComponentFactory { 36 private RegistryModelImpl model; 37 38 public RegistryComponentFactoryImpl(RegistryModelImpl model) { 39 this.model = model; 40 } 41 42 public RegistryComponent create(Element element, RegistryComponent context) { 43 if (context == null) { 44 if (areSameQName(RegistryQNames.REGISTRY, element)) { 45 return new RegistryImpl(model, element); 46 } else if (areSameQName(RegistryQNames.REGISTRY_09, element)) { 47 return new org.sample.registry.model.v09.impl.RegistryImpl(model, element); 48 } else { 49 return null; 50 } 51 } else { 52 return new CreateVisitor().create(element, context); 53 } 54 } 55 56 public ServiceType createServiceType() { 57 return new ServiceTypeImpl(model); 58 } 59 60 public ServiceProvider createServiceProvider() { 61 return new ServiceProviderImpl(model); 62 } 63 64 public Service createService() { 65 return new ServiceImpl(model); 66 } 67 68 public Service09 createService09() { 69 return new org.sample.registry.model.v09.impl.ServiceImpl(model); 70 } 71 72 public Registry createRegistry() { 73 return new RegistryImpl(model); 74 } 75 76 public Registry09 createRegistry09() { 77 return new org.sample.registry.model.v09.impl.RegistryImpl(model); 78 } 79 80 public static boolean areSameQName(RegistryQNames q, Element e) { 81 return q.getQName().equals(AbstractDocumentComponent.getQName(e)); 82 } 83 84 public static class CreateVisitor extends RegistryVisitor.Default { 85 Element element; 86 RegistryComponent created; 87 88 RegistryComponent create(Element element, RegistryComponent context) { 89 this.element = element; 90 context.accept(this); 91 return created; 92 } 93 94 private boolean isElementQName(RegistryQNames q) { 95 return areSameQName(q, element); 96 } 97 98 public void visit(Registry context) { 99 if (isElementQName(RegistryQNames.ENTRIES)) { 100 created = new EntriesImpl((RegistryModelImpl) context.getModel(), element); 101 } else if (isElementQName(RegistryQNames.KNOWN_TYPES)) { 102 created = new KnownTypesImpl((RegistryModelImpl) context.getModel(), element); 103 } 104 } 105 106 public void visit(Registry09 context) { 107 if (isElementQName(RegistryQNames.SERVICE_09)) { 108 created = new org.sample.registry.model.v09.impl.ServiceImpl( 109 (RegistryModelImpl) context.getModel(), element); 110 } 111 } 112 113 public void visit(Entries context) { 114 if (isElementQName(RegistryQNames.SERVICE)) { 115 created = new ServiceImpl((RegistryModelImpl)context.getModel(), element); 116 } 117 } 118 119 public void visit(KnownTypes context) { 120 if (isElementQName(RegistryQNames.TYPE)) { 121 created = new ServiceTypeImpl((RegistryModelImpl)context.getModel(), element); 122 } 123 } 124 125 public void visit(Service context) { 126 if (isElementQName(RegistryQNames.SERVICE_PROVIDER)) { 127 created = new ServiceProviderImpl((RegistryModelImpl)context.getModel(), element); 128 } 129 } 130 131 } 132 } 133 | Popular Tags |