KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > schema > Schema


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.betwixt.schema;
18
19 import java.beans.IntrospectionException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Collection JavaDoc;
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 JavaDoc elements = new ArrayList JavaDoc();
38     private List JavaDoc complexTypes = new ArrayList JavaDoc();
39     private List JavaDoc simpleTypes = new ArrayList JavaDoc();
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 JavaDoc type) throws IntrospectionException JavaDoc {
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc {
117         // need to create a global element declaration and a complex type
118
// use the fully qualified class name as the type name
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 JavaDoc 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 JavaDoc one, Collection JavaDoc two)
141     {
142         // doesn't check cardinality but should be ok
143
if (one.size() != two.size()) {
144             return false;
145         }
146         for (Iterator JavaDoc it=one.iterator();it.hasNext();) {
147             Object JavaDoc 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 JavaDoc toString() {
162         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
163         buffer.append("<?xml version='1.0'?>");
164         buffer.append("<xsd:schema xmlns:xsd='http://www.w3c.org/2001/XMLSchema'>");
165         
166         for (Iterator JavaDoc it=simpleTypes.iterator(); it.hasNext();) {
167               buffer.append(it.next());
168         }
169         
170         for (Iterator JavaDoc it=complexTypes.iterator(); it.hasNext();) {
171               buffer.append(it.next());
172         }
173         
174   
175         for (Iterator JavaDoc it=elements.iterator(); it.hasNext();) {
176               buffer.append(it.next());
177         }
178         buffer.append("</xsd:schema>");
179         return buffer.toString();
180     }
181 }
182
Popular Tags