View Javadoc

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;
20  import java.util.Iterator;
21  import java.util.Collection;
22  
23  import org.apache.commons.betwixt.ElementDescriptor;
24  
25  /***
26   * Models a <code>complexType</code> from an XML schema.
27   * A complex type may contain element content and may have attributes.
28   * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
29   * @version $Revision: 1.2.2.1 $
30   */
31  public class GlobalComplexType extends ComplexType {
32  	
33  	private String name;
34  	
35  	public GlobalComplexType() {}
36      
37      /***
38       * Constructs a new ComplexType from the descriptor given.
39       * @param elementDescriptor
40       */
41      public GlobalComplexType(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException {
42          super(configuration, elementDescriptor, schema);    
43      }
44  
45      protected void init(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException {
46          setName(elementDescriptor.getPropertyType().getName());
47          super.init(configuration, elementDescriptor, schema);
48      }
49  
50  	/***
51       * Gets the name of this type.
52       * @return
53       */
54      public String getName() {
55          return name;
56      }
57  
58      /***
59       * Sets the name of this type.
60       * @param string
61       */
62      public void setName(String string) {
63          name = string;
64      }
65  
66      public boolean equals(Object obj) {
67            boolean result = false;
68            if (obj instanceof GlobalComplexType) {
69                GlobalComplexType complexType = (GlobalComplexType) obj;
70                result =  isEqual(name, complexType.name) &&
71                          equalContents(attributes, complexType.attributes) &&
72                          equalContents(elements, complexType.elements);
73                                     
74            }
75            return result;
76        }
77  
78      public int hashCode() {
79          return 0;
80      }
81      
82      private boolean equalContents(Collection one, Collection two)
83      {
84          // doesn't check cardinality but should be ok
85          if (one.size() != two.size()) {
86              return false;
87          }
88          for (Iterator it=one.iterator();it.hasNext();) {
89              Object object = it.next();
90              if (!two.contains(object)) {
91                  return false;
92              }
93          }
94          return true;
95      }
96  
97        /***
98         * Null safe equals method
99         * @param one
100        * @param two
101        * @return
102        */
103       private boolean isEqual(String one, String two) {
104           boolean result = false;
105           if (one == null) {
106               result = (two == null); 
107           }
108           else
109           {
110               result = one.equals(two);
111           }
112         
113           return result;
114       }
115       
116       public String toString() {
117           StringBuffer buffer = new StringBuffer();
118           buffer.append("<xsd:complexType name='");
119           buffer.append(name);
120           buffer.append("'>");
121           buffer.append("<xsd:sequence>");
122           for (Iterator it=elements.iterator(); it.hasNext();) {
123                 buffer.append(it.next());    
124           }
125           buffer.append("</xsd:sequence>");
126           
127           for (Iterator it=attributes.iterator(); it.hasNext();) {
128                 buffer.append(it.next());    
129           }
130           buffer.append("</xsd:complexType>");
131           return buffer.toString();
132       }
133 }