001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.betwixt.schema;
019:
020: import java.beans.IntrospectionException;
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import org.apache.commons.betwixt.ElementDescriptor;
027: import org.apache.commons.betwixt.XMLBeanInfo;
028: import org.apache.commons.betwixt.XMLIntrospector;
029:
030: /**
031: * Model for top level element in an XML Schema
032: *
033: * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
034: * @version $Revision: 438373 $
035: */
036: public class Schema {
037:
038: private List elements = new ArrayList();
039: private List complexTypes = new ArrayList();
040: private List simpleTypes = new ArrayList();
041:
042: private XMLIntrospector introspector;
043:
044: public Schema() {
045: this (new XMLIntrospector());
046: }
047:
048: public Schema(XMLIntrospector introspector) {
049: this .introspector = introspector;
050: }
051:
052: /**
053: * Introspects the given type giving an <code>XMLBeanInfo</code>.
054: * @param type Class to introspect, not null
055: * @return <code>XMLBeanInfo</code>, not null
056: * @throws IntrospectionException
057: */
058: public XMLBeanInfo introspect(Class type)
059: throws IntrospectionException {
060: return introspector.introspect(type);
061: }
062:
063: /**
064: * Gets the complex types defined
065: * @return list of <code>ComplexType</code>'s not null
066: */
067: public List getComplexTypes() {
068: return complexTypes;
069: }
070:
071: /**
072: * Adds a new complex type to those defined
073: * @param complexType not null
074: */
075: public void addComplexType(GlobalComplexType complexType) {
076: complexTypes.add(complexType);
077: }
078:
079: /**
080: * Gets the elements definied
081: * @return list of <code>Element</code>s not null
082: */
083: public List getElements() {
084: return elements;
085: }
086:
087: /**
088: * Adds a new element to those defined.
089: * @param element not null
090: */
091: public void addElement(GlobalElement element) {
092: elements.add(element);
093: }
094:
095: /**
096: * Gets the simple types defined.
097: * @return list of <code>SimpleType</code>s not null
098: */
099: 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: * Adds global (top level) element and type declarations matching the given descriptor.
113: * @param elementDescriptor ElementDescriptor not null
114: */
115: public void addGlobalElementType(
116: TranscriptionConfiguration configuration,
117: ElementDescriptor elementDescriptor)
118: throws IntrospectionException {
119: // need to create a global element declaration and a complex type
120: // use the fully qualified class name as the type name
121: GlobalElement element = new GlobalElement(elementDescriptor
122: .getLocalName(), configuration
123: .getSchemaTypeNamingStrategy().nameSchemaType(
124: elementDescriptor));
125: addElement(element);
126: addGlobalComplexType(configuration, elementDescriptor);
127: }
128:
129: /**
130: * Adds a new global complex type definition matching the given element descriptor.
131: * If this element descriptor has already been mapped to a global type then
132: * that is returned.
133: * @since 0.7
134: * @param configuration <code>TranscriptionConfiguration</code>, not null
135: * @param elementDescriptor <code>ElementDescriptor</code>, not null
136: * @return <code>GlobalComplexType</code>
137: * @throws IntrospectionException
138: */
139: public GlobalComplexType addGlobalComplexType(
140: TranscriptionConfiguration configuration,
141: ElementDescriptor elementDescriptor)
142: throws IntrospectionException {
143: GlobalComplexType type = null;
144: for (Iterator it = complexTypes.iterator(); it.hasNext();) {
145: GlobalComplexType complexType = (GlobalComplexType) it
146: .next();
147: if (complexType.matches(elementDescriptor)) {
148: type = complexType;
149: break;
150: }
151: }
152: if (type == null) {
153: type = new GlobalComplexType(configuration,
154: elementDescriptor, this );
155: addComplexType(type);
156: type.fill(configuration, elementDescriptor, this );
157: }
158: return type;
159: }
160:
161: public boolean equals(Object obj) {
162: boolean result = false;
163: if (obj instanceof Schema) {
164: Schema schema = (Schema) obj;
165: result = equalContents(elements, schema.elements)
166: && equalContents(complexTypes, schema.complexTypes)
167: && equalContents(simpleTypes, schema.simpleTypes);
168: }
169: return result;
170: }
171:
172: private boolean equalContents(Collection one, Collection two) {
173: // doesn't check cardinality but should be ok
174: if (one.size() != two.size()) {
175: return false;
176: }
177: for (Iterator it = one.iterator(); it.hasNext();) {
178: Object object = it.next();
179: if (!two.contains(object)) {
180: return false;
181: }
182: }
183: return true;
184: }
185:
186: public int hashCode() {
187: return 0;
188: }
189:
190: public String toString() {
191: StringBuffer buffer = new StringBuffer();
192: buffer.append("<?xml version='1.0'?>");
193: buffer
194: .append("<xsd:schema xmlns:xsd='http://www.w3c.org/2001/XMLSchema'>");
195:
196: for (Iterator it = simpleTypes.iterator(); it.hasNext();) {
197: buffer.append(it.next());
198: }
199:
200: for (Iterator it = complexTypes.iterator(); it.hasNext();) {
201: buffer.append(it.next());
202: }
203:
204: for (Iterator it = elements.iterator(); it.hasNext();) {
205: buffer.append(it.next());
206: }
207: buffer.append("</xsd:schema>");
208: return buffer.toString();
209: }
210: }
|