KickJava   Java API By Example, From Geeks To Geeks.

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


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.Message;
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.data.def.Snippet;
21 import org.jbpm.bpel.wsdl.def.Property;
22 import org.jbpm.bpel.wsdl.def.PropertyAlias;
23 import org.jbpm.bpel.wsdl.util.WsdlUtil;
24 import org.jbpm.bpel.xml.BpelConstants;
25 import org.jbpm.bpel.xml.util.NodeUtil;
26
27 /**
28  * Translates between <code>&lt;propertyAlias/&gt;</code> XML elements and
29  * {@link PropertyAlias} instances.
30  * @author Alejandro Guízar
31  * @version $Revision: 1.5 $ $Date: 2005/06/23 02:22:47 $
32  */

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

49   public ExtensibilityElement unmarshall(Class JavaDoc parentType, QName JavaDoc elementType,
50     Element elem, Definition def, ExtensionRegistry extReg) throws WSDLException {
51     PropertyAlias alias = (PropertyAlias) extReg.createExtension(parentType, elementType);
52
53     // property attribute - required
54
String JavaDoc prefixedName = elem.getAttribute(WsdlConstants.ATTR_PROPERTY_NAME);
55     QName JavaDoc qualifiedName = NodeUtil.getQName(prefixedName, elem);
56     Property property = WsdlUtil.getProperty(def, qualifiedName);
57     if (property == null) {
58       property = (Property) extReg.createExtension(Definition.class, WsdlConstants.Q_PROPERTY);
59       property.setQName(qualifiedName);
60       def.addExtensibilityElement(property);
61     }
62     alias.setProperty(property);
63     
64     // message attribute - required
65
prefixedName = elem.getAttribute(WsdlConstants.ATTR_MESSAGE_TYPE);
66     qualifiedName = NodeUtil.getQName(prefixedName, elem);
67     Message message = def.getMessage(qualifiedName);
68     if (message == null) {
69       message = def.createMessage();
70       message.setQName(qualifiedName);
71       def.addMessage(message);
72     }
73     alias.setMessage(message);
74     
75     // wsdl:required attribute
76
String JavaDoc required = DOMUtils.getAttributeNS(elem, Constants.NS_URI_WSDL, Constants.ATTR_REQUIRED);
77     if (required != null) {
78       alias.setRequired(Boolean.valueOf(required));
79     }
80
81     // query element
82
Element queryElem = DOMUtils.getFirstChildElement(elem);
83     if (queryElem != null) {
84       alias.setQuery(unmarshallQuery(queryElem));
85     }
86     
87     return alias;
88   }
89
90   /**
91    * Serializes a {@link PropertyAlias} instance into the given
92    * {@link PrintWriter}.
93    * @param parentType class object indicating where in the WSDL document
94    * this extensibility element was encountered
95    * @param elementType the qname of the extensibility element
96    * @param extension the instance to serialize
97    * @param pw the stream to write in
98    * @param def the definition this extensibility element was encountered in
99    * @param extReg the extension registry to use (if needed again)
100    * @throws WSDLException if serialization fails
101    */

102   public void marshall(Class JavaDoc parentType, QName JavaDoc elementType,
103     ExtensibilityElement extension, PrintWriter JavaDoc pw, Definition def,
104     ExtensionRegistry extReg) throws WSDLException {
105     if (extension == null) {
106       return;
107     }
108     PropertyAlias alias = (PropertyAlias) extension;
109     // open tag
110
String JavaDoc tagName = DOMUtils.getQualifiedValue(BpelConstants.NS_BPWS,
111         WsdlConstants.ELEM_PROPERTY_ALIAS, def);
112     pw.print(" <" + tagName);
113     // property attribute
114
DOMUtils.printQualifiedAttribute(WsdlConstants.ATTR_PROPERTY_NAME,
115       alias.getProperty().getQName(), def, pw);
116     // message type attribute
117
DOMUtils.printQualifiedAttribute(WsdlConstants.ATTR_MESSAGE_TYPE,
118       alias.getMessage().getQName(), def, pw);
119     // wsdl:required attribute
120
Boolean JavaDoc required = alias.getRequired();
121     if (required != null) {
122       DOMUtils.printQualifiedAttribute(Constants.Q_ATTR_REQUIRED, required.toString(), def, pw);
123     }
124     Snippet query = alias.getQuery();
125     if (query != null) {
126       pw.println('>');
127       // query element
128
marshallQuery(alias.getQuery(), pw, def);
129       // close tag
130
pw.println(" </" + tagName + '>');
131     }
132     else {
133       pw.println("/>");
134     }
135   }
136
137   /**
138    * Deserializes a DOM element into a {@link Snippet} instance.
139    * @param queryElem the element to deserialize
140    * @return the deserialized instance
141    */

142   protected Snippet unmarshallQuery(Element queryElem) {
143     Snippet query = new Snippet();
144     // language attribute
145
query.setLanguage(DOMUtils.getAttribute(queryElem, WsdlConstants.ATTR_QUERY_LANGUAGE));
146     // snippet cdata
147
query.setText(DOMUtils.getChildCharacterData(queryElem));
148     // namespace declarations
149
query.setNamespaces(NodeUtil.getNamespaceDeclarations(queryElem));
150     // use
151
query.setUse(Snippet.Use.PROPERTY_ALIAS);
152     return query;
153   }
154
155   /**
156    * Serializes a {@link Snippet} instance into the given {@link PrintWriter}.
157    * @param query the instance to serialize
158    * @param pw the stream to write in
159    * @param def the definition where the extensibility element appears
160    * @throws WSDLException if serialization fails
161    */

162   protected void marshallQuery(Snippet query, PrintWriter JavaDoc pw, Definition def) throws WSDLException {
163     // open tag
164
String JavaDoc queryTag = DOMUtils.getQualifiedValue(BpelConstants.NS_BPWS, WsdlConstants.ELEM_QUERY, def);
165     pw.print(" <" + queryTag);
166     // language attribute
167
DOMUtils.printAttribute(WsdlConstants.ATTR_QUERY_LANGUAGE, query.getLanguage(), pw);
168     pw.print('>');
169     // code cdata
170
pw.print(DOMUtils.cleanString(query.getText()));
171     // close tag
172
pw.println("</" + queryTag + '>');
173   }
174 }
175
Popular Tags