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
20 import java.beans.PropertyDescriptor;
21 import java.lang.reflect.Method;
22
23 import org.apache.commons.betwixt.ElementDescriptor;
24 import org.apache.commons.betwixt.TextDescriptor;
25 import org.apache.commons.betwixt.XMLBeanInfo;
26 import org.apache.commons.betwixt.expression.ConstantExpression;
27 import org.apache.commons.betwixt.expression.MethodExpression;
28 import org.apache.commons.betwixt.expression.MethodUpdater;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.xml.sax.Attributes;
32 import org.xml.sax.SAXException;
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class TextRule extends MappedPropertyRule {
47
48
49 private static final Log log = LogFactory.getLog( TextRule.class );
50
51 public TextRule() {}
52
53
54
55
56
57
58
59
60
61
62
63
64 public void begin(String name, String namespace, Attributes attributes) throws SAXException {
65
66 TextDescriptor descriptor = new TextDescriptor();
67
68 String value = attributes.getValue( "value" );
69 String propertyName = attributes.getValue( "property" );
70 String propertyType = attributes.getValue( "type" );
71
72 if ( value != null) {
73 if ( propertyName != null || propertyType != null ) {
74
75 throw new SAXException(
76 "You cannot specify attribute 'value' together with either "
77 + " the 'property' or 'type' attributes");
78 }
79
80 descriptor.setTextExpression( new ConstantExpression( value ) );
81
82 } else {
83
84 descriptor.setPropertyName( propertyName );
85
86 Class beanClass = getBeanClass();
87
88
89 descriptor.setPropertyType(
90 getPropertyType( propertyType, beanClass, propertyName )
91 );
92
93 if ( beanClass != null ) {
94 String descriptorPropertyName = descriptor.getPropertyName();
95 PropertyDescriptor propertyDescriptor =
96 getPropertyDescriptor( beanClass, descriptorPropertyName );
97 if ( propertyDescriptor != null ) {
98 Method readMethod = propertyDescriptor.getReadMethod();
99 descriptor.setTextExpression( new MethodExpression( readMethod ) );
100 Method writeMethod = propertyDescriptor.getWriteMethod();
101 if (writeMethod != null) {
102 descriptor.setUpdater( new MethodUpdater(writeMethod));
103 }
104 getProcessedPropertyNameSet().add( descriptorPropertyName );
105 }
106 }
107 }
108
109 Object top = digester.peek();
110 if ( top instanceof XMLBeanInfo ) {
111 XMLBeanInfo beanInfo = (XMLBeanInfo) top;
112 ElementDescriptor elementDescriptor = beanInfo.getElementDescriptor();
113 if (elementDescriptor != null) {
114 elementDescriptor.addContentDescriptor( descriptor );
115 }
116
117 } else if ( top instanceof ElementDescriptor ) {
118 ElementDescriptor parent = (ElementDescriptor) top;
119 parent.addContentDescriptor( descriptor );
120
121 } else {
122 throw new SAXException( "Invalid use of <text>. It should "
123 + "be nested <text> nodes" );
124 }
125 }
126 }