View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17  
18  package org.apache.commons.betwixt.schema;
19  
20  import java.beans.IntrospectionException;
21  import java.util.Collection;
22  import java.util.Iterator;
23  
24  import org.apache.commons.betwixt.ElementDescriptor;
25  
26  /**
27   * Models a <code>complexType</code> from an XML schema.
28   * A complex type may contain element content and may have attributes.
29   * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
30   * @version $Revision: 561314 $
31   */
32  public class GlobalComplexType extends ComplexType {
33  	
34  	private String name;
35      private TranscriptionConfiguration configuration;
36      
37  	public GlobalComplexType() {}
38      
39      /**
40       * Constructs a new ComplexType from the descriptor given.
41       * @param elementDescriptor
42       */
43      public GlobalComplexType(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException {
44          super(configuration, elementDescriptor, schema); 
45      }
46  
47      protected void init(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException {
48          this.configuration = configuration;
49          setName(nameFromDescriptor( elementDescriptor ));
50      }
51  
52      /**
53       * Fills the complex type description.
54       * @since 0.7
55       * @param configuration
56       * @param elementDescriptor
57       * @param schema
58       * @throws IntrospectionException
59       */
60      protected void fill(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException
61      {
62          elementDescriptor = fillDescriptor(elementDescriptor, schema);
63          super.init(configuration, elementDescriptor, schema);
64      }
65      
66      private String nameFromDescriptor( ElementDescriptor elementDescriptor ) {
67          return configuration.getSchemaTypeNamingStrategy().nameSchemaType(elementDescriptor);
68      }
69      
70      /**
71       * Does the given element descriptor match this complex type?
72       * @since 0.7
73       * @param elementDescriptor
74       * @return true if the descriptor matches
75       */
76      public boolean matches(ElementDescriptor elementDescriptor) {
77          String nameFromDescriptor = nameFromDescriptor ( elementDescriptor );
78          return nameFromDescriptor.equals(getName());
79      }
80      
81  	/**
82       * Gets the name of this type.
83       * @return the name of this type
84       */
85      public String getName() {
86          return name;
87      }
88  
89      /**
90       * Sets the name of this type.
91       * @param string
92       */
93      public void setName(String string) {
94          name = string;
95      }
96  
97      public boolean equals(Object obj) {
98            boolean result = false;
99            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 }