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

Classes in this File Line Coverage Branch Coverage Complexity
TextRule
82% 
88% 
6

 1  
 package org.apache.commons.betwixt.digester;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2004 The Apache Software Foundation.
 5  
  * 
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  * 
 10  
  *      http://www.apache.org/licenses/LICENSE-2.0
 11  
  * 
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */ 
 18  
 
 19  
 import java.beans.PropertyDescriptor;
 20  
 import java.lang.reflect.Method;
 21  
 
 22  
 import org.apache.commons.betwixt.ElementDescriptor;
 23  
 import org.apache.commons.betwixt.TextDescriptor;
 24  
 import org.apache.commons.betwixt.XMLBeanInfo;
 25  
 import org.apache.commons.betwixt.expression.ConstantExpression;
 26  
 import org.apache.commons.betwixt.expression.MethodExpression;
 27  
 import org.apache.commons.betwixt.expression.MethodUpdater;
 28  
 import org.apache.commons.logging.Log;
 29  
 import org.apache.commons.logging.LogFactory;
 30  
 import org.xml.sax.Attributes;
 31  
 import org.xml.sax.SAXException;
 32  
 
 33  
 /** 
 34  
   * <p>Rule for parsing &lt;text&gt; elements.
 35  
   * These allow mixed content text to be specified.
 36  
   * A mixed content element example:
 37  
   * <pre>
 38  
   *     &lt;foo&gt;text&lt;bar/&gt;&lt;/foo&gt;
 39  
   * </pre>
 40  
   * </p>
 41  
   *
 42  
   * @author Robert Burrell Donkin
 43  
   * @version $Id: TextRule.java 312492 2005-10-09 20:12:38 +0100 (Sun, 09 Oct 2005) rdonkin $
 44  
   */
 45  604
 public class TextRule extends MappedPropertyRule {
 46  
 
 47  
     /** Logger */
 48  604
     private static final Log log = LogFactory.getLog( TextRule.class );
 49  
     /** Base constructor */
 50  1624
     public TextRule() {}
 51  
     
 52  
     // Rule interface
 53  
     //-------------------------------------------------------------------------    
 54  
     
 55  
     /**
 56  
      * Process the beginning of this element.
 57  
      *
 58  
      * @param attributes The attribute list of this element
 59  
      * @throws SAXException 1. If this tag's parent is not an element tag.
 60  
      * 2. If this tag has a value attribute together with either a property
 61  
      * or type attribute.
 62  
      */
 63  
     public void begin(String name, String namespace, Attributes attributes) throws SAXException {
 64  
         
 65  78
         TextDescriptor descriptor = new TextDescriptor();
 66  
         
 67  78
         String value = attributes.getValue( "value" );
 68  78
         String propertyName = attributes.getValue( "property" );
 69  78
         String propertyType = attributes.getValue( "type" );
 70  
         
 71  78
         if ( value != null) {
 72  13
             if ( propertyName != null || propertyType != null ) {
 73  
                 // not allowed
 74  0
                 throw new SAXException(
 75  0
                     "You cannot specify attribute 'value' together with either " 
 76  
                     + " the 'property' or 'type' attributes");                
 77  
             }
 78  
             // fixed value text
 79  13
             descriptor.setTextExpression( new ConstantExpression( value ) );
 80  
             
 81  
         } else {
 82  
             // property based text
 83  65
             descriptor.setPropertyName( propertyName );
 84  
             
 85  65
             Class beanClass = getBeanClass();
 86  
             
 87  
             // set the property type using reflection
 88  130
             descriptor.setPropertyType( 
 89  65
                 getPropertyType( propertyType, beanClass, propertyName ) 
 90  
             );
 91  
             
 92  65
             if ( beanClass != null ) {
 93  65
                 String descriptorPropertyName = descriptor.getPropertyName();
 94  65
                 PropertyDescriptor propertyDescriptor = 
 95  65
                     getPropertyDescriptor( beanClass, descriptorPropertyName );
 96  65
                 if ( propertyDescriptor != null ) { 
 97  65
                         Method readMethod = propertyDescriptor.getReadMethod();
 98  65
                         descriptor.setTextExpression( new MethodExpression( readMethod ) );
 99  65
                         Method writeMethod = propertyDescriptor.getWriteMethod();
 100  65
                         if (writeMethod != null) {
 101  65
                             descriptor.setUpdater( new MethodUpdater(writeMethod));
 102  
                         }
 103  65
                         getProcessedPropertyNameSet().add( descriptorPropertyName );
 104  
                 }
 105  
             }
 106  
         }
 107  
         
 108  78
         Object top = digester.peek();
 109  78
         if ( top instanceof XMLBeanInfo ) {
 110  0
             XMLBeanInfo beanInfo = (XMLBeanInfo) top;
 111  0
             ElementDescriptor elementDescriptor = beanInfo.getElementDescriptor();
 112  0
             if (elementDescriptor != null) {
 113  0
                 elementDescriptor.addContentDescriptor( descriptor );
 114  
             }
 115  
             
 116  78
         } else if ( top instanceof ElementDescriptor ) {
 117  78
             ElementDescriptor parent = (ElementDescriptor) top;
 118  78
             parent.addContentDescriptor( descriptor );
 119  
             
 120  
         } else {
 121  0
             throw new SAXException( "Invalid use of <text>. It should " 
 122  
                 + "be nested <text> nodes" );
 123  
         }
 124  78
     }
 125  
 }