1 package org.apache.commons.betwixt.digester;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 <text> elements.
35 * These allow mixed content text to be specified.
36 * A mixed content element example:
37 * <pre>
38 * <foo>text<bar/></foo>
39 * </pre>
40 * </p>
41 *
42 * @author Robert Burrell Donkin
43 * @version $Id: TextRule.java,v 1.8 2004/03/31 21:11:52 rdonkin Exp $
44 */
45 public class TextRule extends MappedPropertyRule {
46
47 /*** Logger */
48 private static final Log log = LogFactory.getLog( TextRule.class );
49 /*** Base constructor */
50 public TextRule() {}
51
52
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(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
74 throw new SAXException(
75 "You cannot specify attribute 'value' together with either "
76 + " the 'property' or 'type' attributes");
77 }
78
79 descriptor.setTextExpression( new ConstantExpression( value ) );
80
81 } else {
82
83 descriptor.setPropertyName( propertyName );
84
85 Class beanClass = getBeanClass();
86
87
88 descriptor.setPropertyType(
89 getPropertyType( propertyType, beanClass, propertyName )
90 );
91
92 if ( beanClass != null ) {
93 String name = descriptor.getPropertyName();
94 PropertyDescriptor propertyDescriptor =
95 getPropertyDescriptor( beanClass, name );
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( name );
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 }