Coverage Report - org.apache.commons.betwixt.XMLBeanInfo

Classes in this File Line Coverage Branch Coverage Complexity
XMLBeanInfo
71% 
100% 
1.417

 1  
 /*
 2  
  * Copyright 2001-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;
 17  
 
 18  
 /** <p><code>XMLBeanInfo</code> represents the XML metadata information
 19  
   * used to map a Java Bean cleanly to XML. This provides a default
 20  
   * introspection mechansim, rather like {@link java.beans.BeanInfo} 
 21  
   * which can be customized through some mechansim, either via Java code 
 22  
   * or XSLT for example.</p>
 23  
   *
 24  
   * <h4><code>ID</code> and <code>IDREF</code> Attribute Names</h4>
 25  
   * <p>These special attributes are defined in the xml specification.
 26  
   * They are used by Betwixt to write bean graphs with cyclic references.
 27  
   * In most cases, these will take the values 'id' and 'idref' respectively 
 28  
   * but these names can be varied in the DTD.
 29  
   * Therefore, these names are specified by this class but default to the
 30  
   * usual values.</p>
 31  
   *
 32  
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 33  
   * @version $Revision: 155402 $
 34  
   */
 35  
 public class XMLBeanInfo {
 36  
     /** Descriptor for main element */
 37  
     private ElementDescriptor elementDescriptor;
 38  
     
 39  
     /** the beans class that this XML info refers to */
 40  
     private Class beanClass;
 41  
     /** <code>ID</code> attribute name */
 42  8758
     private String idAttributeName = "id";
 43  
     /** <code>IDREF</code> attribute name */
 44  8758
     private String idrefAttributeName = "idref";
 45  
     /** Have we already cached the <code>idAttributeDescriptor</code>? */
 46  8758
     private boolean cachedIDAttribute = false;
 47  
     /** Cached <code>ID</code> attribute descriptor */
 48  
     private AttributeDescriptor idAttributeDescriptor;
 49  
     
 50  
     /** 
 51  
      * Base constructor 
 52  
      * @param beanClass for this Class
 53  
      */
 54  8758
     public XMLBeanInfo( Class beanClass ) {
 55  8758
         this.beanClass = beanClass;        
 56  8758
     }
 57  
 
 58  
     /** 
 59  
      * Gets descriptor for bean represention 
 60  
      *
 61  
      * @return ElementDescriptor describing root element
 62  
      */
 63  
     public ElementDescriptor getElementDescriptor() {
 64  37949
         return elementDescriptor;
 65  
     }
 66  
 
 67  
     /** 
 68  
      * Sets descriptor for bean represention 
 69  
      *
 70  
      * @param elementDescriptor descriptor for bean
 71  
      */
 72  
     public void setElementDescriptor(ElementDescriptor elementDescriptor) {
 73  8732
         this.elementDescriptor = elementDescriptor;
 74  8732
     }    
 75  
     
 76  
     /**  
 77  
      * Gets the beans class that this XML info refers to
 78  
      *
 79  
      * @return the beans class that this XML info refers to 
 80  
      */
 81  
     public Class getBeanClass() {
 82  6081
         return beanClass;
 83  
     }
 84  
     
 85  
     /** 
 86  
      * Sets the beans class that this XML info refers to 
 87  
      *
 88  
      * @param beanClass the class that this refers to
 89  
      */
 90  
     public void setBeanClass(Class beanClass) {
 91  0
         this.beanClass = beanClass;
 92  0
     }
 93  
     
 94  
     /** 
 95  
      * Search attributes for one matching <code>ID</code> attribute name 
 96  
      *
 97  
      * @return the xml ID attribute
 98  
      */
 99  
     public AttributeDescriptor getIDAttribute() {
 100  
         //
 101  
         // XXX for some reason caching isn't working at the moment
 102  
         // it could be that this method is called too early
 103  
         // and not reset when attributes change
 104  
         // on the other hand, the speed gain is likely to be too
 105  
         // small to bother about
 106  
         //
 107  
         //if ( cachedIDAttribute = false ) {
 108  7806
             idAttributeDescriptor = findIDAttribute();
 109  
           //  cachedIDAttribute = true;
 110  
         //}
 111  7806
         return idAttributeDescriptor;
 112  
     }
 113  
     
 114  
     /** 
 115  
      * ID attribute search implementation 
 116  
      * @return the AttributeDescriptor for the <code>ID</code> attribute
 117  
      */
 118  
     private AttributeDescriptor findIDAttribute() {
 119  
         // we'll check to see if the bean already has an id
 120  7806
         if ( getElementDescriptor().hasAttributes() ) {
 121  3510
             AttributeDescriptor[] attributes = getElementDescriptor().getAttributeDescriptors();
 122  3510
             if ( attributes != null ) {
 123  10634
                 for ( int i = 0, size = attributes.length; i < size; i++ ) {
 124  
                     // support a match either on local or qualified name
 125  7137
                     if ( getIDAttributeName().equals( attributes[i].getQualifiedName() ) 
 126  7124
                         || getIDAttributeName().equals( attributes[i].getLocalName() )) {
 127  
                         // we've got a match so use this attribute
 128  13
                         return attributes[i];
 129  
                         
 130  
                     }
 131  
                 }
 132  
             }
 133  
         }
 134  7793
         return null;
 135  
     }
 136  
     
 137  
     /** 
 138  
       * <p>Get name of <code>ID</code> attribute.
 139  
       * This is used to write (for example) automatic <code>ID</code>
 140  
       * attribute values.</p>
 141  
       * 
 142  
       * <p>The default name is 'id'.</p>
 143  
       *
 144  
       * @return name for the special <code>ID</code> attribute
 145  
       */
 146  
     public String getIDAttributeName() {
 147  15314
         return idAttributeName;
 148  
     }
 149  
     
 150  
     /** 
 151  
       * Set name of <code>ID</code> attribute 
 152  
       * This is used to write (for example) automatic <code>ID</code>
 153  
       * attribute values.</p>
 154  
       * 
 155  
       * <p>The default name is 'id'.</p>
 156  
       *
 157  
       * @param idAttributeName the attribute name for the special <code>ID</code> attribute
 158  
       */
 159  
     public void setIDAttributeName(String idAttributeName) {
 160  0
         this.idAttributeName = idAttributeName;
 161  0
     }
 162  
     
 163  
     /** 
 164  
       * <p>Get <code>IDREF</code> attribute name 
 165  
       * This is used (for example) to deal with cyclic references.
 166  
       *
 167  
       * <p>The default name is 'idref'.</p>
 168  
       *
 169  
       * @return name for the special <code>IDREF</code> attribute
 170  
       */
 171  
     public String getIDREFAttributeName() {
 172  65
         return idrefAttributeName;
 173  
     }
 174  
     
 175  
     /** 
 176  
       * Set <code>IDREF</code> attribute name 
 177  
       * This is used (for example) to deal with cyclic references.
 178  
       *
 179  
       * <p>The default name is 'idref'.</p>
 180  
       *
 181  
       * @param idrefAttributeName the attribute name for the special <code>IDREF</code> attribute
 182  
       */
 183  
     public void setIDREFAttributeName(String idrefAttributeName) {
 184  0
         this.idrefAttributeName = idrefAttributeName;
 185  0
     }
 186  
     
 187  
     /**
 188  
      * Gets log-friendly string representation.
 189  
      *
 190  
      * @return something useful for logging
 191  
      */
 192  
     public String toString() {
 193  0
         return 
 194  0
                 "XMLBeanInfo [class=" + getBeanClass() 
 195  0
                 + ", descriptor=" + getElementDescriptor() + "]";
 196  
     }
 197  
 }