KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > remote > soap > ComplexType


1 /* *****************************************************************************
2  * ComplexType.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.remote.soap;
11
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import javax.xml.namespace.QName JavaDoc;
16
17 /**
18  * This class represents a WSDL Schema type.
19  */

20 public class ComplexType
21 {
22     /** Unknown type */
23     final static public int TYPE_UNKNOWN = 0;
24
25     /** Simple type */
26     final static public int TYPE_SIMPLE = 1;
27
28     /** Struct/Complex type */
29     final static public int TYPE_STRUCT = 2;
30
31     /** Array type */
32     final static public int TYPE_ARRAY = 3;
33
34
35     /**
36      * The QName of this ComplexType.
37      */

38     QName JavaDoc mName;
39
40     /**
41      * The type of this ComplexType. One of TYPE_UNKNOWN, TYPE_SIMPLE,
42      * TYPE_STRUCT, TYPE_ARRAY.
43      */

44     int mType = TYPE_UNKNOWN;
45
46     /**
47      * If mType == TYPE_ARRAY, mArrayItemType is set to the actual
48      */

49     ComplexType mArrayItemType = null;
50
51     /**
52      * Members of struct. Null if this is a simple type or array.
53      */

54     Map JavaDoc mMembers = null;
55
56     // TODO [2005-05-15 pkang]: find out if we really need this? -pk
57
/** The order of the members. Used when order of members matters, as defined
58         in WSDL schema. */

59     List JavaDoc mMemberSequence = null;
60
61     /**
62      * Base of this ComplexType. Null if this is a simple type or a struct that
63      * doesn't extend. This should always be set for arrays.
64      */

65     ComplexType mBase = null;
66
67
68     /**
69      * Constructor to create a simple type.
70      * @param name QName of this simple type.
71      */

72     public ComplexType(QName JavaDoc name) {
73         this(name, false);
74     }
75
76     /**
77      * Constructor to create complex types (structs).
78      * @param name QName of this ComplexType.
79      * @param members Map of members that belong to this complex type.
80      */

81     public ComplexType(QName JavaDoc name, Map JavaDoc members) {
82         mName = name;
83         mType = TYPE_STRUCT;
84         setMembers(members);
85     }
86
87     /**
88      * Constructor to create arrays or simple type.
89      *
90      * @param name QName of this ComplexType.
91      * @param isArray if true, complex type is array type, else simple type.
92      */

93     public ComplexType(QName JavaDoc name, boolean isArray) {
94         mName = name;
95         mType = (isArray ? TYPE_ARRAY : TYPE_SIMPLE);
96     }
97
98     /**
99      * @return true if this is an array.
100      */

101     public boolean isArray() {
102         return mType == TYPE_ARRAY;
103     }
104
105     /**
106      * @return true if this is a struct (complex type).
107      */

108     public boolean isComplex() {
109         return mType == TYPE_STRUCT;
110     }
111
112     /**
113      * @return QName for this complext type.
114      */

115     public QName JavaDoc getName() {
116         return mName;
117     }
118
119     /**
120      * @return an integer representing the complex type. See TYPE_UNKNOWN,
121      * TYPE_SIMPLE, TYPE_STRUCT, TYPE_ARRAY.
122      */

123     public int getType() {
124         return mType;
125     }
126
127     /**
128      * @return a string representing the complex type. One of simple, struct,
129      * array, or unknown.
130      */

131     public String JavaDoc getTypeString() {
132         switch (mType) {
133         case TYPE_SIMPLE: return "simple";
134         case TYPE_STRUCT: return "struct";
135         case TYPE_ARRAY: return "array";
136         default:
137             return "unknown";
138         }
139     }
140
141     /**
142      * @return QName for array type.
143      */

144     public QName JavaDoc getArrayItemTypeQName() {
145         return mArrayItemType != null ? mArrayItemType.getName() : null;
146     }
147
148     /**
149      * @return map of complex type members; null for arrays or simple types.
150      */

151     public Map JavaDoc getMembers() {
152         return mMembers;
153     }
154
155     /**
156      * Set the base type for this complex type. Should always be called by
157      * arrays or for complex types that extend a base type.
158      */

159     public void setBase(ComplexType base) {
160         mBase = base;
161     }
162
163     /**
164      * @return the base type. For arrays, it will be always be soapenc:Array
165      * (soapenc namespace may vary). Simple types and complex types that don't
166      * extend will return null
167      */

168     public ComplexType getBase() {
169         return mBase;
170     }
171
172     /**
173      * @return the item type for this array, or null if this isn't an array.
174      */

175     public ComplexType getArrayItemType() {
176         return mArrayItemType;
177     }
178
179     /**
180      * This should only be called by array types.
181      * @param arrayItemType item type of array.
182      */

183     public void setArrayItemType(ComplexType arrayItemType) {
184         mArrayItemType = arrayItemType;
185     }
186
187
188     /**
189      * @param members values of members are QName.
190      */

191     public void setMembers(Map JavaDoc members) {
192         mMembers = members;
193     }
194
195     /**
196      * @param members values of members are QName.
197      * @param sequence order of members.
198      */

199     public void setMembers(Map JavaDoc members, List JavaDoc sequence) {
200         mMembers = members;
201         mMemberSequence = sequence;
202     }
203
204
205     /**
206      * Appends member information to buffer.
207      */

208     void toMembersXML(StringBuffer JavaDoc sb) {
209         if (mMembers != null) {
210             sb.append("<members");
211             if (mBase != null) {
212                 sb.append(" extends=\"").append(mBase.mName).append("\"");
213             }
214             sb.append(">");
215
216             Iterator JavaDoc iter = mMembers.entrySet().iterator();
217             while (iter.hasNext()) {
218                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
219                 String JavaDoc key = (String JavaDoc)entry.getKey();
220                 QName JavaDoc qname =(QName JavaDoc)entry.getValue();
221                 sb.append("<member")
222                     .append(" name=\"").append(key).append("\"")
223                     .append(" type=\"").append(qname).append("\"")
224                     .append("/>");
225             }
226
227             sb.append("</members>");
228         }
229     }
230
231
232     /**
233      * Append XML ComplexType information to buffer.
234      */

235     public void toXML(StringBuffer JavaDoc sb) {
236         sb.append("<complex-type")
237             .append(" name=\"").append(mName).append("\"")
238             .append(" type=\"").append(getTypeString()).append("\"")
239             .append(" base=\"").append(mBase == null ? "" : mBase.mName.toString()).append("\"");
240         if (mType == TYPE_ARRAY) {
241             sb.append(" array-type=\"").append(mArrayItemType.getName()).append("\"");
242         }
243         sb.append(">");
244         toMembersXML(sb);
245         sb.append("</complex-type>");
246     }
247 }
248
Popular Tags