KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > deployment > BeanXMLMetaData


1 /**
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.webservice.deployment;
8
9 // $Id: BeanXMLMetaData.java,v 1.1.2.7 2005/06/15 22:59:29 jgreene Exp $
10

11 import java.io.PrintWriter JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import javax.xml.namespace.QName JavaDoc;
18
19 import org.jboss.axis.utils.LinkedHashMap;
20 import org.jboss.webservice.util.DOMUtils;
21 import org.w3c.dom.Element JavaDoc;
22 import org.w3c.dom.NodeList JavaDoc;
23
24 /**
25  * Additional bean serialization meta data.
26  *
27  * Here is an example:
28  *
29  * <typeMapping qname='ns1:SequenceStruct2'
30  * xmlns:ns1='http://MarshallTest.org/xsd'
31  * type='java:org.jboss.ws4ee.test.marshalltest.rpcenc.SequenceStruct2'
32  * serializer='org.jboss.webservice.encoding.ser.MetaDataBeanSerializerFactory'
33  * deserializer='org.jboss.webservice.encoding.ser.MetaDataBeanDeserializerFactory'
34  * encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'> <typeDesc>
35  * <elementDesc fieldName="varBase64Binary" xmlName="varBase64Binary"
36  * xmlType="xsd:base64Binary"/> <elementDesc fieldName="varHexBinary"
37  * xmlName="varHexBinary" xmlType="xsd:hexBinary"/> <elementDesc
38  * fieldName="varSoapString" xmlName="varSoapString" xmlType="soapenc:string"/>
39  * <elementDesc fieldName="varSoapBoolean" xmlName="varSoapBoolean"
40  * xmlType="soapenc:boolean"/> <elementDesc fieldName="varSoapFloat"
41  * xmlName="varSoapFloat" xmlType="soapenc:float"/> <elementDesc
42  * fieldName="varSoapDouble" xmlName="varSoapDouble" xmlType="soapenc:double"/>
43  * <elementDesc fieldName="varSoapDecimal" xmlName="varSoapDecimal"
44  * xmlType="soapenc:decimal"/> <elementDesc fieldName="varSoapInt"
45  * xmlName="varSoapInt" xmlType="soapenc:int"/> <elementDesc
46  * fieldName="varSoapShort" xmlName="varSoapShort" xmlType="soapenc:short"/>
47  * <elementDesc fieldName="varSoapByte" xmlName="varSoapByte"
48  * xmlType="soapenc:byte"/> <elementDesc fieldName="varSoapBase64"
49  * xmlName="varSoapBase64" xmlType="soapenc:base64"/> <elementDesc
50  * fieldName="varDateTimeArray" xmlName="varDateTimeArray"
51  * itemXmlType="xsd:datetime"/> <elementOrder> <element name="varString"/>
52  * <element name="varInteger"/> <element name="varInt"/> <element
53  * name="varLong"/> <element name="varShort"/> <element name="varDecimal"/>
54  * <element name="varFloat"/> <element name="varDouble"/> <element
55  * name="varBoolean"/> <element name="varByte"/> <element name="varQName"/>
56  * <element name="varDateTime"/> <element name="varSoapString"/> <element
57  * name="varSoapBoolean"/> <element name="varSoapFloat"/> <element
58  * name="varSoapDouble"/> <element name="varSoapDecimal"/> <element
59  * name="varSoapInt"/> <element name="varSoapShort"/> <element
60  * name="varSoapByte"/> <element name="varBase64Binary"/> <element
61  * name="varHexBinary"/> <element name="varSoapBase64"/> <element
62  * name="varSequenceStruct"/> <element name="varDateTimeArray"/> </elementOrder>
63  * </typeDesc> </typeMapping>
64  *
65  * @author thomas.diesler@jboss.org
66  * @since 18-June-2004
67  */

68 public class BeanXMLMetaData
69 {
70    private Map JavaDoc elementDescMap = new LinkedHashMap();
71
72    private List JavaDoc elementOrderList = new ArrayList JavaDoc();
73
74    // Hide constructoer
75
private BeanXMLMetaData()
76    {
77    }
78
79    /**
80     * Parse the bean mets data from XML
81     */

82    public static BeanXMLMetaData parse(Element JavaDoc metaData)
83    {
84       BeanXMLMetaData beanMetaData = new BeanXMLMetaData();
85
86       if (metaData != null)
87       {
88          ArrayList JavaDoc qnames = new ArrayList JavaDoc();
89
90          // Parse element descriptions
91
NodeList JavaDoc nlistElementDesc = metaData.getElementsByTagName("elementDesc");
92          for (int i = 0; i < nlistElementDesc.getLength(); i++)
93          {
94             Element JavaDoc elDesc = (Element JavaDoc)nlistElementDesc.item(i);
95             String JavaDoc fieldName = DOMUtils.getAttributeValue(elDesc, "fieldName");
96
97             QName JavaDoc xmlName = DOMUtils.getAttributeValueAsQName(elDesc, "xmlName");
98             if (xmlName == null)
99                xmlName = new QName JavaDoc("", fieldName);
100
101             QName JavaDoc xmlType = DOMUtils.getAttributeValueAsQName(elDesc, "xmlType");
102             if (xmlType != null)
103             {
104                if (qnames.contains(xmlType))
105                   throw new IllegalStateException JavaDoc("Duplicate type mapping definition: " + xmlType);
106
107                qnames.add(xmlType);
108             }
109
110             ElementMetaData elMetaData = new ElementMetaData(fieldName, xmlName, xmlType);
111             beanMetaData.elementDescMap.put(fieldName, elMetaData);
112
113             boolean asAttribute = DOMUtils.getAttributeValueAsBoolean(elDesc, "asAttr");
114             elMetaData.setAsAttribute(asAttribute);
115
116             boolean asContent = DOMUtils.getAttributeValueAsBoolean(elDesc, "asContent");
117             elMetaData.setAsContent(asContent);
118
119             Integer JavaDoc minOccurs = DOMUtils.getAttributeValueAsInteger(elDesc, "minOccurs");
120             elMetaData.setMinOccurs(minOccurs);
121
122             QName JavaDoc itemXmlType = DOMUtils.getAttributeValueAsQName(elDesc, "itemXmlType");
123             elMetaData.setItemXmlType(itemXmlType);
124          }
125
126          // Parse element order
127
Element JavaDoc orderEl = DOMUtils.getFirstChildElement(metaData, "elementOrder");
128          if (orderEl != null)
129          {
130             // copy the BeanPropertyDescriptors according to the meta data
131
// elementOrder
132
NodeList JavaDoc nlistElementOrder = orderEl.getElementsByTagName("element");
133             for (int i = 0; i < nlistElementOrder.getLength(); i++)
134             {
135                Element JavaDoc el = (Element JavaDoc)nlistElementOrder.item(i);
136                String JavaDoc fieldName = el.getAttribute("name");
137                beanMetaData.elementOrderList.add(fieldName);
138             }
139          }
140       }
141
142       return beanMetaData;
143    }
144
145    public Iterator JavaDoc getElementMetaData()
146    {
147       return elementDescMap.values().iterator();
148    }
149
150    public Iterator JavaDoc getElementOrder()
151    {
152       return elementOrderList.iterator();
153    }
154
155    public void serializeAsXML(PrintWriter JavaDoc out)
156    {
157       String JavaDoc pad = " ";
158       out.println(pad + "<typeDesc>");
159       Iterator JavaDoc itElDesc = elementDescMap.values().iterator();
160       while (itElDesc.hasNext())
161       {
162          ElementMetaData elMetaData = (ElementMetaData)itElDesc.next();
163          String JavaDoc prefixedXmlName = elMetaData.getPrefixedXmlName();
164          out.print(pad + pad + "<elementDesc fieldName='" + elMetaData.getFieldName() + "' xmlName='" + prefixedXmlName + "'");
165
166          if (elMetaData.getXmlType() != null)
167          {
168             String JavaDoc prefixedXmlType = elMetaData.getPrefixedXmlType();
169             out.print(" xmlType='" + prefixedXmlType + "'");
170          }
171
172          if (elMetaData.isAsAttribute())
173             out.print(" asAttr='true'");
174
175          if (elMetaData.isAsContent())
176             out.print(" asContent='true'");
177
178          if (elMetaData.getMinOccurs() != null)
179             out.print(" minOccurs='" + elMetaData.getMinOccurs() + "'");
180
181          if (elMetaData.getItemXmlType() != null)
182             out.print(" itemXmlType='" + elMetaData.getPrefixedItemXmlType() + "'");
183
184          out.println("/>");
185       }
186
187       if (elementOrderList.size() > 0)
188       {
189          out.println(pad + pad + "<elementOrder>");
190          Iterator JavaDoc itElOrder = elementOrderList.iterator();
191          while (itElOrder.hasNext())
192          {
193             String JavaDoc fieldName = (String JavaDoc)itElOrder.next();
194             out.println(pad + pad + pad + "<element name='" + fieldName + "'/>");
195          }
196          out.println(pad + pad + "</elementOrder>");
197       }
198       out.println(pad + "</typeDesc>");
199    }
200
201    /**
202     * An XML element description
203     */

204    public static class ElementMetaData
205    {
206
207       private String JavaDoc fieldName;
208
209       private QName JavaDoc xmlName;
210
211       private QName JavaDoc xmlType;
212
213       private QName JavaDoc itemXmlType;
214
215       private boolean asAttribute;
216
217       private boolean asContent;
218
219       private Integer JavaDoc minOccurs;
220
221       public ElementMetaData(String JavaDoc fieldName, QName JavaDoc xmlName, QName JavaDoc xmlType)
222       {
223          this.fieldName = fieldName;
224          this.xmlName = xmlName;
225          this.xmlType = xmlType;
226       }
227
228       public String JavaDoc getFieldName()
229       {
230          return fieldName;
231       }
232
233       public QName JavaDoc getXmlName()
234       {
235          return xmlName;
236       }
237
238       public String JavaDoc getPrefixedXmlName()
239       {
240          String JavaDoc retStr = null;
241          if (xmlName != null)
242          {
243             retStr = xmlName.getLocalPart();
244             if (xmlName.getPrefix().length() > 0)
245                retStr = xmlName.getPrefix() + ":" + retStr;
246          }
247          return retStr;
248       }
249
250       public QName JavaDoc getXmlType()
251       {
252          return xmlType;
253       }
254
255       public String JavaDoc getPrefixedXmlType()
256       {
257          String JavaDoc retStr = null;
258          if (xmlType != null)
259          {
260             retStr = xmlType.getLocalPart();
261             if (xmlType.getPrefix().length() > 0)
262                retStr = xmlType.getPrefix() + ":" + retStr;
263          }
264          return retStr;
265       }
266
267       public boolean isAsAttribute()
268       {
269          return asAttribute;
270       }
271
272       public void setAsAttribute(boolean asAttribute)
273       {
274          this.asAttribute = asAttribute;
275       }
276
277       public boolean isAsContent()
278       {
279          return asContent;
280       }
281
282       public void setAsContent(boolean asContent)
283       {
284          this.asContent = asContent;
285       }
286
287       public Integer JavaDoc getMinOccurs()
288       {
289          return minOccurs;
290       }
291
292       public void setMinOccurs(Integer JavaDoc minOccurs)
293       {
294          this.minOccurs = minOccurs;
295       }
296
297       public QName JavaDoc getItemXmlType()
298       {
299          return itemXmlType;
300       }
301
302       public String JavaDoc getPrefixedItemXmlType()
303       {
304          String JavaDoc retStr = null;
305          if (itemXmlType != null)
306          {
307             retStr = itemXmlType.getLocalPart();
308             if (itemXmlType.getPrefix().length() > 0)
309                retStr = itemXmlType.getPrefix() + ":" + retStr;
310          }
311          return retStr;
312       }
313
314       /**
315        * Solves mapping problems were a nested type in an array can not be properly determined
316        */

317       public void setItemXmlType(QName JavaDoc itemXmlType)
318       {
319          this.itemXmlType = itemXmlType;
320       }
321    }
322 }
323
Popular Tags