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. import java.beans.IntrospectionException;
  18. import java.util.Iterator;
  19. import java.util.Collection;
  20. import org.apache.commons.betwixt.ElementDescriptor;
  21. /**
  22. * Models a <code>complexType</code> from an XML schema.
  23. * A complex type may contain element content and may have attributes.
  24. * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
  25. * @version $Revision: 1.2.2.1 $
  26. */
  27. public class GlobalComplexType extends ComplexType {
  28. private String name;
  29. public GlobalComplexType() {}
  30. /**
  31. * Constructs a new ComplexType from the descriptor given.
  32. * @param elementDescriptor
  33. */
  34. public GlobalComplexType(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException {
  35. super(configuration, elementDescriptor, schema);
  36. }
  37. protected void init(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException {
  38. setName(elementDescriptor.getPropertyType().getName());
  39. super.init(configuration, elementDescriptor, schema);
  40. }
  41. /**
  42. * Gets the name of this type.
  43. * @return
  44. */
  45. public String getName() {
  46. return name;
  47. }
  48. /**
  49. * Sets the name of this type.
  50. * @param string
  51. */
  52. public void setName(String string) {
  53. name = string;
  54. }
  55. public boolean equals(Object obj) {
  56. boolean result = false;
  57. if (obj instanceof GlobalComplexType) {
  58. GlobalComplexType complexType = (GlobalComplexType) obj;
  59. result = isEqual(name, complexType.name) &&
  60. equalContents(attributes, complexType.attributes) &&
  61. equalContents(elements, complexType.elements);
  62. }
  63. return result;
  64. }
  65. public int hashCode() {
  66. return 0;
  67. }
  68. private boolean equalContents(Collection one, Collection two)
  69. {
  70. // doesn't check cardinality but should be ok
  71. if (one.size() != two.size()) {
  72. return false;
  73. }
  74. for (Iterator it=one.iterator();it.hasNext();) {
  75. Object object = it.next();
  76. if (!two.contains(object)) {
  77. return false;
  78. }
  79. }
  80. return true;
  81. }
  82. /**
  83. * Null safe equals method
  84. * @param one
  85. * @param two
  86. * @return
  87. */
  88. private boolean isEqual(String one, String two) {
  89. boolean result = false;
  90. if (one == null) {
  91. result = (two == null);
  92. }
  93. else
  94. {
  95. result = one.equals(two);
  96. }
  97. return result;
  98. }
  99. public String toString() {
  100. StringBuffer buffer = new StringBuffer();
  101. buffer.append("<xsd:complexType name='");
  102. buffer.append(name);
  103. buffer.append("'>");
  104. buffer.append("<xsd:sequence>");
  105. for (Iterator it=elements.iterator(); it.hasNext();) {
  106. buffer.append(it.next());
  107. }
  108. buffer.append("</xsd:sequence>");
  109. for (Iterator it=attributes.iterator(); it.hasNext();) {
  110. buffer.append(it.next());
  111. }
  112. buffer.append("</xsd:complexType>");
  113. return buffer.toString();
  114. }
  115. }