1
2
3
4
5
6
7
8
9 package bexee.wsdl.extensions.partnerlinktype;
10
11 import java.io.PrintWriter;
12
13 import javax.wsdl.Definition;
14 import javax.wsdl.PortType;
15 import javax.wsdl.WSDLException;
16 import javax.wsdl.extensions.ExtensibilityElement;
17 import javax.wsdl.extensions.ExtensionDeserializer;
18 import javax.wsdl.extensions.ExtensionRegistry;
19 import javax.wsdl.extensions.ExtensionSerializer;
20 import javax.xml.namespace.QName;
21
22 import org.w3c.dom.Element;
23 import org.w3c.dom.Node;
24 import org.w3c.dom.NodeList;
25
26 import bexee.ecs.XML;
27 import bexee.util.StringUtils;
28 import bexee.wsdl.extensions.partnerlinktype.impl.RoleImpl;
29
30 /***
31 * This is a Serializer and a Deserializer as used by the JWSDL standard. It is
32 * used serialization and deserialization of extension elements, i.e. in this
33 * particular case it is used for the PartnerLinkType extension element added to
34 * WSDL by the BPEL standard.
35 *
36 * @author Pawel Kowalski
37 * @version $Revision: 1.1 $, $Date: 2004/12/15 14:18:21 $
38 */
39 public class PartnerLinkTypeDeserializer implements ExtensionDeserializer,
40 ExtensionSerializer {
41
42 /***
43 * Deserialize a PartnerLinkType from the xml element string.
44 */
45 public ExtensibilityElement unmarshall(Class parentType, QName elementType,
46 Element element, Definition definition, ExtensionRegistry extReg)
47 throws WSDLException {
48
49
50
51 PartnerLinkType partnerLType = (PartnerLinkType) extReg
52 .createExtension(parentType, elementType);
53
54
55
56 partnerLType.setName(element.getAttribute("name"));
57
58
59
60 setRoles(partnerLType, element, definition);
61
62 return partnerLType;
63 }
64
65 /***
66 * Serialize a PartnerLinkType to an xml string.
67 */
68 public void marshall(Class parentType, QName elementType,
69 ExtensibilityElement element, PrintWriter pw,
70 Definition definition, ExtensionRegistry extReg)
71 throws WSDLException {
72
73 PartnerLinkType linkType = (PartnerLinkType) element;
74
75 String prefix = getPrefix(definition, PartnerLinkTypeConstants.NS_URI);
76
77 String pLinkTypeElementName = prefix
78 + PartnerLinkTypeConstants.LOCAL_NAME;
79 String roleElementName = prefix + RoleConstants.LOCAL_NAME;
80 String portTypeElementName = prefix
81 + RoleConstants.PORT_TYPE_LOCAL_NAME;
82
83 XML plinkXml = (XML) new XML(pLinkTypeElementName).addXMLAttribute(
84 PartnerLinkTypeConstants.PLINK_TYPE_NAME, linkType.getName());
85
86 Role role;
87 if ((role = linkType.getMyRole()) != null) {
88 plinkXml.addElement(getRoleXMLNode(definition, role,
89 roleElementName, portTypeElementName));
90 }
91 if ((role = linkType.getPartnerRole()) != null) {
92 plinkXml.addElement(getRoleXMLNode(definition, role,
93 roleElementName, portTypeElementName));
94 }
95
96 pw.print(plinkXml.toString() + "\n");
97
98 }
99
100 private XML getRoleXMLNode(Definition definition, Role role,
101 String roleElementName, String portTypeElementName) {
102
103 XML roleXml = (XML) new XML(roleElementName).addXMLAttribute(
104 RoleConstants.ROLE_NAME, role.getName());
105
106 PortType portType = role.getPortType();
107 String portTypeName = getPrefix(definition, portType.getQName()
108 .getNamespaceURI())
109 + portType.getQName().getLocalPart();
110 XML portTypeXml = (XML) new XML(portTypeElementName).addXMLAttribute(
111 RoleConstants.PORT_TYPE_NAME, portTypeName);
112
113 roleXml.addElement(portTypeXml);
114 return roleXml;
115 }
116
117 private String getPrefix(Definition definition, String namespaceURI) {
118 String prefix = definition.getPrefix(namespaceURI);
119 return (StringUtils.isNullOrEmpty(prefix) ? "" : prefix + ":");
120 }
121
122 private void setRoles(PartnerLinkType partnerLType, Element element,
123 Definition definition) throws WSDLException {
124 NodeList list = element.getChildNodes();
125 for (int i = 0; i < list.getLength(); i++) {
126 Node node = (Node) list.item(i);
127 if (node instanceof Element) {
128 Element nodeElement = (Element) node;
129 if (nodeElement.getLocalName().equals(RoleConstants.LOCAL_NAME)
130 && nodeElement.getNamespaceURI().equals(
131 RoleConstants.NS_URI)) {
132 Role role = new RoleImpl();
133 role.setName(nodeElement
134 .getAttribute(RoleConstants.ROLE_NAME));
135 setPortType(role, nodeElement, definition);
136 if (partnerLType.getMyRole() == null) {
137 partnerLType.setMyRole(role);
138 } else {
139 partnerLType.setPartnerRole(role);
140 }
141 }
142 }
143 }
144 }
145
146 private void setPortType(Role role, Element nodeElement,
147 Definition definition) throws WSDLException {
148 role.setPortType(getPortType(definition, nodeElement));
149 }
150
151 /***
152 * Extract the right PortType from the definition. The name of the PortType
153 * is extracted from the following xml element:
154 *
155 * <role name="Buyer"> <portType
156 * name="buy:BuyerPortType"/> </role>
157 *
158 * @param roleElement
159 * @return a PortType
160 * @throws WSDLSemanticException
161 */
162 private PortType getPortType(Definition definition, Element roleElement)
163 throws WSDLException {
164
165 NodeList list = roleElement.getChildNodes();
166 for (int i = 0; i < list.getLength(); i++) {
167 Node node = (Node) list.item(i);
168 if (node instanceof Element) {
169 Element nodeElement = (Element) node;
170 if (nodeElement.getNamespaceURI().equals(RoleConstants.NS_URI)
171 && nodeElement.getLocalName().equals(
172 RoleConstants.PORT_TYPE_LOCAL_NAME)) {
173 Node portTypeNode = nodeElement
174 .getAttributeNode(PartnerLinkTypeConstants.PLINK_TYPE_NAME);
175 String[] portTypeName = StringUtils.split(portTypeNode
176 .getNodeValue());
177 portTypeName[0] = definition.getNamespace(portTypeName[0]);
178 QName portTypeQName = new QName(portTypeName[0],
179 portTypeName[1]);
180
181 PortType portType = definition.getPortType(portTypeQName);
182 if (portType != null) {
183 return portType;
184 } else {
185
186 throw new WSDLException(WSDLException.INVALID_WSDL,
187 "PortType: " + portTypeQName + " not found");
188 }
189 }
190 }
191 }
192
193
194 throw new WSDLException(WSDLException.INVALID_WSDL,
195 "PortType not found");
196 }
197 }