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.List;
  20. import org.apache.commons.betwixt.AttributeDescriptor;
  21. import org.apache.commons.betwixt.ElementDescriptor;
  22. import org.apache.commons.betwixt.XMLBeanInfo;
  23. /**
  24. * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
  25. * @version $Revision: 1.2 $
  26. */
  27. public abstract class ComplexType {
  28. protected List elements = new ArrayList();
  29. protected List attributes = new ArrayList();
  30. public ComplexType() {}
  31. public ComplexType(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException {
  32. if (elementDescriptor.isHollow()) {
  33. // need to introspector for filled descriptor
  34. Class type = elementDescriptor.getSingularPropertyType();
  35. if (type == null) {
  36. type = elementDescriptor.getPropertyType();
  37. }
  38. XMLBeanInfo filledBeanInfo = schema.introspect(type);
  39. elementDescriptor = filledBeanInfo.getElementDescriptor();
  40. }
  41. init(configuration, elementDescriptor, schema);
  42. }
  43. protected void init(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException {
  44. AttributeDescriptor[] attributeDescriptors = elementDescriptor.getAttributeDescriptors();
  45. for (int i=0,length=attributeDescriptors.length; i<length ; i++) {
  46. //TODO: need to think about computing schema types from descriptors
  47. // this will probably depend on the class mapped to
  48. String uri = attributeDescriptors[i].getURI();
  49. if (! SchemaTranscriber.W3C_SCHEMA_INSTANCE_URI.equals(uri)) {
  50. attributes.add(new Attribute(attributeDescriptors[i]));
  51. }
  52. }
  53. //TODO: add support for spacing elements
  54. ElementDescriptor[] elementDescriptors = elementDescriptor.getElementDescriptors();
  55. for (int i=0,length=elementDescriptors.length; i<length ; i++) {
  56. if (elementDescriptors[i].isHollow()) {
  57. elements.add(new ElementReference(configuration, elementDescriptors[i], schema));
  58. } else if (elementDescriptors[i].isSimple()){
  59. elements.add(new SimpleLocalElement(configuration, elementDescriptors[i], schema));
  60. } else {
  61. elements.add(new ComplexLocalElement(configuration, elementDescriptors[i], schema));
  62. }
  63. }
  64. }
  65. /**
  66. * Gets the elements contained by this type
  67. * @return
  68. */
  69. public List getElements() {
  70. return elements;
  71. }
  72. /**
  73. * Adds an element to those contained by this type
  74. * @param element
  75. */
  76. public void addElement(ElementReference element) {
  77. elements.add(element);
  78. }
  79. /**
  80. * Adds an element to those contained by this type
  81. * @param element
  82. */
  83. public void addElement(LocalElement element) {
  84. elements.add(element);
  85. }
  86. /**
  87. * Gets the attributes contained by this type.
  88. * @return
  89. */
  90. public List getAttributes() {
  91. return attributes;
  92. }
  93. /**
  94. * Adds an attribute to those contained by this type
  95. * @param attribute
  96. */
  97. public void addAttribute(Attribute attribute) {
  98. attributes.add(attribute);
  99. }
  100. }