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