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

Classes in this File Line Coverage Branch Coverage Complexity
GlobalComplexType
68% 
73% 
1.692

 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.Collection;
 21  
 import java.util.Iterator;
 22  
 
 23  
 import org.apache.commons.betwixt.ElementDescriptor;
 24  
 
 25  
 /**
 26  
  * Models a <code>complexType</code> from an XML schema.
 27  
  * A complex type may contain element content and may have attributes.
 28  
  * @author <a href='http://jakarta.apache.org/'>Apache Commons Team</a>
 29  
  * @version $Revision: 397648 $
 30  
  */
 31  
 public class GlobalComplexType extends ComplexType {
 32  
         
 33  
         private String name;
 34  
     private TranscriptionConfiguration configuration;
 35  
     
 36  130
         public GlobalComplexType() {}
 37  
     
 38  
     /**
 39  
      * Constructs a new ComplexType from the descriptor given.
 40  
      * @param elementDescriptor
 41  
      */
 42  
     public GlobalComplexType(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException {
 43  468
         super(configuration, elementDescriptor, schema); 
 44  468
     }
 45  
 
 46  
     protected void init(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException {
 47  468
         this.configuration = configuration;
 48  468
         setName(nameFromDescriptor( elementDescriptor ));
 49  468
     }
 50  
 
 51  
     /**
 52  
      * Fills the complex type description.
 53  
      * @since 0.7
 54  
      * @param configuration
 55  
      * @param elementDescriptor
 56  
      * @param schema
 57  
      * @throws IntrospectionException
 58  
      */
 59  
     protected void fill(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor, Schema schema) throws IntrospectionException
 60  
     {
 61  468
         elementDescriptor = fillDescriptor(elementDescriptor, schema);
 62  468
         super.init(configuration, elementDescriptor, schema);
 63  468
     }
 64  
     
 65  
     private String nameFromDescriptor( ElementDescriptor elementDescriptor ) {
 66  884
         return configuration.getSchemaTypeNamingStrategy().nameSchemaType(elementDescriptor);
 67  
     }
 68  
     
 69  
     /**
 70  
      * Does the given element descriptor match this complex type?
 71  
      * @since 0.7
 72  
      * @param elementDescriptor
 73  
      * @return true if the descriptor matches
 74  
      */
 75  
     public boolean matches(ElementDescriptor elementDescriptor) {
 76  416
         String nameFromDescriptor = nameFromDescriptor ( elementDescriptor );
 77  416
         return nameFromDescriptor.equals(getName());
 78  
     }
 79  
     
 80  
         /**
 81  
      * Gets the name of this type.
 82  
      * @return the name of this type
 83  
      */
 84  
     public String getName() {
 85  1053
         return name;
 86  
     }
 87  
 
 88  
     /**
 89  
      * Sets the name of this type.
 90  
      * @param string
 91  
      */
 92  
     public void setName(String string) {
 93  598
         name = string;
 94  598
     }
 95  
 
 96  
     public boolean equals(Object obj) {
 97  4121
           boolean result = false;
 98  4121
           if (obj instanceof GlobalComplexType) {
 99  819
               GlobalComplexType complexType = (GlobalComplexType) obj;
 100  1638
               result =  isEqual(name, complexType.name) &&
 101  130
                         equalContents(attributes, complexType.attributes) &&
 102  130
                         equalContents(elements, complexType.elements);
 103  
                                    
 104  
           }
 105  4121
           return result;
 106  
       }
 107  
 
 108  
     public int hashCode() {
 109  676
         return 0;
 110  
     }
 111  
     
 112  
     private boolean equalContents(Collection one, Collection two)
 113  
     {
 114  
         // doesn't check cardinality but should be ok
 115  260
         if (one.size() != two.size()) {
 116  0
             return false;
 117  
         }
 118  1131
         for (Iterator it=one.iterator();it.hasNext();) {
 119  611
             Object object = it.next();
 120  611
             if (!two.contains(object)) {
 121  0
                 return false;
 122  
             }
 123  
         }
 124  260
         return true;
 125  
     }
 126  
 
 127  
       /**
 128  
        * Null safe equals method
 129  
        * @param one
 130  
        * @param two
 131  
        * @return
 132  
        */
 133  
       private boolean isEqual(String one, String two) {
 134  819
           boolean result = false;
 135  819
           if (one == null) {
 136  0
               result = (two == null); 
 137  
           }
 138  
           else
 139  
           {
 140  819
               result = one.equals(two);
 141  
           }
 142  
         
 143  819
           return result;
 144  
       }
 145  
       
 146  
       public String toString() {
 147  0
           StringBuffer buffer = new StringBuffer();
 148  0
           buffer.append("<xsd:complexType name='");
 149  0
           buffer.append(name);
 150  0
           buffer.append("'>");
 151  0
           buffer.append("<xsd:sequence>");
 152  0
           for (Iterator it=elements.iterator(); it.hasNext();) {
 153  0
                 buffer.append(it.next());    
 154  
           }
 155  0
           buffer.append("</xsd:sequence>");
 156  
           
 157  0
           for (Iterator it=attributes.iterator(); it.hasNext();) {
 158  0
                 buffer.append(it.next());    
 159  
           }
 160  0
           buffer.append("</xsd:complexType>");
 161  0
           return buffer.toString();
 162  
       }
 163  
 }