View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.betwixt.schema;
19  
20  import java.beans.IntrospectionException;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.commons.betwixt.AttributeDescriptor;
25  import org.apache.commons.betwixt.ElementDescriptor;
26  import org.apache.commons.betwixt.XMLBeanInfo;
27  
28  /***
29   * Models a <code>complexType</code>. Global (top level) complex types are
30   * represented by {@link GlobalComplexType}. Locally defined or referenced
31   * complex types are represented by {@link LocalComplexType}.
32   * 
33   * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team </a>
34   * @version $Revision: 438373 $
35   */
36  public abstract class ComplexType {
37  
38      protected List elements = new ArrayList();
39  
40      protected List attributes = new ArrayList();
41  
42      public ComplexType() {
43      }
44  
45      public ComplexType(TranscriptionConfiguration configuration,
46              ElementDescriptor elementDescriptor, Schema schema)
47              throws IntrospectionException {
48          elementDescriptor = fillDescriptor(elementDescriptor, schema);
49          init(configuration, elementDescriptor, schema);
50      }
51  
52      /***
53       * Fills the given descriptor
54       * @since 0.7
55       * @param elementDescriptor
56       * @param schema
57       * @return @throws
58       *         IntrospectionException
59       */
60      protected ElementDescriptor fillDescriptor(
61              ElementDescriptor elementDescriptor, Schema schema)
62              throws IntrospectionException {
63          if (elementDescriptor.isHollow()) {
64              // need to introspector for filled descriptor
65              Class type = elementDescriptor.getSingularPropertyType();
66              if (type == null) {
67                  type = elementDescriptor.getPropertyType();
68              }
69              if (type == null) {
70                 // no type!
71                 // TODO: handle this
72                 // TODO: add support for logging
73                 // TODO: maybe should try singular type?
74              } else {
75                  XMLBeanInfo filledBeanInfo = schema.introspect(type);
76                  elementDescriptor = filledBeanInfo.getElementDescriptor();
77              }
78          }
79          return elementDescriptor;
80      }
81  
82      protected void init(TranscriptionConfiguration configuration,
83              ElementDescriptor elementDescriptor, Schema schema)
84              throws IntrospectionException {
85  
86          AttributeDescriptor[] attributeDescriptors = elementDescriptor
87                  .getAttributeDescriptors();
88          for (int i = 0, length = attributeDescriptors.length; i < length; i++) {
89              //TODO: need to think about computing schema types from descriptors
90              // this will probably depend on the class mapped to
91              String uri = attributeDescriptors[i].getURI();
92              if (!SchemaTranscriber.W3C_SCHEMA_INSTANCE_URI.equals(uri)) {
93                  attributes.add(new Attribute(attributeDescriptors[i]));
94              }
95          }
96  
97          //TODO: add support for spacing elements
98          ElementDescriptor[] elementDescriptors = elementDescriptor
99                  .getElementDescriptors();
100         for (int i = 0, length = elementDescriptors.length; i < length; i++) {
101             if (elementDescriptors[i].isHollow()) {
102                 elements.add(new ElementReference(configuration,
103                         elementDescriptors[i], schema));
104             } else if (elementDescriptors[i].isSimple()) {
105                 elements.add(new SimpleLocalElement(configuration,
106                         elementDescriptors[i], schema));
107             } else {
108                 elements.add(new ComplexLocalElement(configuration,
109                         elementDescriptors[i], schema));
110             }
111         }
112     }
113 
114     /***
115      * Gets the elements contained by this type
116      * 
117      * @return <code>List</code> of contained elements, not null
118      */
119     public List getElements() {
120         return elements;
121     }
122 
123     /***
124      * Adds an element to those contained by this type
125      * 
126      * @param element
127      */
128     public void addElement(ElementReference element) {
129         elements.add(element);
130     }
131 
132     /***
133      * Adds an element to those contained by this type
134      * 
135      * @param element
136      */
137     public void addElement(LocalElement element) {
138         elements.add(element);
139     }
140 
141     /***
142      * Gets the attributes contained by this type.
143      * 
144      * @return <code>List</code> of attributes
145      */
146     public List getAttributes() {
147         return attributes;
148     }
149 
150     /***
151      * Adds an attribute to those contained by this type
152      * 
153      * @param attribute
154      */
155     public void addAttribute(Attribute attribute) {
156         attributes.add(attribute);
157     }
158 
159 }