View Javadoc

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  
17  package org.apache.commons.betwixt.schema;
18  
19  import java.beans.IntrospectionException;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.apache.commons.betwixt.AttributeDescriptor;
24  import org.apache.commons.betwixt.ElementDescriptor;
25  import org.apache.commons.betwixt.XMLBeanInfo;
26  
27  /***
28   * Models a <code>complexType</code>. Global (top level) complex types are
29   * represented by {@link GlobalComplexType}. Locally defined or referenced
30   * complex types are represented by {@link LocalComplexType}.
31   * 
32   * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team </a>
33   * @version $Revision: 190515 $
34   */
35  public abstract class ComplexType {
36  
37      protected List elements = new ArrayList();
38  
39      protected List attributes = new ArrayList();
40  
41      public ComplexType() {
42      }
43  
44      public ComplexType(TranscriptionConfiguration configuration,
45              ElementDescriptor elementDescriptor, Schema schema)
46              throws IntrospectionException {
47          elementDescriptor = fillDescriptor(elementDescriptor, schema);
48          init(configuration, elementDescriptor, schema);
49      }
50  
51      /***
52       * Fills the given descriptor
53       * @since 0.7
54       * @param elementDescriptor
55       * @param schema
56       * @return @throws
57       *         IntrospectionException
58       */
59      protected ElementDescriptor fillDescriptor(
60              ElementDescriptor elementDescriptor, Schema schema)
61              throws IntrospectionException {
62          if (elementDescriptor.isHollow()) {
63              // need to introspector for filled descriptor
64              Class type = elementDescriptor.getSingularPropertyType();
65              if (type == null) {
66                  type = elementDescriptor.getPropertyType();
67              }
68              XMLBeanInfo filledBeanInfo = schema.introspect(type);
69              elementDescriptor = filledBeanInfo.getElementDescriptor();
70          }
71          return elementDescriptor;
72      }
73  
74      protected void init(TranscriptionConfiguration configuration,
75              ElementDescriptor elementDescriptor, Schema schema)
76              throws IntrospectionException {
77  
78          AttributeDescriptor[] attributeDescriptors = elementDescriptor
79                  .getAttributeDescriptors();
80          for (int i = 0, length = attributeDescriptors.length; i < length; i++) {
81              //TODO: need to think about computing schema types from descriptors
82              // this will probably depend on the class mapped to
83              String uri = attributeDescriptors[i].getURI();
84              if (!SchemaTranscriber.W3C_SCHEMA_INSTANCE_URI.equals(uri)) {
85                  attributes.add(new Attribute(attributeDescriptors[i]));
86              }
87          }
88  
89          //TODO: add support for spacing elements
90          ElementDescriptor[] elementDescriptors = elementDescriptor
91                  .getElementDescriptors();
92          for (int i = 0, length = elementDescriptors.length; i < length; i++) {
93              if (elementDescriptors[i].isHollow()) {
94                  elements.add(new ElementReference(configuration,
95                          elementDescriptors[i], schema));
96              } else if (elementDescriptors[i].isSimple()) {
97                  elements.add(new SimpleLocalElement(configuration,
98                          elementDescriptors[i], schema));
99              } else {
100                 elements.add(new ComplexLocalElement(configuration,
101                         elementDescriptors[i], schema));
102             }
103         }
104     }
105 
106     /***
107      * Gets the elements contained by this type
108      * 
109      * @return <code>List</code> of contained elements, not null
110      */
111     public List getElements() {
112         return elements;
113     }
114 
115     /***
116      * Adds an element to those contained by this type
117      * 
118      * @param element
119      */
120     public void addElement(ElementReference element) {
121         elements.add(element);
122     }
123 
124     /***
125      * Adds an element to those contained by this type
126      * 
127      * @param element
128      */
129     public void addElement(LocalElement element) {
130         elements.add(element);
131     }
132 
133     /***
134      * Gets the attributes contained by this type.
135      * 
136      * @return <code>List</code> of attributes
137      */
138     public List getAttributes() {
139         return attributes;
140     }
141 
142     /***
143      * Adds an attribute to those contained by this type
144      * 
145      * @param attribute
146      */
147     public void addAttribute(Attribute attribute) {
148         attributes.add(attribute);
149     }
150 
151 }