1 package org.apache.commons.betwixt.digester; 2 3 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 45 public class TextRule extends MappedPropertyRule { 46 47 48 private static final Log log = LogFactory.getLog( TextRule.class ); 49 50 public TextRule() {} 51 52 55 63 public void begin(String name, String namespace, Attributes attributes) throws SAXException { 64 65 TextDescriptor descriptor = new TextDescriptor(); 66 67 String value = attributes.getValue( "value" ); 68 String propertyName = attributes.getValue( "property" ); 69 String propertyType = attributes.getValue( "type" ); 70 71 if ( value != null) { 72 if ( propertyName != null || propertyType != null ) { 73 throw new SAXException ( 75 "You cannot specify attribute 'value' together with either " 76 + " the 'property' or 'type' attributes"); 77 } 78 descriptor.setTextExpression( new ConstantExpression( value ) ); 80 81 } else { 82 descriptor.setPropertyName( propertyName ); 84 85 Class beanClass = getBeanClass(); 86 87 descriptor.setPropertyType( 89 getPropertyType( propertyType, beanClass, propertyName ) 90 ); 91 92 if ( beanClass != null ) { 93 String descriptorPropertyName = descriptor.getPropertyName(); 94 PropertyDescriptor propertyDescriptor = 95 getPropertyDescriptor( beanClass, descriptorPropertyName ); 96 if ( propertyDescriptor != null ) { 97 Method readMethod = propertyDescriptor.getReadMethod(); 98 descriptor.setTextExpression( new MethodExpression( readMethod ) ); 99 Method writeMethod = propertyDescriptor.getWriteMethod(); 100 if (writeMethod != null) { 101 descriptor.setUpdater( new MethodUpdater(writeMethod)); 102 } 103 getProcessedPropertyNameSet().add( descriptorPropertyName ); 104 } 105 } 106 } 107 108 Object top = digester.peek(); 109 if ( top instanceof XMLBeanInfo ) { 110 XMLBeanInfo beanInfo = (XMLBeanInfo) top; 111 ElementDescriptor elementDescriptor = beanInfo.getElementDescriptor(); 112 if (elementDescriptor == null) { 113 elementDescriptor.addContentDescriptor( descriptor ); 114 } 115 116 } else if ( top instanceof ElementDescriptor ) { 117 ElementDescriptor parent = (ElementDescriptor) top; 118 parent.addContentDescriptor( descriptor ); 119 120 } else { 121 throw new SAXException ( "Invalid use of <text>. It should " 122 + "be nested <text> nodes" ); 123 } 124 } 125 } 126 | Popular Tags |