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