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.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Collection;
24
25 import org.apache.commons.betwixt.ElementDescriptor;
26 import org.apache.commons.betwixt.XMLBeanInfo;
27 import org.apache.commons.betwixt.XMLIntrospector;
28
29 /***
30 * Model for top level element in an XML Schema
31 *
32 * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
33 * @version $Revision: 1.2.2.1 $
34 */
35 public class Schema {
36
37 private List elements = new ArrayList();
38 private List complexTypes = new ArrayList();
39 private List simpleTypes = new ArrayList();
40
41 private XMLIntrospector introspector;
42
43 public Schema() {
44 this(new XMLIntrospector());
45 }
46
47 public Schema(XMLIntrospector introspector) {
48 this.introspector = introspector;
49 }
50
51 /***
52 * Introspects the given type giving an <code>XMLBeanInfo</code>.
53 * @param type Class to introspect, not null
54 * @return <code>XMLBeanInfo</code>, not null
55 * @throws IntrospectionException
56 */
57 public XMLBeanInfo introspect(Class type) throws IntrospectionException {
58 return introspector.introspect(type);
59 }
60
61 /***
62 * Gets the complex types defined
63 * @return list of <code>ComplexType</code>'s not null
64 */
65 public List getComplexTypes() {
66 return complexTypes;
67 }
68
69
70 /***
71 * Adds a new complex type to those defined
72 * @param complexType not null
73 */
74 public void addComplexType(GlobalComplexType complexType) {
75 complexTypes.add(complexType);
76 }
77
78
79 /***
80 * Gets the elements definied
81 * @return list of <code>Element</code>s not null
82 */
83 public List getElements() {
84 return elements;
85 }
86
87 /***
88 * Adds a new element to those defined.
89 * @param element not null
90 */
91 public void addElement(GlobalElement element) {
92 elements.add(element);
93 }
94
95 /***
96 * Gets the simple types defined.
97 * @return list of <code>SimpleType</code>s not null
98 */
99 public List getSimpleTypes() {
100 return simpleTypes;
101 }
102
103 /***
104 * Adds a new simple type to those defined.
105 * @param simpleType
106 */
107 public void addSimpleType(SimpleType simpleType) {
108 simpleTypes.add(simpleType);
109 }
110
111
112 /***
113 * Adds global (top level) element and type declarations matching the given descriptor.
114 * @param elementDescriptor ElementDescriptor not null
115 */
116 public void addGlobalElementType(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor) throws IntrospectionException {
117
118
119 GlobalElement element = new GlobalElement(
120 elementDescriptor.getLocalName(),
121 elementDescriptor.getPropertyType().getName());
122 addElement(element);
123
124 GlobalComplexType type = new GlobalComplexType(configuration, elementDescriptor, this);
125 addComplexType(type);
126 }
127
128 public boolean equals(Object obj) {
129 boolean result = false;
130 if (obj instanceof Schema) {
131 Schema schema = (Schema) obj;
132 result =
133 equalContents(elements, schema.elements) &&
134 equalContents(complexTypes, schema.complexTypes) &&
135 equalContents(simpleTypes, schema.simpleTypes);
136 }
137 return result;
138 }
139
140 private boolean equalContents(Collection one, Collection two)
141 {
142
143 if (one.size() != two.size()) {
144 return false;
145 }
146 for (Iterator it=one.iterator();it.hasNext();) {
147 Object object = it.next();
148 if (!two.contains(object)) {
149 return false;
150 }
151 }
152 return true;
153 }
154
155 public int hashCode() {
156 return 0;
157 }
158
159
160
161 public String toString() {
162 StringBuffer buffer = new StringBuffer();
163 buffer.append("<?xml version='1.0'?>");
164 buffer.append("<xsd:schema xmlns:xsd='http://www.w3c.org/2001/XMLSchema'>");
165
166 for (Iterator it=simpleTypes.iterator(); it.hasNext();) {
167 buffer.append(it.next());
168 }
169
170 for (Iterator it=complexTypes.iterator(); it.hasNext();) {
171 buffer.append(it.next());
172 }
173
174
175 for (Iterator it=elements.iterator(); it.hasNext();) {
176 buffer.append(it.next());
177 }
178 buffer.append("</xsd:schema>");
179 return buffer.toString();
180 }
181 }