1 19 20 package org.netbeans.modules.xml.wsdl.model.impl; 21 22 import java.util.Collections ; 23 import java.util.Set ; 24 import javax.xml.namespace.QName ; 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 ; 33 import org.w3c.dom.Node ; 34 import org.w3c.dom.NodeList ; 35 36 37 41 public class WSDLElementFactoryProvider { 42 43 public static class DefinitionsFactory extends ElementFactory { 44 public Set <QName > getElementQNames() { 45 return Collections.singleton(WSDLQNames.DEFINITIONS.getQName()); 46 } 47 public WSDLComponent create(WSDLComponent context, Element el) { 48 throw new UnsupportedOperationException ("Root 'definitions' should be bootstrapped when WSDL model is created"); } 50 } 51 52 public static class BindingFactory extends ElementFactory { 53 public Set <QName > getElementQNames() { 54 return Collections.singleton(WSDLQNames.BINDING.getQName()); 55 } 56 public WSDLComponent create(WSDLComponent context, Element 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 <QName > getElementQNames() { 64 return Collections.singleton(WSDLQNames.DOCUMENTATION.getQName()); 65 } 66 public WSDLComponent create(WSDLComponent context, Element 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 <QName > getElementQNames() { 74 return Collections.singleton(WSDLQNames.FAULT.getQName()); 75 } 76 public WSDLComponent create(WSDLComponent context, Element 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 ("Wrong parent for 'fault'"); } 85 } 86 } 87 88 public static class OperationFactory extends ElementFactory { 89 public Set <QName > getElementQNames() { 90 return Collections.singleton(WSDLQNames.OPERATION.getQName()); 91 } 92 public WSDLComponent create(WSDLComponent context, Element 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 ("Wrong parent for 'operation'"); } 99 100 NodeList 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 n = list.item(i); 108 if (!(n instanceof Element )) { 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 <QName > getElementQNames() { 134 return Collections.singleton(WSDLQNames.INPUT.getQName()); 135 } 136 public WSDLComponent create(WSDLComponent context, Element 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 ("Wrong parent for 'input'"); } 145 } 146 } 147 148 public static class ImportFactory extends ElementFactory { 149 public Set <QName > getElementQNames() { 150 return Collections.singleton(WSDLQNames.IMPORT.getQName()); 151 } 152 public WSDLComponent create(WSDLComponent context, Element 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 <QName > getElementQNames() { 160 return Collections.singleton(WSDLQNames.MESSAGE.getQName()); 161 } 162 public WSDLComponent create(WSDLComponent context, Element 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 <QName > getElementQNames() { 170 return Collections.singleton(WSDLQNames.OUTPUT.getQName()); 171 } 172 public WSDLComponent create(WSDLComponent context, Element 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 ("Wrong parent for 'output'"); } 181 } 182 } 183 184 public static class PartFactory extends ElementFactory { 185 public Set <QName > getElementQNames() { 186 return Collections.singleton(WSDLQNames.PART.getQName()); 187 } 188 public WSDLComponent create(WSDLComponent context, Element 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 <QName > getElementQNames() { 196 return Collections.singleton(WSDLQNames.PORT.getQName()); 197 } 198 public WSDLComponent create(WSDLComponent context, Element 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 <QName > getElementQNames() { 206 return Collections.singleton(WSDLQNames.PORTTYPE.getQName()); 207 } 208 public WSDLComponent create(WSDLComponent context, Element 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 <QName > getElementQNames() { 216 return Collections.singleton(WSDLQNames.SERVICE.getQName()); 217 } 218 public WSDLComponent create(WSDLComponent context, Element 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 <QName > getElementQNames() { 226 return Collections.singleton(WSDLQNames.TYPES.getQName()); 227 } 228 public WSDLComponent create(WSDLComponent context, Element el) { 229 checkArgument(getElementQNames(), Util.getQName(el, (WSDLComponentBase) context)); 230 return new TypesImpl(context.getModel(), el); 231 } 232 } 233 234 private static QName getImpliedQName(Element el) { 235 String ns = el.getNamespaceURI(); 236 if (ns == null) { ns = WSDLQNames.WSDL_NS_URI; 238 } 239 return new QName (ns, el.getLocalName()); 240 } 241 242 private static void checkArgument(Set <QName > wqnames, QName qname) { 243 checkArgument(wqnames.iterator().next(), qname); 244 } 245 246 private static void checkArgument(QName wqname, QName qname) { 247 if (! wqname.equals(qname)) { 248 throw new IllegalArgumentException ("Invalid element "+qname.getLocalPart()); } 250 } 251 } 252 | Popular Tags |