1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.wsdl.wsdl11.provider;
18
19 import java.util.Iterator;
20 import javax.wsdl.Definition;
21 import javax.wsdl.Message;
22 import javax.wsdl.Part;
23 import javax.wsdl.Types;
24 import javax.wsdl.WSDLException;
25 import javax.wsdl.extensions.ExtensibilityElement;
26 import javax.wsdl.extensions.schema.Schema;
27 import javax.xml.namespace.QName;
28
29 import org.springframework.util.Assert;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.w3c.dom.Element;
34 import org.w3c.dom.Node;
35 import org.w3c.dom.NodeList;
36
37
38
39
40
41
42
43
44
45 public class DefaultMessagesProvider implements MessagesProvider {
46
47 private static final Log logger = LogFactory.getLog(DefaultMessagesProvider.class);
48
49 public void addMessages(Definition definition) throws WSDLException {
50 Types types = definition.getTypes();
51 Assert.notNull(types, "No types element present in definition");
52 for (Iterator<?> iterator = types.getExtensibilityElements().iterator(); iterator.hasNext();) {
53 ExtensibilityElement extensibilityElement = (ExtensibilityElement) iterator.next();
54 if (extensibilityElement instanceof Schema) {
55 Schema schema = (Schema) extensibilityElement;
56 if (schema.getElement() != null) {
57 createMessages(definition, schema.getElement());
58 }
59 }
60 }
61 if (definition.getMessages().isEmpty() && logger.isWarnEnabled()) {
62 logger.warn("No messages were created, make sure the referenced schema(s) contain elements");
63 }
64 }
65
66 private void createMessages(Definition definition, Element schemaElement) throws WSDLException {
67 String schemaTargetNamespace = schemaElement.getAttribute("targetNamespace");
68 Assert.hasText(schemaTargetNamespace, "No targetNamespace defined on schema");
69 if (logger.isDebugEnabled()) {
70 logger.debug("Looking for elements in schema with target namespace [" + schemaTargetNamespace + "]");
71 }
72 NodeList children = schemaElement.getChildNodes();
73 for (int i = 0; i < children.getLength(); i++) {
74 Node child = children.item(i);
75 if (child.getNodeType() == Node.ELEMENT_NODE) {
76 Element childElement = (Element) child;
77 if (isMessageElement(childElement)) {
78 QName elementName = new QName(schemaTargetNamespace, getElementName(childElement));
79 Message message = definition.createMessage();
80 populateMessage(definition, message, elementName);
81 Part part = definition.createPart();
82 populatePart(definition, part, elementName);
83 message.addPart(part);
84 message.setUndefined(false);
85 definition.addMessage(message);
86 }
87 }
88 }
89 }
90
91
92
93
94
95
96
97 protected String getElementName(Element element) {
98 return element.getAttribute("name");
99 }
100
101
102
103
104
105
106
107
108
109
110 protected boolean isMessageElement(Element element) {
111 return "element".equals(element.getLocalName()) &&
112 "http://www.w3.org/2001/XMLSchema".equals(element.getNamespaceURI());
113 }
114
115
116
117
118
119
120
121
122
123
124
125 protected void populateMessage(Definition definition, Message message, QName elementName) throws WSDLException {
126 QName messageName = new QName(definition.getTargetNamespace(), elementName.getLocalPart());
127 if (logger.isDebugEnabled()) {
128 logger.debug("Creating message [" + messageName + "]");
129 }
130 message.setQName(messageName);
131 }
132
133
134
135
136
137
138
139
140
141
142
143 protected void populatePart(Definition definition, Part part, QName elementName) throws WSDLException {
144 part.setElementName(elementName);
145 part.setName(elementName.getLocalPart());
146 }
147
148 }