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