KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sample > registry > model > impl > RegistryComponentFactoryImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
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 JavaDoc 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 JavaDoc e) {
81         return q.getQName().equals(AbstractDocumentComponent.getQName(e));
82     }
83     
84     public static class CreateVisitor extends RegistryVisitor.Default {
85         Element JavaDoc element;
86         RegistryComponent created;
87         
88         RegistryComponent create(Element JavaDoc 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