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  
20  
21  /***
22   * Models the Element tag in the XML schema.
23   * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
24   * @version $Revision: 1.2 $
25   */
26  public class GlobalElement implements Element {
27  	//TODO: going to ignore the issue of namespacing for the moment
28  	public static final String STRING_SIMPLE_TYPE="xsd:string";
29  	
30  	private String name;
31  	private String type;
32  
33      private GlobalComplexType complexType;
34  	
35  	public GlobalElement() {}
36      
37      public GlobalElement(String name, String type) {
38          setName(name);
39          setType(type);
40      }
41      
42      public GlobalElement(String name, GlobalComplexType complexType) {
43          setName(name);
44          setComplexType(complexType);
45      }
46      
47  
48      
49  
50      /***
51       * Gets the element name
52       * @return element name, not null
53       */
54      public String getName() {
55          return name;
56      }
57  
58      /***
59       * Sets the element name
60       * @param string not null
61       */
62      public void setName(String string) {
63          name = string;
64      }
65  
66      /***
67       * Gets the element type
68       * @return
69       */
70      public String getType() {
71          return type;
72      }
73  
74      /***
75       * Sets the element type
76       * @param string
77       */
78      public void setType(String string) {
79          type = string;
80      }
81  
82  
83      /***
84       * Gets the anonymous type definition for this element, if one exists.
85       * @return ComplexType, null if there is no associated anonymous type definition
86       */
87      public GlobalComplexType getComplexType() {
88          return complexType;
89      }
90  
91      /***
92       * Sets the anonymous type definition for this element
93       * @param type ComplexType to be set as the anonymous type definition, 
94       * null if the type is to be referenced
95       */
96      public void setComplexType(GlobalComplexType type) {
97          this.type = type.getName();
98          complexType = type;
99      }    
100 
101 	public boolean equals(Object obj) {
102 		boolean result = false;
103 		if (obj instanceof GlobalElement) {
104             GlobalElement element = (GlobalElement) obj;
105             result = isEqual(type, element.type) &&
106                      isEqual(name, element.name);   		
107 		}
108 		return result;
109 	}
110     
111     public int hashCode() {
112         return 0;
113     }
114 
115 	/***
116 	 * Null safe equals method
117 	 * @param one
118 	 * @param two
119 	 * @return
120 	 */
121 	private boolean isEqual(String one, String two) {
122 		boolean result = false;
123 		if (one == null) {
124 			result = (two == null); 
125 		}
126 		else
127 		{
128 			result = one.equals(two);
129 		}
130 		
131 		return result;
132 	}
133 
134     public String toString() {
135         StringBuffer buffer = new StringBuffer();
136         buffer.append("<xsd:element name='");
137         buffer.append(name);
138         buffer.append("' type='");
139         buffer.append(type);
140         buffer.append("'>");
141         
142         if (complexType != null) {
143             buffer.append(complexType);
144         }
145         buffer.append("</xsd:element>");
146         return buffer.toString();
147     }
148 
149 
150 }