1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.betwixt.schema;
19
20 import java.beans.IntrospectionException;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import org.apache.commons.betwixt.ElementDescriptor;
27 import org.apache.commons.betwixt.XMLBeanInfo;
28 import org.apache.commons.betwixt.XMLIntrospector;
29
30
31
32
33
34
35
36 public class Schema {
37
38 private List elements = new ArrayList();
39 private List complexTypes = new ArrayList();
40 private List simpleTypes = new ArrayList();
41
42 private XMLIntrospector introspector;
43
44 public Schema() {
45 this(new XMLIntrospector());
46 }
47
48 public Schema(XMLIntrospector introspector) {
49 this.introspector = introspector;
50 }
51
52
53
54
55
56
57
58 public XMLBeanInfo introspect(Class type) throws IntrospectionException {
59 return introspector.introspect(type);
60 }
61
62
63
64
65
66 public List getComplexTypes() {
67 return complexTypes;
68 }
69
70
71
72
73
74
75 public void addComplexType(GlobalComplexType complexType) {
76 complexTypes.add(complexType);
77 }
78
79
80
81
82
83
84 public List getElements() {
85 return elements;
86 }
87
88
89
90
91
92 public void addElement(GlobalElement element) {
93 elements.add(element);
94 }
95
96
97
98
99
100 public List getSimpleTypes() {
101 return simpleTypes;
102 }
103
104
105
106
107
108 public void addSimpleType(SimpleType simpleType) {
109 simpleTypes.add(simpleType);
110 }
111
112
113
114
115
116
117 public void addGlobalElementType(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor) throws IntrospectionException {
118
119
120 GlobalElement element = new GlobalElement(
121 elementDescriptor.getLocalName(),
122 configuration.getSchemaTypeNamingStrategy().nameSchemaType(elementDescriptor));
123 addElement(element);
124 addGlobalComplexType(configuration, elementDescriptor);
125 }
126
127
128
129
130
131
132
133
134
135
136
137 public GlobalComplexType addGlobalComplexType(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor) throws IntrospectionException {
138 GlobalComplexType type = null;
139 for (Iterator it = complexTypes.iterator(); it.hasNext();) {
140 GlobalComplexType complexType = (GlobalComplexType) it.next();
141 if (complexType.matches( elementDescriptor )) {
142 type = complexType;
143 break;
144 }
145 }
146 if (type == null) {
147 type = new GlobalComplexType(configuration, elementDescriptor, this);
148 addComplexType(type);
149 type.fill(configuration, elementDescriptor, this);
150 }
151 return type;
152 }
153
154 public boolean equals(Object obj) {
155 boolean result = false;
156 if (obj instanceof Schema) {
157 Schema schema = (Schema) obj;
158 result =
159 equalContents(elements, schema.elements) &&
160 equalContents(complexTypes, schema.complexTypes) &&
161 equalContents(simpleTypes, schema.simpleTypes);
162 }
163 return result;
164 }
165
166 private boolean equalContents(Collection one, Collection two)
167 {
168
169 if (one.size() != two.size()) {
170 return false;
171 }
172 for (Iterator it=one.iterator();it.hasNext();) {
173 Object object = it.next();
174 if (!two.contains(object)) {
175 return false;
176 }
177 }
178 return true;
179 }
180
181 public int hashCode() {
182 return 0;
183 }
184
185
186
187 public String toString() {
188 StringBuffer buffer = new StringBuffer();
189 buffer.append("<?xml version='1.0'?>");
190 buffer.append("<xsd:schema xmlns:xsd='http://www.w3c.org/2001/XMLSchema'>");
191
192 for (Iterator it=simpleTypes.iterator(); it.hasNext();) {
193 buffer.append(it.next());
194 }
195
196 for (Iterator it=complexTypes.iterator(); it.hasNext();) {
197 buffer.append(it.next());
198 }
199
200
201 for (Iterator it=elements.iterator(); it.hasNext();) {
202 buffer.append(it.next());
203 }
204 buffer.append("</xsd:schema>");
205 return buffer.toString();
206 }
207 }