KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > model > impl > WSDLElementFactoryProvider


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
20 package org.netbeans.modules.xml.wsdl.model.impl;
21
22 import java.util.Collections JavaDoc;
23 import java.util.Set JavaDoc;
24 import javax.xml.namespace.QName JavaDoc;
25 import org.netbeans.modules.xml.wsdl.model.Binding;
26 import org.netbeans.modules.xml.wsdl.model.BindingOperation;
27 import org.netbeans.modules.xml.wsdl.model.Operation;
28 import org.netbeans.modules.xml.wsdl.model.PortType;
29 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
30 import org.netbeans.modules.xml.wsdl.model.spi.ElementFactory;
31 import org.netbeans.modules.xml.wsdl.model.spi.WSDLComponentBase;
32 import org.w3c.dom.Element JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 import org.w3c.dom.NodeList JavaDoc;
35
36
37 /**
38  * @author Nam Nguyen
39  * @author rico
40  */

41 public class WSDLElementFactoryProvider {
42    
43     public static class DefinitionsFactory extends ElementFactory {
44         public Set JavaDoc<QName JavaDoc> getElementQNames() {
45             return Collections.singleton(WSDLQNames.DEFINITIONS.getQName());
46         }
47         public WSDLComponent create(WSDLComponent context, Element JavaDoc el) {
48             throw new UnsupportedOperationException JavaDoc("Root 'definitions' should be bootstrapped when WSDL model is created"); //NOI18N
49
}
50     }
51     
52     public static class BindingFactory extends ElementFactory {
53         public Set JavaDoc<QName JavaDoc> getElementQNames() {
54             return Collections.singleton(WSDLQNames.BINDING.getQName());
55         }
56         public WSDLComponent create(WSDLComponent context, Element JavaDoc el) {
57             checkArgument(getElementQNames(), Util.getQName(el, (WSDLComponentBase) context));
58             return new BindingImpl(context.getModel(), el);
59         }
60     }
61     
62     public static class DocumentationFactory extends ElementFactory {
63         public Set JavaDoc<QName JavaDoc> getElementQNames() {
64             return Collections.singleton(WSDLQNames.DOCUMENTATION.getQName());
65         }
66         public WSDLComponent create(WSDLComponent context, Element JavaDoc el) {
67             checkArgument(getElementQNames(), Util.getQName(el, (WSDLComponentBase) context));
68             return new DocumentationImpl(context.getModel(), el);
69         }
70     }
71     
72     public static class FaultFactory extends ElementFactory {
73         public Set JavaDoc<QName JavaDoc> getElementQNames() {
74             return Collections.singleton(WSDLQNames.FAULT.getQName());
75         }
76         public WSDLComponent create(WSDLComponent context, Element JavaDoc el) {
77             checkArgument(getElementQNames(), Util.getQName(el, (WSDLComponentBase) context));
78             if (context instanceof BindingOperation) {
79                 return new BindingFaultImpl(context.getModel(), el);
80             } else if (context instanceof Operation) {
81                 return new FaultImpl(context.getModel(), el);
82             } else {
83                 throw new IllegalArgumentException JavaDoc("Wrong parent for 'fault'"); //NOI18N
84
}
85         }
86     }
87     
88     public static class OperationFactory extends ElementFactory {
89         public Set JavaDoc<QName JavaDoc> getElementQNames() {
90             return Collections.singleton(WSDLQNames.OPERATION.getQName());
91         }
92         public WSDLComponent create(WSDLComponent context, Element JavaDoc el) {
93             checkArgument(getElementQNames(), getImpliedQName(el));
94             if (context instanceof Binding) {
95                 return new BindingOperationImpl(context.getModel(), el);
96             } else if (! (context instanceof PortType)) {
97                 throw new IllegalArgumentException JavaDoc("Wrong parent for 'operation'"); //NOI18N
98
}
99             
100             //portType/operation
101
NodeList JavaDoc list = el.getChildNodes();
102             int in = 0, out = 0;
103             for (int i=0; i<list.getLength(); i++) {
104                 if (in > 0 && out > 0) {
105                     break;
106                 }
107                 Node JavaDoc n = list.item(i);
108                 if (!(n instanceof Element JavaDoc)) {
109                     continue;
110                 }
111                 if (n.getLocalName().equals(WSDLQNames.INPUT.getQName().getLocalPart())) {
112                     in = out == 0 ? 1 : 2;
113                 } else if (n.getLocalName().equals(WSDLQNames.OUTPUT.getQName().getLocalPart())) {
114                     out = in == 0 ? 1 : 2;
115                 }
116             }
117             
118             WSDLComponent ret = null;
119             if (in == 0 && out > 0) {
120                 ret = new NotificationOperationImpl(context.getModel(), el);
121             } else if (in > 0 && out == 0) {
122                 ret = new OneWayOperationImpl(context.getModel(), el);
123             } else if (in > out) {
124                 ret = new SolicitResponseOperationImpl(context.getModel(), el);
125             } else if (in < out) {
126                 ret = new RequestResponseOperationImpl(context.getModel(), el);
127             }
128             return ret;
129         }
130     }
131
132     public static class InputFactory extends ElementFactory {
133         public Set JavaDoc<QName JavaDoc> getElementQNames() {
134             return Collections.singleton(WSDLQNames.INPUT.getQName());
135         }
136         public WSDLComponent create(WSDLComponent context, Element JavaDoc el) {
137             checkArgument(getElementQNames(), Util.getQName(el, (WSDLComponentBase) context));
138             if (context instanceof BindingOperation) {
139                 return new BindingInputImpl(context.getModel(), el);
140             } else if (context instanceof Operation) {
141                 return new InputImpl(context.getModel(), el);
142             } else {
143                 throw new IllegalArgumentException JavaDoc("Wrong parent for 'input'"); //NOI18N
144
}
145         }
146     }
147     
148     public static class ImportFactory extends ElementFactory {
149         public Set JavaDoc<QName JavaDoc> getElementQNames() {
150             return Collections.singleton(WSDLQNames.IMPORT.getQName());
151         }
152         public WSDLComponent create(WSDLComponent context, Element JavaDoc el) {
153             checkArgument(getElementQNames(), Util.getQName(el, (WSDLComponentBase) context));
154             return new ImportImpl(context.getModel(), el);
155         }
156     }
157     
158     public static class MessageFactory extends ElementFactory {
159         public Set JavaDoc<QName JavaDoc> getElementQNames() {
160             return Collections.singleton(WSDLQNames.MESSAGE.getQName());
161         }
162         public WSDLComponent create(WSDLComponent context, Element JavaDoc el) {
163             checkArgument(getElementQNames(), Util.getQName(el, (WSDLComponentBase) context));
164             return new MessageImpl(context.getModel(), el);
165         }
166     }
167     
168     public static class OutputFactory extends ElementFactory {
169         public Set JavaDoc<QName JavaDoc> getElementQNames() {
170             return Collections.singleton(WSDLQNames.OUTPUT.getQName());
171         }
172         public WSDLComponent create(WSDLComponent context, Element JavaDoc el) {
173             checkArgument(getElementQNames(), Util.getQName(el, (WSDLComponentBase) context));
174             if (context instanceof BindingOperation) {
175                 return new BindingOutputImpl(context.getModel(), el);
176             } else if (context instanceof Operation) {
177                 return new OutputImpl(context.getModel(), el);
178             } else {
179                 throw new IllegalArgumentException JavaDoc("Wrong parent for 'output'"); //NOI18N
180
}
181         }
182     }
183     
184     public static class PartFactory extends ElementFactory {
185         public Set JavaDoc<QName JavaDoc> getElementQNames() {
186             return Collections.singleton(WSDLQNames.PART.getQName());
187         }
188         public WSDLComponent create(WSDLComponent context, Element JavaDoc el) {
189             checkArgument(getElementQNames(), Util.getQName(el, (WSDLComponentBase) context));
190             return new PartImpl(context.getModel(), el);
191         }
192     }
193     
194     public static class PortFactory extends ElementFactory {
195         public Set JavaDoc<QName JavaDoc> getElementQNames() {
196             return Collections.singleton(WSDLQNames.PORT.getQName());
197         }
198         public WSDLComponent create(WSDLComponent context, Element JavaDoc el) {
199             checkArgument(getElementQNames(), Util.getQName(el, (WSDLComponentBase) context));
200             return new PortImpl(context.getModel(), el);
201         }
202     }
203     
204     public static class PortTypeFactory extends ElementFactory {
205         public Set JavaDoc<QName JavaDoc> getElementQNames() {
206             return Collections.singleton(WSDLQNames.PORTTYPE.getQName());
207         }
208         public WSDLComponent create(WSDLComponent context, Element JavaDoc el) {
209             checkArgument(getElementQNames(), Util.getQName(el, (WSDLComponentBase) context));
210             return new PortTypeImpl(context.getModel(), el);
211         }
212     }
213     
214     public static class ServiceFactory extends ElementFactory {
215         public Set JavaDoc<QName JavaDoc> getElementQNames() {
216             return Collections.singleton(WSDLQNames.SERVICE.getQName());
217         }
218         public WSDLComponent create(WSDLComponent context, Element JavaDoc el) {
219             checkArgument(getElementQNames(), Util.getQName(el, (WSDLComponentBase) context));
220             return new ServiceImpl(context.getModel(), el);
221         }
222     }
223     
224     public static class TypesFactory extends ElementFactory {
225         public Set JavaDoc<QName JavaDoc> getElementQNames() {
226             return Collections.singleton(WSDLQNames.TYPES.getQName());
227         }
228         public WSDLComponent create(WSDLComponent context, Element JavaDoc el) {
229             checkArgument(getElementQNames(), Util.getQName(el, (WSDLComponentBase) context));
230             return new TypesImpl(context.getModel(), el);
231         }
232     }
233
234     private static QName JavaDoc getImpliedQName(Element JavaDoc el) {
235         String JavaDoc ns = el.getNamespaceURI();
236         if (ns == null) { // this can happen if new element has not added to xdm tree
237
ns = WSDLQNames.WSDL_NS_URI;
238         }
239         return new QName JavaDoc(ns, el.getLocalName());
240     }
241     
242     private static void checkArgument(Set JavaDoc<QName JavaDoc> wqnames, QName JavaDoc qname) {
243         checkArgument(wqnames.iterator().next(), qname);
244     }
245
246     private static void checkArgument(QName JavaDoc wqname, QName JavaDoc qname) {
247         if (! wqname.equals(qname)) {
248             throw new IllegalArgumentException JavaDoc("Invalid element "+qname.getLocalPart()); //NOI18N
249
}
250     }
251 }
252
Popular Tags