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.Collection;
022: import java.util.Iterator;
023:
024: import org.apache.commons.betwixt.ElementDescriptor;
025:
026: /**
027: * Models a <code>complexType</code> from an XML schema.
028: * A complex type may contain element content and may have attributes.
029: * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
030: * @version $Revision: 438373 $
031: */
032: public class GlobalComplexType extends ComplexType {
033:
034: private String name;
035: private TranscriptionConfiguration configuration;
036:
037: public GlobalComplexType() {
038: }
039:
040: /**
041: * Constructs a new ComplexType from the descriptor given.
042: * @param elementDescriptor
043: */
044: public GlobalComplexType(TranscriptionConfiguration configuration,
045: ElementDescriptor elementDescriptor, Schema schema)
046: throws IntrospectionException {
047: super (configuration, elementDescriptor, schema);
048: }
049:
050: protected void init(TranscriptionConfiguration configuration,
051: ElementDescriptor elementDescriptor, Schema schema)
052: throws IntrospectionException {
053: this .configuration = configuration;
054: setName(nameFromDescriptor(elementDescriptor));
055: }
056:
057: /**
058: * Fills the complex type description.
059: * @since 0.7
060: * @param configuration
061: * @param elementDescriptor
062: * @param schema
063: * @throws IntrospectionException
064: */
065: protected void fill(TranscriptionConfiguration configuration,
066: ElementDescriptor elementDescriptor, Schema schema)
067: throws IntrospectionException {
068: elementDescriptor = fillDescriptor(elementDescriptor, schema);
069: super .init(configuration, elementDescriptor, schema);
070: }
071:
072: private String nameFromDescriptor(
073: ElementDescriptor elementDescriptor) {
074: return configuration.getSchemaTypeNamingStrategy()
075: .nameSchemaType(elementDescriptor);
076: }
077:
078: /**
079: * Does the given element descriptor match this complex type?
080: * @since 0.7
081: * @param elementDescriptor
082: * @return true if the descriptor matches
083: */
084: public boolean matches(ElementDescriptor elementDescriptor) {
085: String nameFromDescriptor = nameFromDescriptor(elementDescriptor);
086: return nameFromDescriptor.equals(getName());
087: }
088:
089: /**
090: * Gets the name of this type.
091: * @return the name of this type
092: */
093: public String getName() {
094: return name;
095: }
096:
097: /**
098: * Sets the name of this type.
099: * @param string
100: */
101: public void setName(String string) {
102: name = string;
103: }
104:
105: public boolean equals(Object obj) {
106: boolean result = false;
107: if (obj instanceof GlobalComplexType) {
108: GlobalComplexType complexType = (GlobalComplexType) obj;
109: result = isEqual(name, complexType.name)
110: && equalContents(attributes, complexType.attributes)
111: && equalContents(elements, complexType.elements);
112:
113: }
114: return result;
115: }
116:
117: public int hashCode() {
118: return 0;
119: }
120:
121: private boolean equalContents(Collection one, Collection two) {
122: // doesn't check cardinality but should be ok
123: if (one.size() != two.size()) {
124: return false;
125: }
126: for (Iterator it = one.iterator(); it.hasNext();) {
127: Object object = it.next();
128: if (!two.contains(object)) {
129: return false;
130: }
131: }
132: return true;
133: }
134:
135: /**
136: * Null safe equals method
137: * @param one
138: * @param two
139: * @return
140: */
141: private boolean isEqual(String one, String two) {
142: boolean result = false;
143: if (one == null) {
144: result = (two == null);
145: } else {
146: result = one.equals(two);
147: }
148:
149: return result;
150: }
151:
152: public String toString() {
153: StringBuffer buffer = new StringBuffer();
154: buffer.append("<xsd:complexType name='");
155: buffer.append(name);
156: buffer.append("'>");
157: buffer.append("<xsd:sequence>");
158: for (Iterator it = elements.iterator(); it.hasNext();) {
159: buffer.append(it.next());
160: }
161: buffer.append("</xsd:sequence>");
162:
163: for (Iterator it = attributes.iterator(); it.hasNext();) {
164: buffer.append(it.next());
165: }
166: buffer.append("</xsd:complexType>");
167: return buffer.toString();
168: }
169: }
|