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: /**
021: * Models a global definition of an <code>element</code>.
022: * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
023: * @version $Revision: 438373 $
024: */
025: public class GlobalElement implements Element {
026: //TODO: going to ignore the issue of namespacing for the moment
027: public static final String STRING_SIMPLE_TYPE = "xsd:string";
028:
029: private String name;
030: private String type;
031:
032: private GlobalComplexType complexType;
033:
034: public GlobalElement() {
035: }
036:
037: public GlobalElement(String name, String type) {
038: setName(name);
039: setType(type);
040: }
041:
042: public GlobalElement(String name, GlobalComplexType complexType) {
043: setName(name);
044: setComplexType(complexType);
045: }
046:
047: /**
048: * Gets the element name
049: * @return element name, not null
050: */
051: public String getName() {
052: return name;
053: }
054:
055: /**
056: * Sets the element name
057: * @param string not null
058: */
059: public void setName(String string) {
060: name = string;
061: }
062:
063: /**
064: * Gets the element type
065: * @return the type of the element
066: */
067: public String getType() {
068: return type;
069: }
070:
071: /**
072: * Sets the element type
073: * @param string
074: */
075: public void setType(String string) {
076: type = string;
077: }
078:
079: /**
080: * Gets the anonymous type definition for this element, if one exists.
081: * @return ComplexType, null if there is no associated anonymous type definition
082: */
083: public GlobalComplexType getComplexType() {
084: return complexType;
085: }
086:
087: /**
088: * Sets the anonymous type definition for this element
089: * @param type ComplexType to be set as the anonymous type definition,
090: * null if the type is to be referenced
091: */
092: public void setComplexType(GlobalComplexType type) {
093: this .type = type.getName();
094: complexType = type;
095: }
096:
097: public boolean equals(Object obj) {
098: boolean result = false;
099: if (obj instanceof GlobalElement) {
100: GlobalElement element = (GlobalElement) obj;
101: result = isEqual(type, element.type)
102: && isEqual(name, element.name);
103: }
104: return result;
105: }
106:
107: public int hashCode() {
108: return 0;
109: }
110:
111: /**
112: * Null safe equals method
113: * @param one
114: * @param two
115: * @return
116: */
117: private boolean isEqual(String one, String two) {
118: boolean result = false;
119: if (one == null) {
120: result = (two == null);
121: } else {
122: result = one.equals(two);
123: }
124:
125: return result;
126: }
127:
128: public String toString() {
129: StringBuffer buffer = new StringBuffer();
130: buffer.append("<xsd:element name='");
131: buffer.append(name);
132: buffer.append("' type='");
133: buffer.append(type);
134: buffer.append("'>");
135:
136: if (complexType != null) {
137: buffer.append(complexType);
138: }
139: buffer.append("</xsd:element>");
140: return buffer.toString();
141: }
142:
143: }
|