Coverage Report - org.apache.commons.betwixt.schema.ComplexType

Classes in this File Line Coverage Branch Coverage Complexity
ComplexType
98% 
100% 
1.889

 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/'>Apache Commons Team </a>
 33  
  * @version $Revision: 240123 $
 34  
  */
 35  
 public abstract class ComplexType {
 36  
 
 37  663
     protected List elements = new ArrayList();
 38  
 
 39  663
     protected List attributes = new ArrayList();
 40  
 
 41  130
     public ComplexType() {
 42  130
     }
 43  
 
 44  533
     public ComplexType(TranscriptionConfiguration configuration,
 45  
             ElementDescriptor elementDescriptor, Schema schema)
 46  
             throws IntrospectionException {
 47  533
         elementDescriptor = fillDescriptor(elementDescriptor, schema);
 48  533
         init(configuration, elementDescriptor, schema);
 49  533
     }
 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  1001
         if (elementDescriptor.isHollow()) {
 63  
             // need to introspector for filled descriptor
 64  442
             Class type = elementDescriptor.getSingularPropertyType();
 65  442
             if (type == null) {
 66  0
                 type = elementDescriptor.getPropertyType();
 67  
             }
 68  442
             if (type == null) {
 69  
                // no type!
 70  
                // TODO: handle this
 71  
                // TODO: add support for logging
 72  
                // TODO: maybe should try singular type?
 73  
             } else {
 74  442
                 XMLBeanInfo filledBeanInfo = schema.introspect(type);
 75  442
                 elementDescriptor = filledBeanInfo.getElementDescriptor();
 76  
             }
 77  
         }
 78  1001
         return elementDescriptor;
 79  
     }
 80  
 
 81  
     protected void init(TranscriptionConfiguration configuration,
 82  
             ElementDescriptor elementDescriptor, Schema schema)
 83  
             throws IntrospectionException {
 84  
 
 85  1066
         AttributeDescriptor[] attributeDescriptors = elementDescriptor
 86  533
                 .getAttributeDescriptors();
 87  1456
         for (int i = 0, length = attributeDescriptors.length; i < length; i++) {
 88  
             //TODO: need to think about computing schema types from descriptors
 89  
             // this will probably depend on the class mapped to
 90  923
             String uri = attributeDescriptors[i].getURI();
 91  923
             if (!SchemaTranscriber.W3C_SCHEMA_INSTANCE_URI.equals(uri)) {
 92  845
                 attributes.add(new Attribute(attributeDescriptors[i]));
 93  
             }
 94  
         }
 95  
 
 96  
         //TODO: add support for spacing elements
 97  1066
         ElementDescriptor[] elementDescriptors = elementDescriptor
 98  533
                 .getElementDescriptors();
 99  1560
         for (int i = 0, length = elementDescriptors.length; i < length; i++) {
 100  1027
             if (elementDescriptors[i].isHollow()) {
 101  494
                 elements.add(new ElementReference(configuration,
 102  247
                         elementDescriptors[i], schema));
 103  780
             } else if (elementDescriptors[i].isSimple()) {
 104  1430
                 elements.add(new SimpleLocalElement(configuration,
 105  715
                         elementDescriptors[i], schema));
 106  
             } else {
 107  130
                 elements.add(new ComplexLocalElement(configuration,
 108  65
                         elementDescriptors[i], schema));
 109  
             }
 110  
         }
 111  533
     }
 112  
 
 113  
     /**
 114  
      * Gets the elements contained by this type
 115  
      * 
 116  
      * @return <code>List</code> of contained elements, not null
 117  
      */
 118  
     public List getElements() {
 119  403
         return elements;
 120  
     }
 121  
 
 122  
     /**
 123  
      * Adds an element to those contained by this type
 124  
      * 
 125  
      * @param element
 126  
      */
 127  
     public void addElement(ElementReference element) {
 128  52
         elements.add(element);
 129  52
     }
 130  
 
 131  
     /**
 132  
      * Adds an element to those contained by this type
 133  
      * 
 134  
      * @param element
 135  
      */
 136  
     public void addElement(LocalElement element) {
 137  299
         elements.add(element);
 138  299
     }
 139  
 
 140  
     /**
 141  
      * Gets the attributes contained by this type.
 142  
      * 
 143  
      * @return <code>List</code> of attributes
 144  
      */
 145  
     public List getAttributes() {
 146  403
         return attributes;
 147  
     }
 148  
 
 149  
     /**
 150  
      * Adds an attribute to those contained by this type
 151  
      * 
 152  
      * @param attribute
 153  
      */
 154  
     public void addAttribute(Attribute attribute) {
 155  260
         attributes.add(attribute);
 156  260
     }
 157  
 
 158  
 }