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.ArrayList;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.Collection;
  22. import org.apache.commons.betwixt.ElementDescriptor;
  23. import org.apache.commons.betwixt.XMLBeanInfo;
  24. import org.apache.commons.betwixt.XMLIntrospector;
  25. /**
  26. * Model for top level element in an XML Schema
  27. *
  28. * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
  29. * @version $Revision: 1.2.2.1 $
  30. */
  31. public class Schema {
  32. private List elements = new ArrayList();
  33. private List complexTypes = new ArrayList();
  34. private List simpleTypes = new ArrayList();
  35. private XMLIntrospector introspector;
  36. public Schema() {
  37. this(new XMLIntrospector());
  38. }
  39. public Schema(XMLIntrospector introspector) {
  40. this.introspector = introspector;
  41. }
  42. /**
  43. * Introspects the given type giving an <code>XMLBeanInfo</code>.
  44. * @param type Class to introspect, not null
  45. * @return <code>XMLBeanInfo</code>, not null
  46. * @throws IntrospectionException
  47. */
  48. public XMLBeanInfo introspect(Class type) throws IntrospectionException {
  49. return introspector.introspect(type);
  50. }
  51. /**
  52. * Gets the complex types defined
  53. * @return list of <code>ComplexType</code>'s not null
  54. */
  55. public List getComplexTypes() {
  56. return complexTypes;
  57. }
  58. /**
  59. * Adds a new complex type to those defined
  60. * @param complexType not null
  61. */
  62. public void addComplexType(GlobalComplexType complexType) {
  63. complexTypes.add(complexType);
  64. }
  65. /**
  66. * Gets the elements definied
  67. * @return list of <code>Element</code>s not null
  68. */
  69. public List getElements() {
  70. return elements;
  71. }
  72. /**
  73. * Adds a new element to those defined.
  74. * @param element not null
  75. */
  76. public void addElement(GlobalElement element) {
  77. elements.add(element);
  78. }
  79. /**
  80. * Gets the simple types defined.
  81. * @return list of <code>SimpleType</code>s not null
  82. */
  83. public List getSimpleTypes() {
  84. return simpleTypes;
  85. }
  86. /**
  87. * Adds a new simple type to those defined.
  88. * @param simpleType
  89. */
  90. public void addSimpleType(SimpleType simpleType) {
  91. simpleTypes.add(simpleType);
  92. }
  93. /**
  94. * Adds global (top level) element and type declarations matching the given descriptor.
  95. * @param elementDescriptor ElementDescriptor not null
  96. */
  97. public void addGlobalElementType(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor) throws IntrospectionException {
  98. // need to create a global element declaration and a complex type
  99. // use the fully qualified class name as the type name
  100. GlobalElement element = new GlobalElement(
  101. elementDescriptor.getLocalName(),
  102. elementDescriptor.getPropertyType().getName());
  103. addElement(element);
  104. GlobalComplexType type = new GlobalComplexType(configuration, elementDescriptor, this);
  105. addComplexType(type);
  106. }
  107. public boolean equals(Object obj) {
  108. boolean result = false;
  109. if (obj instanceof Schema) {
  110. Schema schema = (Schema) obj;
  111. result =
  112. equalContents(elements, schema.elements) &&
  113. equalContents(complexTypes, schema.complexTypes) &&
  114. equalContents(simpleTypes, schema.simpleTypes);
  115. }
  116. return result;
  117. }
  118. private boolean equalContents(Collection one, Collection two)
  119. {
  120. // doesn't check cardinality but should be ok
  121. if (one.size() != two.size()) {
  122. return false;
  123. }
  124. for (Iterator it=one.iterator();it.hasNext();) {
  125. Object object = it.next();
  126. if (!two.contains(object)) {
  127. return false;
  128. }
  129. }
  130. return true;
  131. }
  132. public int hashCode() {
  133. return 0;
  134. }
  135. public String toString() {
  136. StringBuffer buffer = new StringBuffer();
  137. buffer.append("<?xml version='1.0'?>");
  138. buffer.append("<xsd:schema xmlns:xsd='http://www.w3c.org/2001/XMLSchema'>");
  139. for (Iterator it=simpleTypes.iterator(); it.hasNext();) {
  140. buffer.append(it.next());
  141. }
  142. for (Iterator it=complexTypes.iterator(); it.hasNext();) {
  143. buffer.append(it.next());
  144. }
  145. for (Iterator it=elements.iterator(); it.hasNext();) {
  146. buffer.append(it.next());
  147. }
  148. buffer.append("</xsd:schema>");
  149. return buffer.toString();
  150. }
  151. }