KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > wsdl > xml > PartnerLinkTypeSerializer


1 package org.jbpm.bpel.wsdl.xml;
2
3 import java.io.PrintWriter JavaDoc;
4 import java.io.Serializable JavaDoc;
5
6 import javax.wsdl.Definition;
7 import javax.wsdl.PortType;
8 import javax.wsdl.WSDLException;
9 import javax.wsdl.extensions.ExtensibilityElement;
10 import javax.wsdl.extensions.ExtensionDeserializer;
11 import javax.wsdl.extensions.ExtensionRegistry;
12 import javax.wsdl.extensions.ExtensionSerializer;
13 import javax.xml.namespace.QName JavaDoc;
14
15 import org.w3c.dom.Element JavaDoc;
16
17 import com.ibm.wsdl.Constants;
18 import com.ibm.wsdl.util.xml.DOMUtils;
19
20 import org.jbpm.bpel.wsdl.def.PartnerLinkType;
21 import org.jbpm.bpel.xml.util.NodeUtil;
22
23 /**
24  * Translates between <code>&lt;partnerLinkType/&gt;</code> XML elements and
25  * {@link PartnerLinkType} instances.
26  * @author Alejandro Guízar
27  * @version $Revision: 1.6 $ $Date: 2005/06/23 02:22:47 $
28  */

29 public class PartnerLinkTypeSerializer implements ExtensionDeserializer,
30   ExtensionSerializer, Serializable JavaDoc {
31
32   private static final long serialVersionUID = 1L;
33
34   /**
35    * Deserializes a DOM element into a {@link PartnerLinkType} instance.
36    * @param parentType class object indicating where in the WSDL document
37    * this extensibility element was encountered
38    * @param elementType the qname of the extensibility element
39    * @param elem the extensibility element to deserialize
40    * @param def the definition this extensibility element was encountered in
41    * @param extReg the ExtensionRegistry to use (if needed again)
42    * @return the deserialized instance.
43    * @throws WSDLException if deserialization fails
44    */

45   public ExtensibilityElement unmarshall(Class JavaDoc parentType, QName JavaDoc elementType,
46     Element elem, Definition def, ExtensionRegistry extReg) throws WSDLException {
47     PartnerLinkType partnerLinkType = (PartnerLinkType) extReg.createExtension(parentType, elementType);
48
49     // name attribute
50
String JavaDoc name = elem.getAttribute(WsdlConstants.ATTR_NAME);
51     partnerLinkType.setQName(new QName JavaDoc(def.getTargetNamespace(), name));
52
53     // wsdl:required attribute
54
String JavaDoc required = DOMUtils.getAttributeNS(elem, Constants.NS_URI_WSDL,
55         Constants.ATTR_REQUIRED);
56     if (required != null) {
57       partnerLinkType.setRequired(Boolean.valueOf(required));
58     }
59
60     // first role element
61
Element roleElem = DOMUtils.getFirstChildElement(elem);
62     partnerLinkType.setFirstRole(unmarshallRole(roleElem, partnerLinkType, def));
63     
64     // second role element (optional)
65
roleElem = DOMUtils.getNextSiblingElement(roleElem);
66     if (roleElem != null) {
67       partnerLinkType.setSecondRole(unmarshallRole(roleElem, partnerLinkType, def));
68     }
69     return partnerLinkType;
70   }
71
72   /**
73    * Serializes a {@link PartnerLinkType} instance into the given
74    * {@link PrintWriter}.
75    * @param parentType class object indicating where in the WSDL document
76    * this extensibility element was encountered
77    * @param elementType the qname of the extensibility element
78    * @param extension the instance to serialize
79    * @param pw the stream to write in
80    * @param def the definition this extensibility element was encountered in
81    * @param extReg the ExtensionRegistry to use (if needed again)
82    * @throws WSDLException if serialization fails
83    */

84   public void marshall(Class JavaDoc parentType, QName JavaDoc elementType,
85     ExtensibilityElement extension, PrintWriter JavaDoc pw, Definition def,
86     ExtensionRegistry extReg) throws WSDLException {
87     if (extension == null)
88       return;
89     
90     PartnerLinkType partnerLinkType = (PartnerLinkType) extension;
91     
92     // open tag
93
String JavaDoc tagName = DOMUtils.getQualifiedValue(WsdlConstants.NS_PLNK,
94         WsdlConstants.ELEM_PARTNER_LINK_TYPE, def);
95     pw.print(" <" + tagName);
96
97     // name attribute
98
DOMUtils.printAttribute(WsdlConstants.ATTR_NAME,
99       partnerLinkType.getQName().getLocalPart(), pw);
100
101     // wsdl:required attribute
102
Boolean JavaDoc required = partnerLinkType.getRequired();
103     if (required != null) {
104       DOMUtils.printQualifiedAttribute(Constants.Q_ATTR_REQUIRED,
105         required.toString(), def, pw);
106     }
107     pw.println('>');
108
109     // role elements
110
marshallRole(partnerLinkType.getFirstRole(), pw, def);
111     marshallRole(partnerLinkType.getSecondRole(), pw, def);
112
113     // close tag
114
pw.println(" </" + tagName + '>');
115   }
116
117   /**
118    * Deserializes a DOM element into a {@link org.jbpm.bpel.wsdl.def.PartnerLinkType.Role}
119    * instance.
120    * @param roleElem the element to deserialize
121    * @param partnerLinkType the partner link type that contains this role
122    * @param def the definition where the extensibility element appears
123    * @return the deserialized instance
124    * @throws WSDLException if deserialization fails
125    */

126   protected PartnerLinkType.Role unmarshallRole(Element roleElem,
127       PartnerLinkType partnerLinkType, Definition def) {
128     PartnerLinkType.Role role = partnerLinkType.createRole();
129
130     // name attribute
131
String JavaDoc name = roleElem.getAttribute(WsdlConstants.ATTR_NAME);
132     role.setName(name);
133
134     // portType attribute
135
String JavaDoc portTypeName = roleElem.getAttribute(WsdlConstants.ATTR_PORT_TYPE);
136     QName JavaDoc portTypeQName = NodeUtil.getQName(portTypeName, roleElem);
137     
138     PortType portType = def.getPortType(portTypeQName);
139     if (portType == null) {
140       portType = def.createPortType();
141       portType.setQName(portTypeQName);
142       def.addPortType(portType);
143     }
144     role.setPortType(portType);
145     return role;
146   }
147
148   /**
149    * Serializes a {@link org.jbpm.bpel.wsdl.def.PartnerLinkType.Role} instance into the given
150    * {@link PrintWriter}.
151    * @param role the instance to serialize
152    * @param pw the stream to write in
153    * @param def the definition where the extensibility element appears
154    * @throws WSDLException if serialization fails
155    */

156   protected void marshallRole(PartnerLinkType.Role role, PrintWriter JavaDoc pw, Definition def)
157     throws WSDLException {
158     if (role == null)
159       return;
160     
161     // Open tag
162
String JavaDoc roleTag = DOMUtils.getQualifiedValue(WsdlConstants.NS_PLNK,
163         WsdlConstants.ELEM_ROLE, def);
164     pw.print(" <" + roleTag);
165
166     // name attribute
167
DOMUtils.printAttribute(WsdlConstants.ATTR_NAME, role.getName(), pw);
168
169     // port type attribute
170
DOMUtils.printQualifiedAttribute(WsdlConstants.ATTR_PORT_TYPE,
171         role.getPortType().getQName(), def, pw);
172     
173     // Close tag
174
pw.println("/>");
175   }
176 }
177
Popular Tags