1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt.schema;
18
19 import java.beans.IntrospectionException;
20 import java.util.Iterator;
21 import java.util.Collection;
22
23 import org.apache.commons.betwixt.ElementDescriptor;
24
25 /***
26 * Models a <code>complexType</code> from an XML schema.
27 * A complex type may contain element content and may have attributes.
28 * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
29 * @version $Revision: 190515 $
30 */
31 public class GlobalComplexType extends ComplexType {
32
33 private String name;
34
35 public GlobalComplexType() {}
36
37 /***
38 * Constructs a new ComplexType from the descriptor given.
39 * @param elementDescriptor
40 */
41 public GlobalComplexType(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException {
42 super(configuration, elementDescriptor, schema);
43 }
44
45 protected void init(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException {
46 setName(nameFromDescriptor( elementDescriptor ));
47 }
48
49 /***
50 * Fills the complex type description.
51 * @since 0.7
52 * @param configuration
53 * @param elementDescriptor
54 * @param schema
55 * @throws IntrospectionException
56 */
57 protected void fill(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException
58 {
59 elementDescriptor = fillDescriptor(elementDescriptor, schema);
60 super.init(configuration, elementDescriptor, schema);
61 }
62
63 private String nameFromDescriptor( ElementDescriptor elementDescriptor ) {
64 return elementDescriptor.getPropertyType().getName();
65 }
66
67 /***
68 * Does the given element descriptor match this complex type?
69 * @since 0.7
70 * @param elementDescriptor
71 * @return true if the descriptor matches
72 */
73 public boolean matches(ElementDescriptor elementDescriptor) {
74 String nameFromDescriptor = nameFromDescriptor ( elementDescriptor );
75 return nameFromDescriptor.equals(getName());
76 }
77
78 /***
79 * Gets the name of this type.
80 * @return the name of this type
81 */
82 public String getName() {
83 return name;
84 }
85
86 /***
87 * Sets the name of this type.
88 * @param string
89 */
90 public void setName(String string) {
91 name = string;
92 }
93
94 public boolean equals(Object obj) {
95 boolean result = false;
96 if (obj instanceof GlobalComplexType) {
97 GlobalComplexType complexType = (GlobalComplexType) obj;
98 result = isEqual(name, complexType.name) &&
99 equalContents(attributes, complexType.attributes) &&
100 equalContents(elements, complexType.elements);
101
102 }
103 return result;
104 }
105
106 public int hashCode() {
107 return 0;
108 }
109
110 private boolean equalContents(Collection one, Collection two)
111 {
112
113 if (one.size() != two.size()) {
114 return false;
115 }
116 for (Iterator it=one.iterator();it.hasNext();) {
117 Object object = it.next();
118 if (!two.contains(object)) {
119 return false;
120 }
121 }
122 return true;
123 }
124
125 /***
126 * Null safe equals method
127 * @param one
128 * @param two
129 * @return
130 */
131 private boolean isEqual(String one, String two) {
132 boolean result = false;
133 if (one == null) {
134 result = (two == null);
135 }
136 else
137 {
138 result = one.equals(two);
139 }
140
141 return result;
142 }
143
144 public String toString() {
145 StringBuffer buffer = new StringBuffer();
146 buffer.append("<xsd:complexType name='");
147 buffer.append(name);
148 buffer.append("'>");
149 buffer.append("<xsd:sequence>");
150 for (Iterator it=elements.iterator(); it.hasNext();) {
151 buffer.append(it.next());
152 }
153 buffer.append("</xsd:sequence>");
154
155 for (Iterator it=attributes.iterator(); it.hasNext();) {
156 buffer.append(it.next());
157 }
158 buffer.append("</xsd:complexType>");
159 return buffer.toString();
160 }
161 }