Coverage Report - org.apache.commons.betwixt.digester.XMLBeanInfoDigester

Classes in this File Line Coverage Branch Coverage Complexity
XMLBeanInfoDigester
86% 
100% 
1.182

 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.digester;
 17  
 
 18  
 import java.util.HashSet;
 19  
 import java.util.Set;
 20  
 
 21  
 import javax.xml.parsers.SAXParser;
 22  
 
 23  
 import org.apache.commons.betwixt.XMLIntrospector;
 24  
 import org.apache.commons.digester.Digester;
 25  
 import org.apache.commons.logging.Log;
 26  
 import org.apache.commons.logging.LogFactory;
 27  
 import org.xml.sax.XMLReader;
 28  
 
 29  
 /** <p><code>XMLBeanInfoDigester</code> is a digester of XML files
 30  
   * containing XMLBeanInfo definitions for a JavaBean.</p>
 31  
   *
 32  
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 33  
   * @version $Revision: 312489 $
 34  
   */
 35  604
 public class XMLBeanInfoDigester extends Digester {
 36  
 
 37  
     /** Logger */
 38  604
     private static final Log log = LogFactory.getLog( XMLBeanInfoDigester.class );
 39  
     
 40  
     /** the beans class for this XML descriptor */
 41  
     private Class beanClass;
 42  
     
 43  
     /** should attributes or elements be used for primitive types */
 44  
     private boolean attributesForPrimitives;
 45  
     
 46  
     /** the set of property names processed so far */
 47  1624
     private Set processedPropertyNameSet = new HashSet();
 48  
 
 49  
     /** the introspector that is using me */
 50  
     private XMLIntrospector introspector;
 51  
     
 52  
     /**
 53  
      * Construct a new XMLBeanInfoDigester with default properties.
 54  
      */
 55  1624
     public XMLBeanInfoDigester() {
 56  1624
     }
 57  
 
 58  
     /**
 59  
      * Construct a new XMLBeanInfoDigester, allowing a SAXParser to be passed in.  This
 60  
      * allows XMLBeanInfoDigester to be used in environments which are unfriendly to
 61  
      * JAXP1.1 (such as WebLogic 6.0).  Thanks for the request to change go to
 62  
      * James House (james@interobjective.com).  This may help in places where
 63  
      * you are able to load JAXP 1.1 classes yourself.
 64  
      *
 65  
      * @param parser the <code>SAXParser</code> to be used to parse the xml
 66  
      */
 67  
     public XMLBeanInfoDigester(SAXParser parser) {
 68  0
         super(parser);
 69  0
     }
 70  
 
 71  
     /**
 72  
      * Construct a new XMLBeanInfoDigester, allowing an XMLReader to be passed in.  This
 73  
      * allows XMLBeanInfoDigester to be used in environments which are unfriendly to
 74  
      * JAXP1.1 (such as WebLogic 6.0).  Note that if you use this option you
 75  
      * have to configure namespace and validation support yourself, as these
 76  
      * properties only affect the SAXParser and emtpy constructor.
 77  
      *
 78  
      * @param reader the <code>XMLReader</code> to be used to parse the xml
 79  
      */
 80  
     public XMLBeanInfoDigester(XMLReader reader) {
 81  0
         super(reader);
 82  0
     }
 83  
     
 84  
     /**
 85  
      * Gets the class of the bean whose .betwixt file is being processed 
 86  
      *
 87  
      * @return the beans class for this XML descriptor 
 88  
      */
 89  
     public Class getBeanClass() {
 90  10387
         return beanClass;
 91  
     }
 92  
     
 93  
     /** 
 94  
      * Sets the beans class for this XML descriptor 
 95  
      *
 96  
      * @param beanClass the <code>Class</code> of the bean being processed
 97  
      */
 98  
     public void setBeanClass(Class beanClass) {
 99  5105
         this.beanClass = beanClass;
 100  5105
     }
 101  
     
 102  
     
 103  
     /** 
 104  
      * Gets the property names already processed
 105  
      *
 106  
      * @return the set of property names that have been processed so far 
 107  
      */
 108  
     public Set getProcessedPropertyNameSet() {
 109  8110
         return processedPropertyNameSet;
 110  
     }
 111  
     
 112  
     /** 
 113  
      * Should attributes (or elements) be used for primitive types?
 114  
      * @return true if primitive properties should be written as attributes in the xml
 115  
      */
 116  
     public boolean isAttributesForPrimitives() {
 117  884
         return attributesForPrimitives;
 118  
     }
 119  
 
 120  
     /** 
 121  
      * Set whether attributes (or elements) should be used for primitive types. 
 122  
      * @param attributesForPrimitives pass true if primitive properties should be 
 123  
      * written as attributes
 124  
      */
 125  
     public void setAttributesForPrimitives(boolean attributesForPrimitives) {
 126  4979
         this.attributesForPrimitives = attributesForPrimitives;
 127  4979
         if ( introspector != null ) {
 128  4979
             introspector.getConfiguration()
 129  4979
                 .setAttributesForPrimitives( attributesForPrimitives );
 130  
         }
 131  4979
     }
 132  
 
 133  
     /** 
 134  
      * Gets the XMLIntrospector that's using this digester.
 135  
      *
 136  
      * @return the introspector that is using me 
 137  
      */
 138  
     public XMLIntrospector getXMLIntrospector() {
 139  39581
         return introspector;
 140  
     }
 141  
     
 142  
     /** 
 143  
      * Sets the introspector that is using me 
 144  
      * @param introspector the <code>XMLIntrospector</code> that using this for .betwixt 
 145  
      * digestion
 146  
      */
 147  
     public void setXMLIntrospector(XMLIntrospector introspector) {
 148  1624
         this.introspector = introspector;
 149  1624
     }
 150  
     
 151  
     // Implementation methods
 152  
     //-------------------------------------------------------------------------        
 153  
     /** Reset configure for new digestion */
 154  
     protected void configure() {
 155  4134
         if (! configured) {
 156  1209
             configured = true;
 157  
          
 158  
             // add the various rules
 159  
             
 160  1209
             addRule( "info", new InfoRule() );
 161  1209
             addRuleSet(new CommonRuleSet());
 162  
             
 163  
         }
 164  
         
 165  
         // now initialize
 166  4134
         setAttributesForPrimitives(attributesForPrimitives);
 167  4134
         processedPropertyNameSet.clear();
 168  4134
     }
 169  
     
 170  
 }